Beispiel #1
0
        public virtual void Update(double total_seconds_elapsed, CollisionGrid grid)
        {
            if (total_seconds_elapsed == 0.0)
                return;
            Vector2 new_velocity = _velocity;
            new_velocity += (_accelleration * (float)total_seconds_elapsed);
            new_velocity.Y += _gravitationalAccelleration * (float)total_seconds_elapsed;
            new_velocity = Vector2.Clamp(new_velocity, Vector2.Negate(_maxVelocity), _maxVelocity);

            Vector2 distance_to_travel = (new_velocity * (float)total_seconds_elapsed) + _positionCarryOver;
            Vector2 distance_traveled = grid.CheckCollision(BoundingBox, distance_to_travel, _fallThrough);

            //Check to see if we moved as expected, if not we hit a block, so set the new velocity to 0.
            if (distance_traveled.X != distance_to_travel.X)
                new_velocity.X = 0;
            if (distance_traveled.Y != distance_to_travel.Y)
            {
                new_velocity.Y = 0;
            }

            Velocity = new_velocity;

            int original_y_position = _boundingBox.Y;

            AddToPositionAndCarry(distance_traveled);

            //If we were stopped from traveling downward, set the isAirborn flag to false
            if (distance_to_travel.Y > distance_traveled.Y)
            {
                _isAirborn = false;
            }
            //Otherwise if we moved in the Y direction, set us as airborn
            else if (original_y_position != _boundingBox.Y)
            {
                _isAirborn = true;
            }
        }