Crystal Space
Welcome, Guest. Please login or register.
December 18, 2014, 03:39:11 pm

Login with username, password and session length
Search:     Advanced search
9032 Posts in 2046 Topics by 9288 Members
Latest Member: Lorrainewhite
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  General Crystal Space Discussion
| | |-+  How to rotate an object around itself?
« previous next »
Pages: [1] Print
Author Topic: How to rotate an object around itself?  (Read 3911 times)
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« on: April 01, 2006, 09:40:31 am »

This:
Code:
- - - -
csRef<iMovable> cube_movable = cube->GetMovable ();
cube_movable->GetTransform ().RotateThis (csVector3 (0, 1, 0), 0.7853);
- - - -
rotates a thing named "cube" 45 degrees, but around center of world space. How to rotate it around itself?

Thanks.
Logged

jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: April 01, 2006, 02:25:30 pm »

This:
Code:
- - - -
csRef<iMovable> cube_movable = cube->GetMovable ();
cube_movable->GetTransform ().RotateThis (csVector3 (0, 1, 0), 0.7853);
- - - -
rotates a thing named "cube" 45 degrees, but around center of world space. How to rotate it around itself?

Thanks.

Try RotateOther() instead of RotateThis()?

Greetings,
Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #2 on: April 15, 2006, 03:43:29 pm »

I have:
Code:
- - - -
    csRef<iMeshWrapper> cube = engine->CreateThingMesh (room, "cube");
    csRef<iMeshObject> cube_object = cube->GetMeshObject ();
    csRef<iMeshObjectFactory> cube_factory = cube_object->GetFactory ();
    csRef<iThingFactoryState> cube_state = scfQueryInterface<iThingFactoryState> (
            cube_factory);

    cube_state->AddOutsideBox (csVector3 (7, 0, 7), csVector3 (9, 3, 9));
- - - -
    csRef<iMovable> cube_movable = cube->GetMovable ();
    csVector3 xyz = cube_movable->GetFullPosition ();
    cout << "xyz = (" << xyz.x << ", " << xyz.y << ", " << xyz.z << ")" << endl;
    cube_movable->GetTransform ().RotateThis (csVector3 (0, 1, 0), 45 * PI / 180 );
    cube_movable->UpdateMove ();
- - - -
Before-Rotate-This Screenshot
After-Rotate-This Screenshot
So how to rotate the object around itself?

Thanks.
Logged

jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #3 on: April 15, 2006, 04:07:31 pm »

Tried RotateOther? Also it is best to make sure the box is centered around 0,0,0. That way it is a lot easier to make it rotate around itself.

Greetings,
Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #4 on: April 15, 2006, 04:11:21 pm »

What do you mean saying centered around 0, 0, 0? GetFullPosition () returns 0, 0, 0.
RotateOther, as described in the first message, doesn't work too.

Thanks.
Logged

jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #5 on: April 15, 2006, 04:54:35 pm »

What do you mean saying centered around 0, 0, 0? GetFullPosition () returns 0, 0, 0.
RotateOther, as described in the first message, doesn't work too.

Thanks.

You are doing this:

cube_state->AddOutsideBox (csVector3 (7, 0, 7), csVector3 (9, 3, 9));

That will make a box with center (8,1.5,8). So if you rotate the box it will rotate around 0,0,0 which is not the center of the box. So you should make a box which has 0,0,0 as the center by doing the right AddOutsideBox.

Greetings,
Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #6 on: April 15, 2006, 05:00:42 pm »

I should first make a box at 0,0,0; rotate it; and eventually move to the corner? Well, it seems very stupid.
Is there any way to rotate around itself?
Or am I misunderstanding you?

Thanks.
Logged

jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #7 on: April 15, 2006, 05:04:11 pm »

I should first make a box at 0,0,0; rotate it; and eventually move to the corner? Well, it seems very stupid.
Is there any way to rotate around itself?
Or am I misunderstanding you?

Thanks.

No the box can be at any position. It is just that you need to make it so that it's local center is at 0,0,0. That way you can rotate it around itself while the box is at any position in the world. A good box that is centered at 0,0,0 can be made with (for example):

cube_state->AddOutsideBox (csVector3 (-3, -1, -3), csVector3 (3, 1, 3));

That box is centered around 0,0,0 and you can put that at any position you want.

Greetings,
Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #8 on: April 15, 2006, 05:06:35 pm »

But it *is* located in the center of the world, isn't it?
So I need to create all objects in the center of the world and then move it?
Logged

jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #9 on: April 15, 2006, 05:11:01 pm »

But it *is* located in the center of the world, isn't it?
So I need to create all objects in the center of the world and then move it?

Hmm, how to explain...

You create the object so that in its own coordinate system it is centered around 0,0,0. i.e. with the last box coordinates I gave you have a box centered around 0,0,0. But that is not 0,0,0 in the world! That 0,0,0 is local space for the object. You can move that object to any place you want at any time and you can also rotate that object around itself at any time (even when the object itself is at another position). So the object can be at 3,4,5 (for example) while it is still centered around it's own local 0,0,0 origin.

Greetings,
Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #10 on: April 15, 2006, 05:28:55 pm »

Thanks. Now all works Smiley
Here's a "full" code if anyone is interested:
Code:
- - - -
    // It's training in using thing mesh obj
    csRef<iMeshWrapper> cube = engine->CreateThingMesh (room, "cube");
    csRef<iMeshObject> cube_object = cube->GetMeshObject ();
    csRef<iMeshObjectFactory> cube_factory = cube_object->GetFactory ();
    csRef<iThingFactoryState> cube_state = scfQueryInterface<iThingFactoryState> (
            cube_factory);

    // create a cube at (0, 0, 0) local position, then we'll move it to the necessary
    // place
    //cube_state->AddOutsideBox (csVector3 (7, 0, 7), csVector3 (9, 3, 9));
    cube_state->AddOutsideBox (csVector3 (-1, -1.5, -1), csVector3 (1, 1.5, 1));

    cube_state->SetPolygonMaterial (csPolygonRange (1, 5), cube_tex_wall_mw);
    cube_state->SetPolygonTextureMapping (csPolygonRange (1, 5), 1);

    cube_state->SetPolygonMaterial (csPolygonRange (0, 0), cube_tex_mw);
    cube_state->SetPolygonTextureMapping (csPolygonRange (0, 0),
            csVector2 (0, 0),
            csVector2 (1, 0),
            csVector2 (1, 1)
            );

    // It's training in moving a mesh

    csRef<iMovable> cube_movable = cube->GetMovable ();
    cube_movable->SetPosition (csVector3 (7, 1.5, 7));
    csVector3 xyz = cube_movable->GetFullPosition ();
    cout << "xyz = (" << xyz.x << ", " << xyz.y << ", " << xyz.z << ")" << endl;
    cube_movable->GetTransform ().RotateThis (csVector3 (0, 1, 0), 45 * PI / 180 );
    cube_movable->UpdateMove ();
- - - -
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.176 seconds with 16 queries.