Loading
Dev Blog: Project Moldering

I was having a lot of problems trying to create a drag force as a result of speed. I finally got it after realizing that you don’t multiply the drag to the acceleration you add it as it’s an opposing force.


For anyone interested the code looks like this:

        k1=.09;
        k2=.01;
        drag=(k1*accAccumX)+
(k2*(accAccumX*Math.abs(accAccumX))); accAccumX-=drag; grandVelX=accAccumX;

In this case there are two coefficients of drag, k1 and k2. K1 is general drag and k2 is the limiting drag. As the object reaches higher speeds the k2 drag will be higher and as a result you will have more negative force.

In the real world you would have much higher coefficients, but in a game you’ll want to mess with this value to get the desired movement you want.

Now we can alter these drag values when in water or other fluids.

Feel free to use this code without my permission. It’s a very general equation.

-James

Bookmark and Share