Skip to content

Commit

Permalink
Fix formula used for acceleration (minetest#12353)
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Sep 20, 2022
1 parent 11905a6 commit 1317cd1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/breakages.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This document contains a list of breaking changes to be made in the next major version.

* Remove attachment space multiplier (*10)
* Remove player gravity multiplier (*2)
* `get_sky()` returns a table (without arg)
* `game.conf` name/id mess
* remove `depends.txt` / `description.txt` (would simplify ContentDB and Minetest code a little)
Expand Down
16 changes: 10 additions & 6 deletions src/client/clientenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,24 @@ void ClientEnvironment::step(float dtime)
lplayer->applyControl(dtime_part, this);

// Apply physics
lplayer->gravity = 0;
if (!free_move) {
// Gravity
v3f speed = lplayer->getSpeed();
if (!is_climbing && !lplayer->in_liquid)
speed.Y -= lplayer->movement_gravity *
lplayer->physics_override.gravity * dtime_part * 2.0f;
// HACK the factor 2 for gravity is arbitrary and should be removed eventually
lplayer->gravity = 2 * lplayer->movement_gravity * lplayer->physics_override.gravity;

// Liquid floating / sinking
if (!is_climbing && lplayer->in_liquid &&
!lplayer->swimming_vertical &&
!lplayer->swimming_pitch)
speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
// HACK the factor 2 for gravity is arbitrary and should be removed eventually
lplayer->gravity = 2 * lplayer->movement_liquid_sink;

// Movement resistance
if (lplayer->move_resistance > 0) {
v3f speed = lplayer->getSpeed();

// How much the node's move_resistance blocks movement, ranges
// between 0 and 1. Should match the scale at which liquid_viscosity
// increase affects other liquid attributes.
Expand All @@ -232,15 +235,16 @@ void ClientEnvironment::step(float dtime)
(1 - resistance_factor);
v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
speed += d;
}

lplayer->setSpeed(speed);
lplayer->setSpeed(speed);
}
}

/*
Move the lplayer.
This also does collision detection.
*/

lplayer->move(dtime_part, this, position_max_increment,
&player_collisions);
}
Expand Down
11 changes: 8 additions & 3 deletions src/client/localplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
float player_stepheight = (m_cao == nullptr) ? 0.0f :
(touching_ground ? m_cao->getStepHeight() : (0.2f * BS));

v3f accel_f;
v3f accel_f(0, -gravity, 0);
const v3f initial_position = position;
const v3f initial_speed = m_speed;

Expand Down Expand Up @@ -778,6 +778,9 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
m_speed += m_added_velocity;
m_added_velocity = v3f(0.0f);

// Apply gravity (note: this is broken, but kept since this is *old* move code)
m_speed.Y -= gravity * dtime;

/*
Collision detection
*/
Expand Down Expand Up @@ -1117,8 +1120,10 @@ void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
}
}

float jump_height = 1.1f; // TODO: better than a magic number
v3f jump_pos = initial_position + v3f(0.0f, jump_height * BS, 0.0f);
float jumpspeed = movement_speed_jump * physics_override.jump;
float peak_dtime = jumpspeed / gravity; // at the peak of the jump v = gt <=> t = v / g
float jump_height = (jumpspeed - 0.5f * gravity * peak_dtime) * peak_dtime; // s = vt - 1/2 gt^2
v3f jump_pos = initial_position + v3f(0.0f, jump_height, 0.0f);
v3f jump_speed = initial_speed;

// try at peak of jump, zero step height
Expand Down
2 changes: 2 additions & 0 deletions src/client/localplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class LocalPlayer : public Player
bool swimming_vertical = false;
bool swimming_pitch = false;

f32 gravity = 0; // total downwards acceleration

void move(f32 dtime, Environment *env, f32 pos_max_d);
void move(f32 dtime, Environment *env, f32 pos_max_d,
std::vector<CollisionInfo> *collision_info);
Expand Down
5 changes: 3 additions & 2 deletions src/client/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ void Particle::step(float dtime)
}
m_pos = p_pos / BS;
} else {
// apply acceleration
// apply velocity and acceleration to position
m_pos += (m_velocity + m_acceleration * 0.5f * dtime) * dtime;
// apply acceleration to velocity
m_velocity += m_acceleration * dtime;
m_pos += m_velocity * dtime;
}

if (m_animation.type != TAT_NONE) {
Expand Down
7 changes: 4 additions & 3 deletions src/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
} else {
time_notification_done = false;
}

v3f newpos_f = *pos_f + (*speed_f + accel_f * 0.5f * dtime) * dtime;
*speed_f += accel_f * dtime;

// If there is no speed, there are no collisions
if (speed_f->getLength() == 0)
// If the object is static, there are no collisions
if (newpos_f == *pos_f)
return result;

// Limit speed for avoiding hangs
Expand All @@ -270,7 +272,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
//TimeTaker tt2("collisionMoveSimple collect boxes");
ScopeProfiler sp2(g_profiler, "collisionMoveSimple(): collect boxes", SPT_AVG);

v3f newpos_f = *pos_f + *speed_f * dtime;
v3f minpos_f(
MYMIN(pos_f->X, newpos_f.X),
MYMIN(pos_f->Y, newpos_f.Y) + 0.01f * BS, // bias rounding, player often at +/-n.5
Expand Down
3 changes: 1 addition & 2 deletions src/server/luaentity_sao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
m_velocity = p_velocity;
m_acceleration = p_acceleration;
} else {
m_base_position += dtime * m_velocity + 0.5 * dtime
* dtime * m_acceleration;
m_base_position += (m_velocity + m_acceleration * 0.5f * dtime) * dtime;
m_velocity += dtime * m_acceleration;
}

Expand Down

0 comments on commit 1317cd1

Please sign in to comment.