3d picking lwjgl

E2a735e125ecf3c0d2e202e406059888
0
Viped 101 Nov 03, 2012 at 12:25 java

Hi,

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:

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(fov, aspect, near, far);
  glMatrixMode(GL_MODELVIEW);
  glEnable(GL_DEPTH_TEST);

and I am trying to draw ammo like this:

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

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.

728d45552a7b4b6cfd14d41bd92f1d53
0
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