Game Development Community

setUsesPhysics(true) but no collisions and no movements on the device

by Zilla · in iTorque 2D · 12/17/2009 (6:57 am) · 3 replies

Explanation:
This thread is a continuation of a thread in the TGB forum:
www.torquepowered.com/community/forums/viewthread/106985
But since I have now a problem that is iTGB specific and could not be solved in the TGB forum, I will continue this thread here.


I am currently working on a game where the player has to jump from platform to platform.
The platforms are moving from the bottom to the top of the screen.

www.gymprojects.ch/zilla/images/chalkup01.jpg

I think you get the picture ;)

The game is based on the mini platformer demo from TDN.

I have a problem that could not be solved entirely on the TGB forum - not even by mastermind Mike Lilligreen - but only because he has no iPhone ;)

I implemented his new mini platformer behaviour for the player and it works great on the desktop but there are no collisions and no movements on the device though I switch physics on for every object on the scene.

$player = %this;   
$player.setUsesPhysics(true);
platform0.setUsesPhysics(true);
Platform.setUsesPhysics(true);
killZone.setUsesPhysics(true);
platform_fade.setUsesPhysics(true);
player_fade.setUsesPhysics(true);

If you want to have a look at it, you can download and test the project here.
As I said, movements and collisions work perfectly on the desktop but unfortunately not on the device...

Here is the complete listing of this behaviour.
What did I miss to make the movements and collisions work?

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// WIP Behavior by Mike Lilligreen
//-----------------------------------------------------------------------------

if (!isObject(PlatformerControlsBehavior))
{
   %template = new BehaviorTemplate(PlatformerControlsBehavior);
   
   %template.friendlyName = "Platformer Controls";
   %template.behaviorType = "Input";
   %template.description  = "Platformer style movement control";
   
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");
   %template.addBehaviorField(jumpKey, "Key to bind to jump movement", keybind, "keyboard space");
   
   %template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 150.0);
   %template.addBehaviorField(jumpSpeed, "Speed when jumping", float, -150.0);
   %template.addBehaviorField(gravity, "Force of gravity", float, 100.0);
}

function PlatformerControlsBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
      return;
   
   moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
   moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
   moveMap.bindObj(getWord(%this.jumpKey, 0), getWord(%this.jumpKey, 1), "jumpAction", %this);
   
   %this.left = 0;
   %this.right = 0;
   %this.jump = 0;
   
   %this.enableUpdateCallback();
   
   $player = %this;
   
   $player.setUsesPhysics(true);
   platform0.setUsesPhysics(true);
   Platform.setUsesPhysics(true);
   killZone.setUsesPhysics(true);
   platform_fade.setUsesPhysics(true);
   player_fade.setUsesPhysics(true);
   
   %this.owner.setConstantForceY(%this.gravity);

}

function PlatformerControlsBehavior::onBehaviorRemove(%this)
{
   if (!isObject(moveMap))
      return;
   
   moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
   moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
   moveMap.unbindObj(getWord(%this.jumpKey, 0), getWord(%this.jumpKey, 1), %this);
   
   %this.left = 0;
   %this.right = 0;
   %this.jump = 0;
}

function PlatformerControlsBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
   %this.updateMovementX();
   %this.updateAnimation();
}

function PlatformerControlsBehavior::moveRight(%this, %val)
{
   %this.right = %val;
   %this.updateMovementX();
   %this.updateAnimation();
}

function PlatformerControlsBehavior::jumpAction(%this, %val)
{
   %this.jump = %val;
   %gravity = %this.owner.getConstantForceY();
   
   if (!%gravity)
      %this.updateMovementY();
   
   %this.updateAnimation();
}

