Configurable bounce object types for projectiles
by Luke D · 07/27/2004 (8:58 am) · 16 comments
In the default engine, projectiles can be set to either explode immediately on impact, or bounce for a set duration (the 'armingDelay') then explode on the very next collision.
Bouncing is currently all-or-nothing; if the projectile's current life time is less than the arming delay, the projectile will bounce off everything including players/vehicles.
The following minor code changes to the engine add a variable to the ProjectileData datablock called 'noBounceObjTypes' against which a projectile will *always* explode. This opens up several possibilities, such as richochet-fire or grenades that bounce along the ground but will explode when they hit a player.
All changes are contained to projectile.cc and projectile.h.
First, in projectile.h find the following (~line 62):
Just below this, add the following line:
The remaining changes are in projectile.cc. at around line 69 find:
Below this add the following line:
The above change dictates the default behavior when no variable is specified in a datablock. The value of '0' means projectiles will bounce off of all object types (current behavior).
Next up, adding the new datablock variable and its read/write support. Around line 132 find:
and add the following line below it:
Next find around line 287:
and add the following below the last stream->write line (but within the if() ):
Finally, around line 353 find:
and add the following below the last stream->read line (but within the if() ):
The utility code to read and write the new 'noBounceObjTypes' variable is now in place. Next, to enable selective bouncing, find (around line 791):
And replace the conditional line with:
Also around line 796, find:
and replace it with the following line:
That's it! Compile the source, and crack open your favorite weapon .cs file. Within the ProjectileData definition, you need to have certain other variables set properly to enable bouncing in general. Add (or change) the following variables:
The above values are reasonable (read: they work for me) to start with.
Finally, add the following variable to the above set:
Multiple TypeMasks can be OR'd together (e.g. $TypeMasks::PlayerObjectType | $TypeMasks::VehicleObjectType) if so desired.
Run the above example, get into a good firefight in a small room and voila! The RUBBER BALL OF DEATH!
Enjoy! I look forward to any comments/corrections.
Bouncing is currently all-or-nothing; if the projectile's current life time is less than the arming delay, the projectile will bounce off everything including players/vehicles.
The following minor code changes to the engine add a variable to the ProjectileData datablock called 'noBounceObjTypes' against which a projectile will *always* explode. This opens up several possibilities, such as richochet-fire or grenades that bounce along the ground but will explode when they hit a player.
All changes are contained to projectile.cc and projectile.h.
First, in projectile.h find the following (~line 62):
bool isBallistic;
Just below this, add the following line:
U32 noBounceObjTypes;
The remaining changes are in projectile.cc. at around line 69 find:
isBallistic = false;
Below this add the following line:
noBounceObjTypes = 0;
The above change dictates the default behavior when no variable is specified in a datablock. The value of '0' means projectiles will bounce off of all object types (current behavior).
Next up, adding the new datablock variable and its read/write support. Around line 132 find:
addNamedField(isBallistic, TypeBool, ProjectileData);
and add the following line below it:
addNamedField(noBounceObjTypes, TypeS32, ProjectileData);
Next find around line 287:
if(stream->writeFlag(isBallistic))
{
stream->write(gravityMod);
stream->write(bounceElasticity);
stream->write(bounceFriction);
}and add the following below the last stream->write line (but within the if() ):
stream->write(noBounceObjTypes);
Finally, around line 353 find:
if(isBallistic)
{
stream->read(&gravityMod);
stream->read(&bounceElasticity);
stream->read(&bounceFriction);
}and add the following below the last stream->read line (but within the if() ):
stream->read(&noBounceObjTypes);
The utility code to read and write the new 'noBounceObjTypes' variable is now in place. Next, to enable selective bouncing, find (around line 791):
// make sure the client knows to bounce
if(isServerObject() && (rInfo.object->getType() & csmStaticCollisionMask) == 0)
setMaskBits(BounceMask);And replace the conditional line with:
if(isServerObject() && ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) == 0))
Also around line 796, find:
if(mCurrTick > mDataBlock->armingDelay)
and replace it with the following line:
if (mCurrTick > mDataBlock->armingDelay || ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) != 0) )
That's it! Compile the source, and crack open your favorite weapon .cs file. Within the ProjectileData definition, you need to have certain other variables set properly to enable bouncing in general. Add (or change) the following variables:
armingDelay = 5000; bounceElasticity = 0.5; bounceFriction = 0; isBallistic = true;
The above values are reasonable (read: they work for me) to start with.
Finally, add the following variable to the above set:
noBounceObjTypes = $TypeMasks::PlayerObjectType;
Multiple TypeMasks can be OR'd together (e.g. $TypeMasks::PlayerObjectType | $TypeMasks::VehicleObjectType) if so desired.
Run the above example, get into a good firefight in a small room and voila! The RUBBER BALL OF DEATH!
Enjoy! I look forward to any comments/corrections.
About the author
#2
I added this and now my ball bounces "not!" it disappears under the terrain.. and now avoids the collision on all dts shapes..
so in essence it doesnt work as is..
I dont know how to fix it but I think it would be nice if it can be fixed to enable collision.. :)
07/27/2004 (4:16 pm)
oops spoke to soon...I added this and now my ball bounces "not!" it disappears under the terrain.. and now avoids the collision on all dts shapes..
so in essence it doesnt work as is..
I dont know how to fix it but I think it would be nice if it can be fixed to enable collision.. :)
#3
07/27/2004 (5:03 pm)
That sounds like isBallistic is not being set to true in the datablock. If there's a armDelay but no isBallistic set, no collision happens at all (somewhat of an original bug I think.) Can you post your projectile datablock so I can take a look at it? I applied this to a clean copy of the cvs head on the 19th and it works great for my bouncy projectile.
#4
thats the projectile datablock..
dont know why it wouldnt work..
07/27/2004 (8:56 pm)
datablock ProjectileData(ballAmmoProjectile)
{
projectileShapeName = "~/data/shapes/tennisBall/tennisBall.dts";
directDamage = 30;
radiusDamage = 20;
damageRadius = 6.5;
explosion = ballLauncherExplosion;
waterExplosion = ballLauncherWaterExplosion;
particleEmitter = ballLauncherBoltEmitter;
particleWaterEmitter= ballLauncherBoltBubbleEmitter;
splash = ballLauncherSplash;
muzzleVelocity = 50;
velInheritFactor = 0.3;
armingDelay = 2000;
lifetime = 18000;
fadeDelay = 15000;
numBounces = 6;
bounceVariance = 1;
explodeOnMaxBounce = true;
staticOnMaxBounce = true;
snapOnMaxBounce = true;
bounceElasticity = 0.5;
bounceFriction = 0.0;
isBallistic = true;
gravityMod = 0.85;
hasLight = true;
lightRadius = 8;
lightColor = "0.4 0.7 0.9";
hasWaterLight = true;
waterLightColor = "0 0.8 0.9";
};thats the projectile datablock..
dont know why it wouldnt work..
#5
BTW, I scanned the source code for the variables listed above, and they are only used in DebrisData datablocks. ProjectileData doesn't have any of those, so they're superfluous.
Also, you didn't have a noBounceObjTypes in that Datablock, but that means it should bounce of *everything*, not nothing.
What date was the head version you applied this to? If it was the last few days, I'll grab the very very latest and try it myself again to make sure no recent changes broke it.
07/28/2004 (12:18 am)
I changed my bouncer projectile to use all the relevent lines in your datablock and it still bounces great. I didn't have numBounces, bounceVariance, explodeOnBaxBounce, staticOnMaxBounce or snapOnMaxBounce so I added those just to be sure, and adjusted all my other variables appropriately to match yours and everything works great.BTW, I scanned the source code for the variables listed above, and they are only used in DebrisData datablocks. ProjectileData doesn't have any of those, so they're superfluous.
Also, you didn't have a noBounceObjTypes in that Datablock, but that means it should bounce of *everything*, not nothing.
What date was the head version you applied this to? If it was the last few days, I'll grab the very very latest and try it myself again to make sure no recent changes broke it.
#6
I will try again later and see..
bit busy to work on it now, but if I get it going I will post again.. :)
07/28/2004 (5:07 pm)
I am using the head form about a week ago.. I just changed the crossbow cs to my weapon cs, so alot of it is related to the crossbow.. now it may be that the crossbow being non bouncing projectile may be causing some conflicts.. so I may have to rewrite the whole thing.. :)I will try again later and see..
bit busy to work on it now, but if I get it going I will post again.. :)
#7
I set isBallistic to 'true' and adjusted the relevent numeric settings to match your datablock, and the crossbow arrow bounces like a champ. I'm wondering if perhaps you missed one of the two if statement replacements in projectile.cc?
07/28/2004 (7:12 pm)
I just grabbed the latest head and applied the code changes verbatim above. I then changed the crossbow ProjectileData datablock to the following:datablock ProjectileData(CrossbowProjectile)
{
projectileShapeName = "~/data/shapes/crossbow/projectile.dts";
directDamage = 20;
radiusDamage = 20;
damageRadius = 1.5;
explosion = CrossbowExplosion;
waterExplosion = CrossbowWaterExplosion;
particleEmitter = CrossbowBoltEmitter;
particleWaterEmitter= CrossbowBoltBubbleEmitter;
splash = CrossbowSplash;
muzzleVelocity = 50;
velInheritFactor = 0.3;
armingDelay = 2000;
lifetime = 18000;
fadeDelay = 15000;
bounceElasticity = 0.5;
bounceFriction = 0;
isBallistic = true;
gravityMod = 0.85;
hasLight = true;
lightRadius = 4;
lightColor = "0.5 0.5 0.25";
hasWaterLight = true;
waterLightColor = "0 0.5 0.5";
};I set isBallistic to 'true' and adjusted the relevent numeric settings to match your datablock, and the crossbow arrow bounces like a champ. I'm wondering if perhaps you missed one of the two if statement replacements in projectile.cc?
#8
it works finally..
I made an error.. i changed it form this
if(isServerObject() && ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) == 0))
to this
if(isServerObject() && ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) == 0))
setMaskBits(BounceMask);
I had removed the - setMaskBits(BounceMask);
so it wasnt hitting any collision.. :)
so works like a charm now.. :)
thanks again
07/29/2004 (6:36 am)
ok.it works finally..
I made an error.. i changed it form this
if(isServerObject() && ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) == 0))
to this
if(isServerObject() && ((rInfo.object->getType() & mDataBlock->noBounceObjTypes) == 0))
setMaskBits(BounceMask);
I had removed the - setMaskBits(BounceMask);
so it wasnt hitting any collision.. :)
so works like a charm now.. :)
thanks again
#9
07/29/2004 (7:16 am)
Great! Glad to hear it. :)
#10
08/15/2004 (3:30 am)
I want my projectiles to explode on vehicles, and that works fine... but I can't get them to explode on wheels. What object type should I use to have them explode on collision with a wheel?
#11
echo( %col.getType() );
I tried this, but I only have one test vehicle in my current project and normal projectiles (crossbow ammo) went right through the wheel, which leads me to believe that the wheels aren't currently tested for collision whatsoever. I've not looked into wheeledVehicle.cc to see for sure though, but I imagine that'd be the place to start.
08/15/2004 (9:04 am)
Looking at the list of objectTypes, I imagine that a wheel is considered nothing special, probably a ShapeBaseObjectType. You could throw a:echo( %col.getType() );
I tried this, but I only have one test vehicle in my current project and normal projectiles (crossbow ammo) went right through the wheel, which leads me to believe that the wheels aren't currently tested for collision whatsoever. I've not looked into wheeledVehicle.cc to see for sure though, but I imagine that'd be the place to start.
#12
08/26/2004 (9:12 am)
my bouncy ball weapon finnaly will be achieved thanks!!!
#13
right now, my crossbow will do damage only on explosion...is it possible to have direct damage too? I have it set to 20 in my crossbow.cs...but it just bounces right off me
12/24/2004 (5:08 am)
Niiice....question thoughright now, my crossbow will do damage only on explosion...is it possible to have direct damage too? I have it set to 20 in my crossbow.cs...but it just bounces right off me
#14
12/06/2005 (3:13 pm)
Very good! Thank you so much! It would have takin' me months to find those places. (I'm still getting used to the engine set-up)
#16
This solves the arming time issue with non-ballistic projectiles as well!
04/01/2008 (7:52 am)
Thanks for this!This solves the arming time issue with non-ballistic projectiles as well!
Torque 3D Owner Tom Feni
my tennis ball gun is in need of some code like this..I wanted it to be explode on contact with players. and bounce around otherwise.. :)
so this will work wonders... :)
I will include you in the credits.. :)
thanks for the resource..