Example #1
0
    public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force, decimal deltaTime = -1m)
    {
        if (deltaTime < 0.0m)
        {
            deltaTime = (decimal)Time.deltaTime;
        }

        //Debug.Log($"deltaTime: {deltaTime}, force: {force}");
        switch (mode)
        {
        case ForceMode.Acceleration:
            vel += force * (decimal)deltaTime;
            return;

        case ForceMode.Force:
            vel += (force / (decimal)mass) * (decimal)deltaTime;
            return;

        case ForceMode.Impulse:
            vel += force / mass;
            return;

        case ForceMode.VelocityChange:
            vel += force;
            return;
        }
    }
Example #2
0
    void Update()
    {
        for (int i = 0; i < bodies.Count - 1; i++)
        {
            for (int j = i + 1; j < bodies.Count; j++)
            {
                VirtualPhysicsTransform bodyA = bodies[i].PhysicsTransform, bodyB = bodies[j].PhysicsTransform;

                decimal squardDist = Vector3.DistanceSquared(bodyA.Position, bodyB.Position);

                decimal forceMagnitude = 0m;

                if (squardDist > 0.001m)
                {
                    forceMagnitude = (G * bodyA.Mass * bodyB.Mass) / squardDist;
                }

                Vector3 force = (bodyA.Position - bodyB.Position).Normalized * forceMagnitude;

                //Two birds with one stone
                bodyA.AddForce(-force);
                Debug.DrawRay(bodyA.Position, -force / bodyA.Mass, Color.green, Time.deltaTime, false);
                bodyB.AddForce(force);
                Debug.DrawRay(bodyB.Position, force / bodyB.Mass, Color.yellow, Time.deltaTime, false);
            }
        }
    }
Example #3
0
    public virtual void Tick(decimal deltaTime)
    {
        prevPos    = pos;
        prevVel    = vel;
        prevRot    = rot;
        prevRotVel = rotVel;

        pos += vel * deltaTime;
        rot *= Quaternion.Lerp(Quaternion.Identity, RotationalVelocity, deltaTime);
    }
Example #4
0
    void Start()
    {
        GravitySim.Instance.Add(this);

        if (orbitTarget != null)
        {
            physicsTransform.Velocity =
                Vector3.Cross((physicsTransform.Position - orbitTarget.PhysicsTransform.Position), Vector3.Up).Normalized *
                MathHelper.DSqrt((GravitySim.G * orbitTarget.PhysicsTransform.Mass) / Vector3.Distance(physicsTransform.Position, orbitTarget.PhysicsTransform.Position));
        }

        Debug.Log(physicsTransform.Velocity);
    }
Example #5
0
    public void Initialise(Vector3 initPos, Vector3 initVel, Quaternion initRot, Quaternion initRotVel)
    {
        pos     = initPos;
        prevPos = initPos;

        vel     = initVel;
        prevVel = initVel;

        rot     = initRot;
        prevRot = initRot;

        rotVel     = initRotVel;
        prevRotVel = initRotVel;
    }
Example #6
0
 public void Translate(Vector3 dist) => Position += dist;
Example #7
0
    //public void LookAt(Vector3 pos) => LookAt(pos, Vector3.Up);

    //public void LookAt(Vector3 pos, Vector3 up) => Rotation = Quaternion.LookRotation(pos - Position, up);

    public void RotateAround(Vector3 axis, decimal degrees) => rot *= Quaternion.CreateFromAxisAngle(axis, (decimal)degrees);
Example #8
0
 public VirtualPhysicsTransform(Vector3 initPos, Vector3 initVel, Quaternion initRot, Quaternion initRotVel)
 {
     Initialise(initPos, initVel, initRot, initRotVel);
 }