Thank you for visiting Blazing Games

Newton's Laws

You may remember from physics classes that Newton had created his famous 3 laws to explain motion. The first law says that once an object starts moving it wants to keep moving in that direction unless another force acts upon that object. On Earth, we have both gravity and the friction of the air/ground to decelerate a moving object. There is no air in space. In deep space, while there is still some effect of gravity, the objects causing the gravitational pull are usually too far away to have a large influence.

In physics, motion is represented as a vector. A vector has both size and direction. Adding two vectors together results in a new vector that combines the first two. For example, if you added a vector moving north 10 units and another moving east 10 units the resulting vector is moving north-east 10 units.

When coding the game, instead of using vectors two scalar variables are used. One for the speed along the x axis and one for the speed along the y axis. When the player accelerates, the two speed variables are modified based on the current direction that the player is facing. To keep the game playable, speed limits are applied.

        public function accelerate(delta:Number)
        {
            var rad:Number = rotation * Math.PI/180;

            _xspeed = Math.max(-MAX_SPEED,
                    Math.min(MAX_SPEED,
                    _xspeed + delta * ACCELERATION * Math.sin(rad)));
            _yspeed = Math.max(-MAX_SPEED,
                    Math.min(MAX_SPEED,
                    _yspeed + delta * ACCELERATION * -Math.cos(rad)));
            trace ("speed: " + _xspeed + "," + _yspeed);
        }

Previous page
Chapter 35 Page 4

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2009 Blazing Games Inc. All Rights Reserved