Sign in to follow this  
Firefly08

Collision Response With Moving Object

Recommended Posts

Firefly08    164
Hi, I am currently trying to add in collision detection / response between my player (represented by a sphere) and a platform (represented by a box). I have got it almost working, as long as my platform does not move in any sort of downwards direction. However when my player is standing on a platform that moves down then my player starts jittering up an down and slides around on the platform slightly! I think it could be to do with the fact that whenever my play lands on any surface he sinks in slightly then gets moved slightly to it's correct position. Here is the code I am using to handle my collisions: // velocity is players velocity // platformV is the platforms velocity if (sphere.Intersects(boxes[boxes.Count - 1])) // If player is on platform velocity += platformV; foreach (BoundingBox box in boxes) // Level collision boxes { if (box.Intersects(sphere)) // Sphere is player sphere { Vector3 closestPoint = box.ClosestPoint(sphere.Center); Vector3 planeNormal = -Vector3.Normalize(sphere.Center - closestPoint); float penetrationDepth = Math.Abs(sphere.Radius - Vector3.Distance(closestPoint, sphere.Center)); float velocityAlongNormal = Vector3.Dot(velocity, planeNormal); float velocityCorrection = (penetrationDepth * 0.25f) + float.Epsilon; if (velocityAlongNormal > -velocityCorrection) { velocity -= planeNormal * (velocityAlongNormal + velocityCorrection); } if (Math.Acos(Vector3.Dot(-planeNormal, Vector3.Up)) < 0.5) isOnGround = true; } } position += velocity; Can anyone see a problem with this code?

Share this post


Link to post
Share on other sites
Pyrogame    106
Hmm, I have no experience with this, but I think, that you have not to calculate the collision each time the platform moves in the world space.

I mean, that whenever the player hits the platform, it should "connect" the player with it. Then you only have to calculate the collision agains a static platform. It the player is on the ground and the platform is flat, then you have nothing to do :).

Ofcourse, on the end of the calculation, the movement of the platform should be applied to the player. If the player jumpes while the platform moves, then the player should get "disconnected" from the platform. Now you have your initial state: a falling sphere and a moving platform ^^. Whenever some room (train, car, or whatever) moves, calculate the collisions in its own space for the children. You can add acceleration forces of the whole room to the moveable elements.

But calculating the collision of two things, while they both move, is difficult and may end up in solving of some kind of equations to be correct.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this