Game Development Community

Non-Exploding grenades!

by FruitBatInShades · in Torque Game Engine · 01/27/2005 (2:33 am) · 60 replies

My grenade don't explode, they just sit there! Any idea as to what i've missed :)
datablock ItemData(Grenade)
{
   category = "Mine";
   shapeFile = "starter.fps/data/shapes/grenade.dts";
   mass = 1;
   friction = 1;
   elasticity = 0.3; //how much bounce 0.1 = none, 1 = funny
   repairAmount = 50;
   maxDamage = 0.2;
   directDamage = 125;
   damageRadius = 20;
   radiusDamage = 150;
   dynamicType = $TypeMasks::DamagableItemObjectType;
   explosion = CrossbowExplosion;
   fadeIn = 0;

};

function Grenade::Explode( %data, %obj, %col )
{
   echo("Grenade::Explode called");
   %obj.setDamageState(Destroyed);
   %obj.schedule(999, "delete");
}

function Grenade::onDestroyed(%data,%obj)
{
   echo("Grenade::onDestroyed called");
   radiusDamage(%obj,%pos,%data.damageRadius,%data.radiusDamage,"Mine",0);
}

function ShapeBase::throwItem(%this,%data,%client){
   // Construct item to throw from the given datablock
   // (with a random rotation around the z axis)
   echo("ShapeBase::throwItem grenade, creating item");
   %item = new Item() {
      dataBlock    = %data;
      rotation     = "0 0 1 " @ (getRandom() * 360);
      sourceObject   = %client.player;
      client       = %client;
   };
   echo("ShapeBase::throwObject grenade, setting explode time to 100ms");
    %item.schedule(3000, "explode"); //<------- explode in 5555 miliseconds
   // Call the workhorse throw method...
   echo("ShapeBase::throwItem grenade, calling throwObject");
   %this.throwObject(%item);
   MissionCleanup.add(%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);
      %obj.applyImpulse(%pos,%vec);
      // Since the object is thrown from the center of the
      // shape, the object needs to avoid colliding with it&#180;s
      // thrower.
      %obj.setCollisionTimeout(%this);
}
Page«First 1 2 3 Next»
#41
01/27/2005 (2:37 pm)
Hey, Billy....eh, what would you trade for your dynamite code?.....hint, hint; I'm also trying to get a 'bearTrap' device to work like a 'mine'. I've got 3 'thrown' type weapons to deal with....impact explosive 'molotov cocktail', time-delayed 'stick-o-TNT', and the final item; bearTrap.....it's intended to be tossed, run a 'trap open/set' animation, wait for a trigger[either Col or LOS callback], and then run it's 'close'/damage/attack sequence; staying attached to a specific Node I've placed within the player; while damage is applied. Is this a hard bit of scripting???
#42
01/27/2005 (4:28 pm)
Still no damage! I just have bad torque karma!
#43
01/27/2005 (4:48 pm)
Silly billy

radiusDamage(%obj,%pos,%data.damageRadius,%data.radiusDamage,"Mine",0);

is now know as %this


scratch that . . .did you just edit it. . . let me look it over . . .in class so answers will be sparse. . .gotta tend to the kids also
#44
01/27/2005 (4:55 pm)
%pos = %obj.getPosition; IS NOT %pos = %obj.getPosition() ;
#45
01/27/2005 (5:13 pm)
Fruitbat muses briefly as he ties the rope around the tree branch. Intellisense and syntax completion have made the old fruit a sloppy coder. He puts his head in the noose and has a final curse at torques lack of decent error reporting and jumps.......

Forgetting he can just flap his wings and carry on coding!
#46
01/27/2005 (5:38 pm)
I have been testing the code so far and come up with 2 issues. Can you guys see if its just me or whether you have the same problems?

1. If the character is running, the grenade just drops at his feet!
// Add the shape's velocity
      %vec = vectorAdd(%vec,%this.getVelocity());
I would have thought this line should add the players velocity but it doesn't seem to be, otherwise I'm assuming the grenade is colliding with the player itself although we have turned collision off.

2. I cannot seem to throw a grenade through a portal, the object collides with the portal brush. On some portals it passes through one way, but not the other! Very strange.
#47
01/28/2005 (2:54 am)
Quote:Silly billy

Whats up guys ?
Does it work or not ?
#48
01/28/2005 (3:43 am)
Yes billy it works, i'm just posting a resource. The 2 issues above have not been fixed but I'm going to try and get some help to solve them.
#49
01/28/2005 (4:05 am)
Just figuired out the problem when running, fixed it :0D and added this as a resource
#50
01/28/2005 (5:48 am)
Next problem :0)

I'm trying to make grenades explode when damaged (shot, next to another grenade etc). I've got the basic code running but cannot seem to pass the right object to the .schedule() function, AGAIN!
function Grenade::Damage(%this,%obj,%sourceObject, %position, %damage, %damageType)
{
   echo("Grenade::Damage called");
   //Grenade has recieved damage so detonate it
   if (%obj != %sourceObject)
	{
		//%obj.getdatablock().dump();
		%obj.getdatablock().schedule(1, "Explode", %obj);
	}
}
The passed object does not exist, even though the dump is correct. Either %obj is the the wrong thing to pass or it is already destroyed by the time this function executes?

