Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Rotate Around Local Axis

4: Adsense
  • You cannot reply to this topic
1 reply to this topic

#1 PyemanKOP   Members   

143
Like
0Likes
Like

Posted 21 April 2017 - 04:46 AM

Hi, I want to implement some sort of rotation gizmo,everything works find until i came to this problem that i want my object to rotate around its local axis.
what i mean is when you rotate and object around x axis 90 degrees its local Y and Z axis axis also rotates, so i want to rotate based on these new axis.
here is what i done so far:

axisX = DirectX::XMVector3Transform(axisX,
AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetWorldMatrix());
axisX = DirectX::XMVector3Normalize(axisX);
 
DirectX::XMVECTOR axisY = DirectX::XMVectorSet(0, 1, 0, 1);
axisY = DirectX::XMVector3Transform(axisY,
AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetWorldMatrix());
axisY = DirectX::XMVector3Normalize(axisY);
 
DirectX::XMVECTOR axisZ = DirectX::XMVectorSet(0, 0, 1, 1);
axisZ = DirectX::XMVector3Transform(axisZ,
AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetWorldMatrix());
axisZ = DirectX::XMVector3Normalize(axisZ);

What above code do is rotate each axis based on object rotation. when i draw  a line for each axis they are in correct direction but when i try to rotate around them everything goes wrong!

here is what i do to rotate:

AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetRotation()->m_x += (axisInObject.m128_f32[0] * angle * axisX.m128_f32[0]);


AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetRotation()->m_y += (axisInObject.m128_f32[0] * angle * axisX.m128_f32[1]);


AngelCore::AngelSubSystem::SubSystemManager::GetInstance()->GetActorFactory()->
GetActors()[0]
->GetComponent<AngelCore::AngelWorldSystem::Transform>()->GetRotation()->m_z += (axisInObject.m128_f32[0] * angle * axisX.m128_f32[2]);

Above code is just for axis X. but its not working! what is wrong with that?

note that "axisInObject" is direction where user dragged mouse after click on axis X of rotation gizmo and angle is how many degrees he or she rotates that.

thanks.


Edited by PyemanKOP, 21 April 2017 - 04:49 AM.


#2 BBeck   Members   

815
Like
0Likes
Like

Posted 21 April 2017 - 06:56 AM

I don't have time to read through the code right now, but what determines whether you rotate on the global or local axis is the order of multiplication between the world matrix and the rotation matrix. Switch the order of multiplication if it's not working the way you want.

 

I happen to be on the wrong computer right now where I can't get to my own source code to find an example, but you may want to download and look at my DX11 3D "engine". That code does local rotations. It's on my website at VirtuallyProgramming.com and has the full VS project including shader code, Python Blender exporter code to export models, art assets, etc. available in one download file. So, there's a lot to look at there for examples. And with a bit of work it should compile although it's coded for Windows 7 and not Windows 8 or Windows 10 (which have to be coded slightly different). Still rotations are the same no matter what environment you are in.

 

You may also want to watch my vector and matrix videos on my YouTube channel probably in that order if you are not super familiar with those Linear Algebra topics.

 

I wish I were on my computer where I could copy and paste a code example, but it would have been from the code I linked above anyway. But the bottom line is that you just have to reverse the order of multiplication. When that is not possible, usually because you are using someone else's library that doesn't allow you to do the math yourself, there is a work around that works fine. And that is to move the object to the center (origin), do the rotation, and then move it back to where it was. Works like a charm and you do it in a single frame, so it is undetectable to the user. That always works too.