Skip to main content
gamedev.net
🔒 Locked

Movement

Started by Damengo Jan 19, 2002 at 7:32 AM 3 replies 200+ views
Original Post
Damengo
Damengo
I need some help with a problem. I have made a little demo with a background and a cube thats spinnning infront. My problem is that i want the cube to move from the middle(It´s positioned there from the begining) to the right edge and there change direction and move to the left edge and there change and move to there right and so on. I know how to move the cube with the command gltranslatef(x,y,z); for example gltranslatef(xmov,0.0f,-5.0f); and then make xmov+=0.01f; so the cube moves to the right. I have tried something like if (xmov==(4.0f) { xmov-=0.01f; } so it would change direction when the xmov=4.0f which is at the right edge of my window. but it doesn´t work.
Aprosenf
Aprosenf
  int x_pos;
int dir = 0;

if(dir == 0)
{
x_pos += 0.1f;

if(x_pos >= 4.0f)
dir = 1;
}
else
{
x_pos -= 0.1f;

if(x_pos <= -4.0f)
dir = 0;
}
Damengo
Damengo
I have created a piece of code exactly like the one you posted but it doesnt work.. (same goes for the thing you posted.(

it just keeps moving to the direction you choose first.


Fruny
Fruny
declare ''dir'' static, that way it''ll be only initialized once

static int dir = 0;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Damengo
Damengo
Thanks now it works like a charm

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.