Object back and forth loop

I need a square to go back and forth on the screen and never stop. It would start in one corner and go until it hits the next corner then flip and go back. This is what I've come up with but I'm stuck. It just stays in the origin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Place global variables here
double translate = 0.0;

void renderFrame(float lastFrameDuration) {

      setWireframeRendering(false);

      glPushMatrix();
      setColor(1.0, 0.0, 1.0); //magenta
      glTranslatef(translate, 0.0, 0.0);
      drawRectangle(0.0, 0.0, 50.0, 50.0);
      glPopMatrix();

      if (translate == 0)
      {
              translate++;
      }
      else if (translate > 800);
      {
              translate--;
      }

}
Movement typically works by having a velocity. You add the velocity to the object's position every update. If you want to move right, you add a velocity that has a positive X value. If you want to move left, you add a velocity that has a negative X value.

Once you have that.... to make the object move continually back and forth... just give it a left-ward velocity when it reaches the right side of the screen, and a right-ward velocity when it reaches the left side.



Side note:

You are using outdated/deprecated OpenGL. I recommend you stop reading whatever tutorial you're using and start reading this one instead:
http://www.arcsynthesis.org/gltut/


Another side note:

Updating the object's position has nothing to do with rendering. Your "render" logic and your "update" logic should be separated.
Sorry was at work, then came home and got back into the code. We haven't talked about using velocity/acceleration yet in my class. And this is the way he showed us how to do it. He said I'm close to figuring it out. Right now I have it so when it gets to the edge it resets. Could you/anyone correct it so it just bounces back and forth?

And this is the template I'm supposed to put my code in.

if (translate < 735)
{
translate++;
}
else if (translate > 725)
{
translate = 0;
}

The code above, like I said, just makes the square travel across screen then reset and do it all over again.
You want it to oscillate back and forth, correct? If "translate" represents the x-position, it will keep going right, but then immediately jump back to 0 once it reaches 725. (edit: oh you already stated this, sorry)

I did something similar when making some randomized images, I used a bool such as "increasing".

Might be better to used a signed integer just to save some code.

1
2
3
4
5
6
7
8
9
int move_direction = 1;
while (true) { //it will oscillate back and forth forever
    if (move_direction == 1)
        position++;
    else
       position--;
    if (position <= 0 || position >= 725)
        move_direction = move_direction * -1;
}

Last edited on
Ok that works, however they're flying on my screen lol. Super fast mode, how do I slow them down?
My mistake, it appears that the function that you're working with is only dealing with 1 frame at a time, so the "while (true)" loop should not be there.

You also need to declare the move_direction somewhere outside of the actual function so that it isn't being redefined every frame, perhaps have it as a parameter of your function.
ex:
1
2
3
4
5
void renderFrame(float lastFrameDuration, int& move_direction) {
    ...
    if (position <= 0 || position >= 725)
        move_direction = move_direction * -1;
}

If you can't change the function like that for your class, then *bites tongue* have it as a global variable, since you appear to already have translate as a global. I would avoid this, though.

If it's still flying too fast after getting rid of the while loop, then it's a problem with the timestep of your program, and not the render/update function.
Last edited on
I had to move the block of code down to the console, it was crashing my window.

I already saw I needed to declare the variable.

If I take out the loop the squares just sit in the corners and don't move.

I think I'm at 60 frames a second, although I remember my professor saying that it calls/draws it at 30 frames.

If there's a better way to do this please elaborate, this is just what I came up with.
Just to clarify, the "template" code you were using...
1
2
3
4
5
if (translate < 735)
translate++;
else if (translate > 725)
translate = 0;
}

...did not make the logic run too fast, but the change I suggested made it not run at all/too fast? That doesn't seem right, but it's hard to say more without actually knowing what's done with the values after you change them.

You might want to post the renderFrame function code you had at your 2nd post, and the code you say is moving too fast (and not at all without the infinite loop).
Last edited on
I'd tend to approach it something like this:
do this just once:
 
int velocity = 1;


do this for every frame:
1
2
3
4
position += velocity;

if ((position <= 0 && velocity < 0) || (position >=725 && velocity > 0))
    velocity = - velocity; // reverse direction of motion. 


Yep that slims it down a lot from mine.
When I said template I meant the whole project file, sorry for the confusion. It was going fast with the loop in there, when I took out the while look, the squares weren't moving at all.

Edit: I got it working now. Ty both!
Last edited on
Topic archived. No new replies allowed.