Raypicking with Mouse (Fixed Camera position working)

Hey Java-People,

i’ve got a problem and can’t find a solution. I’m working with LWJGL and OpenGL.

x and y are both values between [-1, 1] and work properly, if i add -ySinReal to the both underlaying lines, i can manipulate the ray against the y-axis in the center.


float hFov = c.getFov() * c.getAspectRatio();
float x = (Mouse.getX() - Display.getWidth() * 0.5f) / (Display.getWidth() * 0.5f);
float y = (Mouse.getY() - Display.getHeight() * 0.5f) / (Display.getHeight() * 0.5f);

//float xSinReal = (float) (x * (hFov * 0.5f));
float ySinReal = (float) (y * (c.getFov() * 0.5f) + Math.sin(y * Math.PI * 0.9f));

// - ySinReal
float pitch = (float) Math.toRadians(this.pitch);
float yaw = (float) Math.toRadians(this.yaw);

// pitch -> up / down
// yaw -> left / right
this.direction_x = (float) (Math.sin(yaw) * Math.cos(pitch));
//- ySinReal
this.direction_y = (float) (Math.sin(Math.toRadians(this.pitch - 180f)));
this.direction_z = -(float) (Math.cos(yaw) * Math.cos(pitch));

// -> at this point i have a ray with origin point and direction, which will perfectly work! (centered to camera)
// -> my intention is to manipulate x,y,z direction or yaw/pitch to move the ray to the position where my mouse points at (like the camera would move around and has the ray centered).

// other usable variables from my Ray class
this.x = x; // origin
this.y = y; // origin
this.z = z; // origin
this.yaw = yaw; // left/right
this.pitch = pitch; // up/down
this.roll = roll; // is allways 0


Did you have a suggestion how i can solve my problem to let the ray follow my mouse instead centered by camera?

Greetings
Spezies