Game Development Community

Platform Tutorial Problems

by Ismael Placa · in Torque Game Builder · 07/20/2006 (1:09 pm) · 5 replies

Hi,

I have been following the platform tutorial and it has been kind of difficult to follow (somethings are implicit but not explained like the actionMap.cs), but i have managed to put up something. Anyway my player now jumps, but it jumps even when not on a platform and when walking on platforms it slows down randomly, 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();
}

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);
      }
   }	
}

About the author

Recent Threads


#1
07/20/2006 (3:37 pm)
In your create player function do $pPlayer.setCollisionMaxIterations("2"); and the slow down while on platforms should be solved.

Edit: Thanks for pointing out the typo:)
#2
07/21/2006 (1:35 am)
Luke is right about 'setCollisionMaxIterations' (<- proper). However, I have to suggest that you don't use the current platformer tutorial as a starting point for a game or even as a learning tool. It was designed for a much earlier version of TGB (before namespaces were fully functional) and learning TGB through that tutorial will send you in a very wrong direction. We've even discussed pulling several tutorials from TDN (and specifically that one) for that exact reason. I'm working on a brand new platformer template for the community that is based on that tutorial but designed for the current version of TGB (the next pont release). Until that is done, I would suggest learning the basics of TS and TGB from more recent tutorials and only taking general concepts from that tutorial. I wish I had a better answer for you, but TGB is still very young and it's constantly improving by leaps and bounds and one of the biggest challenges (especially with such a small team) is keeping the documentation and all the tutorials up-to-date.
#3
07/23/2006 (8:15 pm)
Thanks for the reply. The jumping on the air issue was due to a wrong property on the background sky imagemap. I should try your recomendation for the slow down on platforms and post back. Btw, what does that funcion do anyway?
#4
08/15/2006 (2:13 pm)
Is there any ETA on that tutorial, the game Im working on right now is actually a platformer and I started following that tutorial only to realize it IS heavily outdated.

Maybe you could just Update that tutorial? most of it is good, but some parts of it are no longer functional.
#5
08/17/2006 (6:07 pm)
The platformer template is pretty much done, it's just going through some final polish. We want it to be as easy to use as possible, so in order to do that we decided to use it for a couple different projects - which will also be released to the community as examples with accompanying tutorials. I can't give an ETA because theres no telling how long it will take to get things just right. Already in remaking MightyFist I've run into loads of things that needed to be added or changed in the template that I just never thought of before (like adding the option to set what frame in the character jump animation to start the actual jump, etc). The reason it's taking a while is because we are trying to make it as expandable as possible while still including everything that all platformers need.