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.

Click here for the libary I am using: TVout

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. :disappointed_relieved:

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 :slightly_smiling_face:

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 :slightly_smiling_face:

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?

Google "clipping algorithms"

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

Basically, you check if the new x,y coordinates you're about to move to are the same as the x,y coordinates of some part of another object. In other words, you check if the objects would overlap if you moved one.

How would I begin on the object detection?

Each bat will have a topY and bottomY value and fixed batX value so you can check whether the y axis of the ball is in that range (or will be next time it moves) when the ball x value equals the batX value.