function PlatformerControlsBehavior::updateMovementX(%this)
{
   %this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
   
   // Here we get a point just below the player object
   %positionX = %this.owner.getPositionX();
   %positionY = %this.owner.getPositionY();
   %sizeY = %this.owner.getSizeY() / 2;
   %pointY = %positionY + %sizeY + 1;
   %point = %positionX SPC %pointY;
   
   // Get the scene graph and then use pick point to find out if a platform is
   // below the player. If there is no platform below the player, gravity is
   // turned on. Note that platforms are all in group 1, objects in any other
   // group are ignored.
   %sceneGraph = sceneWindow2D.getSceneGraph();
   %object = %sceneGraph.pickPoint(%point, bit(1));
   
   if (!isObject(%object))
      %this.owner.setConstantForceY(%this.gravity);

   if (%this.right || %this.left)
      %this.schedule(100, "updateMovementX");
}

function PlatformerControlsBehavior::updateMovementY(%this)
{
   %this.owner.setLinearVelocityY(%this.jump * %this.jumpSpeed);
   
   if (%this.jump)
   {
      %this.owner.setConstantForceY(%this.gravity);
   }
}

function PlatformerControlsBehavior::updateAnimation(%this)
{
   %xVelocity = %this.owner.getLinearVelocityX();
   %yVelocity = %this.owner.getLinearVelocityY();
   
   if (%xVelocity > 0)
   {
      %this.owner.setFlip(false, false);
   }
   else if (%xVelocity < 0)
   {
      %this.owner.setFlip(true, false);
   }
   
   if(!%yVelocity == 0)
   {
      //%this.owner.playAnimation(PlayerJumpUpAnimation);
      //return;
   }
   
   if (!%xVelocity == 0)
   {
      if (%this.owner.getAnimationName() $= "playerRunAnimation")
      {
         if(%this.owner.getIsAnimationFinished())
         {
            %this.owner.playAnimation(PlayerRunAnimation);
            return;
         }
      }else
      {
         %this.owner.playAnimation(PlayerRunAnimation);
      }
   }else
   {
      %this.owner.playAnimation(PlayerStandAnimation);
   }
}

function PlatformerControlsBehavior::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
   // Only turn off gravity if the player's "feet" collide with the ground
   %normalY = getWord(%normal, 1);
   
   if (%normalY == -1)
   {
      // turn gravity off
      %srcObject.owner.setConstantForceY(0);
      
      // get the location of the top surface of the platform
      %platformPosY = %dstObject.getPositionY();
      %platformSizeY = %dstObject.getSizeY() / 2;
      %topSurface = %platformPosY - %platformSizeY;
      
      // get the size of the player to place the feet on the top surface of the platform properly
      %sizeY = %srcObject.owner.getSizeY() / 2;
      %srcObject.owner.setPositionY(%topSurface - %sizeY + 3);
      
      // have the player inherit the velocity of the platform
      %platformVelocity = %dstObject.getLinearVelocityY();
      %srcObject.owner.setLinearVelocityY(%platformVelocity);
   }
      
   %srcObject.updateAnimation();
}

function sceneWindow2D::onMouseDown( %this, %modifier, %worldPosition, %mouseClicks )
{
	%xpos = getWord(%worldPosition,0);
	%xplayer = $player.getPositionX();
	
	if (%xpos < %xplayer)
		moveLeft();
	if (%xpos > %xplayer)
		moveRight();
	if ($player.getIsPointInObject(%worldPosition))
		jumpAction();
}

#1
03/12/2010 (9:44 am)
Hello

I am no expert on iTGB, but I am learning. I have downloaded your code and updated to version 1.31. I have it working, there are still a couple of errors I did not fix.

Problems found.

$player = %this;

should be

$player = %this.owner;

These are called in the wrong location. They need to be called after your loadlevel. All the items are part of the level and not the behavior.

platform0.setUsesPhysics(true);
Platform.setUsesPhysics(true);
killZone.setUsesPhysics(true);
platform_fade.setUsesPhysics(true);
player_fade.setUsesPhysics(true);

You can download the updated version from

www.deanparker.com/chalkup3.zip
#2
03/12/2010 (6:38 pm)
I fixed up a few more errors so it will work a little better. The controls will have to be changed to support iphone.

I removed the playerclass.cs file as it is a duplicate of the player control behavoir.

The zip file has been updated.
#3
03/16/2010 (12:28 pm)
Hi, Dean

Thank you very much! I really appreciate your help :)))