Beispiel #1
0
    public static DVec3 computeGravity(DoublePhysicsBody src, DoublePhysicsBody dst)
    {
        var srcComp = src.GetComponent <DoubleTransform>();
        var dstComp = dst.GetComponent <DoubleTransform>();

        var srcPos = srcComp.position;
        var dstPos = dstComp.position;

        var diff = dstPos - srcPos;
        var r2   = DVec3.dot(diff, diff);

        double fMagnitude = gravitationalConstant * src.mass * dst.mass / r2;

        var result = diff.normalized * fMagnitude;

        return(result);
    }
    void updatePhysics(double dt)
    {
        physRecords.Clear();
        collectPhysicsObjects(transform, physRecords);

        for (int i = 0; i < physRecords.Count; i++)
        {
            var cur = physRecords[i];
            cur.phys.resetForces();
        }

        for (int i = 0; i < physRecords.Count; i++)
        {
            var src = physRecords[i];
            if (!src.phys.createGravity)
            {
                continue;
            }
            for (int j = 0; j < physRecords.Count; j++)
            {
                if (i == j)
                {
                    continue;
                }
                var dst   = physRecords[j];
                var force = DoublePhysicsBody.computeGravity(src.phys, dst.phys);
                if (src.phys.receiveGravity)
                {
                    src.phys.addForce(force);
                }
                if (dst.phys.receiveGravity)
                {
                    dst.phys.addForce(-force);
                }
            }
        }

        for (int i = 0; i < physRecords.Count; i++)
        {
            var cur = physRecords[i];
            cur.phys.updatePositions(dt);
        }
    }
 public PhysBodyRecord(DoublePhysicsBody phys_, DoubleTransform dtr_)
 {
     phys = phys_;
     dtr  = dtr_;
 }