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 formula used for acceleration #12353

Merged
merged 8 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/breakages.md
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
Expand Up @@ -194,21 +194,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 @@ -231,15 +234,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
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 += added_velocity;
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
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
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;
appgurueu marked this conversation as resolved.
Show resolved Hide resolved
// 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
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)
sfan5 marked this conversation as resolved.
Show resolved Hide resolved
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
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