i always thought there was some *magical* air friction going on under the hood.
Entity.LinearDamping defaults to 0.03, so there is a bit of magical air friction unless it's manually set to 0. The character does so in its constructor so that the horizontal motion constraint doesn't have to fight an extra force.
Any advice on the subject? I suppose i'll need to do some sort of counter impulses, one inverse gravity continuously, and some lerps between actual entity.linearVelocity and zero. Where should i put it?
You can prevent an entity from having gravity applied to it by setting the entity.IsAffectedByGravity property to false.
Clamping falling speed would look something like this:
Code: Select all
var velocity = entity.LinearVelocity;
if (velocity.Y < -50)
{
velocity.Y = -50;
entity.LinearVelocity = velocity;
}
Stopping all linear motion can be done by just setting the linear velocity to the zero vector. Multiplying the linear velocity by some fixed factor (say, 0.95f) every frame where you aren't trying to move is one easy and cheap way to slow things down smoothly. A lerp is another option.
A slightly more involved option would be to compute the 'unwanted' motion at any given time, and apply an impulse/change the velocity to specifically remove that motion. If you aren't trying to move, then the unwanted motion is any and all motion. If you are trying to move, then the unwanted motion is any side-to-side (and optionally, going faster than a maximum speed). The unwanted motion can be immediately removed by subtracting it from the linear velocity, or you can remove it over time using a lerp/exponential falloff/explicit counter impulse.
The velocity changes can be put just about anywhere. Just doing it external to the space update should work fine. If you want, you could put it into the forces stage of the space update using an IDuringForcesUpdateable or hooking onto the starting/finishing event of a nearby update stage in the Space, but that's generally only useful for systems with extremely tight timing requirements.
One final option would be to use a SingleEntityLinearMotor (or the EntityMover which uses a SingleEntityLinearMotor internally) to handle the motion. Specify a goal (position or velocity wise), configure the motor strength, and it will do the rest.
Edit:
Tell us the truth, you sleeping here right?
Maybe...