# 3d picking lwjgl

3d java

### #1Viped

New Member

• Members
• 1 posts

Posted 03 November 2012 - 12:25 PM

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);
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.

### #2flokkienathur

New Member

• Members
• 1 posts

Posted 20 April 2013 - 05:26 AM

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

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users