Skip to content

Commit

Permalink
fix: wrong velocity when speed is reduced
Browse files Browse the repository at this point in the history
  • Loading branch information
KalinZhang committed Apr 3, 2021
1 parent f28dfca commit 126fc66
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/json
Submodule json updated from 176d8e to 1cd5ab
2 changes: 1 addition & 1 deletion include/record-sdk
25 changes: 18 additions & 7 deletions world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,25 @@ bool thuai::World::Update(int FPS, int32 velocityIterations,
}
// reduce player's velocity if distance satisfies part II
if (isSpeedDown) {
auto current_velocity_normalized =
Vec2D{b2currentPlayer->GetLinearVelocity().x,
b2currentPlayer->GetLinearVelocity().y}
.normalized(); // current direction


float current_velocity_norm =
std::min(SPEED_ON_SPEED_REDUCE,
double(sqrt(b2currentPlayer->GetLinearVelocity().x *
b2currentPlayer->GetLinearVelocity().x +
b2currentPlayer->GetLinearVelocity().y *
b2currentPlayer->GetLinearVelocity()
.y))); // make sure the volocity leq 0.5

b2currentPlayer->SetLinearVelocity(
{static_cast<float>(std::min(
currentPlayer->facing().x * SPEED_ON_SPEED_REDUCE,
static_cast<double>(b2currentPlayer->GetLinearVelocity().x))),
static_cast<float>(
std::min(currentPlayer->facing().y * SPEED_ON_SPEED_REDUCE,
static_cast<double>(
b2currentPlayer->GetLinearVelocity().y)))});
{static_cast<float>(current_velocity_norm *
current_velocity_normalized.x),
static_cast<float>(current_velocity_norm *
current_velocity_normalized.y)});
}

} // Player update ends
Expand Down

0 comments on commit 126fc66

Please sign in to comment.