Followers 0

# Rotate Around Local Axis

## 1 post in this topic

Posted (edited)

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
0

##### Share on other sites

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.

0

## Create an account

Register a new account