Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closest orthagonal distance function #426

Merged
merged 6 commits into from
Oct 4, 2024

Conversation

dragoncoder047
Copy link
Contributor

idk this might be useful?

Copy link

pkg-pr-new bot commented Oct 3, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/kaplayjs/kaplay@426

commit: 968e35a

Copy link
Collaborator

@lajbel lajbel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@imaginarny
Copy link
Contributor

Wait a second. There are a few cosmetic issues. It is 'orthOgonal', not 'orthAgonal'. Also, let would be preferable instead of var, although it's not much of an issue in this particular case (probably). Just a heads-up.

@mflerackers
Copy link
Member

Isn't this just return vec2(this.x < 0 ? left : right, this.y < 0 ? up : down) ?

@dragoncoder047
Copy link
Contributor Author

Isn't this just return vec2(this.x < 0 ? left : right, this.y < 0 ? up : down) ?

Not quite. It does the orthogonal direction closest to the angle of this, so say if you have vec2(-1, -10) it will return UP not LEFT.

Since it is only 4 candidates, I suspect it can be done with a bunch of conditionals, however, it made more sense to use a loop to me. 🤷 It might be faster to use conditionals, but until this function is used somewhere I really can't say.

@mflerackers
Copy link
Member

So

return Math.abs(this.x) > Math.abs(this.y) ?
this.x < 0 ? left : right :
this.y < 0 ? up : down;

Two conditionals will always be faster than calculating 4 square roots (which weren't really needed, the dot product is enough to compare distances).

@lajbel lajbel merged commit 4b051ae into kaplayjs:master Oct 4, 2024
2 checks passed
@dragoncoder047 dragoncoder047 deleted the closestOrth branch October 4, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants