Sign in to follow this  
SelethD

pong ball vectors

Recommended Posts

I am making a pong clone, and my pong ball has a 2d vector for direction. When the ball hits a wall, (left side of screen or right) I just flip the X component of the 2d vector (from (1,1 to -1,1) This works fine. However when the balls hit the paddles (top and bottom) I would like the return bounce to be a little more unpredictable. I want the ball to do a complete oposite when the paddle is not moving (from 1,1 to 1,-1) and do a more horizontal bounce when paddle is moving in the ball's x direction (paddle x++ ball from (1,1 to 2,-1) and do a more vertical bounce when paddle is moving in opposite x as ball (paddle x-- ball from (1,1 to .5,-1) I would like the sharpness of the angles to be based on the speed of the paddles x movement Is there any easy way to do this?

Share this post


Link to post
Share on other sites
Sure, that would work.

Keep track of a vector for the paddle and ball.

When the ball and paddle collide, subract the x component of the paddle from the ball, and use this number to determine how the ball should be affected.


Will

Share this post


Link to post
Share on other sites
I was tinking about this when I was plaing a breakout clone. My idea was to have an array of x and y vectors which all had a length of 1, precalulate them all using pythagoras (a^2 = b^2 + c^2).
Once you have this you could check the difference in direction and speed of the paddle and ball and pick an according set of values from the array.

A benifit of this (by far as i can see) is you can multiply these values by a seperate speed variable when calculating the balls new position. Changing this variable changes the balls speed but allows all your paddle/ball code to be seperate from it as it only uses the normalised x and y's.

NOTE: I never actually tried as i decided to work on my C++ skills before making the game so it may/may not actually work (but it should in theory, well in my theory). so if im worng i apologise now and if anyone spots any msitakes please point them out :) (in a nice way!).

Hope this helps you.

Share this post


Link to post
Share on other sites
Thats exactly what I would like to do, I just dont know how :P

Do you have any code snip, or pseudo code even, to show me how to do this?

I use C++ in windows

Share this post


Link to post
Share on other sites
Unfortunalty at the moment I dont have time :( since I never started the game I would need to write the code from scratch itll probably take a while. Ill put some together tonight/tomorrow morning if your still in need of help then, Sorry.

Share this post


Link to post
Share on other sites
Ill probable still need help, I cant find anything on the net, and im not very familiar with vectors and such.

Thanks

There needs to be some type of document that lists seral common uses of vectors and other physics type structures in video games, so that anyone can easily acess the information.

Because physics and vector math sites are like in greek :D

And vector libraries can do all kinds of neat stuff like dot product, cross product, but... how do i use that in a game? They sure dont tell you.

So there needs to be some article, or ebook or somthing tha says ok
Pong, for vecotrs of ball bouncing, do this this this
Space Game Filight, do this this this
Pool Games do this this this
games with gravity and jumping do this this this

and so on... So anyone know where somdthing like that is?

Share this post


Link to post
Share on other sites


float BallVectors[MAXVECTORS][2]; //array holding x and y components of vector
float BallPosition[2];

int CurrentVectorIndex;
int NewVectorIndex ;
int speed; // multiplier for vector for speed

NewVectorIndex = ReturnVectorIndex(CurrentVectorIndex); // returns the array index depending on Ball/Paddle collision

// set new position
BallPosition[1] += BallVectors[NewVectorIndex][1] * speed;
BallPosition[2] += BallVectors[NewVectorIndex][2] * speed;



ok theres something quick ive done, thats roughly what my idea was (there are no specifics and this code wont work as is, but it gives the general idea).
one thing which I didnt work out was how to handle direction changeing with the new system, with the old you could just flip the x/y speed but I think there could be problems with this doing it that way so its something your going to have to test and solve yourself :)

Anyway good luck and I hope this helps you! (and if ive explained something badly ill try to expalin better if you let me know :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think ill just leave the boring old predictable bounce and wrap this up so I can move onto somthing else, preferably with no vectors, or physics :)

Share this post


Link to post
Share on other sites
Try modeling the effect with a graph where the x axis is difference in x velocity components and where the y axis is the horizontal bias (negative means a vertical bias). Then all you have to do is approximate that function (which is likely a sine or cosine function) and interpret the outputs. This is a good method to go about creating effects that differ from the norm or conventional physics (good gameplay often defies physics).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this