Sign in to follow this  

simulate movement with drag

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Apply a vector with a coefficient of friction. You can probably make it up. Just apply it in the opposite direction of the objects velocity.

However you might need to clarify more. Like cloth? Leaves or some other objects that float? For solid heavy objects it's simple, but if you mean like object reacting to "fluid" air under themselves it might be different. (like a realistic parachute)

Share this post


Link to post
Share on other sites
This formula computes the normal force a fluid applies on a plane moving inside of it:

F = 0.5*C*p*A*v²

where:
C = drag constant(try something like 0.3 . . .
p = fluid density in kg/m³(air density = 1.29kg/m³)
A = surface area
v = surface velocity

Use the normal vector of the plane inside the function to correctly apply the drag when its normal is not parallel to its velocity vector(dot product)...hope this helps . . .

Share this post


Link to post
Share on other sites
Quote:
Like cloth? Leaves or some other objects that float?
No, a flying robot.

Quote:
F = 0.5*C*p*A*v²
Yes that is the problem I'm having ATM. If I use different time intervals to calculate the acceleration changes. It has a non-uniform acceleration (changes at every instant in time) which affects final velocity. So I'm trying to find an equation to calculate the velocity at a specified time since acceleration can't be used.

Share this post


Link to post
Share on other sites
Quote:
you need to compute the velocity through acceleration. To simulate this drag equation with more precision, try using Runge-Kutta4 Integration.
I don't think I should be using the acceleration since it can't be used to predict future velocities because after that point in time the acceleration changes. What type of graph is RK4? y = x^2? y = 1/x?
Quote:
//gameloop

//your physics here
velocity*=0.9f;

//render
//end of loop


and if your timestep is not fixed, well, fix it
heh, bad pun...
"your physics here" is what I'm trying to figure out. By timestep I'm guessing you mean the time difference between each loop of the program. Technically there's no way to make it "fixed", or either I haven't learned to make a program execute in exact loops/second. Is that what you mean by timestep?

edit: I see that RK4 might be helpful but I was hoping there was a precise way of doing it. I thought there would be an equation for this?

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
your physics here" is what I'm trying to figure out.


In many games, the velocity*=.9f is a decent semblance of drag. (dont quote me on this, but I think it ends up being equivalent to a first order apprxomation)
You sure that wasnt what your game needed?

Quote:
Original post by biscuitz
By timestep I'm guessing you mean the time difference between each loop of the program. Technically there's no way to make it "fixed", or either I haven't learned to make a program execute in exact loops/second. Is that what you mean by timestep?


Not exactly, by timestep, I mean the base unit of time you use when calculating physics and game logic.
At the moment, you probably have a physics update function, and every loop you call it, then in there you plug a time value into your physics equations for motion and whatnot. And you eiter get the time value from the number of loops so far, or from looking at the system clock... but the point is that it changes and could be anything.
Well, with a timestep, rather than plug unknown time values into your equations; you choose some constant time interval to use every time. To do this, on each loop you check the system clock and see how much time has passed since the last physics update, you then see how many of those timesteps can fit into that time passed, and call the physics functions that many times.
The point being that your motion equations use a constant timestep interval, rather than one that could change depending on how lagged your computer was at the moment. This makes your physics calculations more consistent. It also makes time a constant for many calculations and thus simplifies work.


Using a fixed timestep is important for the drag approximation method I listed above.

Share this post


Link to post
Share on other sites
atm I haven't coded anything yet because I'm stuck at the mathematics behind this. I don't see an advantage to using a constant for time interval. It won't make it any faster. Actually it sounds like it will add a divide into the mix. I'd rather use a time-difference technique so it looks better because you'll see what it is supposed to look like at that point in time. Are you talking about calculating the acceleration during that time frame? Is there any way that's not based off of acceleration because it's non-uniform.

Quote:
You sure that wasnt what your game needed?
I won't know unless I understand the extent of what this stuff is doing. As far as I can tell all these methods sound incorrect and I have no idea how far off they could be because I don't even know what the correct answer is.

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
atm I haven't coded anything yet because I'm stuck at the mathematics behind this. I don't see an advantage to using a constant for time interval. It won't make it any faster. Actually it sounds like it will add a divide into the mix. I'd rather use a time-difference technique so it looks better because you'll see what it is supposed to look like at that point in time.


Seeing as how we aren't even sure what integration method you are currently using, a single division operation is highly meaningless in the grand scheme of this game's efficiency.
Don't optimize until after it is complete.
At any rate, the purpose of a fixed timestep is not calculation speed, it is accuracy and consistancy.

Quote:
Original post by biscuitz
Are you talking about calculating the acceleration during that time frame? Is there any way that's not based off of acceleration because it's non-uniform.

But that above point might not mean much for your project at this point... ...whats this talk about acceleration???
How about staring from the beggining, what you're simulating, and what integration/physics methods you are using.


Quote:
Original post by biscuitz
As far as I can tell all these methods sound incorrect and I have no idea how far off they could be because I don't even know what the correct answer is.


*shrug* game physics can start to look pretty far off from the 'real' equations of motion; because in a game typically we use iterative approximations of the real integration math, and do it over succesive timesteps.
Additionally, game physics do not always have the goal of perfect realism, often times behaviour that is Stable and plays well is what we want.
What do You want though, and why?

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
... It has a non-uniform acceleration (changes at every instant in time) which affects final velocity. So I'm trying to find an equation to calculate the velocity at a specified time since acceleration can't be used.


To get the analytic solution, you need to solve the differential equation
    dv = kv2 dt (k = 0.5*C*p*A/m) 

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
atm I haven't coded anything yet because I'm stuck at the mathematics behind this.

And this is exactly your problem. You didn't code anything. If you did, you'd have a nice window with something that moves on the screen and decrease its acceleration because of air drag.

Have you answered the question why do you need precise information about maximum of function you described? In fact you are unlikely to use it, in real game. If it will be between two frames what would you do? In fact as I already told you, you need just calculation for next frame, and that's enough.

And if you'd need a precise number, just let it go, and then look at the final speed.

Quote:
I don't see an advantage to using a constant for time interval. It won't make it any faster. Actually it sounds like it will add a divide into the mix. I'd rather use a time-difference technique so it looks better because you'll see what it is supposed to look like at that point in time. Are you talking about calculating the acceleration during that time frame? Is there any way that's not based off of acceleration because it's non-uniform.

Quote:
You sure that wasnt what your game needed?
I won't know unless I understand the extent of what this stuff is doing. As far as I can tell all these methods sound incorrect and I have no idea how far off they could be because I don't even know what the correct answer is.


I try into anything, that has any time step, add fixed time step. If you have c^3 + ... c^4.... and so on, its significantly easier to compute if c = 1.

Of course there is a way, momentum transfer. You'd just need to create a program that would show a 3D model, and be able to compute collision detection. In fact what method is better for you might depend on how would look like that 3D model and collision detection. Thought this is even less mathematical like than that previous solution. This doesn't mean there isn't any math, because you would need a face normal.

And of course you can use Bernoulli equations, and do some fluid simulations. The problem is Bernoulli equations are just approximations.

Do rather some coding and use computer to find a solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
To get the analytic solution, you need to solve the differential equation
    dv = kv2 dt (k = 0.5*C*p*A/m) 

The problem is he is unable to solve differential equations. So you'd either need to tell him how to do that, or solve it for him in a way he would learn from your solution something.

Share this post


Link to post
Share on other sites
This is just an example of how to accomplish it, it's old code and I haven't reviewed it in a while so might contain errors.


#include <boost/function.hpp>

namespace ode
{
template <class T> inline T rk4(boost::function<T (float, const T&)> f, const T& y, float t, float h)
{
T k1 = f(t, y);
T k2 = f(t + 0.5f * h, y + 0.5f * h * k1);
T k3 = f(t + 0.5f * h, y + 0.5f * h * k2);
T k4 = f(t + h, y + h * k3);

return (y + (h / 6.0f) * (k1 + 2.0f * k2 + 2.0f * k3 + k4));
}
}






Matrix<6, 1> some_ode(float t, const Matrix<6, 1>& y)
{
float g = 9.81;
float p = 1.2;
float A = 0.004;
float m = 0.15;
float w = 60 * 3.14 ;
float SdivM = 4.1f * powf(10.0f, -4.0f);
float C = 0.5; // Drag constant

Matrix<6, 1> v;

v(0, 0) = y(1, 0);
v(1, 0) = - (C * p * A) / (2 * m) * sqrt( powf(y(1, 0), 2.0f) + powf(y(3, 0), 2.0f) ) * y(1, 0);
v(2, 0) = y(3, 0);
v(3, 0) = - g - (C * p * A) / (2 * m) * sqrt( powf(y(1, 0), 2.0f) + powf(y(3, 0), 2.0f) * y(3,0) );
v(4, 0) = y(5, 0);
v(5, 0) = - SdivM * w * y(1, 0) / m;

return v;
}







v(0, 0) = position.x; v(1, 0) = velocity.x; v(2, 0) = position.y; v(3, 0) = velocity.y;
v = ode::rk4<Matrix<6, 1>>(some_ode, v, 0, timeDelta);
position.x = v(0, 0);
position.y = v(2, 0);

velocity.x = v(1, 0);
velocity.y = v(3, 0);





This is for 2-dimensions (although it does add a magnus force in a third direction due to spin), extending it to 3 dimensions is trivial.

Share this post


Link to post
Share on other sites
So some clarification.

In first box you can see a simple one dimensional implementation of a RK4 integrator. (Thought Edgenworth said a version of RK4 optimized for 3D coordinate space should be used, if you need to integrate something in 3D rectangular space.)

The second box contains that integrated function.
(Thought I wonder why he didn't wrote SdivM = 4.1 * 0.0001;)
There would be your function.


Third box is a simple function call that calls that monstrosity. Sometimes throwing a C++ code into preprocessor would increase readibility by quite a lot. In fact this code gives a nice reason why abolish boost function from source code.


BTW
In my experience, integrators should be based on a rigidly specified data types. If there is a need for integrator that uses different data type, the best idea is to create new file, define new namespace, and by copy/paste and a little refactoring create in about one day a new working and tested integrator. (The increase in rigidness, ability of error cancellation, and increase in readability is quite worthy that five minutes typing, and few hours testing.)

Share this post


Link to post
Share on other sites
Quote:
Original post by haphazardlynamed
Is this not accurate enough for your purposes?
//your physics here
velocity*=0.9f;
//render
//end of loop
and if your timestep is not fixed, well, fix it
heh, bad pun...


Yep, this looks good, but is that physically correct?

And if you dont use fixed timestep, you are insane. The gameplay will be really different on different computers. And it is a lot harder to code.
Fixing timestep:

[warning, pascal zone, needs conversion to C]

//Game stuff
repeat
_physicstime=physicstime+10;
_DoPhysicsstep;
until physicstime>gettickcount;

//More game stuff
//Repeat.
[/pascal zone]

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBoltonTo get the analytic solution, you need to solve the differential equation
    dv = kv2 dt (k = 0.5*C*p*A/m) 
I've heard about that (differential equation). I'll check that out.

Quote:
And this is exactly your problem. You didn't code anything. If you did, you'd have a nice window with something that moves on the screen and decrease its acceleration because of air drag.
You can't make a game without math it's impossible. If you remove math from a game you'd end up with a blank window or maybe nothing (which any mediocre programmer can do). I'm not going to learn any more math by trying to program drag into a program if I don't even get the maths of it.

Quote:
Have you answered the question why do you need precise information about maximum of function you described?
I see no advantage in approximating. You won't get much more speed out of approximating with those techniques (as far as I can tell). As far as I can tell approximation techniques are just the lazy way of doing it. The main reason I want to know this information is so I know the difference between it and approximating. Basically I see this as someone trying to convince me to just use a library like SDL rather than use the Win32 library.

Quote:
five minutes typing, and few hours testing.)
Or I can *not waste time* trying to *approximate* the values and just use whatever the equation is supposed to be.

Quote:
And if you dont use fixed timestep, you are insane. The gameplay will be really different on different computers. And it is a lot harder to code.
I'm going to just multiply by the amount of time that has passed. Using fixed timestep requires finding the amount of time passed anyway so I mine as well just multiply by the time difference.

Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
Quote:
And if you dont use fixed timestep, you are insane. The gameplay will be really different on different computers. And it is a lot harder to code.
I'm going to just multiply by the amount of time that has passed. Using fixed timestep requires finding the amount of time passed anyway so I mine as well just multiply by the time difference.

I wouldn't say you are insane for not using a fixed timestep, but there are advantages to a fixed timestep. Gagyi's point is that if the timestep varies while doing numerical integration, then the results will also vary. Numerical integration is an approximation and its accuracy depends on the consistency of the timestep (among other things).

In case you don't know what fixed timestep means: You still measure the time between frames, but you run the simulation at fixed intervals while keeping up with real time. For example, if your fixed time step is 10ms and the last frame took 26ms, then you run the simulation twice (at +10ms and +20ms) this frame and save the remainder (6ms) for the next frame.

Share this post


Link to post
Share on other sites

velocity*=0.9f;


Can be modified to support non fixed timesteps:


velocity *= exp(-dt * c);


Where dt is the delta time for the frame and c is a constant depending on the drag. This formula is based on the analytical solution of a drag differential equation without any other forces.




Share this post


Link to post
Share on other sites
Quote:
Original post by biscuitz
Quote:
And this is exactly your problem. You didn't code anything. If you did, you'd have a nice window with something that moves on the screen and decrease its acceleration because of air drag.
You can't make a game without math it's impossible. If you remove math from a game you'd end up with a blank window or maybe nothing (which any mediocre programmer can do). I'm not going to learn any more math by trying to program drag into a program if I don't even get the maths of it.
It's called algorithm, aka if you'd remove an algorithm from a program it will not be functional.

However, if they didn't tell you this on your highschool yet, there are equations that don't have a closed form solution (in current meaning of the term closed form solution).

BTW how large air drag is at 15 km altitude? As you can see it's not only v^2 that would change. And now something really interesting. Type down a program that calculates 1E-2 + 1E-30.

Quote:
Quote:
Have you answered the question why do you need precise information about maximum of function you described?
I see no advantage in approximating. You won't get much more speed out of approximating with those techniques (as far as I can tell). As far as I can tell approximation techniques are just the lazy way of doing it. The main reason I want to know this information is so I know the difference between it and approximating.

If you have an approximation, you can guess its accuracy by decreasing that fixed time step. Halving a size of time step with first order integrator would double accuracy.

Quote:
Quote:
five minutes typing, and few hours testing.)
Or I can *not waste time* trying to *approximate* the values and just use whatever the equation is supposed to be.
How can you be so sure these equations you would like to use are not only approximations?

For example imagine you have three objects. Each of these objects is affected by F = k*m*M/r^2
each has some initial position, mass, and velocity.
Now write down an equation that would show theirs position after 12 hours.

Share this post


Link to post
Share on other sites

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this