Find Collision Point of two RigidBody2Ds

jgraumanjgrauman Posts: 26Member

Hi,

I have two RigidBody2Ds that collide. I am trying to figure out the collision point. I see that Shape2D has a function collide_and_get_contacts, but unfortunately, my RigidBody2Ds both have CollisionPolygon2Ds to define their shape which isn't derived from Shape2D. Does anyone know of a function that would find the point of collision? And better yet, also the collision normal?

Josh

Comments

  • jgraumanjgrauman Posts: 26Member

    Can I bump this? Surely there has to be a way to figure out where RigidBody2Ds collide? Is there a prefered way of contacting the devs or raising a feature request??

  • TwistedTwiglegTwistedTwigleg Posts: 1,689Admin

    I meant to reply to this earlier but forgot. Sorry.

    You can detect collisions between RigidBody2D nodes using the body_entered signal. That will return the body the RigidBody2D collided with, but not the position or normal.

    The easiest way to get collision position is to use the test_move function combined with the body_entered signal. The test_move function, it will return a Physics2DTestMotionResult containing the collision position, normal, and other collision data. If you pass a velocity of zero to the test_move when you detect a collision, it should return the collision info for that collision.

    Is there a prefered way of contacting the devs or raising a feature request??

    The new way to make a feature request is to make an issue on the Godot proposals repository.

  • jgraumanjgrauman Posts: 26Member

    Super helpful! Thanks, it seems to work! Is there any documentation on what the motion, motion_remainer, and collider_velocity Vectors in Physics2DTestMotionResult mean? I'm wondering if they would be useful in calculating the collision energy? Ideally, if I have two KineticBody2Ds that collide, I would love to have both of their velocity's before and after the collision, and then I can calculate how much energy the collision was...

  • TwistedTwiglegTwistedTwigleg Posts: 1,689Admin

    Great! I'm glad it is working. I only recently learned about the test_move function and its uses, and for certain things (like detection collision positions) it has been super useful.

    I do not know 100% for sure what some of the properties are, here is what I think they are for:

    • motion : The velocity that lead to the impact. I think this should be the same value as the motion/velocity passed in to the test_move function.
    • motion_remainder : I believe this is the resulting motion/velocity from the collision. That said, I'm not totally sure how it calculates the motion/velocity remainder.
    • collider_velocity : I think this is the velocity of the other collider that caused the collision.

    For calculating the energy of a collision, I think you'd probably need to look at the difference of motion and motion_remainder, where the difference is the energy lost in the collision.
    Hopefully this helps!

  • jgraumanjgrauman Posts: 26Member

    I have come across an unfortunate 'bug' of using this method. test_move does not always find a collision inside of body_entered. I'd say 90% of the time it returns the collision information, but some of the time it just returns false to there being a collision. I've tried messing with the parameters to test_move, but haven't gotten a 100% success rate. I would love to see body_entered itself be able to return a Physics2DTestMotionResult for the collision that caused body_entered to be called...

  • TwistedTwiglegTwistedTwigleg Posts: 1,689Admin

    Maybe open a proposal to the Godot proposal repository for including a Physics2DTestMotionResult with the body_entered signal? I did a quick search and it looks like it has not been proposed.
    I could certainly see how it would make the body_entered signal way more useful than it is currently.

    Unfortunately, I don't really know of any workarounds for when the test_move function fails. If I stumble across a potential solution though, I'll let you know.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file