Example #1
0
        public void Update(float dt, PhysicsManager physManager, PhysObject po)
        {
            // Apply friction if the body is not in the air
            if (!po.IsInTheAir && AffectedByPhysics)
            {
                ApplyFriction(physManager, po);
            }

            Vector2 acceleration = Force * (1 / Mass);

            if (IgnoreGravity || !AffectedByPhysics)
            {
                LinearVelocity += (acceleration * dt);
            }
            else
            {
                LinearVelocity += (physManager.Gravity + acceleration) * dt;
            }

            // If projectile, update it
            if (po is Projectile)
            {
                Projectile projectile = (Projectile)po;

                projectile.Update();
            }

            // If pendulum, update it
            if (po is Pendulum)
            {
                Pendulum pendulum = (Pendulum)po;

                pendulum.Update(physManager, dt);
            }

            // If moving platform, update it
            if (po is MovingPlatform)
            {
                MovingPlatform movingPlatform = (MovingPlatform)po;

                movingPlatform.Update();
            }

            // Update the time since last collision
            // Don't care when moving platform or pendulum last collided
            if (!(po is MovingPlatform) && !(po is Pendulum))
            {
                po.TimeSinceLastCollisionWithFloor++;
                po.TimeSinceLastCollision++;

                // Determine if the body is in the air or not
                if (po.TimeSinceLastCollisionWithFloor < 50 && AffectedByPhysics)
                {
                    po.IsInTheAir = false;
                }
                else
                {
                    po.IsInTheAir = true;
                }
            }

            // Ensure things aren't moving too fast
            MaximumVelocityCheck(physManager, po);

            if ((Math.Abs(LinearVelocity.X) > 0 || Math.Abs(LinearVelocity.Y) > 0) && AffectedByPhysics)
            {
                // If there's a body on top of the current rigid body
                for (int i = 0; i < po.BodiesOnTop.Count; i++)
                {
                    // Check if the body is still on top
                    if (po.BodiesOnTop[i].TimeSinceLastCollision < 50)
                    {
                        // If this is a moving platform, move the object on top with this body
                        if (po is MovingPlatform)
                        {
                            po.BodiesOnTop[i].RigidBody.Position += (LinearVelocity * dt);

                            // If there is a stack of bodies on top of the moving platform
                            for (int j = 0; j < po.BodiesOnTop[i].BodiesOnTop.Count; j++)
                            {
                                if (!po.BodiesOnTop.Contains(po.BodiesOnTop[i].BodiesOnTop[j]))
                                {
                                    // Add each unique body to the list of bodies on the platform
                                    po.BodiesOnTop.Add(po.BodiesOnTop[i].BodiesOnTop[j]);
                                }
                            }
                        }
                    }
                    else
                    {
                        po.BodiesOnTop.Remove(po.BodiesOnTop[i]);
                    }
                }
            }

            // Reset the force
            Force = new Vector2(0);

            Position += (LinearVelocity * dt);
        }
Example #2
0
 public void addMovingPlatform(MovingPlatform newPlat)
 {
     movingPlatforms.Add(newPlat);
     dynamicPhysicsObjects.Add(newPlat.platform);
 }