rstralberg
Member
|
[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"
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 |
|
3DRaddict
Member
|
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 |
|
Zervox
Member
|
RE: Rotate an object around another
Code:
Object needs some variables
Flt angle
/*Vec2*/ void functionRot(Game.Obj ¢, 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 |
|
rstralberg
Member
|
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 |
|
3DRaddict
Member
|
RE: [SOLVED] Rotate an object around another
Glad it all worked out for you
|
|
07-05-2015 09:36 AM |
|
georgatos7
Member
|
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 |
|
Tottel
Member
|
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 |
|
georgatos7
Member
|
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 |
|
rstralberg
Member
|
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 |
|
georgatos7
Member
|
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 |
|