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

0

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)?

asked 6 years ago212 views
15 Answers
0
Accepted Answer

Hi @REDACTEDUSER

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
answered 6 years ago
0

Thank @REDACTEDUSER

answered 6 years ago
0

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

answered 6 years ago
0

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 :)

answered 6 years ago
0

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

answered 6 years ago
0

Thank Anatoliy, I see that there is function

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

answered 6 years ago
0

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

answered 6 years ago
0

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);
answered 6 years ago
0

Thank @REDACTEDUSER

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

answered 6 years ago
0

all ok :)

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.

answered 6 years ago
0

Thank @REDACTEDUSER

answered 6 years ago
0

"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 :)

answered 6 years ago
0

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

answered 6 years ago
0

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

answered 6 years ago
0

Is your "m_sliceAssetId" AZ::EntityId?

answered 6 years ago

This post is closed: Adding new answers, comments, and votes is disabled.