Torque 2D/GenreTutorials/PlatformerPhysics
From TDN
[edit] Introduction
[edit] Create a new Project
[edit] Physics and Collision in the Context of a Platformer
[edit] Script SetupOne of the most important practices is to start your projects with a good organization plan and maintain that throughout. Files can easily become unwieldy if you just start adding code randomly. Even though this tutorial only has around 25 lines of code, subsequent tutorials will largely expand that, so we need to prepare. Create two new text documents (in the Platformer/gameScripts directory) and name them 'player.cs' and 'gameData.cs'. We will add code to these shortly, but first, we must tell the engine about them. Open up game.cs in your text editor of choice and change it from this:
function startGame(%level)
{
// Set The GUI.
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
moveMap.push();
if( isFile( %level ) || isFile( %level @ ".dso"))
sceneWindow2D.loadLevel(%level);
}
Change it to the following below by adding the 2 exec statements to our new script files:
function startGame(%level)
{
// Load our custom scripts here
exec("./gameData.cs");
exec("./player.cs");
// Set The GUI.
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
moveMap.push();
if( isFile( %level ) || isFile( %level @ ".dso"))
sceneWindow2D.loadLevel(%level);
}
The exec function tells the engine to load and execute a script file. Any time you add a new file, you will need to add a line right after the two you just added to exec it. |
|
[edit] The PlayerOpen up the Level Builder again.
$platformGroup = 0; $playerGroup = 1; $platformLayer = 5; $playerLayer = 2; $platformGroup and $playerGroup define the collision groups for our platforms and player. The purpose of these will make more sense when we set up collision. $platformLayer and $playerLayer are the layer that the platforms and player will be rendered on respectively. Objects on lower numbered layers will be drawn in front of those on higher numbered layers.
function createPlayer()
{
Here is how we create the actual player object:
$player = new t2dStaticSprite()
{
scenegraph = t2dscene;
};
$player.setImageMap(playerImageMap);
This code just creates an instance of a static sprite in the scenegraph, stores it in $player, and sets it to use our playerImageMap. Now, anytime in any script that you need a reference to the player, you can just use $player. Here is the rest of the code to set up the player’s collision and physics: $player.setCollisionActive(true, true); $player.setCollisionPhysics(true, false); $player.setCollisionResponse(CLAMP); $player.setCollisionCallback(true); $player.setCollisionMaxIterations(2); $player.setLayer($playerLayer); $player.setGraphGroup($playerGroup); $player.setCollisionMasks(BIT($platformGroup), BIT($platformLayer)); $player.setCollisionPolyPrimitive(8); $player.setMaxLinearVelocity(250);
} Now we need to create a spawn function for the player that positions the player and sets up level dependent variables. Here it is:
function resetPlayer(%pos)
{
$player.setPosition(%pos);
$player.setConstantForce(0 SPC $gravity, true);
}
This function takes one parameter, the position of the player. Now we can call this function when a level is loaded to position the player where the level wants it. We also set up gravity here with setConstantForce so each level can have different amounts of gravity. |
|
[edit] The PlatformsEventually we will be using tile maps for our levels, but for now we are going to set up a platform as an image map so you can see the collision in action. First, the image map. Add this after the player’s image map datablock in datablocks.cs:
new t2dImageMapDatablock(platformImageMap)
{
imageMode = "full";
imageName = "~/data/images/tileMap";
};
Open up gameData.cs again and add this function after the variables:
function createLevel()
{
$gravity = 700;
$spawnPoint = "-20 -30";
resetPlayer($spawnPoint);
%platform = new t2dStaticSprite() {
scenegraph = t2dscene;
};
%platform.setImageMap(platformImageMap);
%platform.setCollisionActive(false, true);
%platform.setGraphGroup($platformGroup);
%platform.setLayer($platformLayer);
%platform.setSize("40 2");
%platform.setPosition("-20 30");
}
The first two lines set the level variables gravity and spawnPoint. The next line sets the position of the player by calling the previously defined resetPlayer function with spawnPoint as the parameter.
%platform = new t2dStaticSprite() { scenegraph = t2dscene; };
%platform.setImageMap(platformImageMap);
%platform.setCollisionActive(false, true);
%platform.setGraphGroup($platformGroup);
%platform.setLayer($platformLayer);
%platform.setSize("40 2");
%platform.setPosition("20 10");
%platform.setRotation(-30);
%platform = new t2dStaticSprite() { scenegraph = t2dscene; };
%platform.setImageMap(platformImageMap);
%platform.setCollisionActive(false, true);
%platform.setGraphGroup($platformGroup);
%platform.setLayer($platformLayer);
%platform.setSize("40 2");
%platform.setPosition("-10 -15");
%platform.setRotation(30);
%platform = new t2dStaticSprite() { scenegraph = t2dscene; };
%platform.setImageMap(platformImageMap);
%platform.setCollisionActive(false, true);
%platform.setGraphGroup($platformGroup);
%platform.setLayer($platformLayer);
%platform.setSize("10 2");
%platform.setPosition("-39 25");
%platform.setRotation(90);
Go ahead and play around with creating your own platforms and tweaking some of the variables in the code (like $gravity for instance). The best way to learn is to experiment and see how the changes you make affect the program.
|



