Sign in to follow this  
Sillencer

Question about a trigger system

Recommended Posts

Sillencer    171
Hi, I'm still trying to understand how game works, so I have a noob question, how to implement a trigger system in to a game ? Then an object enters an area, then an object get's hit, and so on...

Can someone explain a bit, or at least tell me about a book or something where I could find something about this...

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665
A trigger such as a door opening when the player walks past is simply a matter of collision detection to decide when the trigger has been activated, and some sort of script that is called to engage the doors open mechanism. That being said, simply is relative.

Share this post


Link to post
Share on other sites
ASnogarD    212
If you are using C++ and SDL you could have a look at these tutorials at
http://lazyfoo.net/index.php , it would help even if you dont use C++ and SDL just to see how collision detection works.

Share this post


Link to post
Share on other sites
Buckeye    10747
As mentioned above, the first step is to implement some sort of collision detection.

Most collision engines allow designating each object with a user-chosen "collision type" tag. For a door, for example, create a box around the door and set its collision type to (something like) OBJECT_EVENT, OBJECT_EVENT just being one of the user chosen enum's for collision object types.

Most collision engines also provide for a collision callback function. I.e., during the collision phase of the game (when all the collisions are being collected), a user function is called with information on what two objects are colliding, the position of the collision, etc.

Setup an event-callback function for each OBJECT_EVENT and push information about each collision onto a stack or add the info to a std::vector.

When the collision phase is complete, iterate through the event collisions and determine what actions to take. E.g., a collision between the door event box and a player - if the door is not open, open it, etc.

Share this post


Link to post
Share on other sites
Sillencer    171
I think one thing is collision detection, and another is a trigger... I don't think there must be a collision detection check, to see if the object enter an area... And if an object hits another, to trigger a trigger, it's not about collision detection...

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665
If you intend for an object to set off the trigger by doing something in the world, collision detection WILL factor in some how. Perhaps a more thorough explanation of what you are attempting to do would help.

Share this post


Link to post
Share on other sites
ASnogarD    212
As far as I know you can have a collision detection checking if x has hit y , or you can trigger events by time ( if time >= this time then do something ) , or by location ( if playerX == thislocation then do stuff ), or even by checking some variable status and doing some action if the variable equals a specified value.

Definitely need more info on your particular problem to provide any advice.

Share this post


Link to post
Share on other sites
This is a good application for a Bloom filter. It can reduce the necessary processing when there are thousands of triggers.
In a client-server setting, you could even offload this task to the client and only verify its validity on the server after the client reports a hit. Since Bloom filters regularly report false positives, there is not much potantial to exploit the "secret knowledge", either.

Share this post


Link to post
Share on other sites
Palidine    1315
Quote:
Original post by Sillencer
to see if the object enter an area...


That's the definition of collision detection: "is one thing inside another". Collision response is then dealing with moving things around that need to be moved around so they are no longer inside each other.

A trigger is simply an object that has collision detection but does not have collision response. That's why all collision detections systems: havok/PhysX/etc have built in triggers. They're basically trivial to add if you already have a collision detection system

-me

Share this post


Link to post
Share on other sites
Sillencer    171
Let's say there's a mage, and he casts a spell that does not have any animation, it just hits the target, don't see what collision detection has to do with it...

So, if i understand right, what you are supposing is just to use a built-in trigger system of an collision detection system ?? Did I understood it right ? Is this a good idea in practice ?

Share this post


Link to post
Share on other sites
fng    154
Quote:
Original post by Sillencer
Let's say there's a mage, and he casts a spell that does not have any animation, it just hits the target, don't see what collision detection has to do with it...

So, if i understand right, what you are supposing is just to use a built-in trigger system of an collision detection system ?? Did I understood it right ? Is this a good idea in practice ?


Yes it is good practice, because there is no other way to do it. How else do you detect that the spell hit something? It all boils down to have bounding shapes, e.g. a sphere for a spell and check the collision of that with the bounding sphere of the target.

The appearance or animation has nothing to do with the trigger itself. However you must detect when the spell hits the target. This is done by collision detection. I would even say most triggers get called when a collision occurs.

Share this post


Link to post
Share on other sites
Sillencer    171
Quote:
Original post by fng
Quote:
Original post by Sillencer
Let's say there's a mage, and he casts a spell that does not have any animation, it just hits the target, don't see what collision detection has to do with it...

So, if i understand right, what you are supposing is just to use a built-in trigger system of an collision detection system ?? Did I understood it right ? Is this a good idea in practice ?


Yes it is good practice, because there is no other way to do it. How else do you detect that the spell hit something? It all boils down to have bounding shapes, e.g. a sphere for a spell and check the collision of that with the bounding sphere of the target.

The appearance or animation has nothing to do with the trigger itself. However you must detect when the spell hits the target. This is done by collision detection. I would even say most triggers get called when a collision occurs.


It's not what I am trying to say... The spell doesn't have a bounding sphere, it just afflicts the target, and that's all... There's nothing to do with collision detection... And I want to check if a target hits another based on distance from one object to another (if the enemy is range, then hit it every "attack speed" seconds, that's all), not with collision detection, I'm saying that I want an trigger system that has nothing to do with collision detection...

Share this post


Link to post
Share on other sites
fng    154
Quote:
Original post by Sillencer
It's not what I am trying to say... The spell doesn't have a bounding sphere, it just afflicts the target, and that's all... There's nothing to do with collision detection... And I want to check if a target hits another based on distance from one object to another (if the enemy is range, then hit it every "attack speed" seconds, that's all), not with collision detection, I'm saying that I want an trigger system that has nothing to do with collision detection...


If I understand correctly, you have a spell at a position S_p = {S_p.x, S_p.y, S_p.z} (the '_p' should indicate, that it is the current position of the spell) and you want to have your trigger system call a function if a target T_p = {T_p.x, T_p.y, T_p.z} is in range of the spell (lets say call that range r). The mentioned function (e.g. target_hit(cur_time, object, spell_type)) should notify the target, that it was hit by a spell. Is that correct?

If not, could you explain a bit clearer what you have in mind?

If it was correct then a simple solution would be similar to this:
In each frame you have to check, whether the distance of T_p to S_p is smaller than r. This is equal to checking whether

.

If this is true you would have to call target_hit(cur_time, object, spell_type). Within the target_hit() function you would then account for the attack speed, and deal the damage.

This solution is very simple and the check has to be performed every frame. Please note, that the check is related to a bounding sphere collision test to a particle. In this example, the spell is a sphere with radius r and the particle has the position of the target.

Alternatively you could use collision detection to find out when the first contact of the spell with the target occurs, add the target to the list of objects in the spells range and in each frame perform the check as above only for the objects in the list. As soon as the expression above results to false for a given object, remove it from the list.

I hope this helps.

Share this post


Link to post
Share on other sites
Sillencer    171
tnx for the formula and for all the explanation :) I think i could use that :)

I was more about how to implement something like in this article: http://www.gamedev.net/reference/articles/article1410.asp , more about the structure, callbacks, I don't know really how to implement something like this :)

Anyway, tnx for all the help and information, I'm sure it will help me :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this