Skip to content

Commit c0c0d1d

Browse files
committed
LimbPath - doc cleanup, sqrt use optimization
1 parent 8ad9f2f commit c0c0d1d

File tree

2 files changed

+60
-68
lines changed

2 files changed

+60
-68
lines changed

Source/Entities/LimbPath.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,16 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
313313
} else if (m_CurrentSegment == m_Segments.end()) {
314314
// Current path has already come to an end. Compute progress and m_Ended based on last segment's target.
315315
Vector distVec = g_SceneMan.ShortestDistance(limbPos, GetCurrentSegTarget());
316-
float distance = distVec.GetMagnitude();
317-
float segMag = (*m_CurrentSegment * GetTotalScaleMultiplier()).GetMagnitude();
316+
float distanceSqr = distVec.GetSqrMagnitude();
317+
float segMagSqr = (*m_CurrentSegment * GetTotalScaleMultiplier()).GetSqrMagnitude();
318318

319319
// Get normalized progress measure toward the target.
320-
m_SegProgress = distance > segMag ? 0.0F : (1.0F - (distance / segMag));
320+
321+
if (distanceSqr > segMagSqr)
322+
// We're too far away from this target.
323+
m_SegProgress = 0.0;
324+
else
325+
m_SegProgress = (1.0F - (std::sqrt(distanceSqr) / std::sqrt(segMagSqr)));
321326

322327
m_Ended = distVec.MagnitudeIsLessThan(m_SegmentEndedThreshold);
323328
} else {
@@ -346,8 +351,10 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
346351

347352
for (std::deque<Vector>::iterator itr = m_CurrentSegment; itr != m_Segments.end(); ++itr) {
348353
if (itr != m_CurrentSegment) {
349-
if (m_FootCollisionsDisabledSegment >= 0 && GetSegCount() - (itr - m_Segments.begin()) <= m_FootCollisionsDisabledSegment) {
350-
// We've already picked a segment, and the remaining ones are ones with collisions disabled.
354+
if (m_FootCollisionsDisabledSegment >= 0 &&
355+
m_Segments.size() - (itr - m_Segments.begin()) <= m_FootCollisionsDisabledSegment) {
356+
// We've already picked a segment (at least the current one),
357+
// and the remaining ones are ones with collisions disabled.
351358
// Ignore these.
352359
break;
353360
}
@@ -370,7 +377,7 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
370377
}
371378

372379
// We will want to compute progress to whatever the new segment is.
373-
float distanceToCurrentSegmentTarget;
380+
float distanceToCurrentSegmentTargetSqr;
374381

375382

376383
if (closestSegmentTargetDistanceSqr < m_SegmentEndedThreshold * m_SegmentEndedThreshold) {
@@ -382,7 +389,7 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
382389
m_Ended = true;
383390
m_CurrentSegment = closestSegment;
384391

385-
distanceToCurrentSegmentTarget = std::sqrt(closestSegmentTargetDistanceSqr);
392+
distanceToCurrentSegmentTargetSqr = closestSegmentTargetDistanceSqr;
386393

387394
} else {
388395
// Time to switch to next segment!
@@ -391,7 +398,7 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
391398
m_CurrentSegment = closestSegment + 1;
392399

393400
Vector currentSegmentTarget = closestSegmentStartPos + *closestSegment + *m_CurrentSegment;
394-
distanceToCurrentSegmentTarget = (currentSegmentTarget - limbPosLocal).GetMagnitude();
401+
distanceToCurrentSegmentTargetSqr = (currentSegmentTarget - limbPosLocal).GetSqrMagnitude();
395402
}
396403
} else {
397404
// We're not close enough to that closest segment's target, but we can still try to do better.
@@ -406,23 +413,23 @@ void LimbPath::ReportProgress(const Vector& limbPos) {
406413

407414
m_CurrentSegment = closestSegment;
408415

409-
distanceToCurrentSegmentTarget = std::sqrt(closestSegmentTargetDistanceSqr);
416+
distanceToCurrentSegmentTargetSqr = closestSegmentTargetDistanceSqr;
410417
} else {
411418
// Just get the distance to current segment's target.
412419
Vector currentSegmentTarget = currentSegmentStartPos + *m_CurrentSegment;
413-
distanceToCurrentSegmentTarget = (currentSegmentTarget - limbPosLocal).GetMagnitude();
420+
distanceToCurrentSegmentTargetSqr = (currentSegmentTarget - limbPosLocal).GetSqrMagnitude();
414421
}
415422
}
416423

417424
// Now compute a normalized progress measure towards the current segment.
418425

419-
float currentSegmentMagnitude = m_CurrentSegment->GetMagnitude();
426+
float currentSegmentMagnitudeSqr = m_CurrentSegment->GetSqrMagnitude();
420427

421-
if (distanceToCurrentSegmentTarget > currentSegmentMagnitude)
428+
if (distanceToCurrentSegmentTargetSqr > currentSegmentMagnitudeSqr)
422429
// We're too far away from this target.
423430
m_SegProgress = 0.0;
424431
else
425-
m_SegProgress = (1.0F - (distanceToCurrentSegmentTarget / currentSegmentMagnitude));
432+
m_SegProgress = (1.0F - (std::sqrt(distanceToCurrentSegmentTargetSqr) / std::sqrt(currentSegmentMagnitudeSqr)));
426433
}
427434
}
428435