Any ideas?
#51
01/28/2005 (8:59 am)
I have damage working (in a fashion - EDIT see posting below for fix) - the grenades destroy other grenades but the original schedule call (set when you threw it) then fires and cannot find an object to work on (since you have already destroyed it). The only side effect is a couple of error lines in the script but I want to get rid of those too.

Add

function Grenade::Damage(%data, %obj, %sourceObj, %pos, %damage, %damageType)
{
   echo("Damage event");	
   %obj.setDamageState(Destroyed);
   %obj.schedule(99, "delete");     
}

If you dont have the delete scheduled here, you get a nasty crash because you call damage repeatedly until you run out of memory. Note that there is no schedule called since setting damage state to destroyed calls the onDestroyed method for you.

Now to work out a way of cancelling the original schedule.

By the way, if you use the same script but without the schedule call to explode and without the explode and throw functions, you now have mines or any other type of object that explodes when damaged :)

Add a MineExplosion datablock and a mine.dts object to the code below and you have mines.

datablock StaticShapeData(Mine)
{
     category = "Misc";
     shapeFile = "~/data/shapes/Mine/Mine.dts";
     damageType = Mineblast;
     damageRadius = 20.0;
     damageImpulse = 50.0;
     explosion = MineExplosion;
     radiusDamage = 40.0;
     maxDamage = 20.0;
};

function Mine::Damage(%data, %obj, %sourceObj, %pos, %damage, %damageType)
{
   %obj.setDamageState(Destroyed);
   %obj.schedule(99, "delete");     
}


function Mine::onDestroyed(%data,%obj)
{
   radiusDamage(%obj,%obj.getposition(),%data.damageRadius,%data.radiusDamage,"Mineblast",%data.areaimpulse);
}
#52
01/28/2005 (9:24 am)
Got it!

To cancel a pending event we need to know the eventID and store it somewhere we can get to it later.

So - in the throwitem function, we need to create a new datafield to hold the eventID that we are going to set in a few moments

%item = new Item()
 {
      dataBlock    = %data;
      rotation     = "0 0 1 " @ (getRandom() * 360);
      sourceObject   = %client.player;
      client       = %client;
      ExplodeEventID=0; //datafield to hold our pending eventID i.e. the call to explode
   };

Then we change our schedule call to store the eventID assigned to the explode schedule call

%item.ExplodeEventID=%data.schedule(5500, "Explode",%item);

and finally, in our grenade::damage function, we cancel the explode event before it happens and therefore get rid of the calls on an object that does not exist

function Grenade::Damage(%data, %obj, %sourceObj, %pos, %damage, %damageType)
{
   echo("Damage event");	
   cancel(%obj.ExplodeEventID); //we are now going to explode anyway
                                                    //so cancel the scheduled explode call
   %obj.setDamageState(Destroyed);
   %obj.schedule(99, "delete");     
  
}
#53
01/28/2005 (9:36 am)
Two things I have noticed with these grenades-

1, Sometimes if you throw 2 in quick succession (sp?) they disappear without exploding. Its not definitely repeatable though.

2, If you throw a couple of grenades and destroy them by shooting them before they explode AND you die as a result, you get the message XX takes his own life. Which I suppose is technically correct if not intended ;)
#54
01/28/2005 (9:43 am)
@David:
Great work :) Knowing about cancelling events is a great help, I'm off for the weekend now but I plan on adding some more features next week :)
I've got halfway through proximity mines too, I'll give you a yell when I get it finished/stuck :o) Have a good weekend.

EDIT: Yeah, I noticed that but in-game you would never be doing that, you have to load, charge and throw the grenade.
#55
01/28/2005 (9:47 am)
@ Fruitbat
Proximity mines are the next thing on my to do list :)
#56
01/28/2005 (9:51 am)
Seen a few theories on proximity mines, most seem very resource hungry. My theory so far is have the mine itself do the checks. some other solutions seem to use the player to check. 2 ,or even larger, second checks should be enough I would have thought.
#57
01/28/2005 (10:39 am)
I was thinking of having the minefield enclosed by an area trigger which did the checks for the whole minefield and when it detected a player switched on the players mine detection/triggering function. Doing that would only have one check per minefield running for most of the time regardless of the number of mines/players. The frequency of checks would have to take into account the radius of the mines effect and the movement speed of the player (depending how many mines are in the field and how they are arranged.)
#58
01/31/2005 (9:50 am)
Back to grenades....

This has annoyed me immensely. I have been trying to get realistic grenade motion and have hit a wall. After a few hours I realised it wasn't me for once but the code for Item::setTransform ONLY rotates around the z-axis. Has anyone got a solution for this problem?
#59
01/31/2005 (11:18 am)
I thought rotation worked by setting a rotation axis and then setting the rotation about that specific axis??

I may have dreamt it though!
#60
01/31/2005 (11:54 am)
The item transformation code only rotates on the Z axis. See this thread
Page«First 1 2 3 Next»