# strafe? ? ?

can some one help me with how to formulate new coords that allow the camera to strafe from side to side?

i know what needs to be gone… i just cant remember from high school how to do it.

i am using gluLookAt for a first person view and i know i just need to move the position and the view coords perpendicular to the slope of position-to-view.

here is a diagram i made if it helps at all: thanks for any help!

i am using something like this:

``````
#define PI		(3.14159265359f)

position.x += float(cos(DEG2RAD(yaw + 90.0)))*strafeSpeed;
position.z += float(sin(DEG2RAD(yaw + 90.0)))*strafeSpeed;
position.x += float(cosYaw)*speed;
position.z += float(sinYaw)*speed;

lookAt.x = float(position.x + (cosYaw*cosPitch));
lookAt.y = float(position.y + sinPitch);
lookAt.z = float(position.z + (sinYaw*cosPitch)); gluLookAt(position.x, position.y, position.z,
lookAt.x, lookAt.y, lookAt.z,
0.0, 1.0, 0.0);
``````

Use the cross product of the view-direction vector and the camera up vector:

``````  // compute unit vector pointing left
vector view = lookAt - eye;
vector left = view x up;
normalize(left);

// compute movement
vector move = left * mouseDelta * scale;

// move camera
eye = eye + move;
lookAt = lookAt + move;
``````

ok… i am grasping what you are saying, but i never really made it past algebra 2/trig in high school, so i am a little confused on how to actually calculate my data.

say my position is (3,5) and my view is at (7,9), is there a formula that i can just punch those numbers (or some version of those numbers) into and come up with my position/view coordinates based on which direction i want to move?

haha does that even make any sense?

sorry, i still havent figured it out.

Yeah, everything that’s been said (from my skimming through it) is probably correct. You’re more a graphical person though, here ya go:

“say my position is (3,5) and my view is at (7,9) is there a formula that i can just punch those numbers (or some version of those numbers) into and come up with my position/view coordinates based on which direction i want to move?”

Sure, we’ll do it trig style…
take the delta (difference) between them
(7-3, 9-5) = (4,4)
take the arctan of that (use new_angle=atn2(4,4) add + (for strafe left) or -(for strafe right) pi/2 radians to the angle (new_angle) you got from atn2

Now, you’ve got your original position (3,5) just add
cos(new_angle) * distance_to_strafe to the x component, and
sin(new_angle) * distance_to_strafe to the y component

Tada! (hope it all works, and) hope you can visualize it. You basically take that direction you’re viewing, find the angle on the unit circle, then + or - 90 degrees (pi/2 radians) to the angle. Then you multiply that new unit circle direction by how far you want to move, then offset your original position.

Any q’s? (Btw, this is probably a really slow way to do this, and it can be done with matricies and all, which is what everyone else is telling you, but hey - figure out what way you understand, then move to a “better” or faster way)

-Michael g.

ps. The position for your view coordinate should obviously also be offset by the strafe value, if you’re trying to continue to view in the same direction.

Theoretically, one could use the modelview matrix generated by gluLookat (or any matrix for that matter) to strafe left by an amount of l.

double m;
double l;
glGetDoublev(GL_MODELVIEW_MATRIX, m);
glTranslated(lm,lm,l*m);

I have used similar techniques, but haven’t tried this one.