3d picking lwjgl

Viped 101 Nov 03, 2012 at 12:25 java


I am making my own first person shooter 3D engine. I am stuck with shooting. I know that I have somehow change my screen coordinates to world coordinates, am I right? This just involves matrixes and I cant do the math, not yet atleast.

More accurately, I have aiming cross at center of the screen and I want to shoot ray to there from players position and if it collides with something it could at this point just print something. All I think I need is to know how to do the conversion from x = WIDTH/2 y = HEIGHT/2 to my world coordinates. Atleast this is what I think I need for now.

There’s propably topics around this and I quickly searched but did’nt find anything I could use. Or how to implement it in my code.

My camera is set up like this:

  glRotatef(rx, 1, 0, 0);
  glRotatef(ry, 0, 1, 0);
  glRotatef(rz, 0, 0, 1);
  glTranslatef(x, y, z);

and OPENGL initialized like this:

  gluPerspective(fov, aspect, near, far);

and I am trying to draw ammo like this:

   glColor3f(1, 0, 0);
   glVertex3f(origin.x, origin.y, origin.z);
   glColor3f(0, 1, 0);
   glVertex3f(direction.x, direction.y, direction.z);

Problem is just that I dont know what are those origin and direction coordinates or how to calculate those.

1 Reply

Please log in or register to post a reply.

flokkienathur 101 Apr 20, 2013 at 05:26

The camera position and rotation is the exact location of where the center of your screen en the angle of your screen is.

If you use glTranslatef at the beginning of your drawing code then the given position here is the origin of the bullet. You can calculate the direction using the sinus and cosinus of the given angle. The angle of the bullet is equal to the glRotatef rotation.

If you wrote/ found a good vector class you can just use that for storing all the data.

It’s not much, its like 5am in the morning, but I hope it helps though