Game Development Community

Problem with the collisions part of the beginner's tutorial

by Jon Mitchell · in Torque Game Builder · 03/12/2005 (10:50 pm) · 8 replies

Hello,
I am having a bit of a problem with collisions. Flatly they are just not happening. I though I had fat fingered something while typing, so I went back and copied and pasted the code, and to no avail. Collisions still do not work.

I guess first I wanted to check to see if there might be an error with the tutorial? I know there was one other issue that someone had pointed out for the movement part.

If there is no error in the tutorial for collision, here are some chunks of my code below, I believe it is all correct?



function CreatePlayer()
{
// Set player's collision info:
$player.setGroup( 1 );
$player.setLayer( 1 );
$player.setCollisionActive( true, true );
$player.setCollisionMaterial( standardMaterial );
$player.setCollisionPolyCustom( 4, "-1 0 -0.1 -0.6 0.98 0.15 -0.1 0.7" );
$player.setCollisionMasks( BIT(2), BIT(
$player.setCollisionCallback( true );
// Create the player's ship sprite
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
$player.setPosition("-35 0");
$player.setSize( "14 7" );
$player.setImageMap( playershipImageMap );
$player.fireLinkPoint = $player.addLinkPoint( "0.45 0.2" );
$player.setWorldLimit( clamp, "-49 -37 40 37" );
// Create an image for the player's missiles
datablock fxImageMapDatablock2D(playermissileImageMap)
{
mode = full;
textureName = "~/client/images/smallbubble.png";
};







function playerFire()
{
// Setup collision info
%projectile.setGroup( 1 );
%projectile.setLayer( 1 );
%projectile.setCollisionActive(true, true);
%projectile.setCollisionMaterial(projectileMaterial);
%projectile.setCollisionScale("0.9 0.5");
%projectile.setCollisionMasks( BIT(2), BIT(
%projectile.setCollisionCallback( true );
// Create player projectile.
%projectile = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%projectile.setPosition( $player.getLinkPoint($player.fireLinkPoint) );
%projectile.setSize( "3 1.5" );
%projectile.setImageMap( playermissileImageMap );
%projectile.setWorldLimit( kill, "-52 -40 52 40" );
%projectile.setLinearVelocityX( 35 );
}






function CreateEnemy()
{
// Set enemy collision info
%enemy.setGroup( 2 );
%enemy.setLayer( 2 );
%enemy.setCollisionActive( true, true );
%enemy.setCollisionMaterial( standardMaterial );
%enemy.setCollisionPolyCustom( 5, "-0.9 0 0 -0.6 1 -0.3 1 0.3 0 0.5" );
%enemy.setCollisionMasks( BIT(1), BIT(
%enemy.setCollisionCallback( true );
// Set up collision info
%enemyFire.setGroup( 2 );
%enemyFire.setLayer( 2 );
%enemyFire.setCollisionActive( true, true );
%enemyFire.setCollisionMaterial( standardMaterial );
%enemyFire.setCollisionScale("0.9 0.5");
%enemyFire.setCollisionMasks( BIT(1), BIT(
%enemyFire.setCollisionCallback( true );
// Create an enemy ship
%enemy = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%enemy.setImageMap( enemyship1ImageMap );
%enemy.setSize( "28 14" );
%enemy.setPosition("40" SPC (-30 + (getRandom() * 60)));
// Move the enemy towards us.
%enemy.setLinearVelocityX(-20);
%enemy.setWorldLimit( kill, "-60 -40 60 40" );
// Create an enemy missile and fire it
%enemyFire = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%enemyFire.setPosition( %enemy.getPosition() );
%enemyFire.setSize( "3 1.5" );
%enemyFire.setImageMap( enemymissileImageMap );
%enemyFire.setLinearVelocityX( -35 );
%enemyFire.setWorldLimit( kill, "-60 -40 60 40" );
schedule(2000, 0, "CreateEnemy");
}







function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
if (%srcObj == $player)
{
KillPlayer();
%dstObj.safeDelete();
}
else if (%dstObj == $player)
{
KillPlayer();
%srcObj.safeDelete();
}
else
{
%srcObj.safeDelete();
%srcObj.safeDelete();
}
}







Oh, one more thing, is there an easier way to search the forums, say if I only wanted to search the T2D private forums for the keywork collision, instead of having it dragnet the all the forums?

Thank you!

-JGM

#1
03/12/2005 (11:03 pm)
This is a c/p from my code, which works (I've finally done the whole tutorial, and have gone back to trying to "upgrade it" so this should work for you
in CreatePlayer()
$player.setGroup( 1 );
$player.setLayer( 1 );
$player.setCollisionActive( true, true );
$player.setCollisionMaterial( standardMaterial );
$player.setCollisionPolyCustom( 4, "-1 0 -0.1 -0.6 0.98 0.15 -0.1 0.7" );
$player.setCollisionMasks( BIT(2), BIT(2) );
$player.setCollisionCallback( true );

in PlayerFire()
%projectile.setGroup( 1 );
%projectile.setLayer( 1 );
%projectile.setCollisionActive(true, true);
%projectile.setCollisionMaterial(projetileMaterial);
%projectile.setCollsionScale("0.9 0.5");
%projectile.setCollisionMasks( BIT(2), BIT(2));
%projectile.setCollisionCallback( true );

in CreateEnemy() (note I modified enemy to be a global ($) instead of a local (%) var for now to get certain tricks in my code to work while I learn torquescript, you will probably need to correct accordingly).
$enemy.setGroup( 2 );
$enemy.setLayer( 2 );
$enemy.setCollisionActive( true, true );
$enemy.setCollisionMaterial( standardMaterial );
$enemy.setCollisionPolyCustom (5, "-0.9 0 0 -0.5 1 -0.3 1 0.3 0 0.5" );
$enemy.setCollisionMasks( BIT(1), BIT(1) );

in my own EnemyFire() function (aka wherever you have the creation of enemy bullets)

%enemyfire.setGroup( 2 );
%enemyfire.setLayer( 2 );
%enemyfire.setCollisionActive( true, true );
%enemyfire.setCollisionMaterial( standardMaterial );
%enemyfire.setCollisionPolyCustom (5, "-0.9 0 0 -0.5 1 -0.3 1 0.3 0 0.5" );
%enemyfire.setCollisionMasks( BIT(1), BIT(1) );
%enemyfire.setCollisionCallback( true);

onCollision()
function fxSceneObject2D::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
createExplosion(%dstObj);

if(%srcObj == $player)
{
KillPlayer();
%dstObj.safeDelete();
}
else if (%dstObj == $player)
{
KillPlayer();
%srcObj.safeDelete();
}
else
{
%srcObj.safeDelete();
%dstObj.safeDelete();
}
}


Any questions based on my copy vs yours? I'm kinda tired hence I'm not going through yours line by line, but maybe this'll give you a comparison against a working copy to give you some ideas (plus trying out globalizing $enemy if you have to, though it shouldn't be necessary as I'm pretty sure I did that for issues I had with the shooting not the collisions).
#2
03/13/2005 (12:27 am)
@Jonothan: your collision function is

function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
if (%srcObj == $player)
{
KillPlayer();
%dstObj.safeDelete();
}
else if (%dstObj == $player)
{
KillPlayer();
%srcObj.safeDelete();
}
else
{
%srcObj.safeDelete();
%srcObj.safeDelete();
}
}

as listed by Patrick it should be


function fxSceneObject2D::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
createExplosion(%dstObj);

if(%srcObj == $player)
{
KillPlayer();
%dstObj.safeDelete();
}
else if (%dstObj == $player)
{
KillPlayer();
%srcObj.safeDelete();
}
else
{
%srcObj.safeDelete();
%dstObj.safeDelete();
}
}
#3
03/13/2005 (5:26 am)
@Jonothan

I just quickly looked at this, and if this is exactly how your scripts look, the problem might be on your setCollisionMasks() calls for each object.

Yours are set up like this:
$player.setCollisionMasks( BIT(2), BIT(

This is incomplete, and probably should have given an error in the console. The actual code should look like this:
$player.setCollisionMasks( BIT(2), BIT(2));

I apologize if this was already figured out, but I thought I'd let you know anyways :)

Hope that helps,
Chris
#4
03/13/2005 (8:46 am)
You hit it on the money Chris!
I checked the log after running it, and sure enough there was a bunch of errors as you pointed out.
The problem I discovered was from copying and pasting from the PDF, it seems to cut some stuff out sometimes.
After going back and fixing it seems to be working normally, although I am not sure if I am supposed to be able to shoot down the enemy missles in the tutorial or not?
#5
03/13/2005 (9:05 am)
You're not supposed to shoot them down in the tutorial. :)
#6
03/13/2005 (11:43 am)
From the wording, I though as much, but it kind of works out because I changed my ships weapon to use the small bubble image, so it looks like it is shooting a plasma bolt.
now to figure out how I did it.
#7
03/13/2005 (11:57 am)
Here is the enemy missle code:
%enemyFire.setGroup( 2 );
%enemyFire.setLayer( 2 );
%enemyFire.setCollisionActive( true, true );
%enemyFire.setCollisionMaterial( standardMaterial );
%enemyFire.setCollisionScale("0.9 0.5");
%enemyFire.setCollisionMasks( BIT(1), BIT(1) );
%enemyFire.setCollisionCallback( true );

It is almost the same as the player weapon, but the player weapon has ( projectionMaterial); instead of ( standardMaterial );
I did change it to projectile, and it had no effect. I also tried to change the group and layer to 3 so the player weapon should have no effect on it, but it still manages to blast it out of the sky.
#8
03/21/2005 (5:19 pm)
Matthew "King BoB" Langley

your answer helped me as well
I had the same problem as Jonothan Mitchell,
the pdf does not have the: createExplosion(%dstObj);
line...
I think this is where some people could get confused

thanks

Edit: well the pdf does have that line... but only later when the explosion is added into the script..
yes it makes sense... but it looks like without that line (before setting up explosoins) on collision assetts do not vanish