Rotate the object (not the camera) by mouse movement

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
PJjerry
Posts: 28
Joined: Mon Oct 24, 2005 6:57 pm

Rotate the object (not the camera) by mouse movement

Post by PJjerry »

My intention is to rotate an object in the scene according to the mouse movement; when moving verically the object would spin along the Z axis and horizontally along the Y axis.

Code: Select all

scene::ISceneNode* pobj = 0;
gui::ICursorControl* pmousecursor;

core::position2d<s32> mousepos_orn;
core::position2d<s32> mousepos_new;
core::position2d<s32> delta;
int div = 100;		//division

if ( pobj != 0  &&  event.EventType == irr::EMOUSE_INPUT_EVENT) {
    mousepos_orn = mousepos_new;
    mousepos_new = pmousecursor->getPosition();
    delta = mousepos_new - mousepos_orn;
	
    /* Mouse.Y = rotation along Z, Mouse.X = rotating along Y */
    pobj -> setRotation(core::vector3df( 0, static_cast<f32>(delta.X)/div, static_cast<f32>(delta.Y)/div) ); 
}

The problem is at the "if...." criteria statement...how do I correct it?
I've tried to find the manual and forum for several hours but still couldn't figure it out... any help?

Thanks!

.| PJjerry
PJjerry
Posts: 28
Joined: Mon Oct 24, 2005 6:57 pm

Post by PJjerry »

Nobody?

Any suggestion?


Thanks again...
Miik3h
Posts: 9
Joined: Mon Oct 17, 2005 1:08 am
Location: Australia

Post by Miik3h »

It looks like your in your custom event reciever, (event.EventType())...

So change it to...

Code: Select all

if(event.EventType == EET_MOUSE_INPUT_EVENT)
{
    static core::vector2di mouse0(event.MouseInput.X, event.MouseInput.Y);
    static float yaw = 0;
    static float roll = 0;

    core::vector2di mouse1(0, 0);

    if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
    {
         mouse1.X = event.MouseInput.X;
         mouse1.Y = event.MouseInput.Y;
            
         if(mouse1.X > mouse0.X)
         {
              yaw++;
              if(yaw > 360.0f)
              {
                   yaw -= 360.0f;
              }
         }   
         if(mouse1.X < mouse0.X)
         {
              yaw--;
              if(yaw > 360.0f)
              {
                   yaw += 360.0f;
               }
         }   
         if(mouse1.X > mouse0.X)
         {
              roll++;
              if(roll > 360.0f)
              {
                   roll -= 360.0f;
              }
         }   
         if(mouse1.X < mouse0.X)
         {
              roll--;
              if(roll > 360.0f)
              {
                   roll += 360.0f;
               }
         }
    }
}
Then set rotation using yaw and roll,

Hope this helps,
Miik3h
PJjerry
Posts: 28
Joined: Mon Oct 24, 2005 6:57 pm

Post by PJjerry »

Thank you so much!
It's a little late so I'll test it tomorrow~

Thanks again!! :D
PJjerry
Posts: 28
Joined: Mon Oct 24, 2005 6:57 pm

Post by PJjerry »

The code below is the one I used to solve my problem finally: :lol:

Code: Select all

static core::vector2d<s32> mousepos_orn (event.MouseInput.X, event.MouseInput.Y);
core::vector2d<s32> mousepos_new;
core::vector2d<s32> delta;
int div = -5;		//division

mousepos_new = core::vector2d<s32> ( event.MouseInput.X, event.MouseInput.Y );
delta = mousepos_new - mousepos_orn;
pobj -> setRotation(core::vector3df( 0, static_cast<f32>(delta.X)/div, static_cast<f32>(delta.Y)/div) ); 
.|PJjerry
Post Reply