|
@@ -1,20 +1,17 @@
|
|
|
/*
|
|
|
-* Copyright (c) Contributors to the Open 3D Engine Project.
|
|
|
-* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
|
-*
|
|
|
-* SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
-*
|
|
|
-*/
|
|
|
+ * Copyright (c) Contributors to the Open 3D Engine Project.
|
|
|
+ * For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
|
+ *
|
|
|
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
+ *
|
|
|
+ */
|
|
|
|
|
|
#include "FollowingCameraComponent.h"
|
|
|
-
|
|
|
-#include <AzCore/Serialization/SerializeContext.h>
|
|
|
#include <AzCore/Serialization/EditContext.h>
|
|
|
-
|
|
|
+#include <AzCore/Serialization/SerializeContext.h>
|
|
|
#include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
|
|
|
#include <MathConversion.h>
|
|
|
|
|
|
-
|
|
|
namespace AppleKraken
|
|
|
{
|
|
|
|
|
@@ -39,10 +36,16 @@ namespace AppleKraken
|
|
|
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
|
|
|
->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
|
|
|
->DataElement(AZ::Edit::UIHandlers::CheckBox, &FollowingCameraComponent::m_isActive, "Active", "")
|
|
|
- ->DataElement(AZ::Edit::UIHandlers::EntityId, &FollowingCameraComponent::m_target, "Target", "Entity of the followed object")
|
|
|
- ->DataElement(AZ::Edit::UIHandlers::Default, &FollowingCameraComponent::m_smoothingBuffer, "SmoothingLength", "Number of past transform used to smooth")
|
|
|
+ ->DataElement(
|
|
|
+ AZ::Edit::UIHandlers::EntityId, &FollowingCameraComponent::m_target, "Target", "Entity of the followed object")
|
|
|
+ ->DataElement(
|
|
|
+ AZ::Edit::UIHandlers::Default,
|
|
|
+ &FollowingCameraComponent::m_smoothingBuffer,
|
|
|
+ "SmoothingLength",
|
|
|
+ "Number of past transform used to smooth")
|
|
|
->DataElement(AZ::Edit::UIHandlers::Default, &FollowingCameraComponent::m_zoomSpeed, "Zoom Speed", "Zoom speed")
|
|
|
- ->DataElement(AZ::Edit::UIHandlers::Default, &FollowingCameraComponent::m_rotationSpeed, "Rotation Speed", "Rotation Speed");
|
|
|
+ ->DataElement(
|
|
|
+ AZ::Edit::UIHandlers::Default, &FollowingCameraComponent::m_rotationSpeed, "Rotation Speed", "Rotation Speed");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -72,7 +75,8 @@ namespace AppleKraken
|
|
|
|
|
|
void FollowingCameraComponent::OnTick(float deltaTime, AZ::ScriptTimePoint /*time*/)
|
|
|
{
|
|
|
- if (!m_target.IsValid()){
|
|
|
+ if (!m_target.IsValid())
|
|
|
+ {
|
|
|
AZ_Warning("FollowingCameraComponent", false, "m_target is empty!");
|
|
|
}
|
|
|
if (!m_isActive)
|
|
@@ -86,47 +90,41 @@ namespace AppleKraken
|
|
|
m_lastTransforms.push_back(AZStd::make_pair(target_world_transform.GetTranslation(), deltaTime));
|
|
|
m_lastRotations.push_back(AZStd::make_pair(target_world_transform.GetRotation(), deltaTime));
|
|
|
|
|
|
- if (m_lastTransforms.size() > m_smoothingBuffer){
|
|
|
+ if (m_lastTransforms.size() > m_smoothingBuffer)
|
|
|
+ {
|
|
|
m_lastTransforms.pop_front();
|
|
|
m_lastRotations.pop_front();
|
|
|
}
|
|
|
AZ::Vector3 translation = SmoothTranslation();
|
|
|
AZ::Quaternion quat = SmoothRotation();
|
|
|
- AZ::Transform filtered_transform =
|
|
|
- {
|
|
|
- translation,
|
|
|
- AZ::Quaternion::CreateRotationZ(quat.GetEulerRadians().GetZ()),
|
|
|
- target_world_transform.GetUniformScale()
|
|
|
- };
|
|
|
+ AZ::Transform filtered_transform = { translation,
|
|
|
+ AZ::Quaternion::CreateRotationZ(quat.GetEulerRadians().GetZ()),
|
|
|
+ target_world_transform.GetUniformScale() };
|
|
|
|
|
|
auto modified_transform = m_initialPose.GetInverse();
|
|
|
- AZ::Transform rotation_transform =
|
|
|
- {
|
|
|
- {0.0, 0.0, 0.0},
|
|
|
- AZ::Quaternion::CreateRotationY(m_rotationChange2) * AZ::Quaternion::CreateRotationZ(m_rotationChange) ,
|
|
|
- 1.0
|
|
|
- };
|
|
|
+ AZ::Transform rotation_transform = {
|
|
|
+ { 0.0, 0.0, 0.0 }, AZ::Quaternion::CreateRotationY(m_rotationChange2) * AZ::Quaternion::CreateRotationZ(m_rotationChange), 1.0
|
|
|
+ };
|
|
|
modified_transform *= rotation_transform;
|
|
|
modified_transform.Invert();
|
|
|
|
|
|
AZ::Vector3 translation_modifier = modified_transform.GetBasisY() * m_zoomChange;
|
|
|
auto modified_translation = modified_transform.GetTranslation() + translation_modifier;
|
|
|
- modified_transform.SetTranslation( modified_translation );
|
|
|
+ modified_transform.SetTranslation(modified_translation);
|
|
|
|
|
|
EBUS_EVENT_ID(GetEntityId(), AZ::TransformBus, SetWorldTM, filtered_transform * modified_transform);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
AZ::Vector3 FollowingCameraComponent::SmoothTranslation() const
|
|
|
{
|
|
|
- AZ::Vector3 sum{0};
|
|
|
- float normalization{0};
|
|
|
- for (const auto& p : m_lastTransforms){
|
|
|
+ AZ::Vector3 sum{ 0 };
|
|
|
+ float normalization{ 0 };
|
|
|
+ for (const auto& p : m_lastTransforms)
|
|
|
+ {
|
|
|
sum += p.first * p.second;
|
|
|
- normalization+=p.second;
|
|
|
+ normalization += p.second;
|
|
|
}
|
|
|
- return sum/normalization;
|
|
|
+ return sum / normalization;
|
|
|
}
|
|
|
|
|
|
AZ::Quaternion FollowingCameraComponent::SmoothRotation() const
|
|
@@ -134,13 +132,14 @@ namespace AppleKraken
|
|
|
// Smoothing is done by taking an exponential map. Note that the exponential map is the same thing as 'ScaledAxisAngle'
|
|
|
// for 3D rotations. This map can be treated as linear space locally.
|
|
|
// In that linear space the average is computed. It should give a pleasant experience for slow-moving objects.
|
|
|
- float normalization{0};
|
|
|
- AZ::Vector3 sum{0};
|
|
|
- for (const auto& p : m_lastRotations){
|
|
|
+ float normalization{ 0 };
|
|
|
+ AZ::Vector3 sum{ 0 };
|
|
|
+ for (const auto& p : m_lastRotations)
|
|
|
+ {
|
|
|
sum += p.second * (p.first.ConvertToScaledAxisAngle());
|
|
|
- normalization+=p.second;
|
|
|
+ normalization += p.second;
|
|
|
}
|
|
|
- return AZ::Quaternion::CreateFromScaledAxisAngle(sum/normalization);
|
|
|
+ return AZ::Quaternion::CreateFromScaledAxisAngle(sum / normalization);
|
|
|
}
|
|
|
|
|
|
bool FollowingCameraComponent::OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel)
|
|
@@ -194,6 +193,5 @@ namespace AppleKraken
|
|
|
{
|
|
|
m_rotationChange2 -= m_rotationSpeed;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-}
|
|
|
+} // namespace AppleKraken
|