How to properly get/set transform (position/rotation/scale) from entity id in Lumberyard?

Currently, if I want to get or set the worldTM of an entity (entity id), I do this

            AZ::TransformBus::EventResult(playerTm, m_pPlayerComponent,
&AZ::TransformBus::Events::GetWorldTM);//Get
EBUS_EVENT_ID_RESULT(playerTm, m_pPlayerComponent, AZ::TransformBus, GetWorldTM);//Get
EBUS_EVENT_ID(m_pCameraComponent, AZ::TransformBus, SetWorldTM, cameraTm);//Set

But I don’t know if it’s the proper way, is there a better way? How to do basic thing like get/set position/rotation/scale of an entity (from its id)?

Thank @Anatoliy very much, please edit the answer so that I can accept your answer.

Wait a minute, your Set method for position/rotation/scale will remove the others, won’t it?

I think the scale, rotation, scale is all bundle inside WorldTM(), Do you know how to update each of them? This question is about update each of them :slight_smile:

Yes just use Interface inside AZ::Transform “dev\Code\Framework\AzCore\AzCore\Math\Transform.h”

Thank Anatoliy, I see that there is function

SetRotationPartFromQuaternion(), so this could be the way to set rotation, but how about getting rotation?

AZ::Quaternion quat = AZ::Quaternion::CreateFromTransform(someTransform);

Hi HDN, Looking good.If i try create get Transform and then set him to different Entity i create code like you.

EBUS_EVENT_ID_RESULT(playerTM, m_PlayerEntityId, AZ::TransformBus, GetWorldTM);
EBUS_EVENT_ID(m_cameraEntityId, AZ::TransformBus, SetWorldTM, playerTM);

Thank @Anatoliy , for future reference’s sake, I think I’ll summary the whole thing after I fully discover how to get/set position/rotation/scale.

I really want to accept your answer, but your answer doesn’t show how to get/set each item: position/rotation/scale.

all ok :slight_smile:

First Get Second Set;

Position

AZ::Vector3 position = someTransform.GetTranslation();
AZ::Transform newTransform = AZ::Transform::CreateTranslation(newPosition)<br>

Rotation

AZ::Quaternion quat = AZ::Quaternion::CreateFromTransform(someTransform);
AZ::Transform newTransform = AZ::Transform::CreateFromQuaternion(quat);

Scale

AZ::Vector3 scale = someTransform.ExtractScale();
AZ::Transform newTransform = AZ::Transform::CreateScale(newScale);

p.s

About scale i trying not working with scale.

p.s. 2

If you want understand how work with Transform Matrix And Quaternions read in math docs it is very big part math.

Thank @Anatoliy I mean: edit the answer, so future visitor can directly see the answer :slight_smile:

“Wait a minute, your Set method for position/rotation/scale will remove the others, won’t it?”

Can`t understand ? You mean if i set position i remove rotation ? If i understand correctly, you must combine all transforms to one using multiplication.

Example:

AZ::Transform onlyPosition = ...;
AZ::Transform onlyRotation = ...;
AZ::Transform rotationAndPosition = onlyPosition * onlyRotation;

Read more about Transform Matrix And Quaternions on math docs.

About accepting don`t worry :slight_smile:

Hi @HDN,

You don’t need the whole GetWorldTM (believe me, I’m us).

Here is the code to get/set position/rotation/scale

        //The player
AZ::EntityId m_PlayerComponentID;
//Edit scale
AZ::Vector3 vScale;
EBUS_EVENT_ID_RESULT(vScale, m_PlayerComponentID, AZ::TransformBus, GetLocalScale);//Get scale
EBUS_EVENT_ID(m_PlayerComponentID, AZ::TransformBus, SetLocalScale, vScale);//Set scale
//Edit position
AZ::Vector3 vPos;
EBUS_EVENT_ID_RESULT(vPos, m_PlayerComponentID, AZ::TransformBus, GetLocalTranslation);//Get position
EBUS_EVENT_ID(m_PlayerComponentID, AZ::TransformBus, SetLocalTranslation, vPos);//Set position
//Edit rotation
AZ::Quaternion quatPlayerRotation;
EBUS_EVENT_ID_RESULT(quatPlayerRotation, m_PlayerComponentID, AZ::TransformBus, GetWorldRotationQuaternion);//Get rotation
EBUS_EVENT_ID(m_PlayerComponentID, AZ::TransformBus, SetLocalRotationQuaternion, quatPlayerRotation);//Set rotation

You understand correctly, but I found a better way to edit the transform (please check my answer). Thanks for your help.

Thanks for the info, I didn’t want my question to clutter the answer space.

Is your “m_sliceAssetId” AZ::EntityId?