Game Development Community

TGB Collision nodes? How do they work?

by Tyler Slabinski · in Torque Game Builder · 03/24/2009 (1:12 pm) · 2 replies

have been attempting to follow the Platformer Tutorial, and I am stuck on the Creating the Player section.

I would like to know how the collision works, mostly this part:

function playerClass::updateVertical(%this)
{
   %yVelocity = %this.getLinearVelocityY();
   
   %this.setLinearVelocityY(10);
   %collision = %this.castCollision(0.005);
   
   %normalX = getWord(%collision, 4);
   %normalY = getWord(%collision, 5);
   
   // no collision
   if (%collision $= "")
   {
      %this.airborne = true;
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the left
   if (%normalX == 1 && %normalY == 0)
   {
      %this.againstLeftWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the right
   if (%normalX == -1 && %normalY == 0)
   {
      %this.againstRightWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // on ground with no wall collisions
   if (%normalX == 0 && %normalY == -1)
   {
      %this.airborne = false;
      %this.againstLeftWall = false;
      %this.againstRightWall = false;
      %this.setConstantForceY(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in air and hits platform with head
   if (%normalY == 1)
   {
      %this.airborne = true;
      %this.setLinearVelocityX(0);
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in case another type of collision normal was detected
   error("another collison type" SPC %normalX SPC %normalY);
   %this.airborne = false;
   %this.againstLeftWall = false;
   %this.againstRightWall = false;
   %this.setLinearVelocityY(%yVelocity);
}

I read what it says it is suppose to do, but I would like to know what each individual piece does. Any help?

#1
03/30/2009 (6:11 pm)
Wow... Sent to the 3rd page without a single reply.

Still need some help?
#2
03/30/2009 (9:06 pm)
Tyler,

Here's info about castCollision

Quote:
If no collision occurs, the return value is an empty string. Otherwise, the list is in the form "object time contact normal".
'object' is the object collided with
'time' is the number of seconds in the future the collision took place as a float (this number will always be less than the %time parameter)
'contact' is the position the objects collided at as a vector
'normal' is the normal of the collision as a vector.

In the code you pasted above, the author try to determine colliding object by analyzing the normal vector (using getWord(%collision, 4) and getWord(%collision, 5))

- if empty, it means there is no collision
- if normal X = 1 and normal Y = 0, it means player is hitting something on the left (since normal is pointing to the right)

similar case for other test (right, above, below)

Hopefully this answers your question,