# [SOLVED] Physics 2D drag problem

## Recommended Posts

Hi, I'm having difficulties with drag. My coefficients are set to: k1=.5 k2=.5 My equation to integrate the drag looks like this:

drag=(k1*accAccumX)+(k2*(accAccumX*accAccumX));
grandVelX=accAccumX*(-drag);


The problem is that I am getting crazy results... i.e. the player goes really fast in the opposite direction I move him. Any idea what I am doing wrong? Thanks, James PS. I've taken mass out of the equation until I get the system to the point where everything works. [Edited by - NotTheCommonDose on June 22, 2009 7:10:35 PM]

##### Share on other sites
h4tt3n    1974
Ah, I made that error once too :-) I assume that accAccumX is either a vector or the vertical part of a vector - in other words it can have both negative and positive value! The trouble lies here: (accAccumX*accAccumX) since + * + and - * - is always positive, your drag will work in the same direction no matter how the body moves. All you need to do is say (accAccumX*abs(accAccumX)), making sure one of the terms is always positive. Then your drag will work in the opposite way of the body's movement.

Cheers,
Mike

##### Share on other sites
I'm starting to get the feeling that setting a maximum velocity as a result of drag isn't going to be simple.

##### Share on other sites

PS. you've been a lot of help :D

##### Share on other sites
By Jove, I think I've got it! Since it's a force I need to add it not multiply it to the acceleration.

The correct code looks like this:

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

Again, thanks! I would have still been stuck on direction otherwise.

James

##### Share on other sites
h4tt3n    1974
Glad you got it working. That math.abs() thing had me ripping my hair out too :-)

Here's what my drag equation looks like. It works on force-level and only depends on an air friction coefficient, particle area, and particle velocity squared.

Force += -AirFriction*Area*Velocity*Abs(Velocity)

Cheers,
Mike

##### Share on other sites
Ilici    862
Quote:
 Original post by NotTheCommonDoseI'm starting to get the feeling that setting a maximum velocity as a result of drag isn't going to be simple.

It's not simple but not too hard either:

Write the equations of motion:

F = a - k*v;
v = integral(F)

=>

a - k*v = dv/dt;

Or as a diff equation:

dv/(a - kv) = k*dt

Thus you get v = vmax*(1 - exp(-k*t))

You want v ~= vmax (equal to some degree) => 0.99vmax = vmax(1-exp(-k*t)) for some t you want: Solve for k to get the constant that will give you a certain maximum velocity, reached within the required time.

From here