# How will the velocities of two moving objects change once they collide?

I'm making a small game where things can fly around and collide. Things like boxes and so on. For each object, I have an array of all forces acting upon it, I have it's mass, it's position and it's velocity in both directions (a 2D vector). I know how to detect collision between them, but I just don't know how to react.

I used to calculate their orientation towards each other, it they were on top one another, I would just negate their y speed (`v.y = -v.y`), and if they were next to each other on the x axis I would negate their x speed (`v.x = -v.x`).

Now, this isn't very realistic, so, how do I do it? All objects are rectangles represented by `[x, y, w, h]` vectors.

Objects can't rotate.

-
If you want it to be realistic you're going to have to use physics equations and transfer the forces between the colliding objects. This may be fairly easy since they can't rotate and it's 2D. Depending on how complex you plan to make it though, you may want to consider using a physics engine like Box2D or Farseer. – Richard Marskell - Drackir Apr 16 '12 at 19:18

A quick Google for 'Physics Collisions' yielded the ever-fabulous wikipedia as a starting point.

-

Chris Hecker has written a lot about the subject of rigid-body dynamics in games. The material can be found here. I think these should be good reference material because the first few articles deal with 2D and only in the last one does he extend everything to 3D.

Also, another note: you might want to consider using 4th Order Runge-Kutta integration rather than Explicit Euler as Hecker does in the articles. The 4th order method gives better long-term accurate results.

-
@Daniel Pray tell, how do you propose to run a physics engine without any integration? – ktodisco Apr 17 '12 at 8:38
nvm, looks like I needed to go read gafferongames.com/game-physics/integration-basics again... – dcousens Apr 17 '12 at 10:24

if you don't let them rotate, that's exactly how you have to implement it. but if rectangles are able to rotate, it's pretty hard to solve equations needed for simulation. though you still are able to solve it yourself, it's much more better if you try using 2D physic engines like Box2D.

-
They aren't able to rotate, forgot to mention that. – jcora Apr 16 '12 at 18:28
Also, I'm looking for a realistic representation. In my model, if an object has 0 x speed, it will just stay in place after the collision! – jcora Apr 16 '12 at 18:29