Source/Entities/LimbPath.h

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace RTE {
2020

2121
/// Public member variable, method and friend function declarations
2222
public:
23-
// Concrete allocation and cloning definitions
23+
/// Concrete allocation and cloning definitions
2424
EntityAllocation(LimbPath);
2525
SerializableOverrideMethods;
2626
ClassInfoGetters;
@@ -83,7 +83,7 @@ namespace RTE {
8383

8484
/// Gets the number of Vector:s the internal array of 'waypoints' or
8585
/// segments of this LimbPath.
86-
/// @return An int with he count.
86+
/// @return An int with the count.
8787
int GetSegCount() const { return m_Segments.size(); }
8888

8989
/// Gets a pointer to the segment at the given index. Ownership is NOT transferred.
@@ -369,77 +369,62 @@ namespace RTE {
369369
protected:
370370
static Entity::ClassInfo m_sClass;
371371

372-
// The starting point of the path.
373-
Vector m_Start;
372+
Vector m_Start; //!< The starting point of the path.
374373

375-
// The number of starting segments, counting into the path from its beginning,
376-
// that upon restart of this path will be tried in reverse order till one which
377-
// yields a starting position that is clear of terrain is found.
374+
/// The number of starting segments, counting into the path from its beginning,
375+
/// that upon restart of this path will be tried in reverse order till one which
376+
/// yields a starting position that is clear of terrain is found.
378377
size_t m_StartSegCount;
379378

380-
// Array containing the actual 'waypoints' or segments for the path.
381-
std::deque<Vector> m_Segments;
379+
std::deque<Vector> m_Segments; //!< Array containing the actual 'waypoints' or segments for the path.
382380

383-
// The iterator to the segment of the path that the limb ended up on the end of
381+
/// The iterator to the segment of the path that the limb ended up on the end of.
384382
std::deque<Vector>::iterator m_CurrentSegment;
385383

386-
// Count of segments at the end of the segments list for which foot collisions should be disabled
387-
// for this limbpath, if it's for legs.
384+
/// Count of segments at the end of the segments list for which foot collisions should be disabled
385+
/// for this limbpath, if it's for legs.
388386
int m_FootCollisionsDisabledSegment;
389387

390-
// Normalized measure of how far the limb has progressed toward the
391-
// current segment's target. 0.0 means its farther away than the
392-
// magnitude of the entire segment. 0.5 means it's half the mag of the segment
393-
// away from the target.
388+
/// Normalized measure of how far the limb has progressed toward the current
389+
/// segment's target.
390+
///
391+
/// 0.0 means its farther away than the magnitude of the entire segment.
392+
/// 0.5 means it's half the mag of the segment away from the target.
394393
float m_SegProgress;
395394

396-
// The constant speed that the limb traveling this path has in m/s.
397-
float m_TravelSpeed;
395+
float m_TravelSpeed; //!< The constant speed that the limb traveling this path has in m/s.
398396

399-
// How close we must get to the end of each segment to consider it finished
400-
float m_SegmentEndedThreshold;
397+
float m_SegmentEndedThreshold; //!< How close we must get to the end of each segment to consider it finished
401398

402-
// The base/current travel speed multiplier
403-
float m_BaseTravelSpeedMultiplier;
404-
float m_CurrentTravelSpeedMultiplier;
399+
float m_BaseTravelSpeedMultiplier; //!< The base travel speed multiplier
400+
float m_CurrentTravelSpeedMultiplier; //!< The current travel speed multiplier
405401

406-
// The base/current scale multiplier (we extend the walkpath when running fast to take longer strides)
407-
// This is a vector to allow scaling on seperate axis
402+
/// The base scale multiplier for both axes.
408403
Vector m_BaseScaleMultiplier;
404+
/// The current scale multiplier for both axes. (we extend the walkpath when running fast to take longer strides)
409405
Vector m_CurrentScaleMultiplier;
410406

411-
// The max force that a limb travelling along this path can push.
412-
// In kg * m/(s^2)
413-
float m_PushForce;
414-
415-
// The latest known position of the owning actor's joint in world coordinates.
416-
Vector m_JointPos;
417-
// The latest known velocity of the owning actor's joint in world coordinates.
418-
Vector m_JointVel;
419-
// The rotation applied to this walkpath.
420-
Matrix m_Rotation;
421-
// The point we should be rotated around, in local space.
422-
Vector m_RotationOffset;
423-
// The offset to apply to our walkpath position, in local space.
424-
Vector m_PositionOffset;
425-
426-
// If GetNextTimeSeg() couldn't use up all frame time because the current segment
427-
// ended,this var stores the remainder of time that should be used to progress
428-
// on the next segment during the same frame.
407+
float m_PushForce; //!< The max force that a limb travelling along this path can push, in kg * m/(s^2).
408+
409+
Vector m_JointPos; //!< The latest known position of the owning actor's joint in world coordinates.
410+
Vector m_JointVel; //!< The latest known velocity of the owning actor's joint in world coordinates.
411+
Matrix m_Rotation; //!< The rotation applied to this walkpath.
412+
Vector m_RotationOffset; //!< The point we should be rotated around, in local space.
413+
Vector m_PositionOffset; //!< The offset to apply to our walkpath position, in local space.
414+
415+
/// If GetNextTimeSeg() couldn't use up all frame time because the current segment
416+
/// ended, this var stores the remainder of time that should be used to progress
417+
/// on the next segment during the same frame.
429418
float m_TimeLeft;
430419

431-
// Times the amount of sim time spent since the last path traversal was started
432-
Timer m_PathTimer;
433-
// Times the amount of sim time spent pursuing the current segment's target.
434-
Timer m_SegTimer;
420+
Timer m_PathTimer; //!< Records the amount of sim time spent since the last path traversal was started.
421+
Timer m_SegTimer; //!< Records the amount of sim time spent pursuing the current segment's target.
435422

436-
// Total length of this LimbPath, including the alternative starting segments, in pixel units
437-
float m_TotalLength;
438-
// Length of this LimbPath, excluding the alternative starting segments.
439-
float m_RegularLength;
440-
bool m_SegmentDone;
441-
bool m_Ended;
442-
bool m_HFlipped;
423+
float m_TotalLength; //!< Total length of this LimbPath, including the alternative starting segments, in pixel units
424+
float m_RegularLength; //!< Length of this LimbPath, excluding the alternative starting segments.
425+
bool m_SegmentDone; //!< Unused?
426+
bool m_Ended; //!< True if this path has ended. See method `PathEnded` for more information.
427+
bool m_HFlipped; //!< True if this path is horizontally flipped.
443428

444429
/// Private member variable and method declarations
445430
private:
@@ -469,7 +454,7 @@ namespace RTE {
469454
/// @returns World space position
470455
Vector ToWorldSpace(const Vector& position) const;
471456

472-
// Gets the current segment's starting position (i.e. last segment's target) in local space.
457+
/// Gets the current segment's starting position (i.e. last segment's target) in local space.
473458
Vector GetCurrentSegStartLocal() const;
474459
};
475460

0 commit comments

Comments
 (0)