How to turn character in the direction of the mouse?

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
AkylaQD
Posts: 2
Joined: Tue Oct 11, 2011 4:15 pm

How to turn character in the direction of the mouse?

Post by AkylaQD »

I new to this engine, and to gamedev. I want try create game with "diablo"-like camera and game management.
Maybe you have examples or you can tell me how it's easier to do?=).

My question is:
-How to turn character on a terrain in the direction of the mouse?

i find setRotation(); function but it take direction in a degrees, but i have a point(vector3df) on a terrain.

thx.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Re: How to turn character in the direction of the mouse?

Post by shadowslair »

You can try the core::vector::getHorizontalAngle() function. There`s even a small example in its description. Here`s a copy:
//! Get the rotations that would make a (0,0,1) direction vector point in the same direction as this direction vector.
                /** Thanks to Arras on the Irrlicht forums for this method.  This utility method is very useful for
                orienting scene nodes towards specific targets.  For example, if this vector represents the difference
                between two scene nodes, then applying the result of getHorizontalAngle() to one scene node will point
                it at the other one.
                Example code:
                // Where target and seeker are of type ISceneNode*
                const vector3df toTarget(target->getAbsolutePosition() - seeker->getAbsolutePosition());
                const vector3df requiredRotation = toTarget.getHorizontalAngle();
                seeker->setRotation(requiredRotation);
 
                \return A rotation vector containing the X (pitch) and Y (raw) rotations (in degrees) that when applied to a
                +Z (e.g. 0, 0, 1) direction vector would make it point in the same direction as this vector. The Z (roll) rotation
                is always 0, since two Euler rotations are sufficient to point in any given direction. */
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Re: How to turn character in the direction of the mouse?

Post by Iyad »

You can use irr::gui::getCursorControl* and call getPosition() so you can have the position of your mouse on the screen (in pixels), the top left corner of your window is 0,0 and the bottom right corner is X, Y. Since you know your characther is in the center of the screen, then you know its position is X/2 and Y/2 (pixel width and lenght of the window).
Image
Substract the position of mouse and character then you have v1 and with vx and vy you have the angle of rotation. Use setRotation(character rotation + vector3df(alpha,0,0)).
#include <Iyad.h>
AkylaQD
Posts: 2
Joined: Tue Oct 11, 2011 4:15 pm

Re: How to turn character in the direction of the mouse?

Post by AkylaQD »

Thanks. getHorizontalAngle() it is what i need to rotate. Now i think about how better to programm walking character.
Post Reply