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'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.

share|improve this question
1  
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

3 Answers 3

up vote 3 down vote accepted

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

I'd start with perfectly elastic collisions before moving on to adding an elasticity constant to your objects.

share|improve this answer

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.

share|improve this answer
    
They aren't able to rotate, forgot to mention that. –  jco 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! –  jco Apr 16 '12 at 18:29

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.

share|improve this answer
    
@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... –  Daniel Apr 17 '12 at 10:24

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.