Game Development Community

Test for collision outside of callback

by Michael Cordner · in Torque Game Builder · 07/18/2005 (8:16 am) · 6 replies

Is there any quick way of querying to find out whether 2 objects are in a collision state without relying on the callback?

In my specific case, I have a character walking along the screen past several hotspots. When the player hits 'fire' (or equivalent), the character performs a certain function depending on the hotspot that he is in front of. That is to say, I don't want to perform anything when the initial collision is detected, only when and if a keypress is detected.

I've tried to rig up a system whereby the onCollision() callback sets a state variable to the effect of $isOverHotspotX=true' but it's extremely inelegant. This is also less than ideal because there doesn't seem to be a way to detect when the player has gone outside the collision zone in order to set the variable back to false.

Alternatively I could just rig up each hotspot with a minX and maxX type variable and just use that in relation to the player's position, but it seems a bit of a shame given the all of the lovely collision detection in T2D.

(While we're here, why does the script not allow you to specify a callback function rather than forcing you to use some single function with a messy case statement? like callbackfunction=MyFunction(). Or am I missing something?)

2 Questions, sorry :)

#1
07/18/2005 (8:22 am)
A callback when collision has ended between objects would be usefull :)
#2
07/18/2005 (8:34 am)
Woot! My first public complaint about missing features! Do I graduate from noob?
#3
07/20/2005 (5:29 am)
A little confused here. All collisions are solved instantly (at least in the latest build). Any object can collide with multiple objects per scene to complicate matters. This means that collisions don't occur continously unless you disable a collision response. If you want to test for a collision, you could call "castCollision()" and use this for test for non-response conditions such as your hot-spots.

- Melv.
#4
07/20/2005 (5:44 am)
In my code anyhow, (in which I may admittedly be doing something wrong,) the collision callback seems to be getting called continuously (i.e. every frame) when two objects (the only two objects on screen) collide, and stops getting called when the objects are no longer colliding. Is this not how it should be?

castCollision() looks like it's about to reduce some awkwardness of my code by about
%150. I don't know why it didn't register with me as being a potential solution, I spent enough time staring at the reference doc.

Thanks!
#5
07/20/2005 (8:22 am)
@Michael: Don't feel bad, lol I've used Cast Collision and forgot about it... an invaluable function Melv put in there.
#6
07/20/2005 (9:41 am)
@Michael: I guess the details here are in your code. As I say though; if you're not reacting to hitting these hotspots e.g. you're using "setCollisionPhysics(false,false);" or some variant then T2D won't resolve the collision e.g. it will continue until the objects move out of each others way. If this is the case then I can see how having something like "OnNoCollision()" could help but it's something that could be abused.

A callback I could provide fairly easily would be one if the object in question didn't collide with anything during this frame. It would be much harder and involve much more overhead if I were to track the finishing of a collision between particular objects.

"castCollision()" is handy but only under the circumstance of detecting a single collision. The advantage it has over picking is that it sweeps the objects collision-polygon through space and is therefore as accurate as the stock collision methods whereas pick uses a sweep of the primitves such as point, line or ellipse.

The best way to implement this would be to extend the "setCollisionCallback()" method to include an extra flag to indicate a callback when the object doesn't have a collision during the frame. These callbacks could get very expensive though if abused. I'll add it to my list to have a think about.

There's also a new feature in the up-and-coming release where you can turn on a periodic timer for any T2D object using "setTimerOn()". You could use this to periodically reset some local-state of the object in question and set the state in any collision callback. FYI, here's the new function...
setTimerOn(timePeriod)
Starts the periodic timer for this object
(timePeriod  Period to produce callback.  This will continue until "setTimerOff()" is called.)
Example Usage: [i]"%Obj.setTimerOn( 1500 )"[/i]
NOTE:- Times are in milliseconds (1000 = 1 sec) as used in the "schedule" function.

- Melv.