combining strafe and walk?

Started by
20 comments, last by cozzie 10 years, 3 months ago
Almost! Most of it looks good.

Right now there's just one problem that I see: Your units of measure are inconsistent. By "units of measure", I mean:

When your movements are being made by input keys, the units-of-measure are unitless.

When you multiply by your walking speed, you convert your unitless value to a distance-per-second value.

When you multiply your distance-per-second value by the time delta, time delta is a seconds-per-frame value, so your result is a distance-per-frame value.

Having different units of measure in different steps of the calculation is fine, you just need to make sure each math expression is consistent.

When you compare, add, or subtract different values, you need to make sure they're using the same units of measure, or the operation won't make sense.

Here's what I would do:


movement = movement * _player.GetWalkSpeed(); // converts movement from unitless to distance-per-second.

float speed = movement.magnitude();

if(speed > _player.GetWalkSpeed()) // distance-per-second vs. distance-per-second
    movement = movement * _player.GetWalkSpeed() / speed; // distance-per-second * (distance-per-second / distance-per-second) => distance-per-second

movement = movement * _timer.GetDelta(); // converts distance-per-second to distance-per-frame.
If you want to have different walking/strafing/flying speeds, then you could do it kind of like you were saying, except leave the time delta part off.

I would always do the GetDelta part at the very end because that always needs to be done, only needs to be done once, and doesn't need to happen before the speed limit check. Putting it at the end means all your other calculations can be done in distance-per-second instead of needing to convert back and forth between distance-per-second and distance-per-frame.

Reference: http://en.wikipedia.org/wiki/Units_of_measurement#Guidelines
Advertisement

I fully get it, thanks for the clear explanation.

All set to go, just did the changes and 'written them out' to make sure I understand.

Always using the same units of measure in the end, will definately give me flexibility for the future and prevents confusion and nasty bugs (thus saves time).

I removed the whole separate strafe speed, because I don't need it anymore. Keeping the code clean :)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement