コード例 #1
0
        public void Execute(int i)
        {
            Position position = recieverPositions[i];
            Physical physical = recieverPhysicals[i];

            float3 forceSum = new float3(0, 0, 0);

            for (int j = 0; j < generatorCount; ++j)
            {
                Position       generatorPosition = generatorPositions[j];
                ForceGenerator forceGenerator    = generatorForces[j];

                float3 distance       = position.Value - generatorPosition.Value;
                float  distanceMagSqr = math.lengthsq(distance);
                if (distanceMagSqr < forceGenerator.distance * forceGenerator.distance)
                {
                    // Linear decay over distance
                    float f = 1 - math.sqrt(distanceMagSqr) / forceGenerator.distance;
                    forceSum += math.normalize(distance) * f * forceGenerator.force;
                }
            }

            recieverPhysicals[i] = new Physical {
                Force       = physical.Force + forceSum,
                InverseMass = physical.InverseMass
            };
        }
コード例 #2
0
    // Update is called once per frame
    void Update()
    {
        if (applyGravity)
        {
            AddForce(ForceGenerator.GenerateForce_gravity(Vector2.up, GRAVITY, Mass));
        }
        if (applyDrag)
        {
            AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(0f, 5f), 1.0f, 1.0f, 0.05f));
        }

        norm = ForceGenerator.GenerateForce_normal(new Vector2(0, GRAVITY), new Vector2(-1f, 1f));
        if (kineticFriction)
        {
            AddForce(ForceGenerator.GenerateForce_friction_kinetic(norm, velocity, 0.55f));
        }
        if (staticFriction)
        {
            AddForce(ForceGenerator.GenerateForce_friction_static(norm, velocity, 0.60f));
        }
        if (isSliding)
        {
            AddForce(ForceGenerator.GenerateForce_sliding(GRAVITY_VEC, -velocity));
        }
        if (isSpring)
        {
            AddForce(ForceGenerator.GenerateForce_spring(position, new Vector2(1.0f, 0.0f), 2.5f, 100f));
        }
    }
コード例 #3
0
    private void FixedUpdate()
    {
        // Change position to the positional variables
        transform.position = position;

        // Apply all torque forces to particle
        for (int i = 0; i < torqueForces.Length; i++)
        {
            ApplyTorque(torqueForces[i].force, torqueForces[i].position);
        }

        // Change rotation to the rotational variables
        if (hasGravity)
        {
            AddForce(ForceGenerator.GenerateForce_Gravtity(mass, -0.99f, WORLD_UP));
        }

        deltaTime = Time.fixedDeltaTime;

        // Update postion and velocity
        updateRotationEulerExplicit(Time.fixedDeltaTime);
        updatePositionEulerExplicit(Time.fixedDeltaTime);

        // Update accelerations
        UpdateAcceleration();
        UpdateAngularAcceleration();

        transform.eulerAngles = new Vector3(0, 0, rotation);
    }
コード例 #4
0
 public static void RemoveForceGenerator(ForceGenerator generator)
 {
     foreach (ForceGenerator gen in mGenerators)
     {
         if (gen.gameObject.Equals(generator.gameObject))
         {
             mGenerators.Remove(gen);
         }
     }
 }
コード例 #5
0
 // [SerializeField] ForceExplosionController forceExplosionController;
 protected override void OnUpdate()
 {
     Entities.ForEach((Entity entity, ref ForceGenerator forceGenerator, ref CopyForceFromExplosion copyForceFromExplosion) => {
         var forceExplosionController = EntityManager.GetComponentObject <ForceExplosionController>(entity);
         forceGenerator = new ForceGenerator {
             force    = forceExplosionController.force,
             distance = forceExplosionController.distance
         };
     });
 }
コード例 #6
0
ファイル: Hero.cs プロジェクト: Matt343/jolly-polarity-game
    void Start()
    {
        this.HeroController = this.GetComponent <HeroController> ();
        this.forceGenerator = this.GetComponentInChildren <ForceGenerator> ();
        this.magneticObject = this.GetComponent <MagneticObject> ();
        this.anim           = this.GetComponent <Animator> ();

        JollyDebug.Watch(this, "FacingRight", delegate() {
            return(this.FacingRight);
        });
    }
コード例 #7
0
ファイル: Particle2D.cs プロジェクト: Bcoop098/bencChamplain
    private void Update()
    {
        if (forceType == Forces.TorqueTest)
        {
            ApplyTorque(localSpace, new Vector2(-5, 0));
        }
        if (forceType == Forces.Gravity)
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass));
        }

        else if (forceType == Forces.NormalForce)
        {
            AddForce(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1.0f, 0.0f).normalized));
        }

        else if (forceType == Forces.SlidingForce)
        {
            AddForce(ForceGenerator.GenerateForce_Sliding(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1f, 1f).normalized)));
        }

        else if (forceType == Forces.StaticFriction)
        {
            Vector2 Normal = ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1.0f, 1.0f).normalized);
            AddForce(ForceGenerator.GenerateForce_friction_static(Normal, new Vector2(5, 5), 0.5f));
        }

        else if (forceType == Forces.KinematicFriction)
        {
            AddForce(ForceGenerator.GenerateForce_friction_kinetic((ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1f, 1f).normalized)), new Vector2(1, 1), 0.5f));
        }

        else if (forceType == Forces.Drag)
        {
            AddForce(ForceGenerator.GenerateForce_drag(new Vector2(1, 1), new Vector2(0.5f, 1), 1.0f, 2.0f, 0.2f));
        }

        else if (forceType == Forces.Spring)
        {
            AddForce(ForceGenerator.GenerateForce_spring(position, new Vector2(0, -2), 10.0f, 1.0f));
        }

        else if (forceType == Forces.Lab3Bonus)
        {
            Vector2 gravity = ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass);
            //AddForce(gravity);
            Vector2 Normal   = ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1.0f, 1.0f).normalized);
            Vector2 Friction = ForceGenerator.GenerateForce_friction_kinetic((ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1f, 1f).normalized)), new Vector2(1, 1), 0.5f);
            //AddForce(Normal);
            AddForce(Friction);
            //ApplyTorque(localSpace, -Friction);
            ApplyTorque(localSpace, gravity);
        }
    }
コード例 #8
0
    // Start is called before the first frame update
    void Start()
    {
        position = transform.position;
        rotation = new PhysicsQuaternion(transform.rotation);
        SetMass(startMass);
        SetMomentOfInertia(inertiaBody);
        worldTransformMatrix = new Matrix4x4();
        centerOfMassLocal    = new Vector3(transform.localScale.x / 2f, transform.localScale.y / 2f, transform.localScale.z / 2f);
        centerOfMassGlobal   = transform.position;

        forceOfGravity = ForceGenerator.GenerateForce_gravity(Vector3.up, accelerationGravity, mass);
    }
