-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
How to get the collision point coordinates from CollidingEntities? #552
Comments
So, this is a bit more involved than one might think; apologies for the long message 😅 You can get the contact data from the Next, this gets a bit complicated:
For (1), if you don't want to worry about all contacts, you could just find the contact with the largest penetration depth. I realized there is no method for this yet, so I made a PR to add it (#556). You could copy the methods from there or wait for next release where they'll be included. For (2), you could always just get e.g. the contact point on the surface of the first entity, For (3), you said you wanted the points in both absolute coordinates and relative to entity positions. To get the point in world-space, but relative to the entity position, you can just apply the entity's rotation like Finally, there's still one more potential issue: there's no guarantee which entity in the contact data is which. With all of that in place, and the methods I added, you could do something like this: pub fn wall_collisions(
mut commands: Commands,
map: Query<(Entity, &Wall, &CollidingEntities, &Transform)>,
collisions: Res<Collisions>,
) {
for (&wall_entity, _map, colliding, transform) in map.iter() {
for &other_entity in colliding.iter() {
let Some(contacts) = collisions.get(wall_entity, other_entity) else {
continue;
};
let Some(mut deepest_contact) = contacts.find_deepest_contact().copied() else {
continue;
};
// Make sure that `wall_entity` is the first entity in the contact data.
if wall_entity != contacts.entity1 {
deepest_contact.flip();
}
// Compute the contact point relative to the wall, and in global space.
let point = transform.rotation * deepest_contact.point1;
let global_point = transform.translation + point;
commands.entity(wall_entity).insert(CollidedWithWall {
contact_x: global_point.x,
contact_y: global_point.y,
});
}
}
} (I haven't tested the code, so it may require tweaks) |
Wow! Thank you, this is incredibly enlightening. I owe you a beer! |
Hi, first of all, thank you for this beautiful project. I went through the docs but can't figure out how to get the coordinates of a collision point. Here for example I need the coordinates of where an entity and a wall collided:
Is this possible? I need both the absolute coordinates and those relative to the entity positions.
The Wall is
RigidBody::Static
and the entities areRigidBody::Kinematic
The text was updated successfully, but these errors were encountered: