Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I implement strafe using opengl in java?

Tags:

camera

opengl

This is my gluLookAt setup:

 glu.gluLookAt(x,y,z,    // eye location
               x + Math.cos(Math.toRadians(eyeAngle)),
               y,
               z - Math.sin(Math.toRadians(eyeAngle)), // point to look at (near middle)
               0.f,1.f,0.f); // the "up" direction

The x, y, z are the coordinates and change when turning left and right and moving forward and backward. But I'm having difficulty implementing strafe right and left. Any help would be appreciated.

like image 306
John Avatar asked May 20 '26 20:05

John


1 Answers

Finally figured everything out. If you have move forward and turn left and right, then strafe is actually simple. Strafe right, for example, is basically you turning 90 degrees to the right, moving forward, and then turning 90 degrees back to the left.

My forward was:

x += distance*Math.cos(Math.toRadians(eyeAngle)); 
z += -distance*Math.sin(Math.toRadians(eyeAngle));

You don't want to change the eyeAngle, because you still want to look in the same direction. For strafe right, you want to move as if you turned 90 degrees to the right and then went forward, similarly for left. So right would be my forward method above with an angle 90 to the right of my original eyeAngle, but because you want to keep eyeAngle the same, you don't change the variable, you just add 90 to it.

So, the right strafe method would be:

    x += distance*Math.cos(Math.toRadians(eyeAngle+90)); 
    z += -distance*Math.sin(Math.toRadians(eyeAngle+90));

Left would be subtracting 90. It is actually pretty simple after you've got forward and the overall glulookat set up. Thanks for the help everyone, it did aid me in understanding how it all works.

like image 70
John Avatar answered May 24 '26 04:05

John