Hi!
Recently i was messing with solving differential equations. I found neat article at code project and followed it. It works but I have some things that i don’t understand. For example check out this code:
IEnumerator Integration()
{
FlyingBall ball = new FlyingBall();
ISecondDerivative acceleration = (ISecondDerivative)ball;
double t0 = 0;
double tmax = 50;
double y0 = 0;
double h = Time.deltaTime * 2f;
double v0 = 50;
double a0 = acceleration.GetValue(t0, y0, v0);
IntegratorRKN integratorRKN = new IntegratorRKN(acceleration, t0, y0, v0,
h, a0);
Debug.Log("TEST4, equation of motion");
Debug.Log(" t,s y,m v,m/s a,m/s2");
Debug.Log("----------------------------");
Debug.Log(string.Format("{0,4:F1}{1,8:F2}{2,8:F2}{3,8:F2}", t0, y0, v0, a0));
double t, y, v, a;
do
{
integratorRKN.Step(out t, out y, out v, out a);
transform.SetY((float)y);
Debug.Log(string.Format("{0,4:F1}{1,8:F2}{2,8:F2}{3,8:F2}", t, y, v, a));
yield return null;
} while (t < tmax);
It is just a coroutine that is called to integrate the acceleration which is second derivative with modified runge kutta method.
here is the flying ball class
public class FlyingBall : ISecondDerivative
{
double dB; // Ball diameter, m
double roB; // Ball density, kg/m^3
double cD; // Drag coefficient
double g; // Gravitational acceleration, m/s^2
double roA; // Air density
double mB; // Ball mass, kg
double aB; // Ball cross-section area, m^2
public FlyingBall()
{
dB = 0.1;
roB = 600;
cD = 0.1;
g = 9.81;
roA = 1.29;
double v = Math.PI * Math.Pow(dB, 3f) / 6f;
mB = roB * v;
aB = 0.25f * Math.PI * dB * dB;
}
public double GetValue(double t, double y, double v)
{
double f = -mB * g -Math.Sign(v) * 0.5f * cD * roA * v * v * aB;
double d2y = f / mB;
return d2y;
}
}
Few questions:
-how to modify this to get calculation for all three coords? currently only y is calculated?
-I am using Time.deltaTime * 2f to step through calculation (Time.deltaTime) seemed too slow. Am I doing this the write way?
-Is it possible to mix rigid body and this type of calculations that affect position of the body? and what would be the proper way of doing it?
I know that i can use rigid body to get very good results, but i am trying to model something that is somewhat more complex so i wanted this approach, it is a good starting point.
Thank you!