Game Development Community

Problems on creating an explosion

by Alexander Bierbrauer · in Torque Game Engine · 08/18/2004 (8:43 pm) · 8 replies

Hi there,

I've got problems creating extra explosions when a specific projectile explodes. The projectile's explosion (given with it's member variable) does work like a charm, but not my own exp in onExplode(). Here's my code.. doesn't anyone see a mistake ??

/-----------------------------------------------------------------------------
// When a rocket hits something, it will explode like this...
//-----------------------------------------------------------------------------
datablock ExplosionData( RocketExplosion )
{
	soundProfile = RocketExplosionSound;

	lifeTimeMS = 1200;
    lifetimeVarianceMS = 0;

	// Volume particles
	particleEmitter = RocketExplosionFireParticleEmitter;
	particleDensity = 100;
	particleRadius  = 2;

	// Point emission particles
	emitter[0] = RocketExplosionSmokeParticleEmitter;
	emitter[1] = RocketExplosionSparksParticleEmitter;

	// This will make the camera shake when a player gets hit by a rocket.
    // Shoot your own feet to see this effect in action.
	shakeCamera      = true;
	camShakeFreq     = "10.0 11.0 10.0";
	camShakeAmp      = "1.0 1.0 1.0";
	camShakeDuration = 0.5;
	camShakeRadius   = 10.0;

	// This will create a dynamic lighting effect in the vicinity of the 
    // rocket's explosion.
	lightStartRadius = 6;
	lightEndRadius   = 3;
	lightStartColor  = "0.5 0.5 0.0";
	lightEndColor    = "0.0 0.0 0.0";
};

//-----------------------------------------------------------------------------
// When a rocket is flying through the air, it will emit exhaust like this...
//-----------------------------------------------------------------------------
datablock ParticleData( RocketExhaustParticle )
{
	textureName = "~/data/shapes/grenade/smoke";
	useInvAlpha = false;

	lifetimeMS = 2000;
	lifetimeVarianceMS = 250;

	times[0] = 0.0;
	times[1] = 0.5;
	times[2] = 1.0;

	colors[0] = "0.8 0.3 0.0 1.0";
	colors[1] = "0.1 0.1 0.1 0.7";
	colors[2] = "0.1 0.1 0.1 0.0";

	sizes[0] = 0.1;
	sizes[1] = 0.5;
	sizes[2] = 1.0;
};

datablock ParticleEmitterData( RocketExhaustParticleEmitter )
{
	particles = RocketExhaustParticle;

	ejectionPeriodMS = 5;
	periodVarianceMS = 2;

	ejectionVelocity = 0.1;
	velocityVariance = 0.1;
};

//-----------------------------------------------------------------------------
// When the rocket launcher fires, it will emit a single rocket which behaves 
// like this...
//-----------------------------------------------------------------------------
datablock ProjectileData( GrenadeProjectile )
{
	projectileShapeName = "~/data/shapes/grenade/grenade.dts";

	muzzleVelocity = 0;
	armingDelay    = 1000;
	lifetime       = 2000;
	fadeDelay      = 5000;
	isBallistic    = true;
	gravityMod     = 1;
	bounceElasticity = 0.2;

    // The projectile will cast light on the ground as it travels.
	hasLight    = true;
	lightRadius = 2;
	lightColor  = "0.5 0.5 0.25";

    // The rocket will emit exhaust particles using this
	particleEmitter = RocketExhaustParticleEmitter;

    // We'll create one of these if our rocket hits something
	//explosion    = RocketExplosion; 
	directDamage = 20;
	radiusDamage = 10;
	damageRadius = 1.5;
};

function GrenadeProjectile::onCollision( %this, %obj, %col, %fade, %pos, %normal )
{
    echo("RocketProjectile::onCollision called! -> obj:" @ %col.getName());
	
    // TO DO: Add code here to calculate rocket damage for any object hit.
}

function GrenadeProjectile::onExplode(%this,%pos,%something)
{
	echo("ONEXPLOSION!!!");


	%vec = VectorAdd(%pos,"1 0 0");	
	%explosion = new Explosion() {
      dataBlock = "RocketExplosion";
      sourceObject   = %obj.sourceObject;
      sourceSlot   = %obj.sourceSlot;
      client      = %obj.client;
	  position = %vec;
   };

   MissionCleanup.add(%explosion);
}

#1
08/18/2004 (9:36 pm)
The projectile DataBlock uses explosion value for the explosion. The explosion is done in the engine I believe. I noticed you commented out

explosion = RocketExplosion;


Not sure if that's what you meant or why your using onExplode.
#2
08/18/2004 (10:16 pm)
I commented it out just for testing purposes. This explosion works but I need additionally explosions to this one from the projectile. Thus the onExplode function which gets called.... I want to create something like a "napalm bomb" but the "cluster explosions" are not random but calculated on different params which I wanted to calc there and create add. explosions on these calculations.
#3
08/18/2004 (10:29 pm)
If you want your explosion to work right, you need to change this...


First, you have the function set up wrong, it should be...

function GrenadeProjectile::onExplode(%data, %proj, %pos)

then

%vec = VectorAdd(%pos,"1 0 0");

    %explosion = new Explosion()
    {
        dataBlock = "RocketExplosion";
        sourceObject   = %obj.sourceObject;
        sourceSlot   = %obj.sourceSlot;
        client      = %obj.client;
        position = %vec;
    };


to this....


%pos = VectorAdd(%pos, "0 0 3");

    %explosion = new Explosion()
    {
        dataBlock = "RocketExplosion";
        sourceObject   = %proj.sourceObject;
        sourceSlot   = %proj.sourceSlot;
        client      = %proj.client;
        position = %pos;
    };



And that's assuming that you've passed that data to the projectile which is "%proj". I've also moved your vector addition to the Z axis to move it up 3 meters above the original explosion so you could see it better.
#4
08/18/2004 (11:10 pm)
Uh... stupid mistake.... but it didn't help. I changed now the code like this:

function GrenadeProjectile::onExplode(%this,%pos,%something)
{
	echo("ONEXPLOSION!!!");
	echo("this:" @ %this);

	%explosion = new Explosion() {
      dataBlock = "RocketExplosion";
      sourceObject   = %this.sourceObject;
      sourceSlot   = %this.sourceSlot;
      client      = %this.client;
	  position = %pos;
   };
   
   MissionCleanup.add(%explosion);

}
#5
08/19/2004 (12:43 am)
You changed it, but it's still wrong. The first % variable that is sent to the function I labeled "%data" because that number is the object number of the projectiles DATABLOCK and not the projectile itself. This is being sent to this function so you can instantly reference any special attributes on the datablock if you need to. The actual projectile that is exploding I labeled "%proj" to illustrate where all your dynamic data was coming from(position, client, etc...) and of course I labeled the position as "%pos".


So, in your version of the function, the datablock is called "%this" your projectile is named "%pos" and your position is named "%something".

So you echo your datablocks ID#, then you set...

sourceObject = 0;
sourceSlot = 0;
client = 0;

and you set the position as your projectiles ID#


THIS is a correctly written function...

function GrenadeProjectile::onExplode(%data, %proj, %pos)
{
    //%pos = VectorAdd(%pos, "0 0 3");
    %explosion = new Explosion()
    {
        dataBlock = "RocketExplosion";
        sourceObject   = %proj.sourceObject;
        sourceSlot   = %proj.sourceSlot;
        client      = %proj.client;
        position = %pos;
    };
    MissionCleanup.add(%explosion);
}


if you absolutely NEED "%this" then this would be the proper function syntax....

function GrenadeProjectile::onExplode(%data, %this, %pos)
{
    //%pos = VectorAdd(%pos, "0 0 3");
    %explosion = new Explosion()
    {
        dataBlock = "RocketExplosion";
        sourceObject   = %this.sourceObject;
        sourceSlot   = %this.sourceSlot;
        client      = %this.client;
        position = %pos;
    };
    MissionCleanup.add(%explosion);
}
#6
08/19/2004 (12:56 am)
Uhhh yeah ! Big thanks man !! ;)

Is there something like a function reference for the different classes/datablocks ?? A member reference can be found in the docs but what about the functions ??
#7
08/19/2004 (1:57 am)
When I'm in doubt about anything, I go straight to the Engine code. The engine code never lies about what is being sent or expect to be received.


There are a few docs out there for functions but a lot of them are out of date or inadaquate to start with.
#8
08/19/2004 (9:15 am)
Often times the console stuff directly wraps an existing C++ engine method, so it's usually pretty easy to figure out what's going on.