Fix inconsistent camera/character yaw, make character speed modifiable

This commit is contained in:
tovjemam 2026-06-05 23:30:24 +02:00
parent d3848169e8
commit dbe2e0da8e
5 changed files with 26 additions and 21 deletions

View File

@ -37,9 +37,8 @@ void game::Character::Update()
Super::Update();
SyncTransformFromController();
root_.UpdateMatrix();
UpdateMovement();
root_.UpdateMatrix();
sync_current_ = 1 - sync_current_;
UpdateSyncState();
@ -147,22 +146,21 @@ void game::Character::SyncTransformFromController()
void game::Character::UpdateMovement()
{
constexpr float dt = 1.0f / 25.0f;
constexpr float running_mult = 3.0f;
bool walking = false;
bool running = false;
glm::vec2 movedir(0.0f);
if (in_ & (1 << CIN_FORWARD))
movedir.x += 1.0f;
movedir.y += 1.0f;
if (in_ & (1 << CIN_BACKWARD))
movedir.x -= 1.0f;
if (in_ & (1 << CIN_RIGHT))
movedir.y -= 1.0f;
if (in_ & (1 << CIN_RIGHT))
movedir.x -= 1.0f;
if (in_ & (1 << CIN_LEFT))
movedir.y += 1.0f;
movedir.x += 1.0f;
glm::vec3 walkdir(0.0f);
@ -173,17 +171,19 @@ void game::Character::UpdateMovement()
if (in_ & (1 << CIN_SPRINT))
running = true;
float target_yaw = forward_yaw_ + std::atan2(movedir.y, movedir.x);
Turn(yaw_, target_yaw, 8.0f * dt);
float target_yaw = forward_yaw_ + std::atan2(movedir.x, movedir.y);
Turn(yaw_, target_yaw, turn_speed_ * dt);
glm::vec3 forward_dir(glm::cos(yaw_), glm::sin(yaw_), 0.0f);
glm::vec3 forward_dir(-glm::sin(yaw_), glm::cos(yaw_), 0.0f);
walkdir = forward_dir * walk_speed_ * dt;
if (running)
walkdir *= running_mult;
walkdir *= run_speed_mult_;
}
root_.local.rotation = glm::angleAxis(yaw_, glm::vec3(0.0f, 0.0f, 1.0f));
if (controller_)
{
auto& bt_character = controller_->GetBtController();
@ -200,7 +200,7 @@ void game::Character::UpdateMovement()
MoveToward(animstate_.loco_blend, run_blend_target, dt * 2.0f);
float anim_speed = glm::mix(0.5f, 1.5f, UnMix(0.0f, 0.5f, animstate_.loco_blend));
if (running)
anim_speed *= running_mult;
anim_speed *= run_speed_mult_;
animstate_.loco_phase = glm::mod(animstate_.loco_phase + anim_speed * dt, 1.0f);
}

View File

@ -71,12 +71,14 @@ public:
void SetPosition(const glm::vec3& position);
~Character() override = default;
protected:
void SetIdleAnim(const std::string& anim_name);
void SetWalkAnim(const std::string& anim_name);
void SetRunAnim(const std::string& anim_name);
~Character() override = default;
private:
void SyncControllerTransform();
void SyncTransformFromController();
@ -88,6 +90,11 @@ private:
assets::AnimIdx GetAnim(const std::string& name) const;
protected:
float turn_speed_ = 8.0f;
float walk_speed_ = 2.0f;
float run_speed_mult_ = 3.0f;
private:
CharacterTuning tuning_;
@ -103,8 +110,6 @@ private:
float yaw_ = 0.0f;
float forward_yaw_ = 0.0f;
float walk_speed_ = 2.0f;
SkeletonInstance sk_;
CharacterAnimState animstate_;

View File

@ -29,7 +29,7 @@ void game::HumanCharacter::SetRideable(Rideable* rideable, size_t seat_idx)
Attach(rideable->GetEntity().GetEntNum());
SetIdleAnim((rideable->GetRideableType() == RIDEABLE_VEHICLE && seat_idx == 0) ? "vehicle_drive" : "vehicle_passenger");
SetYaw(rideable->GetRideableType() == RIDEABLE_VEHICLE ? 0.5f * glm::pi<float>() : 1.0f * glm::pi<float>());
SetYaw(0.0f);
}
else
{

View File

@ -180,8 +180,8 @@ bool game::view::CharacterView::ReadState(net::InMessage* msg)
return false;
net::DecodePosition(sync_.pos, new_state.trans.position);
new_state.trans.rotation = glm::rotate(glm::quat(1.0f, 0.0f, 0.0f, 0.0f),
sync_.yaw.Decode() + glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
new_state.trans.rotation =
glm::rotate(glm::quat(1.0f, 0.0f, 0.0f, 0.0f), sync_.yaw.Decode(), glm::vec3(0, 0, 1));
}
if (fields & CSF_IDLE_ANIM)

View File

@ -131,7 +131,7 @@ void game::view::ClientSession::GetViewInfo(glm::vec3& eye, glm::mat4& view) con
float yaw_sin = glm::sin(yaw_);
float pitch_cos = glm::cos(pitch_);
float pitch_sin = glm::sin(pitch_);
glm::vec3 dir(yaw_cos * pitch_cos, yaw_sin * pitch_cos, pitch_sin);
glm::vec3 dir(-yaw_sin * pitch_cos, yaw_cos * pitch_cos, pitch_sin);
glm::vec3 end = start - dir * distance;