PDA

View Full Version : How to know when collisions are NOT taking place



seiferalmasy
26-12-2007, 03:25 AM
Simple in delphix to work out when a sprite is colliding:

Simply> collision; in the domove and it will fire when a collision occurs.

But what if I need to know when a collision is not taking place at any 1 time.

See the problem I have, is I am collecting jewels. Now, one jewel in particular needs to be hit 3 times before it turns into diamond

When I hit once, it increments the counter of times it has been hit. But, once it has been hit I need to set the boolean to false so it may not be hit again.

I would like to then set back to true second it realises the collision is no longer occuring:

For example: Approaching jewel>>>>collision detected>>docollision is used>>>1 is added to the counter>>>boolean is set to false to stop further collisions>>collision with jewel is over>>>boolean is set back to true awaiting next collision.

Perhaps I am doing this a long way?

Any ideas?

arthurprs
26-12-2007, 04:14 AM
There is a way to detect if the colision is ocurring outside the docollision ?

seiferalmasy
26-12-2007, 04:19 AM
well, since dephix fires only when it detects a collision, presumably it knows when one is not? :P

arthurprs
26-12-2007, 02:58 PM
well, since dephix fires only when it detects a collision, presumably it knows when one is not? :P

my idea :

from the main loop keep checking the collisions
if the bool is true then don't "docollision" if false "docollision" and set the bool to true

chronozphere
26-12-2007, 04:54 PM
You must do something like this:



//---------------
// game loop code
//----------------
var
PrevIsColliding, IsColliding: boolean;
HitCount: integer;
begin
//save previous collision state
PrevIsColliding := IsColliding;

//somewhere in main game loop
IsColliding := false;

//check the collision for each sprite, so that IsColliding might change to true
checkCollision;

//compare old collision state with new one
if PrevIsColliding <> IsColliding then
if IsColliding then Inc(HitCount);
end;

procedure TMySprite.DoCollision( blablabla )
begin
isColliding := true;
end;


I hope you understand this code. If not, i will explain it now.

You must keep track of the Collision state with the "IsCollision" variabele. You must also save the collision state from the last game loop execution. When "PrevIsCollision" and "IsCollision" are not equal, something has happened. e.g The collision has started, or the collision has stopped.

You only have to check, which one of the two things happened. When IsCollision is true, PrevIsCollision should be false, which means that the collision has just begun, because during the last game loop, no collision was detected (PrevIsCollision = false).

Hope you understand this.

Good luck ;)

seiferalmasy
27-12-2007, 12:17 PM
I understand;) thanks!