Differential equatations in Unity3D

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!

It looks like you should be able to adapt the methods to deal with Vector3 instead of double (for position and its derivatives), after which you’ll have to change your doubles to floats, or define your own Vector3-double operators, or else roll out your own version of a Vector3 that’s backed by double fields if you don’t want to lose the precision that a double affords you. At some point you will have to cast to float anyways for Unity to work with it, so you’ll lose some precision, but you can defer that until the point that you actually need to display your results.

You shouldn’t have to scale deltaTime - if it seemed too slow to you then its most likely an issue regarding scale.

You can indeed mix Rigidbody positions and rotations with your own calculations, take a look at the documentation for Rigidbody, in particular the MovePosition and MoveRotation methods.

I hope that helps, if you need more details I might be able to help further but my calculus is spotty at best.