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

Walk basis goes back on landing #74

Open
Dashurai opened this issue Dec 26, 2024 · 1 comment
Open

Walk basis goes back on landing #74

Dashurai opened this issue Dec 26, 2024 · 1 comment

Comments

@Dashurai
Copy link

Dashurai commented Dec 26, 2024

When I land after a jump, the entity walks back a little bit. This does not seem to be influenced by the input of TnuaBuiltinWalk, just some added Velocity that does not impact the rotation like desired_forward. This feels like a slight knockback.

For a full jump of height 4. when walking along -X, I get Vec3(1.0364642, 0., 0.) velocity upon landing.

It even happens when jumping on a dynamic cube. Sometimes it moves the cube normally on contact, sometimes the cube has no added velocity and the control entity moves back on it like on a static surface.

The aforementioned entities :

(
    RigidBody::Dynamic,
    Collider::capsule(0.5, 2.),
    TnuaController::default(),
    TnuaAvian3dSensorShape(Collider::cylinder(0.49, 0.)),
    LockedAxes::new().lock_rotation_x().lock_rotation_z(),
    Transform::from_xyz(0., 2., 0.),
)
(
    Name::new("ground"),
    RigidBody::Static,
    Collider::half_space(Vec3::Y),
)
(
    Name::new("cube"),
    RigidBody::Dynamic,
    Collider::cuboid(3., 3., 3.),
    Transform::from_xyz(6., 1.5, 0.),
)

Here's how I checked velocities :

fn test(
    control: Query<&TnuaController>,
    cube: Query<(&Name, &LinearVelocity), Changed<LinearVelocity>>,
) {
    if let Some((_, walk)) = control.single().concrete_basis::<TnuaBuiltinWalk>() {
        if walk.running_velocity.x < 0. && walk.running_velocity.x.abs() > 0.1 {
            dbg!(walk.running_velocity.x);
        }
    }
    if let Some(cube) = cube.iter().find(|(n, _)| n.as_str() == "cube") {
        if cube.1.x.abs() > 0.1 {
            dbg!(cube.1.x);
        }
    }
}

Lastly, here's where I feed walk and jump :

fn input(
    kb: Res<ButtonInput<KeyCode>>,
    mut control: Query<&mut TnuaController>,
    mut last_dir: Local<Vec3>,
) {
    if let Ok(mut control) = control.get_single_mut() {
        let a = |pos, neg| {
            0. + if kb.pressed(pos) { 1. } else { 0. } + if kb.pressed(neg) { -1. } else { 0. }
        };
        let dir = Vec3::new(a(KeyD, KeyA), 0., a(KeyS, KeyW));
        if dir != Vec3::ZERO {
            *last_dir = dir;
        }
        control.basis(TnuaBuiltinWalk {
            desired_velocity: dir.normalize_or_zero() * 10.,
            desired_forward: Some(
                Dir3::new(dir).unwrap_or(Dir3::new(*last_dir).unwrap_or(Dir3::Z)),
            ),
            float_height: 1.5,
            coyote_time: 0.05,
            ..default()
        });
        if kb.pressed(Space) {
            control.action(TnuaBuiltinJump {
                height: 4.,
                ..default()
            });
        }
    }
}

While I ended up only debugging the x axis, it works the same on z and any combination of the two, it just walks back.
I at first thought it was the velocity intended to move dynamic bodies on landing, also applied to static bodies, ending up applying the inverse velocity to the control entity, but it sometimes does not move them.

@idanarye
Copy link
Owner

This is a friction issue:

  • When I change your example to have a higher float_height, the problem disappear. That's because the character does not touch the ground.
  • When I change your example to have a slightly higher float_height (e.g. - 1.6 instead of 1.5) - the problem still happens - but less often, and with smaller magnitude. That's because the character doesn't always hit the ground when it falls - but sometimes it does.
  • When I add Friction::ZERO.with_combine_rule(CoefficientCombine::Min) to the character (and keep using your original float_height - the problem disappear.

I'll try to keep looking into why it happens, but for now either use a large enough float_height for the character to actually float (which, BTW, is the intended way to use Tnua) or set a zero friction (which is also useful against other problems, like #75)

idanarye added a commit that referenced this issue Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants