Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a game in JavaScript and my current implementation of collision uses the shortest distance to push the intersecting object away, which isn't always correct. I've made this diagram of the ideal collision. The red box represents the starting position, the green box represents the proper position and the black box represents the static object it is colliding against. The blue arrow is the velocity vector. With my current implementation, the object would be placed in its final position and pushed out (and in this case it would be pushed left)

Diagram of collision

How can I fix this?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You can procedurally find out how to do a moving AABB/AABB trace with the formulas on this page.

http://www.realtimerendering.com/intersections.html

Basically those equations will tell you what percentage of your move you should do that frame, which will allow you to place your square at the correct location.

share|improve this answer
    
This looks very promising. That table looks very handy for the future. Thanks! –  echeese Nov 11 '10 at 5:14
add comment

There are a few ways to do this, but one easy way is a binary search over the timeline between last frame and the current one. Say that last frame is at time t, and this frame is at t+1. If a collision happened, back up the objects to t+.5. If a collision is still happening, try t+.25, if not try t+.75. Keep going until the steps are sufficiently small. Then do your test for shortest distance push back. Then make sure to play forward the remaining amount of time (since you backed up to find the collision point.)

The key is to get as close to that point in time as you can, since that will definitively tell you which face collided with which other face.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.