Prevent the character to go out of bound in animation viewport (#7667)

* Prevent the character to go out of bound

Signed-off-by: rhhong <rhhong@amazon.com>

* CR feedback

Signed-off-by: rhhong <rhhong@amazon.com>
This commit is contained in:
Roman
2022-02-18 12:51:13 -08:00
committed by GitHub
parent 7de6bc5b23
commit 464b225042
3 changed files with 22 additions and 0 deletions
@@ -224,6 +224,22 @@ namespace EMStudio
return m_entityContext->GetContextId();
}
void AnimViewportRenderer::CheckBounds()
{
AZ::Vector3 groundPos;
AZ::TransformBus::EventResult(groundPos, m_groundEntity->GetId(), &AZ::TransformBus::Events::GetWorldTranslation);
const AZ::Vector3 characterPos = GetCharacterCenter();
if (AZStd::abs(characterPos.GetX() - groundPos.GetX()) > BoundMaxDistance ||
AZStd::abs(characterPos.GetY() - groundPos.GetY()) > BoundMaxDistance)
{
const float tileOffsetX = AZStd::fmod(characterPos.GetX(), TileSize);
const float tileOffsetY = AZStd::fmod(characterPos.GetX(), TileSize);
const AZ::Vector3 newGroundPos(characterPos.GetX() - tileOffsetX, characterPos.GetY() - tileOffsetY, groundPos.GetZ());
AZ::TransformBus::Event(m_groundEntity->GetId(), &AZ::TransformBus::Events::SetWorldTranslation, newGroundPos);
}
}
void AnimViewportRenderer::ResetEnvironment()
{
// Reset environment
@@ -59,6 +59,9 @@ namespace EMStudio
AZ::EntityId GetEntityId() const;
AzFramework::EntityContextId GetEntityContextId() const;
// Call this function on update to prevent the character going out of bounds of the ground.
void CheckBounds();
private:
// This function resets the light, camera and other environment settings.
@@ -91,6 +94,8 @@ namespace EMStudio
const RenderOptions* m_renderOptions;
const float DefaultFrustumDimension = 128.0f;
const float BoundMaxDistance = 150.0f;
const float TileSize = 1.0f;
AZStd::vector<AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle> m_lightHandles;
};
}
@@ -206,6 +206,7 @@ namespace EMStudio
CalculateCameraProjection();
RenderCustomPluginData();
FollowCharacter();
m_renderer->CheckBounds();
}
void AnimViewportWidget::CalculateCameraProjection()