コード例 #9
0
    // Start is called before the first frame update
    void Start()
    {
        position = transform.position;
        rotation = transform.rotation.eulerAngles.z;
        SetMass(startMass);
        SetMomentOfInertia(inertiaBody);
        centerOfMassLocal  = new Vector2(transform.localScale.x / 2f, transform.localScale.y / 2f);
        centerOfMassGlobal = transform.position;

        forceOfGravity  = ForceGenerator.GenerateForce_gravity(Vector2.up, accelerationGravity, mass);
        normalForceUp   = ForceGenerator.GenerateForce_normal(forceOfGravity, Vector2.up);
        normalForce45   = ForceGenerator.GenerateForce_normal(forceOfGravity, new Vector2(1, 1));
        normalForceLeft = ForceGenerator.GenerateForce_normal(forceOfGravity, new Vector2(1, 0));
    }
コード例 #10
0
ファイル: Particle2D.cs プロジェクト: Connellj99/GamePhysics
 // Update is called once per frame
 void Update()
 {
     applyTorque();
     if (forcetype == forcegen.Gravity)
     {
         AddForce(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up));
     }
     if (forcetype == forcegen.Normal)
     {
         AddForce(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit));
     }
     if (forcetype == forcegen.Sliding)
     {
         AddForce(ForceGenerator.GenerateForce_Sliding(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit)));
     }
     if (forcetype == forcegen.Friction_Static)
     {
         AddForce(ForceGenerator.GenerateForce_Friction_Static(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit), f_opposing, frictionCoefficientStatic));
     }
     if (forcetype == forcegen.Friction_Kinetic)
     {
         AddForce(ForceGenerator.GenerateForce_Friction_Kinetic(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit), velocity, frictionCoefficientKinetic));
     }
     if (forcetype == forcegen.Drag)
     {
         AddForce(ForceGenerator.GenerateForce_Sliding(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit)));
         AddForce(ForceGenerator.GenerateForce_Drag(velocity, fluidVelocity, fluidDensity, objectAreaCrossSection, objectDragCoefficient));
     }
     if (forcetype == forcegen.Spring)
     {
         //AddForce(ForceGenerator.GenerateForce_Spring(position, anchorPosition, springRestingLength,springStiffnessCoefficient));
         AddForce(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up));
         AddForce(ForceGenerator.GenerateForce_Spring(position, -position, springRestingLength, springStiffnessCoefficient));
     }
     if (forcetype == forcegen.None)
     {
     }
     if (forcetype == forcegen.SphereRoll)
     {
         AddForce(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up));
         AddForce(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit));
         AddForce(ForceGenerator.GenerateForce_Friction_Static(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(Mass, kGravity, Vector2.up), surfaceNormalUnit), f_opposing, frictionCoefficientStatic));
     }
 }
コード例 #11
0
ファイル: Particle3D.cs プロジェクト: bradleycham/3DPhysics
    void UpdatePosition()
    {
        if (applyGravity)
        {
            AddForce(ForceGenerator.GenerateForce_gravity(Vector3.up, GRAVITY, mass));
        }
        if (applyDrag)
        {
            AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(), 1.0f, 1.0f, 0.05f));
        }

        if (iskinematic)
        {
            updatePositionKinematic(Time.deltaTime);
        }
        else
        {
            UpdatePositionEulerExplicit(Time.deltaTime);
        }
    }
コード例 #12
0
ファイル: Particle3D.cs プロジェクト: DrabbyPage/GamePhysics
    void UpdateForce()
    {
        // Lab 2 Step 3
        //f_gravity = f = mg = ma
        Vector3 f_gravity = forces.mass * new Vector3(0.0f, -9.8f);

        Vector3 f_normal = ForceGenerator.GenerateForce_Normal(f_gravity, transform.up);

        // AddForce(f_gravity); // works
        if (forces.generateGravity)
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(forces.mass, -9.8f, Vector3.up));
        }

        if (forces.generateNormal)
        {
            AddForce(ForceGenerator.GenerateForce_Normal(f_gravity, forces.surfaceNormal_unit)); // works? more testing (surface normal is 0, 1)
        }
        if (forces.generateSliding)
        {
            AddForce(ForceGenerator.GenerateForce_Sliding(f_gravity, f_normal));  // (surface normal is 0,1)
        }
        if (forces.generateStaticsFriction)
        {
            AddForce(ForceGenerator.GenerateForce_Friction_Static(f_normal, forces.frictionOpposingForce, forces.frictionCoeff_static)); // works (surface normal is 1,1) FOF = (-3,0) FCS = 0.9
        }
        if (forces.generateKineticFriction)
        {
            AddForce(ForceGenerator.GenerateForce_Friction_Kinetic(f_normal, particle3DTransform.velocity, forces.frictionCoeff_kinetic));  // works surface = (1,1) initVel = 15 FCK = 0.3
        }
        if (forces.generateDrag)
        {
            AddForce(ForceGenerator.GenerateForce_Drag(particle3DTransform.velocity, forces.fluidVelocity, forces.fluidDensity, forces.objArea_CrossSection, forces.objDragCoeff));  // not sure if this works ask dan... IV = 1, FV = 1, FD = 1, OACS = 1.5, ODC=1.05
        }
        if (forces.generateSpring && particle3DTransform.position.magnitude != 0)
        {
            AddForce(ForceGenerator.GenerateForce_Spring(particle3DTransform.position, forces.anchorPos, forces.springRestingLength, forces.springStiffnesCoeff)); // pos = 0,100 , AP = 0,0 , SRL = 0.1, SSC = 3 , fricCoKin = 0.15 (turn on gravity and kin fric
        }
        AddForce(forces.basicForce);
    }
コード例 #13
0
        public void Execute(ref Physical physical, [ReadOnly] ref Translation position)
        {
            float3 forceSum = new float3(0, 0, 0);

            for (int j = 0; j < generatorCount; ++j)
            {
                float3         generatorPosition = generatorPositions[j].Position;
                ForceGenerator forceGenerator    = generatorForces[j];

                float3 distance       = position.Value - generatorPosition;
                float  distanceMagSqr = math.lengthsq(distance);
                if (distanceMagSqr < forceGenerator.distance * forceGenerator.distance)
                {
                    // Linear decay over distance
                    float f = 1 - math.sqrt(distanceMagSqr) / forceGenerator.distance;
                    forceSum += math.normalize(distance) * f * forceGenerator.force;
                }
            }

            physical = new Physical {
                Force       = physical.Force + forceSum,
                InverseMass = physical.InverseMass
            };
        }
