3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Richard Marskell - Drackir Apr 16 '12 at 19:18
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ They aren't able to rotate, forgot to mention that. \$\endgroup\$ – jcora Apr 16 '12 at 18:28
  • \$\begingroup\$ 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! \$\endgroup\$ – jcora Apr 16 '12 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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