Previous Blog Next Blog
Prev/Next Blog
by date

Experimenting with weapon collision and using mounted objects

Experimenting with weapon collision and using mounted objects
Name:Manjit Bedi 
Date Posted:Apr 21, 2007
Rating:Not Rated
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Manjit Bedi

Blog post
I have finally got something work after pondering it for some time over the last few days.

The main character uses a weapon to 'swat' the bad guys in the game. In this particular scenario, the player is armed with a shovel. I was trying to think of how to imeplement it.

What I did was:

1) create a weapon object that is just using a transparent bitmap and mounted the object to the player
on a link point.
2) the weapon object script sets the collision states to false when the level is loaded
3) set the frame callback to true for the animation of the player when he is swinging the shovel down
4) the frame callback rountine turns on the weapon collision on a frame and then a few frames later
turns the collision off. The result the collision only is happening when the player is in the middle of the animation wih the frame down.


function player::onFrameChange(%this, %frameIndex)
{
if(%frameIndex == 2)
{
echo ("shovel blade collision on");
$weapon.setCollisionActive(true, true);
}
else if(%frameIndex == 4)
{
echo ("shovel blade collision off");
$weapon.setCollisionActive(false, false);
}
}

Recent Blog Posts
List:09/15/07 - Update on the game project
06/13/07 - Making an arcade style high score name entry screen
05/23/07 - Next steps - getting TGB into an arcade cabinet
04/21/07 - Experimenting with weapon collision and using mounted objects

Submit ResourceSubmit your own resources!

Jameson Bennett   (Apr 21, 2007 at 13:11 GMT)
Consider using triggers in your animation, this would allow the artists more flexibility without bothering the coders for each minute change in the animation sequence and can be extended for use with other characters (see how the orc's footstep sounds are triggered for an example).

J Sears   (Apr 21, 2007 at 16:23 GMT)
I've been trying to figure out a good melee system for my project without using the fake ammunition that most of the melee resources on here use, looks like your off to a pretty solid start there.

Andy Hawkins   (Apr 22, 2007 at 15:46 GMT)
I use this code for the sword melee in the Skeleton Pack. It sets up a schedule to coincide with the sword coming to the front of the swing. At this point is does a short ray check (length of weapon) to see if collided with something. Maybe this information is useful for what you want to do.

function BastardSword_Image::OnFire(%this, %obj, %slot)
{
%player = %obj.client.player;
%client = $pref::Player::ServerID;
if (%slot == 0)
{
%player.playThread(1,"light_recoil");
%this.PlaySchedule = schedule( 1000, 0, stopplay, %this, %player);
// check collision when sword is at front
%this.CheckFire = schedule( 400, 0, DidSwordHit, %this, %player);
}

if (%slot == 1)
{
%player.playThread(1,"slash2");
%this.PlaySchedule = schedule( 1000, 0, stopplay, %this, %player);
// check collision when sword is at front
%this.CheckFire = schedule( 400, 0, DidSwordHit, %this, %player);
}



// generate a swish sound
%knifesound = new AudioEmitter()
{
position = %player.position;
fileName = "~/data/shapes/weapons/bastard_sword/swordswish.ogg";
description = AudioClose3d; //for loud sound
preload = false;
};
%this.killit = %this.schedule(1000,"KillSwishSound",%knifesound);



}

function DidSwordHit(%this,%player)
{
cancel(%this.CheckFire);

// ~~~~ chose vector from player with short range to see if hit ~~~~~~~
%eye = %player.getEyeVector();
%vec = vectorScale(%eye, 2); // distance we can hit from
%start = %player.getEyeTransform();
%end = VectorAdd(%start,%vec);
%found = ContainerRayCast (%start, %end, $TypeMasks::PlayerObjectType, %player);

// ~~~~~ if we got a target then damage it and create a hit sound ~~~~~~~~~~~~~
if (%found>0)
{
// ~~~~~~ hit sound emitter create ~~~~~~~~~~~
%hitsound = new AudioEmitter()
{
position = %player.position;
fileName = "~/data/shapes/weapons/bastard_sword/metalthud.ogg";
description = AudioClose3d; //for loud sound
preload = false;
};
%this.killit = %this.schedule(2000,"KillSwishSound",%hitsound);

%mypos = %player.getposition();
%transform = %found.getTransform();
%posX = getWord(%transform, 0);
%posY = getWord(%transform, 1);
%posZ = getWord(%transform, 2);
%posX = %posX;
%posY = %posY;
%posZ = %posZ + 1.8; // head
%posZ2 = %posZ - 0.2; // torso
%position = %posX SPC %posY SPC %posZ SPC 0 SPC 0 SPC 0 SPC 1;
%position2 = %posX SPC %posY SPC %posZ2 SPC 0 SPC 0 SPC 0 SPC 1;

//~~~~~ create blood splats from hit ~~~~~~~~~~~~
%this.blood1 = %this.schedule(250,"GeneratateBloodFX",%position);
%this.blood2 = %this.schedule(350,"GeneratateBloodFX",%position2);
%theirpos = %found.getposition();
%vector = VectorSub(%theirpos, %mypos);
%vector = VectorNormalize(%vector);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Damage 20 on Bastard Sword. min 5 max 25 base from BastardSword_Image datablock
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%lowerdamage = %this.damage - 5;
%upperdamage = %this.damage + 5;
%hitdamage = getRandom(%lowerdamage,%upperdamage); // random 15 - 25

//~~~~~ APPLY DAMAGE ~~~~~~~~~~~~~~~~~~~~~

%found.getDataBlock().damage(%found, %obj, %vector, %hitdamage, "Melee");
}
}

//~~~~ stop animation after one swing ~~~~~~~
function stopplay(%this,%player)
{
%player.stopThread(1);
}


//~~~~~ kill hit sound ~~~~~~~~~~~~
function BastardSword_Image::KillSwishSound(%this, %soundtokill)
{
%soundtokill.delete();
}


Manjit Bedi   (May 23, 2007 at 13:00 GMT)
Hey thanks for the feedback everyone - much appreciated.

Manjit

Manjit Bedi   (May 23, 2007 at 13:36 GMT)
Having been busy working away with TGB over the last month and learning a lot more about TGB, I realize that I could have just used a scene object for the weapon as opposed to a static sprite with a tranparent bitmap.

I have only just started using triggers a bit - I think I can see how they work in this context.

Hmm. All good.

You must be a member and be logged in to either append comments or rate this resource.