Sticky Grenades
by Scott Manning · in Torque Game Engine · 01/17/2006 (4:59 am) · 2 replies
I've been playing around with the Drop in Grenade resource, after getting it to work with TGE 1.4.
#2
In an attempt to make grenades stick to vehicles and players, I modified item.cc.
Around line 545
this:
was modified to this:
and
Around line 687
this:
It seems to sort of work. After modifiying both lines of code, it seemed to work more effectively on the buggy, sticking to it about over 90% of the area i tried on it, and this is apparently making it stick to either the bounding box or collision portion of the DTS model, since most of my "grenades" would stick to it hanging in mid air. When used on the Orc, it would either stick to it or pass through the model.
While looking at the code, I noticed a script callback for onStickyCollision, Which I would assume could be used to sort out different reactions for colliding w/ different objecttypes, so as to possibly make it stick to some things and not to others. I've never seen this used in any torque script so far, nor have I seen the sticky option used in a script datablock before, I stumbed upon it in the docs.
If anyone has any ideas they can share, as to make this work better, I'd be glad for the help. I'm trying to duplicate the type of sticky grenades in halo 1/2.
01/17/2006 (5:00 am)
I added "sticky = 1" to the datablock, a stock function of the engine for items to stick to certain things. By default it only sticks to terrain or DIF/interiors.In an attempt to make grenades stick to vehicles and players, I modified item.cc.
Around line 545
this:
// Contact!
if (mDataBlock->sticky && rinfo.object->getType() &
(InteriorObjectType|TerrainObjectType)) was modified to this:
// Contact!
if (mDataBlock->sticky && rinfo.object->getType() &
(InteriorObjectType|TerrainObjectType| PlayerObjectType |VehicleObjectType)) and
Around line 687
this:
if (collision && mDataBlock->sticky &&
collision->object->getType() &
(InteriorObjectType|TerrainObjectType))was modified to this:if (collision && mDataBlock->sticky &&
collision->object->getType() &
(InteriorObjectType|TerrainObjectType|PlayerObjectType |VehicleObjectType))It seems to sort of work. After modifiying both lines of code, it seemed to work more effectively on the buggy, sticking to it about over 90% of the area i tried on it, and this is apparently making it stick to either the bounding box or collision portion of the DTS model, since most of my "grenades" would stick to it hanging in mid air. When used on the Orc, it would either stick to it or pass through the model.
While looking at the code, I noticed a script callback for onStickyCollision, Which I would assume could be used to sort out different reactions for colliding w/ different objecttypes, so as to possibly make it stick to some things and not to others. I've never seen this used in any torque script so far, nor have I seen the sticky option used in a script datablock before, I stumbed upon it in the docs.
If anyone has any ideas they can share, as to make this work better, I'd be glad for the help. I'm trying to duplicate the type of sticky grenades in halo 1/2.
Torque Owner Scott Manning
Default Studio Name
//----------------------------------------------------------- // Grenades.cs - Class to add grenades to torque via script //----------------------------------------------------------- // This script file is an easy way to add grenades to your // FPS. Just exec this script from game.cs and bind a key // to call the throwGrenadeDB function //----------------------------------------------------------- datablock ItemData(Grenade) { category = "Grenades"; shapeFile = "starter.fps/data/shapes/items/healthkit.dts"; mass = 0.7; friction = 1; elasticity = 1; //how much bounce 0.1 = none, 1 = funny sticky = 1; repairAmount = 50; maxDamage = 0.2; directDamage = 30; damageRadius = 20; radiusDamage = 20; areaImpulse = 200; dynamicType = $TypeMasks::DamagableItemObjectType; explosion = CrossbowExplosion; fadeIn = 0; }; function Grenade::Explode(%dataBlock, %obj) { echo("Grenade::Explode called"); %obj.ExplodeEventID = 0; %obj.setDamageState(Destroyed); //%obj.schedule(99, "delete"); } function Grenade::Damage(%this,%obj,%sourceObject, %position, %damage, %damageType) { echo("Grenade::Damage called"); if (%obj.ExplodeEventID) { cancel(%obj.ExplodeEventID); %obj.ExplodeEventID=0; %obj.setDamageState(Destroyed); } } function Grenade::onDestroyed(%data,%obj) { echo("Grenade::onDestroyed called"); %pos = %obj.getPosition(); radiusDamage(%obj,%pos,%data.damageRadius,%data.radiusDamage,"Grenade",%data.areaimpulse); %obj.schedule(300, "delete"); } // If you do not want the grenade to explode if someone walks over/into it, just remove this // this oncollision member call.... function Grenade::onCollision(%this,%obj,%col) { echo("Grenade::onCollision -> " @ %col.getDataBlock().className); if ( (%col.getDataBlock().className $= "armor") && (%obj.ExplodeEventID) ) { cancel(%obj.ExplodeEventID); %obj.ExplodeEventID=0; %obj.setDamageState(Destroyed); } } function ShapeBase::throwGrenadeDB(%this,%GrenadeTypeDB,%client,%DelayToExplode) { //%GrenadeTypeDB = the datablock type for this grenade //%client = client object //%time = time in milliseconds before explode //Check time is not silly or negative if (%DelayToExplode < 1) %DelayToExplode = 500; echo("ShapeBase::throwGrenade grenade, creating item"); %item = new Item() { dataBlock = %GrenadeTypeDB; rotation = "1 0 0 " @ (getRandom() * 360); sourceObject= %client.player; client = %client; ExplodeEventID=0; }; //make sure we tidy up :) MissionCleanup.add(%item); //call the more general throwObject %this.throwObject(%item); //set detonantion delay %item.ExplodeEventID = %GrenadeTypeDB.schedule(%DelayToExplode, "Explode",%item); } function ShapeBase::throwObject(%this,%obj) { // Throw the given object in the direction the shape is // looking. The force values are hardcoded... echo("ShapeBase::throwObject grenade, getting direction"); %eye = %this.getEyeVector(); %vec = vectorScale(%eye, 20); // Add a vertical component to give the item a better arc %dot = vectorDot("0 0 1",%eye); if (%dot < 0) %dot = -%dot; %vec = vectorAdd(%vec,vectorScale("0 0 7",1 - %dot)); // Add the shape's velocity %vec = vectorAdd(%vec,%this.getVelocity()); // Set the object's position and initial velocity %pos = getBoxCenter(%this.getWorldBox()); %obj.setTransform(%pos); echo("ShapeBase::throwObject grenade, Applying impulse"); // Since the object is thrown from the center of the // shape, the object needs to avoid colliding with it's // thrower. %obj.setCollisionTimeout(%this); %obj.applyImpulse(%pos,%vec); } function serverCmdthrowGrenadeNow(%client) { %client.player.throwGrenadeDB(Grenade,%client,5000); } moveMap.bindCmd(keyboard, "g", "commandToServer(\'throwGrenadeNow\');", "");