Game Development Community

Preventing random respawn from pushing objects around?

by Jon Mitchell · in Torque Game Builder · 02/23/2007 (1:16 pm) · 9 replies

Hello all,
I am currently using the basic respawn function from the shooter tutorial and I have a problem/question
{
    %this.isDead = false;
    %this.setPositionY(getRandom(%this.minY, %this.maxY));
    %this.setPositionX(getRandom(%this.minX, %this.maxX));
    %this.setEnabled(true);
    P2turret.setEnabled(true);
}

Respawning works fine, I have set the max and min X and Y so the respawn would take place within the world object limit, however from time to time one player will respawn too close to the second player or a terrain object , both objects have collision properties to they can not pass through each other, the result of the respawn is that the object or other player will get pushed to the side.
Is there a way I can perform a check to see if something already occupies that space or have it not push objects upon respawn?

#1
02/23/2007 (1:31 pm)
Try the "pick" functions like pickRect or PickRadius from your t2dSceneGraph object. They give you a list of objects in the particular area.
#2
02/23/2007 (6:03 pm)
So maybe start the function with the get random location, then a pick radius and if there is anything at all in the radius have it go back to the top of the function and try again?

So since I still have no idea how to use pick radius really, this is what I came up with after poking around on a few other threads, I do know it is not right as I get a parse error from the pick radius line.

//layer 3 is terrain and layer 5 would be player 1 layer 6 is player 2 

function player1Tank::spawn(%this);
{
    %this.setPositionY(getRandom(%this.minY, %this.maxY));
    %this.setPositionX(getRandom(%this.minX, %this.maxX));
    %objectList = t2dSceneGraph.pickRadius( %this.getPosition(), 1, (BIT(3), BIT(5)) );
    %objectCount = getWordCount(%objectList); 
    for(%i = 0; %i < %objectCount; %i++)
		if (%i > 0 ) {
			echo ("too close!");
		spawn(%this);
		} else {
   %this.isDead = false;
   %this.setEnabled(true);
   P2turret.setEnabled(true);
   	}
}
#3
02/23/2007 (6:33 pm)
I just noticed if I remove the bits for the mask I get an error in the console
Unable to find object: 't2dSceneGraph' attempting to call function 'pickRadius'
#4
02/23/2007 (6:48 pm)
You need to use the method on an actual instance of a t2dSceneGraph object. Try:

%this.scenegraph.pickRadius(...)

or:

sceneWindow2D.getSceneGraph().pickRadius(...)
#5
02/23/2007 (7:58 pm)
Thanks Dan, %this.scenegraph worked perfectly! :-)
Would you happen to know the correct way to set the layers (bit)?

I fixed the bit problem, it should have been BIT(3)|BIT(6) not how I had it, however it always returns 0, even if I make the radius bigger it always returns zero.
#6
02/23/2007 (9:03 pm)
@Jon - I'm not sure what you mean by "If I have no bits so by default it should detect everything". I think that if the bit is clear it is excluded and if it's set, it's included. Try using 0xFFFFFFFF (i.e. all 32 bits set) for both layer mask and group mask in your call with the giant radius, and see if you start getting things.
#7
02/26/2007 (8:10 am)
Ehh, sorry I really worded that badly. :-p
I said bit because setting which layer mask to do the pick radius from it set using BIT(layer mask number)

Following the documentation in TDN I set the two layer masks I wanted to look for in pick radius which was 3 and 6. According to what I have found it should return results from those two groups only when something is inside the radius. I started echoing the output of the pick radius after each respawn. What I found was with no layer mask set it would return everything within the radius, something between 1 and 4 objects usually, the ground which I wanted to ignore being one of the layers being detected. I have two types of objects residing in layer 3 and 6 so I tried adding the layer bit.

basic pick radius with a range of 10 returns everything inside radius
%this.sceneGraph.pickRadius( %this.getPosition(), 10 );

Looking only for objects in layer 3 and 6 but always returns nothing
%this.sceneGraph.pickRadius( %this.getPosition(), 10, BIT(3)|BIT(6) );
#8
02/26/2007 (8:58 am)
The first parameter after the radius is the group mask, not the layer mask. Try:

%this.sceneGraph.pickRadius( %this.getPosition(), 10, 0xFFFFFFFF, BIT(3)|BIT(6) );
#9
02/26/2007 (9:08 am)
Ha ha ha, well that would be my problem, I though that was the layer mask! :-p
That works perfectly Dan, thank you for the help.