Game Development Community

Basic Collision Gameplay

by David Hutchison · in Torque 3D Professional · 08/25/2010 (3:38 am) · 7 replies

I am trying to create a basic gameplay scenario in Torque 3D using the stock Full template.

If the player collides with an object (that the player can pass through without stopping), the object disappears, a bleep sound is played, and the player's health is decreased by 10.

I'm new to TorqueScript and would really appreciate some help on how to do this. Thanks.

#1
08/25/2010 (3:51 am)
I can get you started with a resource posted by Steve. The link is

http://www.torquepowered.com/community/resources/view/20103

That makes an object disappear on collision.
#2
08/25/2010 (4:14 am)
if the object the player collides with is stationary, you could either use a trigger placed over the position of the object, or you could just find the position of the object, and use a timer to repeatedly check whether the player is close enough to that position.
#3
08/25/2010 (5:29 am)
No need for triggers or schedules for something as simple as this. If your object is created as either an Item or StaticShape you can use an onCollision event callback to remove the object, play a sound, and do damage.

datablock StaticShapeData(EvilRock)  
{     
   category = "Hurtful Things"; // organizes this object in the editor library
   shapeFile = "art/shapes/rocks/rock1.dts";  
};  
  
function EvilRock::onCollision(%this, %obj, %col)  
{  
   if(isObject(%obj)) 
   {  
      if (%col.getClassName() $= "Player")  
      {   
         serverPlay3D(DeathCrySound, %obj.getTransform()); // play sound
         %obj.delete();  // make it disappear
         //%col.applyDamage(10); // hurt the player
         %col.damage(0, %col.getTransform(), 10, "EvilRockDamage");
      }  
   }  
}
The "EvilRockDamage" in there is so you can check for this damagetype in GameConnection::onDeath() and write an appropriate death message if the "evil rock" kills the player.

Also, take a look at the Healthpatch Item, you could just as easily create an anti-health kit.
#4
08/25/2010 (8:45 am)
Michael, are you sure that would work with no collision response?

David mentioned that the player needs to "pass through without stopping". as far as I know, oncollision will only work if the object uses a collision mesh or polysoup, and if it uses one of these two, it will always exhibit a collision response preventing the player from being able to pass through it.

is this correct?
#5
08/25/2010 (1:01 pm)
It will work with an Item object. It receives onCollision() callbacks, but won't generate a collision response on the player, letting it pass through.

That's the entire reason behind the Item class anyway.
#6
08/25/2010 (4:25 pm)
Yeah, it'll work. However, if the Staticshape example is used in a multiplayer environment then it's possible a slight collision pause could occur depending on network latency and the nature of client/server architecture of Torque. In a listen server, or single player game, or even a locally networked multiplayer example, the Staticshape is deleted quick enough to not be an issue

But note that I did mention using either Item Class or Staticshape, the exact same onCollision callback can be used for both. The key points I was illustrating were the commands to play a sound, delete the object, and hurt the player.
#7
08/25/2010 (9:36 pm)
Thanks for all the help. It is appreciated. I will test this out.