rotate node based on mouse pos

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
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

rotate node based on mouse pos

Post by Yustme »

Hi,

How can I rotate a node around it's Y-axis based on the position of the mouse?

For example, if I mouse the mouse to the left, I want the node rotate on it's Y-axis to the right.

Thanks in advance!
squisher
Competition winner
Posts: 91
Joined: Sat May 17, 2008 2:23 am
Contact:

Post by squisher »

In node's constructor:

Code: Select all

    
    cursor = device->getCursorControl();
    cursor->grab();
    cursor->setVisible(false);
    cursorCenterPos = cursor->getRelativePosition();

In node's update method (or animation method, w/e you want to call it)

Code: Select all

    if (cursor)
    {
        position2d<f32> cursorDelta = cursor->getRelativePosition() - cursorCenterPos;
        
        if (cursorDelta.X || cursorDelta.Y)
        {
            deltaYaw = cursorDelta.X * cMouseSensitivity;
            deltaPitch = -cursorDelta.Y * cMouseSensitivity;
            cursor->setPosition(cursorCenterPos);
            cursorCenterPos = cursor->getRelativePosition();
        }
    }

    rotate(vector3df(0, deltaYaw, 0));
    rotate(vector3df(deltaPitch, 0, 0));
And your rotate function (thanks arras) is this:

Code: Select all

void rotate(vector3df rot)
{
    matrix4 m;
    m.setRotationDegrees(node->getRotation());
    matrix4 n;
    n.setRotationDegrees(rot);
    m *= n;
    node->setRotation( m.getRotationDegrees());
    node->updateAbsolutePosition();
}
Hope that helps. There are plenty other ways to do this too... this is just how I do it.[/code]
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Post by Yustme »

Hi squisher,

Thank you very much for the code snippet.

Works like a charm!
Post Reply