Page 1 of 1

How to turn character in the direction of the mouse?

Posted: Tue Oct 11, 2011 5:53 pm
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.

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

Posted: Tue Oct 11, 2011 6:34 pm
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. */

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

Posted: Tue Oct 11, 2011 7:42 pm
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)).

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

Posted: Wed Oct 12, 2011 6:37 pm
by AkylaQD
Thanks. getHorizontalAngle() it is what i need to rotate. Now i think about how better to programm walking character.