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

Re: [pygame] Fwd: Stuck in a ball's velocity vector -.-



To make it general, you'll need to compute the angle of incidence between the ball and wall, see:

http://en.wikipedia.org/wiki/Angle_of_incidence
http://en.wikipedia.org/wiki/Reflection_%28physics%29

To reflect, you would could rotate the incoming vector a half circle plus twice the angle of incidence. The resulting vector would be headed away from the surface in a reflective angle. And if in the angle of incidence is zero, will head back in directly the opposite direction.

As for balls colliding, wikipedia has some good info for this as well (see the section on 2D collisions):

http://en.wikipedia.org/wiki/Elastic_collision

Basically you must compute the tangent angle at the point of collision (which is perpendicular to the difference of the position vectors of the balls), then split the velocity vector into the velocity component parallel to the collision angle and the velocity component perpendicular to it. These become the new velocities of the colliding balls. This assumes the "mass" of the balls are the same.

hth,

-Casey

On May 22, 2007, at 10:56 AM, jfdidj sidjdijas wrote:

First of all, the code: http://phpfi.com/236142

I'm having a hard time trying to figure out what vector operations i'm a supposed to do in order to make simple ball reflections in walls. First, as in the code shown above, walls would be the borders of the screen... But I would like to know how to "reflect" a velocity vector in the case of a collision with another sprite (that's not moving) or with another ball (that's moving too).

I've seen some codes from the main page at pygame, and the simplest way to do the first case, I guess, would be simply to revert the y velocity... But I would like to make a code more general... Working with vector angles and such... So that I could expand the code to like, 30 balls boucing in the screen and at each other. The Vector2d class i'm using is the one in the Pygame Cookbook.

For a horizontal collision with the borders I thought about making the vector_angle = 180 - vector_angle... But that wouldn't work for the collisions in the floor and Ceil. What's the general formula to do that?

And what can I make in the general case of 2 balls hitting each other? (Assuming no loss of "energy"... Just a simple "reflection").

Thank you very much, hope i've been clear. =)

PS: If you've got some suggestion for the other parts of the code as well, feel free to say =)