コード例 #14
0
ファイル: Particle2D.cs プロジェクト: TheMurrMan/Personal-Rep
    // Update is called once per frame
    void FixedUpdate()
    {
        //rb.AddTorque(speed, ForceMode2D.Force);
        // step 3
        //Integrate
        if (isEuler == true)
        {
            UpdateEuler();
        }

        //AddForce(new Vector2(0.0f,2.0f));

        //ApplyTorque();

        //CollisionHull2D.instance.TestCollsionVsCircle(other);

        // SHAPES
        if (Shape == Shapes.NONE)
        {
            // DO NOTHING
        }

        if (Shape == Shapes.CUBE)
        {
            CubeInvInertiaCalc();
        }

        else if (Shape == Shapes.SPHERE)
        {
            SphereInvInertiaCalc();
        }

        else if (Shape == Shapes.CYLINDER)
        {
            CylinderInvInertiaCalc();
        }



        //Update the Accerleration
        UpdateAcceleration();
        //UpdateAngularAcceleration();

        // apply to transform
        transform.position = position;

        // step 4
        // test

        // f_gravity: f = mg
        // Vector2 f_gravity = mass * new Vector2(0.0f, -9.8f);
        Vector2 gravity = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);

        if (Test == Forces.NONE)
        {
            // NOTHING HAPPENS HERE
        }

        if (Test == Forces.NORMAL)
        {
            Vector2 normal = ForceGenerator.GenerateForce_normal(-gravity, force);
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
            AddForce(normal);
        }

        else if (Test == Forces.GRAVITY)
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
        }

        else if (Test == Forces.SLIDE)
        {
            Vector2 sliding_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(45)));
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, sliding_normal));
            AddForce(ForceGenerator.GenerateForce_friction_kinetic(sliding_normal, velocity, friction));
        }

        else if (Test == Forces.DRAG)
        {
            // vel 2,0
            AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(1, 0), 0.5f, 10.0f, 0.1f));
        }

        else if (Test == Forces.SLIDINGANDKINETIC)
        {
            //Vector2 gravity = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
            Vector2 static_friction_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(-45)));
            Vector2 f_opposing             = new Vector2(14.0f, 0.0f);
            AddForce(f_opposing);
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, static_friction_normal));
            AddForce(ForceGenerator.GenerateForce_friction_static(static_friction_normal, force, friction));
        }

        else if (Test == Forces.SLIDINGANDSTATIC)
        {
            // set vel 1,-1
            //Vector2 gravity = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
            Vector2 kinetic_friction_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(-45)));
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, kinetic_friction_normal));
            AddForce(ForceGenerator.GenerateForce_friction_kinetic(kinetic_friction_normal, velocity, friction));
        }

        else if (Test == Forces.SPRING)
        {
            // set vel 0,-8
            Vector2 anchorPosition = GameObject.Find("Anchor").transform.position;
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
            AddForce(ForceGenerator.GenerateForce_spring(transform.position, anchorPosition, 3.0f, 2.0f, velocity));
        }
    }
コード例 #15
0
 public void AttachGlobalForce(ForceGenerator force)
 {
     globalForces.Add(force);
 }
コード例 #16
0
    // Update is called once per frame
    void FixedUpdate()
    {
        //https://www.khanacademy.org/science/ap-physics-1/ap-forces-newtons-laws/friction-ap/v/static-and-kinetic-friction-example
        float dirtWoodStatFricCoeff = .6f, dirtWoodKinFricCoeff = .55f;
        float cubeDragCoeff = 1.05f, airFluidDensity = .001225f;

        if (simulate)
        {
            transform.position = position;
            transform.rotation = Quaternion.Euler(0, 0, rotation);

            switch (updateType)
            {
            case UpdateType.EULER:
            {
                UpdatePositionEulerExplicit(Time.deltaTime);
                UpdateRotationEulerExplicit(Time.deltaTime);
                break;
            }

            case UpdateType.KINEMATIC:
            {
                UpdatePositionKinematic(Time.deltaTime);
                UpdateRotationKinematic(Time.deltaTime);
                break;
            }
            }

            switch (forceType)
            {
            case ForceType.GRAVITY:
            {
                AddForce(forceOfGravity);
                break;
            }

            case ForceType.NORMAL:
            {
                AddForce(forceOfGravity);
                AddForce(normalForceUp);
                break;
            }

            case ForceType.SLIDING:
            {
                AddForce(ForceGenerator.GenerateForce_sliding(forceOfGravity, normalForce45));
                break;
            }

            case ForceType.FRICTION_STATIC:
            {
                AddForce(ForceGenerator.GenerateForce_friction_static(normalForceLeft, forceOfGravity, dirtWoodStatFricCoeff));
                break;
            }

            case ForceType.FRICTION_KINETIC:
            {
                AddForce(ForceGenerator.GenerateForce_friction_kinetic(normalForceUp, velocity, dirtWoodKinFricCoeff));
                break;
            }

            case ForceType.DRAG:
            {
                AddForce(forceOfGravity);
                AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(0, 0), airFluidDensity, 1, cubeDragCoeff));
                break;
            }

            case ForceType.SPRING:
            {
                AddForce(ForceGenerator.GenerateForce_spring(position, new Vector2(0, 100), 5, .2f));
                break;
            }

            case ForceType.TORQUE:
            {
                AddTorque(new Vector2(1, 1), transform.position + new Vector3(.9f, 0, 0), false);
                break;
            }
            }


            UpdateAcceleration();
            UpdateAngularAcceleration();
        }
        else
        {
            position = transform.position;
            rotation = transform.eulerAngles.z;
        }
    }
コード例 #17
0
ファイル: ForceManager.cs プロジェクト: iXiphos/GamePhysics
 public void addForceGenerator(ForceGenerator generator)
 {
     mForceGenerators.Add(generator);
 }
