Crystal Space
Welcome, Guest. Please login or register.
April 24, 2014, 12:26:11 am

Login with username, password and session length
Search:     Advanced search
8990 Posts in 2037 Topics by 7619 Members
Latest Member: Jessicaclayton
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Sprite rotates around wrong coordinate-system
« previous next »
Pages: [1] Print
Author Topic: Sprite rotates around wrong coordinate-system  (Read 1911 times)
macharry
Newbie
*
Posts: 13


View Profile
« on: September 14, 2006, 06:35:23 am »

Hi.

I want to rotate a sprite around its own axes. I have defined somes keyboard-functions, that increments / decrements the angle of the refering axis.
My problem is, that the rotation occurs always around the world coordinate system (axes are parallel to the room-axes) , not around the current axes of the sprite.

I checked the simpcd example,  and to my mind my code does exactly the same...

So: How do I rotate an object around its own coord-system?

Some Code:

  csReversibleTransform old_robot_transform =
    robot_sprite->GetMovable()->GetTransform();

  Pos_Rot_Type pos_d_rot   = Get_D_PosRot(); // get the rot-angles,  caused by key-strokes
 
  // only one of the angles is != 0 
  csXRotMatrix3 XRotMat (pos_d_rot.Rotation.Nick); // if the angle is zero, the matrix is idendity ??
  csYRotMatrix3 YRotMat (pos_d_rot.Rotation.Gier);
  csZRotMatrix3 ZRotMat (pos_d_rot.Rotation.Roll);

  if ( (pos_d_rot.Rotation.Nick != 0) |
       (pos_d_rot.Rotation.Gier != 0) |
       (pos_d_rot.Rotation.Roll != 0) )
    {
      csPrintf ("Nick / Gier / Roll / = %f %f %f;\n",
                pos_d_rot.Rotation.Nick,
                pos_d_rot.Rotation.Gier,
                pos_d_rot.Rotation.Roll);
    }
 
  robot_sprite->GetMovable()->Transform(YRotMat * XRotMat * ZRotMat); // put the rotations together
  Update_Act_PosRot(); // clear rot-angles from variable / clear keyb-inputs
  robot_sprite->GetMovable()->UpdateMove ();


Thanks a lot.
Harald
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1704


View Profile
« Reply #1 on: September 14, 2006, 07:09:45 am »

Check out the csReversibleTransform API. There are functions to transform in any space.

Greetings,
Logged
macharry
Newbie
*
Posts: 13


View Profile
« Reply #2 on: September 15, 2006, 06:11:34 am »

 Thanks Jorrit,

of course this was my first guess, but there must have been a problem between the keyboard and the chair...

For all interested here the solution:
A robot sprite could be moved around by keyboard inputs. Further on, this movement will be done by a math-model.
(Yes, if ever finished this will become a simulator for my robot-brains)

Code:
  csReversibleTransform old_robot_transform =
    robot_sprite->GetMovable()->GetTransform();

  Pos_Rot_Type pos_d_rot   = Get_D_PosRot(); // keyboard inputs increment / decrement rot-angles and position
 
 // Rotate the robot relative
  csXRotMatrix3 XRotMat (pos_d_rot.Rotation.Nick);
  csYRotMatrix3 YRotMat (pos_d_rot.Rotation.Gier);
  csZRotMatrix3 ZRotMat (pos_d_rot.Rotation.Roll);

  old_robot_transform.RotateThis (YRotMat * XRotMat * ZRotMat);
  robot_sprite->GetMovable()->SetTransform(old_robot_transform);

// move the robot relative
  csVector3 translation =
    old_robot_transform.This2Other(csVector3 (pos_d_rot.Position.Ix,
                                              pos_d_rot.Position.Iy,
                                              pos_d_rot.Position.Iz));
  robot_sprite->GetMovable()->SetPosition (translation);

// Makes no sense here, but shows how to get the whole transformation of the object.
// This is needed, in case of logging the transforms to disk (together with other values).
  csMatrix3 same_matrix =
    robot_sprite->GetMovable()->GetTransform().GetT2O();
  csVector3  same_vector =
    - robot_sprite->GetMovable()->GetTransform().GetT2OTranslation(); // negate the vector (why needed ??)
  csReversibleTransform same_transform (same_matrix, same_vector);
  robot_sprite->GetMovable()->SetTransform(same_transform); // just verify, its the same tranlation as before


  csPrintf ("vect  X / Y / Z = %f %f %f; \n",
            same_vector.x, same_vector.y, same_vector.z);
  csPrintf ("Trans X / Y / Z = %f %f %f; \n\n",
            translation.x, translation.y, translation.z);

  Update_Act_PosRot();  // clear keyboart inputs
  robot_sprite->GetMovable()->UpdateMove ();


Greetings,
Harald
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 6.804 seconds with 17 queries.