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

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



jfdidj sidjdijas schrieb:
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 =)

Hi

actually you have to flip the sign of the direction you are colliding: if it is in x direction (leftside or rightside of the screen) then x = -x.
And of course the same for y direction.


If two balls collide then it is a bit trickier: you must find the collision point (it is the point between the centers) and then you will have to find the tangent at this point (it is a line that is 90 degrees rotated against the line that connects the centers) . Then you will have to reflect you movement vector at this tangent (best you make a drawing).

I hope that helps and I was clear enough.

~DR0ID