Talk:Torque 2D/GenreTutorials/PlatformerMovement

From TDN

Well this demo seems great, however I do not have an initializeT2D.cs file in my directory structure. I am BRAND new to Torque and might be over-looking something. Please email me at jsprada(at)gmail(dot)com with any hints.


Thank you, Johnny

I was having some issues following the given steps...

Here is what my player.cs file looks like before the jumping section.  So far, nothing works.


function player::onLevelLoaded(%this, %scenegraph)
{
   // Define the player global variable
   $player = %this;
   
   // Get the value of gravity from the Level Builder
   %force = $player.getConstantForce();
   $gravity = getWord(%force, 1);
   
   // Player movement functions
   moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
   moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
   moveMap.bindCmd(keyboard, "space", "playerJump();", "");
}

//
// This function did not exist when I was told to add a line to it...
//
function playerLeft()
{
	$player.moveLeft = true;
}

//
// This function did not exist when I was told to add a line to it...
//
function playerRight()
{
	$player.moveRight = true;
}

//
// This function did not exist when I was told to add lines to it...
//
function CreatePlayer()
{
	$player.runSpeed = 40;
   $player.airSpeed = 16;
   $player.jumpHeight = 10;
   $player.maxRunSurfaceAngle = 35;
}

//
// I was asked to open this file I have had open since the beginning.  Confusing.
//
function t2dSceneObject::onCollision(%srcObj, %dstObj, %srcRef,
   %dstRef, %time, %normal, %contactCount, %contacts)
{
   switch (%srcObj.getGraphGroup())
   {
      case $playerGroup:
         switch (%dstObj.getGraphGroup())
         {
            case $platformGroup:
               collidePlayerPlatform(%normal);
         }
   }
}

function collidePlayerPlatform(%normal)
{
   %move = $moveRight - $moveLeft;
   $runSurface = isRunSurface(%normal, $player.maxRunSurfaceAngle);
   if ($runSurface > 0)
   {
      $player.setLinearVelocityX(%move * $player.runSpeed);
   }
}

Thoughts?


This is how your collide function look like,

function collidePlayerPlatform(%normal)
{
   //corrected line
   %move = $player.moveRight  - $player.moveLeft;

   $runSurface = isRunSurface(%normal, $player.maxRunSurfaceAngle);
   if ($runSurface > 0)
   {
      $player.setLinearVelocityX(%move * $player.runSpeed);
   }
}

Anyway im also having troble moving the player, it sometimes moves slower (as if it had some glur on the feet) on platforms, feels like a random thing, anyway here is my code, ideas?

function cPlayer::onLevelLoaded(%this, %scenegraph)
{
   // Define the player global variable
   $pPlayer = %this;
   
   // Get the value of gravity from the Level Builder
   %force = $pPlayer.getConstantForce();
   $gravity = getWord(%force, 1);
   
   // Player movement functions
   moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
   moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
   moveMap.bindCmd(keyboard, "space", "playerJump();", "playerJumpStop();");
   
   createPlayer();
}

//This is the collision callback function called by the engine every time a collision occurs in the scene. 
//For a description of all the parameters, check the T2D Reference PDF that comes with the engine. 
//The ones we are using are %srcObj, %dstObj, and %normal. %srcObj is the object doing the colliding and %dstObj is the object being collided with. 
//%normal is the vector perpendicular to the surface of collision. Basically, it tells us the angle that the surface is facing. 
//The first thing we do in this function is figure out which objects are involved in the collision. 
//For this, we use the collision group that we put the object in when we created it. 
//What we are saying here is, if %srcObj is in the group $playerGroup and %dstObj is in the group $platformGroup, call the function collidePlayerPlatform. 
//Or, more simply, if the player is colliding with a surface, call the function collidePlayerPlatform. 
function t2dSceneObject::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   switch (%srcObj.getGraphGroup())
   {
      case $playerGroup:
         switch (%dstObj.getGraphGroup())
         {
            case $platformGroup:
               collidePlayerPlatform(%normal);
         }
   }
}

function t2dSceneGraph::onUpdateScene(%this)
{
   //if (%this != t2dscene.getID())
   //   return;

   updatePlayer();
   
   $runSurface = -1;
}

//The purpose of this function is to determine whether or not a surface, defined by %normal, is not so steep that the player can’t run on it. 
//First we check if the y component of the normal is positive. If it is, the surface is somewhere between a ceiling and a wall, so it is definitely not walkable. 
//The next line finds the steepness angle of the surface using a bit of trig. If that angle is less than the maximum angle, we can walk on the surface, otherwise we can’t.
function isRunSurface(%normal, %maxAngle)
{
   if (getWord(%normal, 1) > 0)
      return 0;
   
   %angle = mRadToDeg(mAsin(getWord(%normal, 0)));
   
   if (mAbs(%angle) < %maxAngle)
      return 1;
      
   return 0;
}

//The only thing we have left to do is defining which surfaces the player can walk up, and which surfaces it will slide down.
//Remember the maxRunSurfaceAngle variable we set up earlier.
function collidePlayerPlatform(%normal)
{
   %move = $pPlayer.moveRight - $pPlayer.moveLeft;
   $runSurface = isRunSurface(%normal, $pPlayer.maxRunSurfaceAngle);
   if ($runSurface > 0)
   {
      $pPlayer.setLinearVelocityX(%move * $pPlayer.runSpeed);
   }
}

function createPlayer()
{
   $pPlayer.runSpeed = 25;
   $pPlayer.airSpeed = 16;
   $pPlayer.jumpHeight = 12;
   $pPlayer.maxRunSurfaceAngle = 35;
}

function playerLeft()
{
   $pPlayer.moveLeft = true;
}

function playerRight()
{
   $pPlayer.moveRight = true;
}

function playerLeftStop()
{
   $pPlayer.moveLeft = false;
}

function playerRightStop()
{
   $pPlayer.moveRight = false;
}

function playerJump()
{
   $jump = true;
}

function playerJumpStop()
{
   $jump = false;
}

function updatePlayer()
{
   %move = $pPlayer.moveRight - $pPlayer.moveLeft;
   if ($runSurface > 0)
   {
      if ($jump)
      {
         $pPlayer.setLinearVelocityY(-10 * $pPlayer.jumpHeight);
      }
   }
   else if ($runSurface < 0)
   {
      %speed = $pPlayer.getLinearVelocityX();
      
      if (%move)
      {
         if (((%speed * %move) <= 0) || (mAbs(%speed) < $pPlayer.airSpeed))
            $pPlayer.setLinearVelocityX(%move * $pPlayer.airSpeed);
      }
   }	
}