I need to do a raycast which accepts a Vector3D StartPos and a Vector3D EndPos.

I have available to me the characters position and the cameras rotation in 3D vectors.

I need to somehow compute a 'forward heading' from the cameras rotation and apply it to the players position and multiply by some large number to get my EndPos vector however I can't figure out the math behind it, I know I can use sin() and cos() here which is the approach i'd like to take.

irr::core::vector3df CamRotation = GetOwner()->GetRotation();
irr::core::vector3df StartPos(GetOwner()->GetPosition().x(), GetOwner()->GetPosition().y(), GetOwner()->GetPosition().z());
irr::core::vector3df EndPos(0, 0, 0);

I've experimented around with using sin() and cos() in different combinations throwing in negative signs into places but quite frankly I don't know how to properly use those functions.


2 Answers 2


You can do the trig functions yourself if you want, but it's a lot easier to use a rotation matrix. In the background it will do the exact same sin/cos stuff, but it's already programmed for you, so why redo it?

I'm not too familiar with Irrlicht (or C++), but adapting some code I found on their forums, it'll probably look something like this:

irr::core::vector3df camRotation = GetOwner()->GetRotation();
irr::core::vector3df forward(0, 0, 1);
irr::core::matrix4 m; 
  • \$\begingroup\$ Matrices would be a better solution however the whole issue here is figuring out the "forward" vector based on the cameras rotation. \$\endgroup\$
    – KKlouzal
    Aug 14, 2014 at 19:24
  • \$\begingroup\$ Wait, what? Matrices are a better solution, but they're not a solution at all, because they don't solve the issue that they're a better solution for? (I have no idea what you're talking about.) \$\endgroup\$ Aug 14, 2014 at 19:45
  • \$\begingroup\$ Maybe then I fail to see how to extract a position out of the matrix. From what I can see going on is the matrix is taking the cameras rotation and rotating it by the forward vector when what needs to happen is take the cameras rotation and figure out the forward vector to apply to the cameras position then multiply out 1000 units to get a new position which is 1000 units in front of the camera in the direction it's facing. I'm sorry if I'm confusing, this is confusing me too. \$\endgroup\$
    – KKlouzal
    Aug 14, 2014 at 19:52
  • \$\begingroup\$ You're reading it backward. It's taking a forward vector (0, 0, 1) and rotating it with the camera's rotation. So if the camera's rotation is 90 degrees to the left, forward will be (-1, 0, 0) at the end. If you want to get 1k units in front of the camera instead of just 1, then multiply forward by 1k afterward. \$\endgroup\$ Aug 14, 2014 at 20:04
  • \$\begingroup\$ Oh! That's exactly what I need facepalm let me try to integrate it and ill accept your answer. :) \$\endgroup\$
    – KKlouzal
    Aug 14, 2014 at 20:06

This seems to get the job done, special thanks to a forum member Entity for this answer:

core::vector3df(sin(core::degToRad(rot.Y)), -sin(core::degToRad(rot.X)), cos(core::degToRad(rot.Y))).normalize();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .