# Bouncing. More of a math question.

Hallo guys I am fiddling around with VCA cables and having fun with making a few stupid games. I was thinking of making a basic gaming console and I mean VERY basic.

For this gaming console I need a bounce a like function (for now only in a 2d space so no gravity)
I am doing a pong game and I need to simulate it hitting the walls and bounce off but also same function have to work if there was lets say a big square in the middle.

2D bounces are usually easy. The object will have an x and y position on the plane and an x and y delta value that when added to the current x and y cause the object to move. When the object reaches the x upper or lower boundary or an object blocking its path is detected, change the sign of the x delta value to make it bounce back but keep going in the same y direction. Do the same for the y limits and y delta value.

If I understood you correctly you want me to take before and after coordinates, with that I can get the current move of the axis. However I need to mirror this when an object is dected.

I got as far as pretenting the movement as being a line and use the fomular of:

yDelta
------ = a
xDelta

next y pos = a*currentX

however for this I would need to mirrored line for this to work...

It looks like you are making things too complicated. Forget object detection for the moment and do something like this

``````//it's time to move
x += xIncrement;                   //move the x position one step to the right or left
if (x >= xRightLimit || x <= xLeftLimit)
{
xIncrement *= -1;  //start moving the other way in the x direction
}
``````

When you have got that working you can add in object detection

And just to re-iterate, the code would be identical to Heli's for the Y axis to get 2D bouncing.

Maybe I formulated myself wrong I will add in a drawing to illustrate what I mean:

The black is solid walls, The blue is the bouncing ball and the Orange is the path it should go.

Thats the reason why I am pretenting it to be a line

spil778:
Maybe I formulated myself wrong I will add in a drawing to illustrate what I mean:

The black is solid walls, The blue is the bouncing ball and the Orange is the path it should go.

Thats the reason why I am pretenting it to be a line

Yes. We understood. Use Heli’s version, it’s much easier, and will do what you want. Check the Maths of what he is doing and it will make sense.

``````//it's time to move
x += xIncrement;                   //move the x position one step to the right or left
if (x >= xRightLimit || x <= xLeftLimit)
{
xIncrement *= -1;  //start moving the other way in the x direction
}
y += yIncrement;                   //move the y position one step up or down
if (y >= yUpLimit || y <= yDownLimit)
{
yIncrement *= -1;  //start moving the other way in the y direction
}
``````

Your drawing makes not difference to what I think you should get working first.

If the positions on the axes are numbered xLeftLimit to xRightLimit left to right and yLowerLimit to yUpperLimit bottom to top then taking your example line in the picture the ball starts by moving up and left. xIncrement is negative (lets say -) and yIncrement is positive (lets say 1).

So the "ball" moves left and up until y is equal to or greater than yUpperLimit. At that point yIncrement is changed to -1 and the "ball" starts to move down and left until y equals or is less than yLowerLimit. At that point yIncrement is changed to 1 and the ball starts to move up and left again.

I got it to move alright. (random direction and spawn point) How would I begin on the object detection?