コード例 #18
0
ファイル: Particle2D.cs プロジェクト: D4VID-B/Game_Physics
    void FixedUpdate()
    {
        //Lab 01 & Lab 02 - Step 3
        if (IntegrationMethod == PositionFunction.PositionEuler)
        {
            updatePosEulerExplicit(Time.fixedDeltaTime);
            updateAcceleration();

            transform.position = position;

            //Rotations
            if (RotationUpdateMethod == RotationFunction.RotationEuler)
            {
                updateRotEulerExplicit(Time.fixedDeltaTime);
                updateAngularAcceleration();

                if (rotation != 0)
                {
                    Debug.Log("Rotation: " + rotation);
                    transform.eulerAngles = new Vector3(0f, 0f, rotation);
                }
            }
            else if (RotationUpdateMethod == RotationFunction.RotationKinematic)
            {
                updateRotKinematic(Time.fixedDeltaTime);
                updateAngularAcceleration();

                if (rotation != 0)
                {
                    transform.eulerAngles = new Vector3(0f, 0f, rotation);
                }
            }
        }
        else if (IntegrationMethod == PositionFunction.PositionKinematic)
        {
            updatePosKinematic(Time.fixedDeltaTime);
            updateAcceleration();

            transform.position = position;

            //Rotations
            if (RotationUpdateMethod == RotationFunction.RotationEuler)
            {
                updateRotEulerExplicit(Time.fixedDeltaTime);
                updateAngularAcceleration();

                if (rotation != 0)
                {
                    transform.eulerAngles = new Vector3(0f, 0f, rotation);
                }
            }
            else if (RotationUpdateMethod == RotationFunction.RotationKinematic)
            {
                updateRotKinematic(Time.fixedDeltaTime);
                updateAngularAcceleration();


                if (rotation != 0)
                {
                    transform.eulerAngles = new Vector3(0f, 0f, rotation);
                }
            }
        }


        //we should already know what the MoI specific to the game model is based on what the enum input was
        if (Input.GetKey(KeyCode.F))
        {
            addTorque(t_Position, t_Force);
        }

        if (SHIP_MODE)
        {
            //F_gravity: f = mg
            Vector2 shipGravity       = ForceGenerator.generateForce_Gravity(mass, -1.625f, Vector2.up); //I USED A DIFFERENT GRAVITY, THE MOONS GRAVITY
            Vector2 surfaceNormalUnit = new Vector2(Mathf.Sin(surfaceTransform.eulerAngles.z), Mathf.Cos(surfaceTransform.eulerAngles.z));
            Vector2 normal            = ForceGenerator.GenerateForce_normal(shipGravity, surfaceNormalUnit);

            addForce(shipGravity);

            //addForce(normal);     //find a way to add normal force only when colliding with ground

            //this is shit and is temporary


            //this normal calc aint workin
            float   RotZOBB  = this.transform.eulerAngles.z * Mathf.Deg2Rad;
            Vector2 xNormOBB = new Vector2(Mathf.Cos(RotZOBB), Mathf.Sin(RotZOBB));
            Vector2 yNormOBB = new Vector2(-Mathf.Sin(RotZOBB), Mathf.Cos(RotZOBB));

            Debug.Log("xNorm = " + xNormOBB);
            Debug.Log("yNorm = " + yNormOBB);

            Vector2 elevationForce = yNormOBB * elevationThrust; //new Vector2(0.0f, 20.0f);// * yNormOBB;
            Vector2 lateralForce   = xNormOBB * lateralThrust;   //new Vector2(4.0f, 0.0f);// * xNormOBB;

            //Vector2 elevationForce = new Vector2(yNormOBB.x * elevationThrust, yNormOBB.y);
            //Vector2 lateralForce = new Vector2(xNormOBB.x , xNormOBB.y * lateralThrust);

            //Vector2 elevationForce = new Vector2(0.0f, elevationThrust);// * yNormOBB;
            //Vector2 lateralForce = new Vector2(lateralThrust, 0.0f);// * xNormOBB;

            //Debug.Log("rotation " + rotation);

            //Yaw control (isnt it pitch though? yaw would be on the Y axis which just pointlessly spins it)
            if (Input.GetKey(KeyCode.Q))
            {
                addTorque(t_Position, -t_Force);
            }
            else if (Input.GetKey(KeyCode.E))
            {
                addTorque(t_Position, t_Force);
            }

            //range restrictions
            if (rotation >= 90)
            {
                rotation        = 89;
                angularVelocity = 0;
            }

            if (rotation <= -90)
            {
                rotation        = -89;
                angularVelocity = 0;
            }


            //Elevation control
            if (Input.GetKey(KeyCode.W))
            {
                addForce(elevationForce);
            }
            if (Input.GetKey(KeyCode.S))
            {
                addForce(-elevationForce);
            }

            // Lateral control
            if (Input.GetKey(KeyCode.A))
            {
                addForce(-lateralForce);
            }
            if (Input.GetKey(KeyCode.D))
            {
                addForce(lateralForce);
            }
        }


        //Step 4
        //if(MovementType == UpdateFormula.Ocilate)
        //{
        //    acceleration.x = -3f * Mathf.Sin(Time.fixedTime);
        //}
        //else if(MovementType == UpdateFormula.ConstantVelocity)
        //{
        //    acceleration.x = 0;
        //}
        //else if(MovementType == UpdateFormula.ConstantAcceleration)
        //{
        //    acceleration.x = accelerationValue;
        //}

        //Lab 02 - Step 4
        //F_gravity: f = mg
        //Vector2 gravity = mass * new Vector2(0.0f, -9.871f);
        //addForce(f_gravity);

        Vector2 gravity = ForceGenerator.generateForce_Gravity(mass, -9.871f, Vector2.up);

        //Vector2 surfaceNormalUnit = new Vector2(Mathf.Sin(surfaceTransform.eulerAngles.z), Mathf.Cos(surfaceTransform.eulerAngles.z));
        //Vector2 normal = ForceGenerator.GenerateForce_normal(gravity, surfaceNormalUnit);

        //Vector2 drag = ForceGenerator.GenerateForce_drag(velocity, fluidVelocity, fluidDensity, objectAreaXSection, dragCoefficient);

        if (gravityOn)
        {
            addForce(gravity);
        }

        //addForce(drag);

        //******** Block on a slanted surface ********
        //if(gameObject.name == "SlideCube") // Demostrating Gravity, Normal (As Sliding) and Friction forces
        //{
        //    //addForce(gravity);
        //    //addForce(normal);
        //    Vector2 sliding = ForceGenerator.GenerateForce_sliding(gravity, normal);
        //    addForce(sliding);

        //    //Using Friction (some more help from brother)
        //    //x = Mass * g * sin()cos() y = mass * g * sin()sin()
        //    //Vector2 fOpposing = new Vector2((mass * -9.871f * Mathf.Sin(surfaceTransform.eulerAngles.z) * Mathf.Cos(surfaceTransform.eulerAngles.z)), (mass * -9.871f * Mathf.Sin(surfaceTransform.eulerAngles.z) * Mathf.Sin(surfaceTransform.eulerAngles.z)));
        //    addForce(ForceGenerator.GenerateForce_friction(normal, sliding, velocity, getCoeff_Static(MaterialType_Static), getCoeff_Kinetic(MaterialType_Kinetic)));

        //}



        ////********  Cube on a Spring ********
        //if (gameObject.name == "HangCube") // Demonstrating Spring and Drag forces
        //{
        //    addForce(ForceGenerator.GenerateForce_spring(transform.position, surfaceTransform.position, spring_resting, spring_stiffness));
        //    addForce(ForceGenerator.GenerateForce_drag(velocity, fluidVelocity, fluidDensity, 1, 1.05f)); //Drag coef & object x-section are pre-calculated for a
        //    //Velocity is taken fronm the particle properties and is integrated by the script,
        //    //while fluid density & velocity are public variables that default to Earth air with no wind
        //}
    }
コード例 #19
0
 public void AddGlobalForceGenerator(ForceGenerator forceGenerator)
 {
     globalForceGenerators.Add(forceGenerator);
 }
