Page 1 of 1

Why is drag only applied when acceleration is 0?

PostPosted: Tue Nov 18, 2014 8:59 pm
by xxbbcc
I'm using Axel 0.9.3 and ran into an issue with my character changing direction while running: drag is not applied to the character because it has acceleration. This is what happens: the character is moving in one direction (let's say right). I press the left arrow to start running left. At this point, acceleration is applied to the character so at first velocity.x decreases and then it turns around.

I also have drag set to imitate friction - when the character is moving (without acceleration), drag kicks in and the character comes to a stop in a short distance. Drag, however, is not applied when there's acceleration - in my above example this leads to the character skidding great distance to the right first and then finally starting to move to the left. The skidding distance is much greater than the 'normal' stopping distance.

Drag is applied in AxEntity.calculateVelocity() which skips applying drag when acceleration is not 0. Why is it done this way?

I'm asking because I'm considering changing this function to apply drag even during acceleration but first I wanted to understand why Axel has it implemented the way it has.

Re: Why is drag only applied when acceleration is 0?

PostPosted: Wed Nov 19, 2014 1:21 am
by Arkeus
The short answer: It's changed in 0.9.4 such that it is only skipped if you are accelerating in the direction you are moving. See for 0.9.4 beta.

Longer answer (in case you can't upgrade):
The biggest reason is that it's not what most games expect, and it requires more intimate knowledge of the inner workings of velocity/acceleration users to see results they expect. A quick example. Let's say someone sets acceleration.x to 10, drag.x to 500 (or something). Typically what people would expect is that while they hold right, they end up with a velocity of 10 after 1 frame, 20 after 2, etc, and when they let go, they decelerate to 0. If drag were applied while you were accelerating, your actual velocity wouldn't match what you set your acceleration to, which can be confusing (and often is for new users). The change in 0.9.4 solves this by still skipping it if you are accelerating in the direction you are moving on the x axis, but applies it if you accelerate in the other direction, which solves the problem you are seeing, (hopefully) without adding confusion.

Re: Why is drag only applied when acceleration is 0?

PostPosted: Wed Nov 19, 2014 7:22 am
by xxbbcc

Thank you for your reply - you confirmed what I thought may have been the reason behind it. I intend changing it the way you did in 0.9.4 - only apply drag when acceleration points away from direction of movement.

I can't upgrade - my game's well underway and a lot of code has been written with 0.9.3 objects and namespaces but I'll get 0.9.4 and merge over your change. I assume it's only that calculateVlocity() function (firther assuming you haven't refactored that bit) that needs to be merged.