0
\$\begingroup\$

I'm having some trouble getting my friction to act properly in my game. My current goal is to add a dash move - which is working fine except that a bug with the friction is messing with me. The Dash move uses a movingAngle variable which basically tells you in what direction you are currently moving and uses that to push the entity in that direction,

The problem however is that when the entity is slowing down due to friction it sometimes moves very very slightly at the opposite direction for that axis. So for example if I'm moving at 90 degrees(up) I want it to keep that degree - even when it stops but it very often changes to 270 even though it's not moving by an amount visible to the eye.

So far I only have a character who has an acceleration, deceleration aka friction and a velocity vector.

Relevant Code

Variables of interest

Vector2 velocity = new Vector2(0, 0);
Vector2 acceleration = new Vector2(0, 0);
float max_moveSpeed = 70f; //The maximum movement speed that acceleration can influence
float move_accel = 80f; //The acceleration amount
float move_decel = 230f; //The deceleration, used when switching directions for snappier movement
float frc = 8f; //The friction that's applied
float max_speed = 1000f; //The overall maximum speed from knockback, dashes etc.
float movingAngle = 0f; //The angle of movement based on the velocity vector

Applying acceleration

//Makes sure the entity cannot accelerate over maximum movement speed
if(!(Math.abs(velocity.x) > max_moveSpeed))
        velocity.x += acceleration.x;
    if(!(Math.abs(velocity.y) > max_moveSpeed))
        velocity.y += acceleration.y;

Applying velocity to position:

transform.getPosition().x += velocity.x * Gdx.graphics.getDeltaTime();
transform.getPosition().y += velocity.y * Gdx.graphics.getDeltaTime();

Friction code

velocity.x -= (Math.min(Math.abs(velocity.x), frc) * Math.signum(velocity.x)) * Gdx.graphics.getDeltaTime() * 100f;
velocity.y -= (Math.min(Math.abs(velocity.y), frc) * Math.signum(velocity.y)) * Gdx.graphics.getDeltaTime() * 100f;

Get Angle from Velocity

public static float angleFromVelocity(Vector2 velocity) {
    float a = (float) Math.toDegrees(Math.atan2(velocity.y,  velocity.x));
    a += 360;

    if(a > 359)
        a -= 359;
    return a;
}

What I've tried

Only change movingAngle while above a threshold

    if(velocity.x > 0) {
        if(velocity.x <= 5f)
            velocity.x = 0f;
    } else if(velocity.x < 0) {
        if(velocity.x >= -5f) {
            velocity.x = 0f;
        }
    }

    if(velocity.y > 0) {
        if(velocity.y < 5f)
            velocity.y = 0f;
    } else if(velocity.y < 0) {
        if(velocity.y > -5f) {
            velocity.y = 0f;
        }
    }

Results: Didn't change much which makes me thing I've fundamentally misunderstood some of these physics calculations. Tried values ranging from 0.1 - 5.

Only change movingAngle based on velocity magnitude

if (velocity.len() > 1f)
    movingAngle = Utils.angleFromVelocity(velocity);

Results: Same as above.

I've also tried with easier fixes like velocity.scl(.9f) but same success with that.

It doesn't seem like many attemps but I'm not sure where to start really. Any help is appreciated!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.