コード例 #20
0
ファイル: Particle2D.cs プロジェクト: Andy608/GamePhysicsRepo
    private void FixedUpdate()
    {
        switch (positionType)
        {
        case PosIntegrationType.EulerExplicit:
            UpdatePositionEulerExplicit(Time.fixedDeltaTime);
            break;

        default:
            UpdatePositionKinematic(Time.fixedDeltaTime);
            break;
        }

        switch (rotationType)
        {
        case RotIntegrationType.EulerExplicit:
            UpdateRotationEulerExplicit(Time.fixedDeltaTime);
            break;

        default:
            UpdateRotationKinematic(Time.fixedDeltaTime);
            break;
        }

        //lab03
        UpdateAngularAcceleration();
        UpdateAcceleration();

        transform.position = position;

        Vector2 gravitationalForce   = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
        Vector2 normalForce          = ForceGenerator.GenerateForce_Normal(-gravitationalForce, testFloor.transform.up);
        Vector2 slideForce           = ForceGenerator.GenerateForce_Sliding(gravitationalForce, normalForce);
        Vector2 frictionForce        = ForceGenerator.GenerateForce_Friction(normalForce, slideForce, velocity, frictionStatic, frictionKinetic);
        Vector2 dragForce            = ForceGenerator.GenerateForce_Drag(velocity, new Vector2(0.2f, 0.0f), 10.0f, 10.0f, 4.0f);
        Vector2 springForce          = ForceGenerator.GenerateForce_Spring(transform.position, testSpringAnchor.position, springRestingLength, springStrength * springStrength);
        Vector2 springDampForce      = ForceGenerator.GenerateForce_SpringDamping(mass, velocity, springStrength, 5.0f);
        Vector2 springMaxLengthForce = ForceGenerator.GenerateForce_SpringWithMax(transform.position, testSpringAnchor.position, springRestingLength, springStrength * springStrength, maxSpringLength);

        switch (forceType)
        {
        case ForceType.gravity:
            AddForce(gravitationalForce);
            break;

        case ForceType.normal:
            AddForce(normalForce);
            break;

        case ForceType.slide:
            AddForce(slideForce);
            break;

        case ForceType.friction:
            AddForce(slideForce);
            AddForce(frictionForce);
            break;

        case ForceType.drag:
            AddForce(dragForce);
            break;

        case ForceType.spring:
            AddForce(springForce);
            break;

        case ForceType.springDamping:
            AddForce(springForce);
            AddForce(springDampForce);
            AddForce(gravitationalForce);
            break;

        case ForceType.springWithMaxLength:
            AddForce(springMaxLengthForce);
            AddForce(springDampForce);
            AddForce(gravitationalForce);
            break;

        case ForceType.none:
            //Debug.Log("We ain't movin chief.");
            break;

        default:
            AddForce(gravitationalForce);
            break;
        }

        //lab03
        ApplyTorque(pointApplied, forceApplied);

        //clamps rotation to 360
        SetRotation(rotation %= 360.0f);
        rotAcceleration       = angularAccel;
    }
コード例 #21
0
 // Update is called once per frame
 void Update()
 {
     // Add a force to simulate buoyancy
     GetComponent <Particle3D>().AddForce(ForceGenerator.GenerateForce_buoyancy(transform.position, waterHeight, maxDepth, volume, liquidDensity));
 }
コード例 #22
0
ファイル: ForceManager.cs プロジェクト: iXiphos/GamePhysics
 public void removeForceGenerator(ForceGenerator generator)
 {
     mForceGenerators.Remove(generator);
 }
コード例 #23
0
    private void FixedUpdate()
    {
        // Add a frictional force if particle is on an object
        if (collidingGameObject != null)
        {
            if (collidingGameObject.GetComponent <PhysicsMaterialScript>() != null)
            {
                AddForce(ForceGenerator.GenerateForce_friction_kinetic(-velocity, velocity, collidingGameObject.GetComponent <PhysicsMaterialScript>().frictionValue));
            }
        }

        // Check if particle is affected by any outside forces
        if (isUsingGravity)
        {
            if (GetComponent <PlayerScript>() != null)
            {
                RaycastHit hit;
                if (!Physics.Raycast(transform.position, Vector3.down, out hit, 1))
                {
                    AddForce(ForceGenerator.GenerateForce_Gravity3d(mass, gravitationalConstant, Vector3.up));
                }
            }
            else
            {
                if (collidingGameObject != null)
                {
                    AddForce(-collidingGameObject.transform.forward);
                }
                else
                {
                    AddForce(ForceGenerator.GenerateForce_Gravity3d(mass, gravitationalConstant, Vector3.up));
                }
            }
        }
        if (enabledByDragForce)
        {
            AddForce(ForceGenerator.GenerateForce_drag(velocity, fluidVelocity, fluidDensity, objCrossSection, dragCoefficient));
        }
        if (isAnchoredSpring)
        {
            for (int i = 0; i < anchors.Length; i++)
            {
                AddForce(ForceGenerator.GenerateForce_spring(position, anchors[i].transform.position, springRestingLength, springCoefficient));
            }
        }

        // Set the transformation matrices
        transformMatrix    = Matrix4x4.TRS(transform.position, rotation, new Vector3(1, 1, 1));
        invTransformMatrix = transformMatrix.inverse;

        // Change position and rotation to the positional and rotational variables
        if (!float.IsNaN(position.x))
        {
            transform.position = position;
        }
        transform.rotation = rotation;

        // Update postion and velocity

        // Should the program update rotation using the kinematic formula?
        if (isUsingKinematicFormula)
        {
            // If yes, then do so
            updateRotationKinematic(Time.fixedDeltaTime);
            updatePositionKinematic(Time.fixedDeltaTime);
        }
        else
        {
            // If no, then use the Euler Explicit formula
            updateRotationEulerExplicit(Time.fixedDeltaTime);
            updatePositionEulerExplicit(Time.deltaTime);
        }

        // Update accelerations
        UpdateAcceleration();
        UpdateAngularAcceleration();
    }
コード例 #24
0
 //functions
 public static void AddForceGenerator(ForceGenerator generator)
 {
     mGenerators.Add(generator);
 }
コード例 #25
0
ファイル: Particle2D.cs プロジェクト: Bcoop098/bencChamplain
    // Update is called once per frame
    void FixedUpdate()
    {
        if (forceType == Forces.Gravity)
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass));
        }

        else if (forceType == Forces.NormalForce)
        {
            AddForce(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(5.0f, 0.0f).normalized));
        }

        else if (forceType == Forces.SlidingForce)
        {
            AddForce(ForceGenerator.GenerateForce_Sliding(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1f, 1f).normalized)));
        }

        else if (forceType == Forces.StaticFriction)
        {
            Vector2 Normal = ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1.0f, 1.0f).normalized);
            AddForce(ForceGenerator.GenerateForce_friction_static(Normal, new Vector2(5, 5), 0.2f));
        }

        else if (forceType == Forces.KinematicFriction)
        {
            AddForce(ForceGenerator.GenerateForce_friction_kinetic((ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(-10.0f, Vector2.up, mass), new Vector2(1f, 1f).normalized)), new Vector2(1, 1), 0.5f));
        }

        else if (forceType == Forces.Drag)
        {
            AddForce(ForceGenerator.GenerateForce_drag(new Vector2(1, 1), new Vector2(0.5f, 1), 1.0f, 2.0f, 0.2f));
        }

        else if (forceType == Forces.Spring)
        {
            AddForce(ForceGenerator.GenerateForce_spring(position, new Vector2(0, -2), 10.0f, 1.0f));
        }

        //if Euler is selected in the drop down menu, do this
        if (calculationType == Physics.Euler)
        {
            UpdatePositionEulerExplicit(Time.fixedDeltaTime);
            transform.position = position;                         //set the new position
            UpdateRotationEulerExplicit(Time.fixedDeltaTime);
            transform.eulerAngles = new Vector3(0f, 0f, rotation); //set the new rotation
            //move cubes back and forth
            //acceleration.x = -Mathf.Sin(Time.time - startTime);
        }

        //if Kinematic is selected, do this
        else if (calculationType == Physics.Kinematic)
        {
            UpdatePositionKinematic(Time.fixedDeltaTime);
            transform.position = position;                         //set the new position
            UpdateRotationKinematic(Time.fixedDeltaTime);
            transform.eulerAngles = new Vector3(0f, 0f, rotation); //set the new rotation
            //move the cubes in a circle
            //acceleration.x = Mathf.Sin(Time.time - startTime);
            //acceleration.y = Mathf.Cos(Time.time - startTime);
        }

        UpdateAcceleration();
    }
