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

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



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 =)