About Store Forum Documentation Contact



Post Reply 
[SOLVED] Rotate an object around another
Author Message
rstralberg Offline
Member

Post: #1
[SOLVED] Rotate an object around another
Hey. I'm a C++ programmer, but definitely not a mathematician.
So I need some help.

I want to rotate a object A around another object B at distance D.
Tested a bit with various Matrix commands and finally came to the solution.
"I need help" smile

Could anyone show the magic lines

Code:
void rot( Game.Obj& A, Game.Obj& B, Flt D )
{
     // rotate B around A at distance D
     // ????
}
(This post was last modified: 07-04-2015 08:17 PM by rstralberg.)
07-04-2015 12:39 PM
Visit this user's website Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #2
RE: Rotate an object around another
Off the top of my head:
---------------------------------------------------------------------------
global variables:
Vec offset_new;
Vec offset;
----------------------------------------------------------------------------
Init:
offset = Vec( (position of A) - (position of B) );
----------------------------------------------------------------------------
Update:
Matrix3 m1;
m1.identity();
m1.setRotateY(Time.time());
offset_new=offset.mul(m1);
-----------------------------------------------------------------------------
Draw/Render:
Matrix m;
m.setPos(offset_new);

Draw A using m
------------------------------------------------------------------------------
(This post was last modified: 07-04-2015 05:37 PM by 3DRaddict.)
07-04-2015 05:36 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #3
RE: Rotate an object around another
Code:
Object needs some variables
Flt angle

/*Vec2*/ void functionRot(Game.Obj &cent, Game.Obj &point,Flt dist, Flt dang=30*Time.d())
{
   point.angle+=DegToRad(dang); // Convert it to radians
   Flt rotX = Cos(angle) * (dist - cent.x) - Sin(angle) * (dist-cent.z) + cent.x;
   Flt rotZ = Sin(angle) * (dist - cent.x) + Cos(angle) * (dist - cent.z) + cent.z;
   point.pos(rotX,cent.pos.y/*or point.pos.y*/,rotZ);
   //return Vec2(rotX, rotZ);
}
(This post was last modified: 07-04-2015 06:48 PM by Zervox.)
07-04-2015 06:41 PM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #4
RE: Rotate an object around another
Thanks both of you.
You made my day. Now I got my thing working.
In my case I needed rotation around y


Code:
// Rotate an object around a point in space (X-axis)
   static void rotate_around_x( Game.Obj* obj,  C Vec& point,  Flt angle_degree )
   {
      Matrix m;
      m  .identity()
      .setRotateX( DegToRad(angle_degree));
        
      m.setPos( Vec(obj->pos()-point).mul(m)+point);
      obj->matrix(m);
   }
(This post was last modified: 07-04-2015 09:33 PM by rstralberg.)
07-04-2015 08:13 PM
Visit this user's website Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #5
RE: [SOLVED] Rotate an object around another
Glad it all worked out for you
07-05-2015 09:36 AM
Visit this user's website Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #6
RE: [SOLVED] Rotate an object around another
Hey i'm just adding a quaternion solution here also.

Code:
// Rotate an object around a point in space (X-axis)
void rotate_around_x( Game.Obj* obj,  C Vec& point,  Flt angle_degree )
{
   Quaternion myRotation;
   myRotation.setRotateX(DegToRad(angle_degree));  
   obj->pos( Vec(obj->pos()-point) * myRotation + point );
}

I think this should have been faster but it's not and i don't know why.
(This post was last modified: 07-05-2015 06:34 PM by georgatos7.)
07-05-2015 06:25 PM
Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #7
RE: [SOLVED] Rotate an object around another
(07-05-2015 06:25 PM)georgatos7 Wrote:  I think this should have been faster but it's not and i don't know why.

What do you mean with faster?
07-05-2015 07:55 PM
Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #8
RE: [SOLVED] Rotate an object around another
(07-05-2015 07:55 PM)Tottel Wrote:  
(07-05-2015 06:25 PM)georgatos7 Wrote:  I think this should have been faster but it's not and i don't know why.

What do you mean with faster?

Well i mean that Quaternion multiplication should be faster than 3x3 matrix multiplication so it shouldn't be as cpu intensive (though i haven't gone through the specifics with the number of calculations) but i tried to benchmark this with a couple of hundred thousand calcs and it seems to be the opposite.
(This post was last modified: 07-05-2015 08:15 PM by georgatos7.)
07-05-2015 08:05 PM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #9
RE: [SOLVED] Rotate an object around another
Reading here http://devcry.heiho.net/2011/05/quaterni...mance.html its said

Quaternions are apparently terrific for combined rotations. If you hardly do combined rotations, matrices will be faster. If you do combined rotations all the time (like for animating skeletons and such) then you probably already knew that quaternions are the way to go

Maybe thats the reason?
(This post was last modified: 07-05-2015 08:22 PM by rstralberg.)
07-05-2015 08:21 PM
Visit this user's website Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #10
RE: [SOLVED] Rotate an object around another
Thanks for the link, i'll make sure i give it a look tommorow.
07-05-2015 11:15 PM
Find all posts by this user Quote this message in a reply
Post Reply