#Time-sensitive positioning errors

18 messages · Page 1 of 1 (latest)

strong knot
#

Hey, I'm working on something like this: https://www.youtube.com/watch?v=MH03ZJaNe8A

I'm having trouble with the balls staying lined up. I made a simple example with just 2 balls to test my problem, and in no time, they start to stray from where they should be.

In my test, I have 2 balls bouncing off the left and right of the window. The top ball is twice as fast as the bottom ball, so they should still line up every other bounce.

I have included a video of what it looks like, and you can see they don't maintain their true position.

Here also is the part of the code that contains the ball positioning:

I think it might be floating point errors but I'm not sure.
Any ideas how to fix this?

Here also is the part of the code that contains the ball positioning:

inner cloakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

strong knot
#
#define maxBalls 20

double ballBPM[maxBalls];
double ballX[maxBalls];
double ballDir[maxBalls];
double ballVelocityX[maxBalls];
double ballXOffset[maxBalls];
double deltaTime[maxBalls];

for (int j=0; j< numBalls; j++) {                // only run once. 
    //for the 2 ball example, BPM will be 30 and 60
    ballBPM[i] = 60/(i+1);                    
    //100 is screen width, (60.0/ballBPM[i]) is time for 1 travel  (r=d/t)
    ballVelocityX[i] = 100.0/(60.0/ballBPM[i]);        
}

timeSave = glutGet(GLUT_ELAPSED_TIME)/1000.0;            //in ball movement function
    for (int j=0; j< numBalls; j++) {                //loop through all balls
      //position = initial position + velocity * change in time * direction (1 pr -1)
        ballX[j] = ballXOffset[j] + ballVelocityX[j] * (timeSave - deltaTime[j]) * ballDir[j];    
        draw_Ball(ballX[j],ballY[j]);        
        if (ballX[j] > 100) {            //if ball hits edge of screen
            ballDir[j] = -1;            //switch direction
            ballXOffset[j] = ballX[j];    //set initial position
            deltaTime[j] = timeSave;    //reset initial time
        }
        if (ballX[j] < 0) {
            ballDir[j] = 1;
            ballXOffset[j] = ballX[j];
            deltaTime[j] = timeSave;
        }
    }
#

I have also tried switching direction based on time, but that didn't help either.

if(timeSave - deltaTime[j] > ballBPM[j]/60.0){
            ballDir[j] = ballDir[j] * -1;
            ballXOffset[j] = ballX[j];
            deltaTime[j] = timeSave;
        }
earnest maple
#

floating point rounding errors accumulate quickly.

#

why two loop with j ?

#

where is i ?

#

why 60/(i+1); integer math? for a double var`

#

why comparing doubles to integers if (ballX[j] > 100)

strong knot
#

this is very much a WIP, and i was something that I ended up removing.
correct me if I'm wrong, but 60/(i+1) will still be stored correctly as a double.
again with if (ballX[j] > 100), it will still do what I am expecting right?

earnest maple
#

60 an integer, over i, an integer.. is integer math.

#

is this what you wanted, an integer result?

strong knot
#

eventually I will want a double as an answer, but right now it doesn't matter because there are only two balls for testing (60/(0+1)=60 , 60/(1+1)=30)

earnest maple
#

and i=6?

strong knot
#
ballBPM[0] = 60.000000
ballBPM[1] = 30.000000
ballBPM[2] = 20.000000
ballBPM[3] = 15.000000
ballBPM[4] = 12.000000
ballBPM[5] = 10.000000
ballBPM[6] = 8.571429
strong knot
#

Still looking for help if anyone has any ideas

inner cloakBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.