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.
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;
}
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:
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.
...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).
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.