# pong ball vectors

This topic is 5147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 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 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 on other sites
float BallVectors[MAXVECTORS][2]; //array holding x and y components of vectorfloat BallPosition[2];int CurrentVectorIndex;int NewVectorIndex ;int speed; // multiplier for vector for speedNewVectorIndex = ReturnVectorIndex(CurrentVectorIndex); // returns the array index depending on Ball/Paddle collision// set new positionBallPosition[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 on other sites
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 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).

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632606
• Total Posts
3007383

×