-
Notifications
You must be signed in to change notification settings - Fork 2
How to fix a fast object not colliding with things
carnalis edited this page Mar 20, 2017
·
3 revisions
If two objects are moving "fast" towards each other, it may happen that the physics engine does not detect a collision and the objects pass through each other. This gets more likely the thinner/smaller the objects are and the faster they move. It could already happen when a player runs against a wall.
To fix this problem a physical raytrace can be done between the last position and the current position to check if there was any collider and the object can be moved backwards.
Example:
// data members:
Urho3D::Vector3 pos_last;
Urho3D::RigidBody* body;
// in the update/movement code:
// get the center position of the object, this one is 2 units high and the
// "0-point" is at the bottom/feet so 1 unit up to get the center
Vector3 object_pos=body->GetPosition()+Vector3(0,1,0);
PhysicsRaycastResult result;
Ray ray(pos_last,object_pos-pos_last);
float l=(object_pos-pos_last).Length();
// make a raycast from the position in the last frame towards the current one if
// this object moved at least 0.5 units
if(l>0.5)
{
globals::instance()->physical_world->SphereCast(result,ray,0.2,l,2);
if(result.distance_<=l) // if there is something between the last and current position
body->SetPosition(pos_last); // move the object back (could also be positioned at the ray hit)
}
pos_last=object_pos;
https://urho3d.github.io/documentation/HEAD/_physics.html#Physics_Movement which describes an easily-applied solution using CCD.
To prevent tunneling of a fast moving rigid body through obstacles, continuous collision detection can be used.
Modify me