Game Development Community

Having issues with onCollision

by Thomas Shaw · in Torque Game Engine · 06/14/2005 (10:24 pm) · 17 replies

What I'm trying to do: Have a plant that is in my level react with an animation when you walk into its bounding box (which I have made larger for this example)


Here is my code:

datablock StaticShapeData( Plant )
{

category = "Plant";


shapeFile = "~/data/shapes/props/plant3.dts";

};


function Plant::onCollision(%this,%obj,%col)
{
%obj.playThread(0,"dance");
}


If I go into the console during the mission and just type in "oject's number.playThread(0,"dance");" it works just fine. But when I walk up to the plant he doesn't do anything. What's wrong?

#1
06/15/2005 (12:20 am)
Ok, nevermind, I got it to play, but I can't STOP it from playing. Once "dance" starts playing after I have collided with the object, it doesn't stop when I leave the bounding box. I want it to only play WHILE im in the box. Here is the new code for review.

function Plant::onCollision( %this, %obj, %col )
{
    echo( "Plant::onCollision called ----------------------------------" );
if (%col.getType() & $TypeMasks::PlayerObjectType) 
{
	%obj.playThread(0,"dance");
}
else
{
	%obj.stopThread(0);
}
	// TO DO: Add code here to have the player react to touching the power-up!
}
#2
06/15/2005 (6:29 am)
The problem is that if your in the box it will keep calling the onCollision Function thus the animation keeps playing. Maybe you want to move the player away from the collision point then play the animation.
#3
06/15/2005 (11:16 am)
"The problem is that if your in the box it will keep calling the onCollision Function thus the animation keeps playing."

Read my post, that is what I want it to do. I want the animation to ONLY play while I'm in the box, and then STOP playing when I move away from the box. Right nwo when I move away from the plant and stare at it from a distance, it keeps animating.
#4
06/15/2005 (11:34 am)
You could place a trigger around the plant. Animate the plant when you hit hte trigger and stop it when you leave hte trigger... Triggers have an onLeave() or somethign like that as well as an onEnter() and a afterXTime().....
#5
06/15/2005 (12:10 pm)
Yes, a trigger would be better, because I've run into similiar problems. There's an onCollision function, but nothing gets called when the collision has 'ended'.
#6
06/15/2005 (12:34 pm)
Wow sorry my reading comprehension must be degrading as I get older. :( I agree with the trigger suggestion then you can stopl the animation on the leaveTrigger function.
#7
06/15/2005 (1:24 pm)
Can anyone point me in the right direction for a trigger solution?
#8
06/15/2005 (8:18 pm)
This has a good example of using triggers. Basically you need to play the animation in onEnterTrigger and stop it in onLeaveTrigger - then, if it has stopped when you check it in onTickTrigger, play it again.
#9
06/16/2005 (12:23 am)
@Chris, how do I reference that PARTICULAR instance of the object (the one that is in the trigger area). What I mean is, I have a lot of this plant in the level, but I only want the animation to play for the plant that the player has entered the trigger for.

I mean I understand how triggers work, and I understand how a Staticshape works, but I don't understand how I can use them together if I am using %obj for everything. Somehow a tigger and a plant need to be linked, so that the trigger animates the plant inside of it when a player walks in.
#10
06/16/2005 (7:31 am)
@Thomas do you need it to animate all the time , can you use so it animate amount of time only ,
then you can use a schedule to stop it,and when you collide again it starts animate again ?.
#11
06/16/2005 (10:10 am)
@Thomas - Multiple ways.. The way I would do it is to give the trigger a dynamic property (F11 -> F3) called instanceNum

then give each plant a similair tihng...

For the trigger... Simply do a radius search for anyhting with the same instanceNum and animate it... Do the same on leaveTrigger...

Mind you that will really suck if you have a large number of objects....

If you don't know how to d oa container radius search take a look at the AIGuard resource. It does a radiussearch in it's loadEntities function for AIMarkers. You could easily adapt it for your plants.
#12
06/16/2005 (1:38 pm)
@Billy I want it to animate while the player is near it, it has a really cool animation where it gets defensive and makes noises. I want it to stop doing that only when the player gets out of its box, and the plant will be calm again.

@Chris I will try looking at the AIGaurd resorce, I have never tried a radius search, though I have heard of them.

I'm still open to ideas everyone, just to have a backup plan in case I can't implement Chris' idea.

Update: I have decided that mimicing the AIGaurd resource is not the avenue I want to explore because I am going to have many similar objects in my level, and the amount of time it will take to rig all of these is too much. Plus, it will just be very slow.

So, I need to have a new idea brought to the table. Read all of the posts I have made so you can understand exactly what I want to do.
#13
06/17/2005 (4:07 am)
@Thomas - I didn't say anything about mimicing the AIGuard resource. just that it had an exellent example of how to do a containerradiussearch.

How many objects do you have? Setting up a trigger that will work for them is not difficult and will take you maybe 4 hours (remember to get the triggers placed all you have to do is get one sized right and paste it around all the others), which isn't a very long time in the game dev world. I mean, I did something similair with zoning off areas of my game. It took me about 2 hours. Yes, it's a lot of work... but game dev is work.

As for it being very slow. What would give you that idea?
#14
06/17/2005 (11:07 am)
Wouldn't it give me a lot of slowdown if I had many of these plants in the level at the sametime with the same setup? The designer hasn't given me an exact number yet, but I know its going to be quite a few.

I guess I'm just having a hard time visualizing exactly what is going to be done here. This could be an excellent thing to add to the site as a resource (hint hint).
#15
06/17/2005 (11:36 am)
Semi hack solution from Billy :)
Try this
First the trigger
datablock TriggerData(AnimateTrigger) 
{    
	tickPeriodMS = 100;
 };

function AnimateTrigger::onEnterTrigger(%this, %trigger, %Obj) 
{ 
      $trId = %trigger.getId();
      Parent::onEnterTrigger(%this,%trigger,%obj);
           
}

 function AnimateTrigger::onLeaveTrigger(%this, %trigger, %Obj)
 {
    Parent::onLeaveTrigger(%this,%trigger,%obj);
      
 }

 function AnimateTrigger::onTickTrigger(%this, %trigger, %obj)
 {
    
 }

In your plant script add
function Plant::onTrigger(%this, %obj, %state)
{
   // this == datablock being used
   // obj  == instance of the trigger
   // state == triggered (1) or untriggered (0)
   
   %objId = $trId.slave.getId();
   
    if(%state == 1)
	 {
       %objId.playThread(0,"dance");
       
 	 }
     else if(%state == 0)
        %this.schedule(1000,"stopshake",%objId);
}


function Plant::stopshake(%this,%objId)
{
  %objId.stopThread(0); 
 
}
add a name on the plant in the editor.
add a dynamicfield on the trigger called slave and the value should be the name on the plant.
Hack finished :)
#16
07/02/2005 (3:17 pm)
Add a dynamic field to the trigger? This is sort of new to me, could you show me where this goes with the example name "plantname"? So this can only be used for one plant? I have to make a new trigger for each of them? That is how I understand it.
#17
07/02/2005 (3:33 pm)
Yes its made for one plant, and you need to set a trigger on every plant.
In the editor if you select the trigger you have a field called Dynamic Fields to the right if you expand All ,you have a button there called add , press this one.
When you press add you get a popup window called dynamic field.
So Name should be Slave and the Value should be the name you put in the plants namefield .

Puh this is not easy for me :)
Im really bad at explaining things.