Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpPosition.cpp
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | enum | ||||
PITCH = 1, | PITCH = 1, | ||||
PITCH_ROLL = 2, | PITCH_ROLL = 2, | ||||
ROLL = 3, | ROLL = 3, | ||||
} m_AnchorType; | } m_AnchorType; | ||||
bool m_Floating; | bool m_Floating; | ||||
entity_pos_t m_FloatDepth; | entity_pos_t m_FloatDepth; | ||||
float m_RotYSpeed; // maximum radians per second, used by InterpolatedRotY to follow RotY | // Maximum radians per second, used by InterpolatedRotY to follow RotY and the unitMotion. | ||||
fixed m_RotYSpeed; | |||||
// Dynamic state: | // Dynamic state: | ||||
bool m_InWorld; | bool m_InWorld; | ||||
// m_LastX/Z contain the position from the start of the most recent turn | // m_LastX/Z contain the position from the start of the most recent turn | ||||
// m_PrevX/Z conatain the position from the turn before that | // m_PrevX/Z conatain the position from the turn before that | ||||
entity_pos_t m_X, m_Z, m_LastX, m_LastZ, m_PrevX, m_PrevZ; // these values contain undefined junk if !InWorld | entity_pos_t m_X, m_Z, m_LastX, m_LastZ, m_PrevX, m_PrevZ; // these values contain undefined junk if !InWorld | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | return | ||||
"<data type='decimal'/>" | "<data type='decimal'/>" | ||||
"</element>" | "</element>" | ||||
"<element name='Floating' a:help='Whether the entity floats on water'>" | "<element name='Floating' a:help='Whether the entity floats on water'>" | ||||
"<data type='boolean'/>" | "<data type='boolean'/>" | ||||
"</element>" | "</element>" | ||||
"<element name='FloatDepth' a:help='The depth at which an entity floats on water (needs Floating to be true)'>" | "<element name='FloatDepth' a:help='The depth at which an entity floats on water (needs Floating to be true)'>" | ||||
"<ref name='nonNegativeDecimal'/>" | "<ref name='nonNegativeDecimal'/>" | ||||
"</element>" | "</element>" | ||||
"<element name='TurnRate' a:help='Maximum graphical rotation speed around Y axis, in radians per second'>" | "<element name='TurnRate' a:help='Maximum rotation speed around Y axis, in radians per second. Used for all graphical rotations and some real unitMotion driven rotations.'>" | ||||
"<ref name='positiveDecimal'/>" | "<ref name='positiveDecimal'/>" | ||||
"</element>"; | "</element>"; | ||||
} | } | ||||
virtual void Init(const CParamNode& paramNode) | virtual void Init(const CParamNode& paramNode) | ||||
{ | { | ||||
std::wstring anchor = paramNode.GetChild("Anchor").ToString(); | std::wstring anchor = paramNode.GetChild("Anchor").ToString(); | ||||
if (anchor == L"pitch") | if (anchor == L"pitch") | ||||
m_AnchorType = PITCH; | m_AnchorType = PITCH; | ||||
else if (anchor == L"pitch-roll") | else if (anchor == L"pitch-roll") | ||||
m_AnchorType = PITCH_ROLL; | m_AnchorType = PITCH_ROLL; | ||||
else if (anchor == L"roll") | else if (anchor == L"roll") | ||||
m_AnchorType = ROLL; | m_AnchorType = ROLL; | ||||
else | else | ||||
m_AnchorType = UPRIGHT; | m_AnchorType = UPRIGHT; | ||||
m_InWorld = false; | m_InWorld = false; | ||||
m_LastYDifference = entity_pos_t::Zero(); | m_LastYDifference = entity_pos_t::Zero(); | ||||
m_Y = paramNode.GetChild("Altitude").ToFixed(); | m_Y = paramNode.GetChild("Altitude").ToFixed(); | ||||
m_RelativeToGround = true; | m_RelativeToGround = true; | ||||
m_Floating = paramNode.GetChild("Floating").ToBool(); | m_Floating = paramNode.GetChild("Floating").ToBool(); | ||||
m_FloatDepth = paramNode.GetChild("FloatDepth").ToFixed(); | m_FloatDepth = paramNode.GetChild("FloatDepth").ToFixed(); | ||||
m_RotYSpeed = paramNode.GetChild("TurnRate").ToFixed().ToFloat(); | m_RotYSpeed = paramNode.GetChild("TurnRate").ToFixed(); | ||||
bb: Lets serialize that, shall we | |||||
m_RotX = m_RotY = m_RotZ = entity_angle_t::FromInt(0); | m_RotX = m_RotY = m_RotZ = entity_angle_t::FromInt(0); | ||||
m_InterpolatedRotX = m_InterpolatedRotY = m_InterpolatedRotZ = 0.f; | m_InterpolatedRotX = m_InterpolatedRotY = m_InterpolatedRotZ = 0.f; | ||||
m_LastInterpolatedRotX = m_LastInterpolatedRotZ = 0.f; | m_LastInterpolatedRotX = m_LastInterpolatedRotZ = 0.f; | ||||
m_Territory = INVALID_PLAYER; | m_Territory = INVALID_PLAYER; | ||||
m_TurretParent = INVALID_ENTITY; | m_TurretParent = INVALID_ENTITY; | ||||
m_TurretPosition = CFixedVector3D(); | m_TurretPosition = CFixedVector3D(); | ||||
▲ Show 20 Lines • Show All 364 Lines • ▼ Show 20 Lines | virtual CFixedVector2D GetPreviousPosition2D() const | ||||
{ | { | ||||
LOGERROR("CCmpPosition::GetPreviousPosition2D called on entity when IsInWorld is false"); | LOGERROR("CCmpPosition::GetPreviousPosition2D called on entity when IsInWorld is false"); | ||||
return CFixedVector2D(); | return CFixedVector2D(); | ||||
} | } | ||||
return CFixedVector2D(m_PrevX, m_PrevZ); | return CFixedVector2D(m_PrevX, m_PrevZ); | ||||
} | } | ||||
virtual fixed GetTurnRate() const | |||||
{ | |||||
return m_RotYSpeed; | |||||
} | |||||
virtual void TurnTo(entity_angle_t y) | virtual void TurnTo(entity_angle_t y) | ||||
{ | { | ||||
if (m_TurretParent != INVALID_ENTITY) | if (m_TurretParent != INVALID_ENTITY) | ||||
{ | { | ||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), m_TurretParent); | CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), m_TurretParent); | ||||
if (cmpPosition) | if (cmpPosition) | ||||
y -= cmpPosition->GetRotation().Y; | y -= cmpPosition->GetRotation().Y; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 228 Lines • ▼ Show 20 Lines | case MT_Interpolate: | ||||
PROFILE("Position::Interpolate"); | PROFILE("Position::Interpolate"); | ||||
const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg); | const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg); | ||||
float rotY = m_RotY.ToFloat(); | float rotY = m_RotY.ToFloat(); | ||||
if (rotY != m_InterpolatedRotY) | if (rotY != m_InterpolatedRotY) | ||||
{ | { | ||||
float rotYSpeed = m_RotYSpeed.ToFloat(); | |||||
float delta = rotY - m_InterpolatedRotY; | float delta = rotY - m_InterpolatedRotY; | ||||
// Wrap delta to -M_PI..M_PI | // Wrap delta to -M_PI..M_PI | ||||
delta = fmodf(delta + (float)M_PI, 2*(float)M_PI); // range -2PI..2PI | delta = fmodf(delta + (float)M_PI, 2*(float)M_PI); // range -2PI..2PI | ||||
if (delta < 0) delta += 2*(float)M_PI; // range 0..2PI | if (delta < 0) delta += 2*(float)M_PI; // range 0..2PI | ||||
delta -= (float)M_PI; // range -M_PI..M_PI | delta -= (float)M_PI; // range -M_PI..M_PI | ||||
// Clamp to max rate | // Clamp to max rate | ||||
float deltaClamped = Clamp(delta, -m_RotYSpeed*msgData.deltaSimTime, +m_RotYSpeed*msgData.deltaSimTime); | float deltaClamped = Clamp(delta, -rotYSpeed*msgData.deltaSimTime, +rotYSpeed*msgData.deltaSimTime); | ||||
// Calculate new orientation, in a peculiar way in order to make sure the | // Calculate new orientation, in a peculiar way in order to make sure the | ||||
// result gets close to m_orientation (rather than being n*2*M_PI out) | // result gets close to m_orientation (rather than being n*2*M_PI out) | ||||
m_InterpolatedRotY = rotY + deltaClamped - delta; | m_InterpolatedRotY = rotY + deltaClamped - delta; | ||||
// update the visual XZ rotation | // update the visual XZ rotation | ||||
if (m_InWorld) | if (m_InWorld) | ||||
{ | { | ||||
m_LastInterpolatedRotX = m_InterpolatedRotX; | m_LastInterpolatedRotX = m_InterpolatedRotX; | ||||
▲ Show 20 Lines • Show All 184 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Lets serialize that, shall we