SDK/Physics2D: Fix entity rotation not being taken into account for static physics

This commit is contained in:
Lynix 2020-06-02 17:06:01 +02:00
parent 1b4199c0a7
commit 52c24e76db
4 changed files with 30 additions and 12 deletions

View File

@ -51,9 +51,15 @@ namespace Ndk
m_object = std::make_unique<Nz::RigidBody2D>(&world, 1.f, geom); m_object = std::make_unique<Nz::RigidBody2D>(&world, 1.f, geom);
m_object->SetPositionOffset(positionOffset); m_object->SetPositionOffset(positionOffset);
m_object->SetPosition(Nz::Vector2f(matrix.GetTranslation()));
m_object->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId()))); m_object->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
if (m_entity->HasComponent<NodeComponent>())
{
auto& entityNode = m_entity->GetComponent<NodeComponent>();
m_object->SetPosition(Nz::Vector2f(entityNode.GetPosition()));
m_object->SetRotation(entityNode.GetRotation().To2DAngle());
}
if (m_pendingStates.valid) if (m_pendingStates.valid)
ApplyPhysicsState(*m_object); ApplyPhysicsState(*m_object);
} }

View File

@ -249,19 +249,16 @@ namespace Ndk
else else
body->SetVelocity(Nz::Vector2f::Zero()); body->SetVelocity(Nz::Vector2f::Zero());
/*if (newRotation != oldRotation) Nz::RadianAnglef oldRotation = body->GetRotation();
{ Nz::RadianAnglef newRotation = node.GetRotation(Nz::CoordSys_Global).To2DAngle();
Nz::Quaternionf transition = newRotation * oldRotation.GetConjugate();
Nz::EulerAnglesf angles = transition.ToEulerAngles();
Nz::Vector3f angularVelocity(Nz::ToRadians(angles.pitch * invElapsedTime),
Nz::ToRadians(angles.yaw * invElapsedTime),
Nz::ToRadians(angles.roll * invElapsedTime));
physObj->SetRotation(oldRotation); if (newRotation != oldRotation)
physObj->SetAngularVelocity(angularVelocity); {
body->SetRotation(oldRotation);
body->SetAngularVelocity((newRotation - oldRotation) * invElapsedTime);
} }
else else
physObj->SetAngularVelocity(Nz::Vector3f::Zero());*/ body->SetAngularVelocity(Nz::RadianAnglef::Zero());
} }
} }

View File

@ -60,6 +60,7 @@ namespace Nz
T SquaredMagnitude() const; T SquaredMagnitude() const;
RadianAngle<T> To2DAngle() const;
EulerAngles<T> ToEulerAngles() const; EulerAngles<T> ToEulerAngles() const;
//Matrix3<T> ToRotationMatrix() const; //Matrix3<T> ToRotationMatrix() const;
String ToString() const; String ToString() const;

View File

@ -462,13 +462,27 @@ namespace Nz
return w * w + x * x + y * y + z * z; return w * w + x * x + y * y + z * z;
} }
/*!
* \brief Returns the "roll angle" of this quaternion
* \return Roll rotation
*
* \remark This function only has sense when quaternion only represents a "roll rotation"
*/
template<typename T>
RadianAngle<T> Quaternion<T>::To2DAngle() const
{
T siny_cosp = T(2.0) * (w * z + x * y);
T cosy_cosp = T(1.0) - T(2.0) * (y * y + z * z);
return std::atan2(siny_cosp, cosy_cosp);
}
/*! /*!
* \brief Converts this quaternion to Euler angles representation * \brief Converts this quaternion to Euler angles representation
* \return EulerAngles which is the representation of this rotation * \return EulerAngles which is the representation of this rotation
* *
* \remark Rotation are "left-handed" * \remark Rotation are "left-handed"
*/ */
template<typename T> template<typename T>
EulerAngles<T> Quaternion<T>::ToEulerAngles() const EulerAngles<T> Quaternion<T>::ToEulerAngles() const
{ {