diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Dynamics/ConstantForce.cpp |
Diffstat (limited to 'Runtime/Dynamics/ConstantForce.cpp')
-rw-r--r-- | Runtime/Dynamics/ConstantForce.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
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<class TransferFunction> +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 |