コード例 #26
0
 void Start()
 {
     this.forceGenerator = this.GetComponentInChildren <ForceGenerator> ();
     this.magneticObject = this.GetComponent <MagneticObject> ();
 }
コード例 #27
0
    // This function checks for all input values and does all appropriate calculations
    void CheckForInput()
    {
        RaycastHit hit;

        inputAmountY = Input.GetAxis("Xbox_LeftStick_X");
        inputAmountX = -Input.GetAxis("Xbox_LeftStick_Y");

        // Create sprint amount
        float sprintAmount = isGrounded ? sprintAmount = 1 + Input.GetAxis("Xbox_RT") : sprintAmount = 1;

        // Set all animations
        animator.SetFloat("Forward", Mathf.Clamp(Mathf.Abs(inputAmountX) + Mathf.Abs(inputAmountY), 0, 1), 0.1f, Time.deltaTime);
        animator.SetBool("isGrounded", isGrounded || (!isAttemptingToJump && (GetComponent <Particle3D>().collidingGameObject != null && !canWallJump)));
        animator.SetBool("isCrouching", isTriggerDown);
        animator.SetInteger("JumpIndex", strongerJumpKey);
        animator.SetFloat("Velocity", GetComponent <Particle3D>().velocity.y);
        animator.SetBool("CanWallJump", canWallJump);
        animator.SetBool("isSliding", isSliding);
        animator.SetBool("isPickingup", isPickingUp);

        if (isSliding)
        {
            GetComponent <Particle3D>().AddForce(ForceGenerator.GenerateForce_sliding(new Vector3(0, GetComponent <Particle3D>().gravitationalConstant, 0), GetComponent <Particle3D>().collidingGameObject.transform.right *slidingForce));
        }

        // Move if nothing is in the way of the player
        if (!Physics.Raycast(playerMovementTransform.position, new Vector3(inputAmountX, 0, inputAmountY).normalized, out hit, movementCheckRaycatHit) && !isTriggerDown && !isProne && !isDead)
        {
            if (!isSliding)
            {
                // Is the player grounded?
                if (isGrounded)
                {
                    // If yes, then move the player in the direction given by controller input
                    GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *movementSpeed *sprintAmount *cameraGameObject.transform.TransformDirection(new Vector3(-inputAmountY, 0, -inputAmountX).normalized));
                }
                else
                {
                    // If no, then move the player in the direction given by controller input but directed by a jump speed
                    GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpMovementSpeed *cameraGameObject.transform.TransformDirection(new Vector3(-inputAmountY, 0, -inputAmountX).normalized));
                }

                // Has the player put in any sort of input?
                if (inputAmountX != 0 || inputAmountY != 0)
                {
                    // If yes, then rotate the object appropriatly
                    transform.GetChild(0).localEulerAngles = new Vector3(0, (Mathf.Atan2(inputAmountX, -inputAmountY) * (180 / Mathf.PI)) + (cameraGameObject.transform.localEulerAngles.y + 90), 0);
                }
            }
            else
            {
                // If yes, then move the player in the direction given by controller input
                GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *movementSpeed *sprintAmount *GetComponent <Particle3D>().collidingGameObject.transform.TransformDirection(new Vector3(0, 0, inputAmountY).normalized));
                transform.GetChild(0).localEulerAngles = new Vector3(0, (Mathf.Atan2(-GetComponent <Particle3D>().collidingGameObject.transform.right.x, -GetComponent <Particle3D>().collidingGameObject.transform.right.z) * (180 / Mathf.PI)) + (cameraGameObject.transform.localEulerAngles.y + 90), 0);
            }

            GetComponent <Particle3D>().isAttemptingToMove = true;
        }

        if (carryingObject == null)
        {
            isPickingUp = false;
        }

        // Is the player pressing the punch button and are they allowed to punch?
        if (Input.GetButtonDown("Xbox_B") && canPunch && !isProne && !isDead)
        {
            animator.SetBool("isPunching", true);

            // If yes, is the player carrying anything?
            if (carryingObject != null)
            {
                // If yes, then throw whatever the player is carrying
                carryingObject.GetComponent <Particle3D>().AddForce(carryingObject.GetComponent <Particle3D>().mass *transform.GetChild(0).forward *throwingOffset);
                carryingObject.GetComponent <Particle3D>().AddForce(carryingObject.GetComponent <Particle3D>().mass *transform.up *throwingOffset);
                carryingObject = null;
            }
            // Is there something in front of the player to punch at?
            if (Physics.Raycast(playerTorsoTransform.position, transform.GetChild(0).transform.forward, out hit, punchDistance))
            {
                // If yes, is it a king Bob omb?
                if (hit.collider.gameObject.tag == "King Bobomb")
                {
                    // If yes, then pick it up
                    carryingObject = hit.collider.gameObject;
                    hit.collider.gameObject.GetComponent <KingBobomb>().SetIsProne(true);
                    isPickingUp = true;
                }

                // If yes, is it a destroyable object?
                if (hit.collider.gameObject.tag == "Destroyable")
                {
                    // If yes, then destroy that object
                    Destroy(hit.collider.gameObject);
                }
                if (hit.collider.gameObject.tag == "Goomba")
                {
                    Destroy(hit.collider.gameObject);
                }
            }

            // Start the punch cooldown afterwards
            StartCoroutine(StartPunchCooldown());
        }


        // Is the player pressing the left trigger down?
        if (Input.GetAxis("Xbox_LT") > 0 && !isProne && !isDead)
        {
            if (!isTriggerDown && !isGrounded)
            {
                // If yes, then perform a ground pound
                animator.SetTrigger("GroundPounding");
                isGroundPounding = true;
            }
            else if (isGrounded && canLongJump && Input.GetButtonDown("Xbox_A") && (Mathf.Abs(inputAmountX) > 0 || Mathf.Abs(inputAmountY) > 0))
            {
                // If yes, is the A button down as well and is the player grounded?
                // If yes, then perform a long jump
                animator.SetTrigger("LongJumping");
                GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *longjumpUpwardforce *Vector3.up *strongJumpMaxIndex);
                GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *longjumpHorizontalforce *transform.GetChild(0).forward);
                GetComponent <Particle3D>().position.y += jumpingOffset;

                isAttemptingToJump = true;
                airTriggeredByJump = true;
                canDoStrongerJump  = false;
            }
            else if (Input.GetButtonDown("Xbox_A") && inputAmountX == 0 && inputAmountY == 0)
            {
                // If no, then perform a backflip
                GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *Vector3.up *(strongJumpMaxIndex + 1));
                GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *backFlipBackForce * -transform.GetChild(0).forward);
                GetComponent <Particle3D>().position.y += jumpingOffset;

                animator.SetTrigger("Backflipping");

                isAttemptingToJump = true;
                airTriggeredByJump = true;
                canDoStrongerJump  = false;
            }
            if (!isTriggerDown)
            {
                StartCoroutine(LongjumpWindow());
                isTriggerDown = true;
            }
        }
        else
        {
            isTriggerDown = false;
            // Is the A button down?
            if (Input.GetButtonDown("Xbox_A") && !isProne && !isDead)
            {
                // If yes, are they grounded?
                if (isGrounded)
                {
                    // If yes, then check if they have done any sudden movements and then opposite movements and then perform a backward sommersault if applicable
                    if (inputAmountX < 0)
                    {
                        if (inputAmountX < lastDirectionX && lastDirectionX > 0)
                        {
                            animator.SetTrigger("SideJumping");
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *Vector3.up *strongJumpMaxIndex);
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *transform.GetChild(0).forward);
                            GetComponent <Particle3D>().position.y += jumpingOffset;

                            isAttemptingToJump = true;
                            airTriggeredByJump = true;
                            canDoStrongerJump  = false;
                        }
                    }
                    if (inputAmountX > 0)
                    {
                        if (inputAmountX > lastDirectionX && lastDirectionX < 0)
                        {
                            animator.SetTrigger("SideJumping");
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *Vector3.up *strongJumpMaxIndex);
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *transform.GetChild(0).forward);
                            GetComponent <Particle3D>().position.y += jumpingOffset;

                            isAttemptingToJump = true;
                            airTriggeredByJump = true;
                            canDoStrongerJump  = false;
                        }
                    }
                    if (inputAmountY > 0)
                    {
                        if (inputAmountY > lastDirectionY && lastDirectionY < 0)
                        {
                            animator.SetTrigger("SideJumping");
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *Vector3.up *strongJumpMaxIndex);
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *transform.GetChild(0).forward);
                            GetComponent <Particle3D>().position.y += jumpingOffset;

                            isAttemptingToJump = true;
                            airTriggeredByJump = true;
                            canDoStrongerJump  = false;
                        }
                    }
                    if (inputAmountY < 0)
                    {
                        if (inputAmountY < lastDirectionY && lastDirectionY > 0)
                        {
                            animator.SetTrigger("SideJumping");
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *Vector3.up *strongJumpMaxIndex);
                            GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *jumpForce *transform.GetChild(0).forward);
                            GetComponent <Particle3D>().position.y += jumpingOffset;

                            isAttemptingToJump = true;
                            airTriggeredByJump = true;
                            canDoStrongerJump  = false;
                        }
                    }

                    // If no to all, can they do a stronger jump?
                    if (canDoStrongerJump && strongerJumpKey < strongJumpMaxIndex)
                    {
                        // If yes, then perform a stronger jump
                        strongerJumpKey++;
                        GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *Vector3.up *(strongerJumpKey + 1) * jumpForce);
                        GetComponent <Particle3D>().position.y += jumpingOffset;
                    }
                    else
                    {
                        // If no, then perform just a regular jump
                        strongerJumpKey = 0;
                        GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *Vector3.up *jumpForce);
                        GetComponent <Particle3D>().position.y += jumpingOffset;
                    }
                    isAttemptingToJump = true;
                    airTriggeredByJump = true;
                    canDoStrongerJump  = false;
                }
                // If all else fails, can the player do a wall jump?
                else if (canWallJump)
                {
                    // If yes, then calculate the normal between the two points and jump accordingly
                    Vector3 normal = transform.position - collidingPoint;
                    GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *wallJumpForce *new Vector3(normal.normalized.x, 0, normal.normalized.z) * strongJumpMaxIndex);
                    GetComponent <Particle3D>().AddForce(GetComponent <Particle3D>().Mass *Vector3.up *jumpForce *(strongJumpMaxIndex + 1));
                    transform.GetChild(0).transform.rotation = Quaternion.Euler(transform.GetChild(0).transform.eulerAngles.x, transform.GetChild(0).transform.eulerAngles.y + 180, transform.GetChild(0).transform.eulerAngles.z);
                    animator.SetTrigger("WallJumping");
                }
            }
        }
    }
