Game Development Community

RC3: bug in t2dSceneObject::onWorldLimit

by Ian Poma · in Torque Game Builder · 06/18/2006 (6:26 am) · 1 replies

I was playing around with asteroids tutorial on TDN and I think I found a bug in RC3.

When moving the player ship forward using setImpulseForcePolar, the onWorldLimit function is triggered properly only if the front of the ship colides with the world. If the ship is moving forward, and you rotate it before it hits the world limit so that the sides or the rear of the collision polygon cause the onWorldLimit collision, the playship continues moving right off the screen.

Here is the code for my onWorldlimit method:

function t2dSceneObject::onWorldLimit(%this, %limitMode, %limit)
{
   
   //echo("limit: ", $limit);
   
   if (%limit $= "top")
   {
      %this.setPositionY(-%this.getPositionY());
   } // top
   if (%limit $= "bottom")
   {
      %this.setPositionY(-%this.getPositionY());
   } // bottom
   if (%limit $= "right")
   {
      %this.setPositionX(-%this.getPositionX());
   } // right
   if (%limit $= "left")
   {
      %this.setPositionX(-%this.getPositionX());
   } // left

}

And this is the code for the player ship. pShip is a t2dStaticSprite with the following properties:

damping = 1
world limit mode is set to null and callback is set to true
the world limits are set to be the same size as the camera
the collision polygon is a square box that is smaller than the sprite

echo("----------loading player--------");

function playerShip::onLevelLoaded(%this, %scenegraph)
{
   echo("calling playerShip::onLevelLoaded");   
   $pShip = %this;
      
   //These commands bind our keys to our functions
	  moveMap.bindCmd(keyboard, "up", "$pShip.Move_Thrust();", "$pShip.Move_Thrust_Stop();");
	  //moveMap.bindCmd(keyboard, "s", "$pShip.playerMovement(down);", "$pShip.playerMovement(stopDown);");
	  moveMap.bindCmd(keyboard, "left", "$pShip.Move_RotateLeft();", "$pShip.Move_RotateLeft_Stop();");
	  moveMap.bindCmd(keyboard, "right", "$pShip.Move_RotateRight();", "$pShip.Move_RotateRight_Stop();");
	  //moveMap.bindCmd(keyboard, "space", "$pShip.isFiring();", "$pShip.notFiring();");
   
}

function Thrust()
{
   $pShip.setImpulseForcePolar($pShip.getRotation() + 0,3);
   
   $playerThrustSchedule = schedule( 20, 0, "Thrust" );   
}

function ThrustStop()
{
   if ( isEventPending($playerThrustSchedule) )
		      cancel( $playerThrustSchedule );
   
}

function playerShip::Move_Thrust(%this)
{  
   echo("thrust"); 
   Thrust();  
}

function playerShip::Move_Thrust_Stop(%this)
{  
    echo("thrust stop");   
    
    ThrustStop();
}

function playerShip::Move_RotateRight(%this)
{
   echo("rotate right");
   
   %this.setAngularVelocity(270);
}

function playerShip::Move_RotateRight_Stop(%this)
{
   echo("rotate right stop");
   
   %this.setAngularVelocity(0);
}


function playerShip::Move_RotateLeft(%this)
{
   echo("rotate left ");
   
   %this.setAngularVelocity(-270);
}

function playerShip::Move_RotateLeft_Stop(%this)
{
   echo("rotate left stop");
   
   %this.setAngularVelocity(0);
}

#1
06/18/2006 (10:39 am)
Your onWorldLimit callback could be the cause for this. If the sprite is rotated it and hits "top" it might be placed in a location that hits "bottom" immediatly and is sent back to "top". Try to incorporate an offset when moving the ship and see if this still occurs.
e.g:

function t2dSceneObject::onWorldLimit(%this, %limitMode, %limit)
{
   %offset = mSqrt( (%this.getSizeX()/2) * (%this.getSizeX()/2) + (%this.getSizeY()/2) * (%this.getSizeY()/2) ) +1;

   if (%limit $= "top")
   {
      %this.setPositionY( -%this.getPositionY() - %offset );
   } // top
   if (%limit $= "bottom")
   {
      %this.setPositionY(-%this.getPositionY() + %offset );
   } // bottom
   if (%limit $= "right")
   {
      %this.setPositionX(-%this.getPositionX() + %offset);
   } // right
   if (%limit $= "left")
   {
      %this.setPositionX(-%this.getPositionX() - %offset);
   } // left

}