Skip to content

Commit

Permalink
fix #4964 + #4496 + #4497
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Feb 1, 2016
1 parent 6a03857 commit f845c44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 3 additions & 3 deletions rts/Sim/MoveTypes/GroundMoveType.cpp
Expand Up @@ -566,12 +566,12 @@ void CGroundMoveType::ChangeSpeed(float newWantedSpeed, bool wantReverse, bool f
const float groundSpeedMod = CMoveMath::GetPosSpeedMod(*md, owner->pos, flatFrontDir);

const float curGoalDistSq = (owner->pos - goalPos).SqLength2D();
const float minGoalDistSq = Square(BrakingDistance(currentSpeed, mix(decRate, accRate, reversing)));
const float minGoalDistSq = Square(BrakingDistance(currentSpeed, decRate));

const float3& waypointDifFwd = waypointDir;
const float3 waypointDifRev = -waypointDifFwd;

const float3& waypointDif = reversing? waypointDifRev: waypointDifFwd;
const float3& waypointDif = mix(waypointDifFwd, waypointDifRev, reversing);
const short turnDeltaHeading = owner->heading - GetHeadingFromVector(waypointDif.x, waypointDif.z);

// NOTE: <= 2 because every CMD_MOVE has a trailing CMD_SET_WANTED_MAX_SPEED
Expand Down Expand Up @@ -1428,7 +1428,7 @@ from current velocity.
*/
float3 CGroundMoveType::Here()
{
const float dist = BrakingDistance(currentSpeed, mix(decRate, accRate, reversing));
const float dist = BrakingDistance(currentSpeed, decRate);
const int sign = Sign(int(!reversing));

const float3 pos2D = owner->pos * XZVector;
Expand Down
34 changes: 23 additions & 11 deletions rts/Sim/Path/IPathController.cpp
Expand Up @@ -15,22 +15,34 @@ void IPathController::FreeInstance(IPathController* instance) {

float GMTDefaultPathController::GetDeltaSpeed(
unsigned int pathID,
float targetSpeed,
float currentSpeed,
float maxAccRate,
float maxDecRate,
float targetSpeed, // unsigned
float currentSpeed, // unsigned
float maxAccRate, // unsigned
float maxDecRate, // unsigned
bool wantReverse,
bool isReversing
) const {
const int targetSpeedSign = Sign(int(!wantReverse));
const int currentSpeedSign = Sign(int(!isReversing));
// Sign(0) is negative which we do not want
const int targetSpeedSign = Sign(int(!wantReverse) * 2 - 1);
const int currentSpeedSign = Sign(int(!isReversing) * 2 - 1);

// if reversing, target and current are swapped
const float speeds[] = {targetSpeed * targetSpeedSign, currentSpeed * currentSpeedSign};

// all possible cases
// tar=[6,+1] && cur=[4,+1] --> dif=+2, delta= accRate*+1 (fwd-acc from +4 to +6)
// tar=[4,+1] && cur=[6,+1] --> dif=-2, delta=-decRate*+1 (fwd-dec from +6 to +4)
// tar=[6,-1] && cur=[4,-1] --> dif=+2, delta= accRate*-1 (rev-acc from -4 to -6)
// tar=[4,-1] && cur=[6,-1] --> dif=-2, delta=-decRate*-1 (rev-dec from -6 to -4)
// tar=[4,-1] && cur=[4,+1] --> dif=-8, delta=-decRate*+1 (fwd-dec from +5 to 0, rev-acc from 0 to -5)
// tar=[4,+1] && cur=[4,-1] --> dif=-8, delta=-decRate*-1 (rev-dec from -5 to 0, fwd-acc from 0 to +5)
//
const float rawSpeedDiff = speeds[isReversing] - speeds[1 - isReversing];
const float absSpeedDiff = std::max(rawSpeedDiff, -rawSpeedDiff);

const float rawSpeedDiff = (targetSpeed * targetSpeedSign) - (currentSpeed * currentSpeedSign);
const float absSpeedDiff = math::fabs(rawSpeedDiff);
// need to clamp, game-supplied values can be much larger than |speedDiff|
// need to clamp, rates can be much larger than |speedDiff|
const float modAccRate = std::min(absSpeedDiff, maxAccRate);
const float modDecRate = std::min(absSpeedDiff, maxDecRate);

const float deltaSpeed = mix(modAccRate, -modDecRate, (rawSpeedDiff < 0.0f));

// no acceleration changes if not on ground
Expand All @@ -39,7 +51,7 @@ float GMTDefaultPathController::GetDeltaSpeed(
// ships should always use (1 - IsInWater()), hovercraft
// should use (1 - IsInWater()) but only when over water,
// tanks should also test if IsInWater() && !IsOnGround()
return (deltaSpeed * (1 - owner->IsInAir()));
return ((deltaSpeed * currentSpeedSign) * (1 - owner->IsInAir()));
}

#if 1
Expand Down

0 comments on commit f845c44

Please sign in to comment.