コード例 #28
0
ファイル: Particle2D.cs プロジェクト: Bcoop098/bencChamplain
    // Update is called once per frame
    void FixedUpdate()
    {
        if (isPlayer) // only runs for the player
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(-0.5f, transform.up, mass));

            /*if (Input.GetAxis("Vertical") < 0) //down
             * {
             *  AddForce(ForceGenerator.GenerateForce_Gravity(-2.0f, transform.up, mass));
             *  //AddForce(ForceGenerator.GenerateForce_Normal(new Vector2(0, 0.5f), new Vector2(0.0f, -1.0f).normalized));
             * }
             * else if (Input.GetAxis("Vertical") > 0) //up
             * {
             *  AddForce(ForceGenerator.GenerateForce_Gravity(2.0f, transform.up, mass));
             *  //AddForce(ForceGenerator.GenerateForce_Normal(new Vector2(0, 0.5f), new Vector2(0.0f, 1.0f).normalized));
             * }
             * else if (Input.GetAxis("Horizontal") > 0) //right
             * {
             *  ApplyTorque(transform.position, ForceGenerator.GenerateForce_Gravity(2.0f, Vector2.up, mass));
             *
             *  //ApplyTorque(new Vector2(transform.position.x - 0.25f, transform.position.y), ForceGenerator.GenerateForce_Gravity(2.0f, Vector2.up, mass));
             * }
             * else if (Input.GetAxis("Horizontal") < 0) //left
             * {
             *  ApplyTorque(transform.position, ForceGenerator.GenerateForce_Gravity(-2.0f, Vector2.up, mass));
             *  //ApplyTorque(new Vector2(transform.position.x + 0.25f,transform.position.y), ForceGenerator.GenerateForce_Gravity(-2.0f, Vector2.up, mass));
             * }*/
            if (transform.eulerAngles.z >= 90.0f && transform.eulerAngles.z < 180f && !(Input.GetAxis("Horizontal") > 0.0f))
            {
                transform.eulerAngles = new Vector3(0f, 0f, 90f);
                StopTorque();
            }
            if (transform.eulerAngles.z <= 270.0f && transform.eulerAngles.z > 180f && !(Input.GetAxis("Horizontal") < 0.0f))
            {
                transform.eulerAngles = new Vector3(0f, 0f, 270f);
                StopTorque();
            }
            if (Mathf.Abs(Input.GetAxis("Vertical")) > 0 && fuel > 0)
            {
                AddForce(ForceGenerator.GenerateForce_Gravity(2.0f, transform.up * Input.GetAxis("Vertical"), mass));
                --fuel;
            }
            if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
            {
                ApplyTorque(transform.position, ForceGenerator.GenerateForce_Gravity(2.0f, (new Vector2(0, 1) * Input.GetAxis("Horizontal")) * -1, mass));
            }
            else
            {
                StopTorque();
            }
        }


        //if Euler is selected in the drop down menu, do this
        if (calculationType == Physics.Euler)
        {
            UpdatePositionEulerExplicit(Time.fixedDeltaTime);
            transform.position = position;                         //set the new position
            UpdateRotationEulerExplicit(Time.fixedDeltaTime);
            transform.eulerAngles = new Vector3(0f, 0f, rotation); //set the new rotation
            //move cubes back and forth
            //acceleration.x = -Mathf.Sin(Time.time - startTime);
        }

        //if Kinematic is selected, do this
        else if (calculationType == Physics.Kinematic)
        {
            UpdatePositionKinematic(Time.fixedDeltaTime);
            transform.position = position;                         //set the new position
            UpdateRotationKinematic(Time.fixedDeltaTime);
            transform.eulerAngles = new Vector3(0f, 0f, rotation); //set the new rotation
            //move the cubes in a circle
            //acceleration.x = Mathf.Sin(Time.time - startTime);
            //acceleration.y = Mathf.Cos(Time.time - startTime);
        }
        UpdateAngularAcceleration();
        UpdateAcceleration();
    }
