[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Racing game Physics



On Sun, 2006-11-05 at 16:53 -0300, Pica wrote:
> Hi, I'm trying to make a racing game with pygame, but I'm having some
> problems with the collisions physics, so I was wondering if anyone
> knows a 2D racing game developed with simple collision physics.
> I downloaded pyRacerz but when the race starts it crashes, and there
> was another one in the list but the link didn't work.

It is important to use separate vector for speed and car orientation.

Speed after collision could be computed as:
 speed1 = v1*(m1-m2)/(m1+m2) + v2*(2*m2)/(m1+m2)
 speed2 = v2*(m2-m1)/(m1+m2) + v1*(2*m1)/(m1+m2)

where:
 speed1 ... velocity of car after collision (vector)
 v1 ... velocity of the first car (vector)
 m1 ... mass of the first car

The above equation could be obtained from two laws:
1) Conservation of momentum
 v1*m1 + v2*m2 = speed1*m1 + speed2*m2

2) Conservation of energy (only kinetic energy for 2D)
 1/2 * m1*v1**2 + 1/2 * m2*v2**2 = 1/2 * m1*speed1**2 + 1/2 * m2*speed2**2

-- 
Ivo Danihelka