[solved] Getting The Camera's 'AimVector'

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

[solved] Getting The Camera's 'AimVector'

Post by kklouzal »

I'm trying to get the 'AimVector' for the camera which in essence would just be a vector3df() with values in the range of 0,0,0 and 1,1,1.

Basically I need to have a function that takes the current rotation of the camera and applies some maths then spits out a vector3df as described previously. I've done some research on this and I thing it needs to call Sin and Cos on the .X and .Y values but I'm not completely sure and I cant seem to figure out the correct combination on my own.

Any help here from the maths geniuses would be most appreciated.
Thank you for your time!
Last edited by kklouzal on Thu Aug 14, 2014 8:11 pm, edited 2 times in total.
Dream Big Or Go Home.
Help Me Help You.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Getting The Camera's 'AimVector'

Post by Seven »

will this give you what you are looking for?

Code: Select all

 
vector3df aimVector = camera->getTarget() - camera->getPosition();
aimVector.normalize();
 
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

I don't actually have access to the camera as this is being ran on the server-side, I'm sure there is a way to get it with sin() cos() I just for the life of me cant figure it out.

I believe it its:

Code: Select all

 
irr::core::vector3df CameraRotation();
irr::core::vector3df AimVector();
 
AimVector.X = Cos(CameraRotation.X);
AimVector.Y = Sin(CameraRotation.Y);
 
But what about the Z axis??
Dream Big Or Go Home.
Help Me Help You.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Getting The Camera's 'AimVector'

Post by mongoose7 »

vector3df(cos(Y)*cos(X), sin(Y)*cos(X), sin(X))
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

That is definitely not working :P

This is *almost* working, it seems to be quite inaccurate though, nor does it take into account the Z-Axis!

Code: Select all

irr::core::vector3df(cos(irr::core::degToRad(Rot.Y)), sin(irr::core::degToRad(Rot.X)), Rot.Z)
Dream Big Or Go Home.
Help Me Help You.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Getting The Camera's 'AimVector'

Post by Foaly »

What about rotationToDirection?
http://irrlicht.sourceforge.net/docu/cl ... a19278474d

irr::core::vector3df cameraDir = rot.rotationToDirection();
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

That's almost exactly the opposite of what I'm trying to do.

I want to input my rotation vector and output the forward vector.

EDIT::

Maybe it would help if I explained the context of what I'm trying to do. I'm trying to do a raytrace from the players position 1000 units out from where he is looking.
I only have access to the players position and the players rotation, based off that I should be able to get a "forward vector" to apply to the equation to figure out my ray's end position.
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

Entity has most graciously posted the highschool grade trig to solve this most simple issue I could not wrap my preschool sized brain around:

Code: Select all

core::vector3df(sin(core::degToRad(rot.Y)), -sin(core::degToRad(rot.X)), cos(core::degToRad(rot.Y))).normalize();
Dream Big Or Go Home.
Help Me Help You.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Getting The Camera's 'AimVector'

Post by Foaly »

Did you try my solution? I still think it's the right answer.
vector3df::rotationToDirection() converts a rotation in euler angles (that's the way scene nodes store it) to a normalized direction vector.

If you want to do the opposite (as you said), you can try vector3df::getHorizontalAngle(), which converts a direction to a euler rotation.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

Foaly I tried to use rotationToDirection() but no matter how I use it when I visualize the raytrace the lines fly out in random directions. This is what I've got at the moment.

Code: Select all

    irr::core::vector3df Rot = GetOwner()->GetRotation();
    irr::core::vector3df StartPos(GetOwner()->GetPosition().x(), GetOwner()->GetPosition().y(), GetOwner()->GetPosition().z());
    //irr::core::vector3df Forward(sin(irr::core::degToRad(Rot.Y)), -sin(irr::core::degToRad(Rot.X)), cos(irr::core::degToRad(Rot.Y)));
    irr::core::vector3df Forward = StartPos.rotationToDirection();
    irr::core::vector3df EndPos = StartPos + (Forward * 1000);
If I uncomment the first Forward and comment the second then the rays trace where I'm looking, but if I look up or down past 90 degrees then it starts to get very inaccurate. using rotationToDirection must require a different formula to get EndPos that I can't figure out.. -.-
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Getting The Camera's 'AimVector'

Post by kklouzal »

This is what finally got perfect results:

Code: Select all

 
    irr::core::vector3df Rot = GetOwner()->GetRotation();
    irr::core::vector3df Forward = irr::core::vector3df(0, 0, 1);
    irr::core::matrix4 m;
    m.setRotationDegrees(Rot);
    m.rotateVect(Forward);
    irr::core::vector3df StartPos(GetOwner()->GetPosition().x(), GetOwner()->GetPosition().y(), GetOwner()->GetPosition().z());
    irr::core::vector3df EndPos = StartPos + (Forward * 1000);
Dream Big Or Go Home.
Help Me Help You.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: [solved] Getting The Camera's 'AimVector'

Post by Seven »

i didnt understand the question before........... here is another way to do it.

http://irrlicht.sourceforge.net/forum/v ... in#p288921

in your case your would just use getPosition() as the start of the line, and getIn() * 1000 as the end.
Post Reply