Example #1
0
        public override void Update(float dt)
        {
            // check if we're moving outside the initial position or final positions
            if (this.Position.X < initialPosition.X || this.Position.Y < initialPosition.Y)
            {
                movingTowardFinal = true;
            }
            else if (this.Position.X > finalPosition.X || this.Position.Y > finalPosition.Y)
            {
                movingTowardFinal = false;
            }

            velocity = maxVelocity;

            // calculate the distance
            if (GameConstants.DistanceBetweenTwoPoints(this.Position, initialPosition) < nearTargetPositionDistance)
            {
                distanceToTargetPosition = GameConstants.DistanceBetweenTwoPoints(this.Position, initialPosition);
            }
            else if (GameConstants.DistanceBetweenTwoPoints(this.Position, finalPosition) < nearTargetPositionDistance)
            {
                distanceToTargetPosition = GameConstants.DistanceBetweenTwoPoints(this.Position, finalPosition);
            }
            else
            {
                distanceToTargetPosition = 0;
            }

            if (distanceToTargetPosition > 0)
            {
                velocity *= (float)Math.Sqrt(distanceToTargetPosition / nearTargetPositionDistance);
            }

            if (!movingTowardFinal)
            {
                velocity = -velocity;
            }

            physicsLine.body.LinearVelocity = velocity;
        }
Example #2
0
        public override void Update(float dt)
        {
            // first, get the centre point of the circle
            Vector2 centrePoint = (finalPosition + initialPosition) * 0.5f;

            float dx = centrePoint.X - this.Position.X;
            float dy = centrePoint.Y - this.Position.Y;

            // get the normal to the current point on the circle
            Vector2 normal;

            if (clockwise)
            {
                normal = new Vector2(dy, -dx);
            }
            else
            {
                normal = new Vector2(-dy, dx);
            }

            // normalize then multiply by the maxVelocity
            float normalLength = (float)Math.Sqrt(normal.X * normal.X + normal.Y * normal.Y);

            normal /= normalLength;
            normal *= maxSpeed;

            float radius = GameConstants.DistanceBetweenTwoPoints(initialPosition, centrePoint);

            Vector2 toCentrePoint = new Vector2(centrePoint.X - this.Position.X, centrePoint.Y - this.Position.Y);

            if (GameConstants.DistanceBetweenTwoPoints(this.Position, centrePoint) > radius)
            {
                physicsLine.body.LinearVelocity = (normal + toCentrePoint) * 0.5f;
            }
            else
            {
                physicsLine.body.LinearVelocity = normal;
            }
        }