Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix movement bugs that break integration tests #32

Draft
wants to merge 3 commits into
base: experimental
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions source/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,7 @@ void AI::MoveToAttack(Ship &ship, Command &command, const Body &target)
command.SetThrust(1.);

// Use an equipped afterburner if possible.
if(command.Has(Command::FORWARD) && direction.Length() < 1000. && ShouldUseAfterburner(ship))
if(command.Thrust() > 0 && direction.Length() < 1000. && ShouldUseAfterburner(ship))
command |= Command::AFTERBURNER;
}

Expand Down Expand Up @@ -2546,7 +2546,7 @@ void AI::PickUp(Ship &ship, Command &command, const Body &target)

// Use the afterburner if it will not cause you to miss your target.
double squareDistance = p.LengthSquared();
if(command.Has(Command::FORWARD) && ShouldUseAfterburner(ship))
if(command.Thrust() > 0 && ShouldUseAfterburner(ship))
if(dp > max(.9, min(.9999, 1. - squareDistance / 10000000.)))
command |= Command::AFTERBURNER;
}
Expand Down Expand Up @@ -3006,10 +3006,11 @@ bool AI::DoCloak(Ship &ship, Command &command)

void AI::DoScatter(Ship &ship, Command &command)
{
if(!command.Has(Command::FORWARD) && !command.Has(Command::BACK))
double thrust = command.Thrust();
if(!thrust)
return;

double flip = command.Has(Command::BACK) ? -1 : 1;
double flip = copysign(1, thrust);
double turnRate = ship.TurnRate();
double acceleration = ship.Acceleration();
// TODO: If there are many ships, use CollisionSet::Circle or another
Expand Down
18 changes: 12 additions & 6 deletions source/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4481,7 +4481,8 @@ void Ship::DoMovement(bool &isUsingAfterburner)
if(acceleration)
{
acceleration *= slowMultiplier;
Point dragAcceleration = acceleration - velocity * (Drag() / mass);
double drag = Drag();
Point dragAcceleration = acceleration - velocity * (drag / mass);
// Make sure dragAcceleration has nonzero length, to avoid divide by zero.
if(dragAcceleration)
{
Expand All @@ -4503,12 +4504,17 @@ void Ship::DoMovement(bool &isUsingAfterburner)
double vNormal = velocity.Dot(angle.Unit());
double aNormal = dragAcceleration.Dot(angle.Unit());
if((aNormal > 0.) != (vNormal > 0.) && fabs(aNormal) > fabs(vNormal))
dragAcceleration = -vNormal * angle.Unit();
{
velocity = acceleration = Point();
return;
}
}
if(velocity.Length() > MaxVelocity() || velocity.Length() < 0.1)
velocity += dragAcceleration;
else
velocity += acceleration;
double maxSpeedSquared = MaxVelocity();
maxSpeedSquared *= maxSpeedSquared;
double speedSquared = velocity.LengthSquared();
if(!commands.Has(Command::STOP) && speedSquared < maxSpeedSquared)
dragAcceleration *= 1 - (speedSquared / maxSpeedSquared);
velocity += dragAcceleration;
}
acceleration = Point();
}
Expand Down