PDA

View Full Version : Movement with OpenGL?

XBCT
09-20-2000, 08:12 AM
Hi!
I have a problem:
To run arround in my Q3-Viewer I do the following calc if the up-key was pressed to move forward in the world:

xpos-= sin(yrot*piover180) * speed;
zpos-= cos(yrot*piover180) * speed;

//yrot is the value I passed into OpenGl
//this way: glRotatef(yrot,0,1,0)

It works perfectly. However if I want to strafe left\right I have to swap sin and cos(at least I calculated this with pencil&paper) but this doesn´t work.

Why?

P.s.:Perhaps I did something wrong ´cause I cannot find a precise definition of the angle
-part of glrotatef(....);

Rob The Bloke
09-20-2000, 12:11 PM
A quick thought...

from what I gather, you've split the vector of your motion into:

xpos-= sin(yrot*piover180) * speed;
zpos-= cos(yrot*piover180) * speed;

(I might be wrong here, I'm just scribbling on some paper)

to strafe right try using:

xpos+= sin(yrot*piover180) * speed;
zpos-= cos(yrot*piover180) * speed;

And left:

xpos-= sin(yrot*piover180) * speed;
zpos+= cos(yrot*piover180) * speed;

Thats what my paper reckons......Hope it works http://www.opengl.org/discussion_boards/ubb/smile.gif

pavlos
09-20-2000, 12:17 PM
I think you must also swap the sin-cos :
to strafe right try using:
xpos+= cos(yrot*piover180) * speed;
zpos-= sin(yrot*piover180) * speed;

update:
And perhaps you must clean your code.
If your code works, then you probably using something like that :
glRotate(360-yrot,0,1,0);
I found that because in my code I use glRotate(yrotation,0,1,0); and I walk with :
void walk(float a)
{
position[0] -= (float)sin(yrotation*0.0174532925f) * a*dt;
position[2] += (float)cos(yrotation*0.0174532925f) * a*dt;
}
//speed=a*dt

[This message has been edited by pavlos (edited 09-20-2000).]

XBCT
09-21-2000, 07:49 AM
Hi!
Thanx guys, I´ll again ask my paper http://www.opengl.org/discussion_boards/ubb/wink.gif to check where I made the fault and try your ideas.

P.s.:I use glRotate(360-yrot,0,1,0) which is the same as glRotate(-yrot,0,1,0).

Greets, XBTC!

XBCT
09-22-2000, 03:32 AM
I looked at the problem and your solutions are both right:One needs to swap cos\sin and negate one of them to strafe.I just ignored the signs in my drawing.

Greets, XBTC!

[This message has been edited by XBCT (edited 09-23-2000).]

hermes
09-22-2000, 10:12 AM
I walk through a maze in one of my programs and this is what I use to do the strafe:
case '0': //sidestep left
incrementX = sin(Theta*(pi/180)-pi/2) * 0.2;
incrementY = cos(Theta*(pi/180)-pi/2) * 0.2;
tempX = transX - (2*incrementX) + int((gridsize-1)/2);
tempY = transY - (2*incrementY) + int((gridsize-1)/2);
if (maze.showcolor(int((gridsize-1) - tempX),int((gridsize-1) - tempY))>0)
{
transX = transX - incrementX;
transY = transY - incrementY;
}
break;
case '.': //sidestep right
incrementX = sin(Theta*(pi/180)-pi/2) * 0.2;
incrementY = cos(Theta*(pi/180)-pi/2) * 0.2;
tempX = transX + (2*incrementX) + int((gridsize-1)/2);
tempY = transY + (2*incrementY) + int((gridsize-1)/2);
if (maze.showcolor(int((gridsize-1) - tempX),int((gridsize-1) - tempY))>0)
{
transX = transX + incrementX;
transY = transY + incrementY;
}
break;
and it works. I know it's been resolved but it is helpful to see it again sometimes.

CitiZen X
09-22-2000, 03:06 PM
If it makes it easier to under stand, you could simply add 90 degrees and subtract 90 degrees and use the same equation. It adds a little to the computation, but if its easier/faster to get something done versus sitting down a calculating, then you could optimize later.

XBCT
09-23-2000, 02:36 AM
Thanx guys but hey as I already said it works now, I just dropped a sign in my calculations on paper.

Greets, XBTC!