Physics Properties
Top  Previous  Next


Description
Shows how various gaming functions can be simplified by using Physics Properties.

Aim
Introduce the concepts of velocity, acceleration and friction.
Introduce the available SWiSH specific Physics Properties and demonstrate how they can simplify such scripting.

.swi file
"game1.swi"

Step 1
Continue from the previous tutorial or open the file "game.swi"
Save the file as "mygame2.swi"

Step 2
Enable Physics properties for the movie. See here for instructions.
The bat, ball and the ball instance objects. See here for detailed instructions.

Step 3
Modify the bat object's onEnterFrame() Event so that it calculates velocity based on pixels per second (as opposed to pixels per frame as it currently does). All physics properties are based on per second values.

onEnterFrame() {
   // Calculate and save the distance moved since the last frame. This is the velocity.
   // The _delta property is used to convert these values to pixels per second. 
   dx = (_X - x0) / _delta;
   dy = (_Y - y0) / _delta;
   // Save the current position for calculation in the next frame
   x0 = _X;
   y0 = _Y;
}


The physics property _delta represents the time period since the last frame. Dividing by this value converts the dx, dy velocities from pixel per frame into pixel per second velocities.
Note that the Physics properties must be enabled for the bat object to allow access to the _delta property.

Step 4
Modify the "ball" Sprite's onEnterFrame() Event so that it uses the physics properties _vX and _vY instead of dx and dy from the previous tutorial.

onEnterFrame() {
   // Move ball to new position (This is now done automatically by _vX and _vY properties).
   //
   // Check for boundary collision. If collision detected, reverse velocity
   if (_X < MinX) {
      // Force _vX to be positive.
      _vX = Math.abs(_vX);
   }

   if (_X > MaxX) {
      // Force _vX to be negative.
      _vX = -Math.abs(_vX);
   }

   if (_Y < MinY) {
      // Force _vY to be positive.
      _vY = Math.abs(_vY);
   }

   if (_Y > MaxY) {
      // Force _vY to be negative.
      _vY = -Math.abs(_vX);
   }

   //

   // Check for collision with bat. 

   if (_parent.bat.isNearThis(batradius)) {
      // We have collided with bat, change ball velocity.
      _vX = CalcNewDx();
      _vY = CalcNewDy();
   }

}


Note that the lines

   // Move ball to new position
   _X += dx;
   _Y += dy;


Have now been removed. The dx, dy lines can also be removed from the ball.onLoad() event handling function as they are no longer used.

Step 5
Test the game.
Select the layout tab and press play. The game should behave in a similar fashion to the previous example.

Step 6
Press the stop button.


Analysis
In this tutorial, physics properties were introduced. These are SWiSHscript-specific. The properties _vX and _vY were used for Velocity, and _delta was used for measuring time. These properties, as used in this tutorial, determine how fast the ball moves - and in which direction. It is important that Physics Support be enabled for both the exported Script and the Selected Object in order for these scripts to work properly.