Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to cast a ray from the center of the screen and check for collisions with objects.

When rendering, I use these calls to set up the camera:

GL11.glRotated(mPitch, 1, 0, 0);
GL11.glRotated(mYaw, 0, 1, 0);
GL11.glTranslated(mPositionX, mPositionY, mPositionZ);

I am having trouble creating the ray, however. This is the code I have so far:

ray.origin = new Vector(mPositionX, mPositionY, mPositionZ);
ray.direction = new Vector(?, ?, ?);

My question is: what should I put in the question mark spots? I.e. how can I create the ray direction from the pitch and roll?

Any help would be much appreciated!

share|improve this question
up vote 3 down vote accepted

You can treat this as a conversion from spherical coordinates to Cartesian coordinates.

Assuming the ray should have some length r, your x, y, and z components can be calculated like this:

x = r * sin(mPitch) * cos(mYaw)
y = r * sin(mPitch) * sin(mYaw)
z = r * cos(mPitch)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.