1

I am currently working on block-breaking in a Minecraft clone. In order to do so, I want use the camera's Euler angles to determine the direction vector of the camera's ray.

I have 2 angles: pitch and yaw. Pitch rotates along the X axis and is positive when the player looks down, and negative when the player looks up. It cannot be greater than 90 degrees (looking straight down) and cannot be smaller than -90 degrees (looking straight up)

Yaw rotates along the Y axis. It can be a negative number and a positive number, depending on how many times the player turned and in which direction. For example, the player spawns in (yaw = 0) and instantly spins counter-clockwise 360 degrees. In such a case, yaw = -360.

As stated in the question, I am using OpenGL, so when yaw = 0, the player is looking down the negative Z-axis.

How can I generate a direction vector (must be a unit-vector) using only the Euler angles with the restrictions described above?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Serket
  • 3,785
  • 3
  • 14
  • 45
  • 1
    if you are in OpenGL and have your camera matrix you can extract the z axis direction vector directly from it ... see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) ... just take in mind camera matrix is inverse in native OpenGL notation (`MVP = model*Innverse(view)*projection`) so you need to separate your non inversed `view` matrix and extract the direction vector from it (or construct it not inversed by applying the rotations/translations with negative angles and reverse order). – Spektre Apr 28 '21 at 07:14
  • Anyway as you most likely want to select voxel at screen center position I would instead use this: [OpenGL 3D-raypicking with high poly meshes](https://stackoverflow.com/a/51764105/2521214) – Spektre Apr 28 '21 at 07:20
  • @Spektre could you write a proper answer since I still haven't found a solution? Also, what do you mean by "you most likely want to select voxel at screen center position:, because that seems to be my main issue? – Serket Apr 28 '21 at 12:53
  • Answer to what? 1. obtaining central direction (in case of perspective projection the projection must be centered too) from `MVP` or `MV*P` (do you have separate `M,V,P` or `MV,P` or `MVP`... is it in OpenGL native notation?) I could construct asimple C++/OpenGL example however it would be best to see how you setting the matrices and in case you transform non standartly also the transforming equation/code so I can mimic your case exactly... 2. using ray picking (that linked answer from previous comment) more or less covers it so I see no point in creating another answer. – Spektre Apr 29 '21 at 07:12
  • You simply render the voxels into 2 buffers (or twice to the same one) once with index instead of color and then with color and just pick the index at screen center position. Just note you can not use Stencil as its only 8bit these days so use FBO or directly frame buffer ... This way you got directly the hit voxel index and do not need to compute/ray cast the intersection between ray and your voxel grid ... – Spektre Apr 29 '21 at 07:14
  • also take a look at this [How to show visible part of planar world rendered with 3D perspective on topside 2D minimap?](https://stackoverflow.com/a/52475105/2521214) the first part of it is more or less the same as #1 taking direction vector from corners of screen (instead of your center) – Spektre Apr 29 '21 at 08:17

1 Answers1

2

From your description of Pitch and Yaw, I am assuming that you are using a left-hand reference (pitch=0 and yaw=0 gives a camera vector (0.0, 0.0, 1.0).

The Pitch parameter moves the camera vector in the YZ plane :

Y = cos(pitchInRadians)
Z = sin(pitchInRadians)

The Yaw parameter moves the camera vector in the XZ plane :

X = sin(yawInRadians)
Z = cos(yawInRadians)

Combining the two would give you your final camera vector :

Cv = (sin(yaw), cos(pitch), sin(pitch)*cos(yaw))

As you should have noted, the angles are in radians. Since your post mentions angles in degrees, you will have to convert your degree angles to radians first :

radians = (degrees * PI) /180
Mickaël C. Guimarães
  • 1,020
  • 2
  • 14
  • 32