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

Sprite.distance & world.raycast implementation #314

Merged
merged 3 commits into from Mar 31, 2024

Conversation

codingMASTER398
Copy link
Contributor

First time contribution - Some of my formatting may not be up to spec, and the minified version isn't currently included.

This PR aims to fix #280, as well as a pet peeve of mine being the lack of Sprite.distance() function.

Sprite.distance(otherSprite)

Returns the distance to the other Sprite. How this isn't already implemented is odd to me.

world.raycast

An implementation atop of the world.rayCast (don't get them confused! I don't know how to override the other one and still use it) that returns the closest sprite from a position and direction of a ray.

Due to limitations I've encountered in the past, I'm still unable to get Planck to return a list of all rays & find the closest one simultaneously. Even with Sprite.distance, I wasn't able to get it working.
Thus, the function currently only returns one "hit" (true/false), "sprite", "point", and "normal".
This can be remedied with the optional excludeFunction, detailed in a comment in the PR.

Here's a sketch demonstrating the functionality.

@quinton-ashley
Copy link
Owner

In p5.js there is already a function for calculating the distance between two points.

dist https://p5js.org/reference/#/p5/dist

dist(spriteA.x, spriteA.y, spriteB.x, spriteB.y)

There's also vector.dist

spriteA.pos.dist(spriteB.pos)

But I do like that use of your function would be far simpler. There's already sprite.angleTo so perhaps for consistency it should be called sprite.distanceTo

spriteA.distanceTo(spriteB)

It also might be nice to make p5's dist function accept sprites, mouse, and any object with x,y properties.

dist(spriteA, mouse)

@codingMASTER398
Copy link
Contributor Author

sprite.distanceTo() is a good idea- I'll add another commit changing the name of sprite.distance()

@quinton-ashley
Copy link
Owner

quinton-ashley commented Mar 31, 2024

Thank you!

Also I forgot to say since p5play's World class extends planck.World you can override the world.rayCast method and then still use planck.World's rayCast inside it by using super.rayCast. I'll make the necessary edits. For an example check out my world.step function.

Merging your pull request!

@quinton-ashley quinton-ashley merged commit 4132e26 into quinton-ashley:main Mar 31, 2024
@quinton-ashley
Copy link
Owner

quinton-ashley commented Mar 31, 2024

I'm going to rename the excludeFunction param to filter and returning true will include the sprite in a list of results, like Array.filter.

Edit: Actually maybe it should be a limit function, ie. if the sprite is a wall it returns true to stop the ray.

@quinton-ashley
Copy link
Owner

I also want world.rayCast to return a list of all the sprites the ray intersects with. I will try implementing this.

@quinton-ashley
Copy link
Owner

I like that you used radius and direction for this function instead of a second point. Great for p5play ease of use!

@quinton-ashley
Copy link
Owner

@codingMASTER398 I added some additional info to your planck issue report: piqnt/planck.js#270

@quinton-ashley
Copy link
Owner

I'm going to switch the input param order to match Unity's as well.

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

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.

world.rayCast
2 participants