3D ball moving forwards and backwards forever problem

hi i just wanted to know how to make a ball go forward and backwards forever and also change the speed i have been able to make it go forward and stop at the wanted point and also to come back but then it just goes on forever.

ball1X = ball1->GetX();
ball1Z = ball1->GetZ();
if (ball1X < 0)
{
kSpeed = +kSpeed;
}
else if (ball1X > -50)
{
kSpeed = -kSpeed;
}



ball1->MoveX(kSpeed);
ball1->MoveZ(kSpeed);
Last edited on
1. What is +x ? x. The + will not change the sign.

2. You want to move between A and B, where A<B?
IF you have advanced "up" to C, where B<C, THEN change direction to "down"
ELSE
IF you have retreated "down" to D, where D<A, THEN change direction to "up"
sorry, do you want it to go on forever or want it to go forward, back, and then stop?

if its going forever and you want it to stop, you need to add something so that when it reaches the original position a second time (the first time being at its start!) it quits.

if you want it to go forever, looping what you have should do it.

abs(x) and -x or rather than the if statement, value*pow(-1, iterationnumber) will flip the sign.
Last edited on
Topic archived. No new replies allowed.