コード例 #29
0
 public void AddGlobalForceGenerator(ForceGenerator forceGenerator)
 {
     globalForceGenerators.Add(forceGenerator);
 }
コード例 #30
0
ファイル: Particle2D.cs プロジェクト: dacuster/GamePhysics
    /*
     *  Lab 1
     *  Integrate user friendly menu.
     */
    // Get selectable items from the inspector menu.
    private void GetInspectorItems()
    {
        /*
         *  Lab 1 Step 3
         */

        // Integrate.
        if (positionType == PositionType.Euler)
        {
            UpdatePositionEulerExplicit(Time.fixedDeltaTime);
        }
        else if (positionType == PositionType.Kinematic)
        {
            UpdatePositionKinematic(Time.fixedDeltaTime);
        }

        if (rotationType == RotationType.Euler)
        {
            UpdateRotationEulerExplicit(Time.fixedDeltaTime);
        }
        else if (rotationType == RotationType.Kinematic)
        {
            UpdateRotationKinematic(Time.fixedDeltaTime);
        }

        if (gravityActive)
        {
            // Gravity force.
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, GRAVITY, Vector2.up));
        }

        if (springActive)
        {
            // Spring force.
            AddForce(ForceGenerator.GenerateForce_Spring(springAnchor.position, transform.position, springRestLength, springStiffness));
        }

        if (staticFrictionActive)
        {
            // Static friction force.
            AddForce(ForceGenerator.GenerateForce_Friction_Static(staticFrictionNormal, staticFrictionOpposingForce, staticFrictionCoefficient));
        }

        if (kinematicFrictionActive)
        {
            // Kinematic friction force.
            AddForce(ForceGenerator.GenerateForce_Friction_Kinetic(kinematicNormalForce, velocity, kinematicFrictionCoefficient));
        }

        if (slidingActive)
        {
            // Sliding force.
            AddForce(ForceGenerator.GenerateForce_Sliding(ForceGenerator.GenerateForce_Gravity(mass, GRAVITY, worldUp), slidingNormalForce));
        }

        if (dragActive)
        {
            // Drag force.
            AddForce(ForceGenerator.GenerateForce_Drag(velocity, fluidVelocity, fluidDensity, objectCrossSection, dragCoefficient));
        }

        if (normalActive)
        {
            // Normal force.
            AddForce(ForceGenerator.GenerateForce_Normal(ForceGenerator.GenerateForce_Gravity(mass, GRAVITY, worldUp), surfaceNormal));
        }

        if (dampingSpringActive)
        {
            // Damping spring force.
            AddForce(ForceGenerator.GenerateForce_Spring_Damping(position, springAnchor.position, springRestLength, springStiffness, springDamping, springConstant, velocity));
        }

        return;
    }
コード例 #31
0
 public void AttachGlobalForce(ForceGenerator force)
 {
     globalForces.Add(force);
 }
コード例 #32
0
ファイル: Particle2D.cs プロジェクト: TheMurrMan/Personal-Rep
    // Update is called once per frame
    void FixedUpdate()
    {
        // step 3
        //Integrate
        if (isEuler == true)
        {
            UpdateEuler();
        }

        // Update the Accerleration
        UpdateAcceleration();

        // apply to transform
        transform.position = position;


        // step 4
        // test

        // f_gravity: f = mg
        // Vector2 f_gravity = mass * new Vector2(0.0f, -9.8f);
        Vector2 gravity        = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
        Vector2 anchorPosition = GameObject.Find("Anchor").transform.position;

        if (Test == Forces.NORMAL)
        {
            Vector2 normal = ForceGenerator.GenerateForce_normal(-gravity, force);
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
            AddForce(normal);
        }


        else if (Test == Forces.GRAVITY)
        {
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
        }

        else if (Test == Forces.SLIDE)
        {
            Vector2 sliding_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(45)));
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, sliding_normal));
            AddForce(ForceGenerator.GenerateForce_friction_kinetic(sliding_normal, velocity, friction));
        }

        else if (Test == Forces.DRAG)
        {
            // vel 2,0
            AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(1, 0), 0.5f, 10.0f, 0.1f));
        }

        else if (Test == Forces.SLIDINGANDKINETIC)
        {
            //Vector2 gravity = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
            Vector2 static_friction_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(-45)));
            Vector2 f_opposing             = new Vector2(14.0f, 0.0f);
            AddForce(f_opposing);
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, static_friction_normal));
            AddForce(ForceGenerator.GenerateForce_friction_static(static_friction_normal, force, friction));
        }

        else if (Test == Forces.SLIDINGANDSTATIC)
        {
            // set vel 1,-1
            //Vector2 gravity = ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up);
            Vector2 kinetic_friction_normal = ForceGenerator.GenerateForce_normal(gravity, new Vector2(Mathf.Sin(45), Mathf.Sin(-45)));
            AddForce(ForceGenerator.GenerateForce_sliding(gravity, kinetic_friction_normal));
            AddForce(ForceGenerator.GenerateForce_friction_kinetic(kinetic_friction_normal, velocity, friction));
        }

        else if (Test == Forces.SPRING)
        {
            // set vel 0,-8
            AddForce(ForceGenerator.GenerateForce_Gravity(mass, -9.8f, Vector2.up));
            AddForce(ForceGenerator.GenerateForce_spring(transform.position, anchorPosition, 3.0f, 2.0f, velocity));
        }
    }