From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Dynamics/ConstantForce.cpp | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Runtime/Dynamics/ConstantForce.cpp (limited to 'Runtime/Dynamics/ConstantForce.cpp') diff --git a/Runtime/Dynamics/ConstantForce.cpp b/Runtime/Dynamics/ConstantForce.cpp new file mode 100644 index 0000000..596b6b7 --- /dev/null +++ b/Runtime/Dynamics/ConstantForce.cpp @@ -0,0 +1,66 @@ +#include "UnityPrefix.h" +#if ENABLE_PHYSICS +#include "ConstantForce.h" +#include "RigidBody.h" +#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h" + +using namespace Unity; + +IMPLEMENT_OBJECT_SERIALIZE (ConstantForce) +IMPLEMENT_CLASS (ConstantForce) + +ConstantForce::ConstantForce (MemLabelId label, ObjectCreationMode mode) +: Super(label, mode) +, m_FixedUpdateNode (this) +{ +} + +ConstantForce::~ConstantForce () +{ +} + +template +void ConstantForce::Transfer (TransferFunction& transfer) +{ + Super::Transfer (transfer); + TRANSFER (m_Force); + TRANSFER (m_RelativeForce); + TRANSFER (m_Torque); + TRANSFER (m_RelativeTorque); +} + +void ConstantForce::Reset () +{ + Super::Reset (); + m_Force = Vector3f::zero; + m_RelativeForce = Vector3f::zero; + m_Torque = Vector3f::zero; + m_RelativeTorque = Vector3f::zero; +} + +void ConstantForce::FixedUpdate() { + Rigidbody* body = QueryComponent (Rigidbody); + if (!body) + { + // It should be impossible to reach this case, since Unity will not allow + // deleting components which are required by other components. But people + // have somehow managed (case 506613), so better check for it. + ErrorStringObject ("ConstantForce requires a Rigidbody component, but non is present.", this); + return; + } + body->AddForce (m_Force); + body->AddRelativeForce (m_RelativeForce); + body->AddTorque (m_Torque); + body->AddRelativeTorque (m_RelativeTorque); +} + +void ConstantForce::AddToManager () +{ + GetFixedBehaviourManager ().AddBehaviour (m_FixedUpdateNode, -1); +} + +void ConstantForce::RemoveFromManager () +{ + GetFixedBehaviourManager ().RemoveBehaviour (m_FixedUpdateNode); +} +#endif //ENABLE_PHSYICS \ No newline at end of file -- cgit v1.1-26-g67d0