Spawning objects that are not on top of each other
by tODD k. · in Torque Game Builder · 08/03/2009 (6:12 pm) · 3 replies
I am trying to place objects on my level randomly but I don't want them laying on top of each other. So, after spawning I'm checking if they are colliding - if they are I want to re-randomize their position. Is that a good way of approaching this? Anyway here is the code I can't get to work! Any ideas?
Thanks.
function objectClass::onLevelLoaded(%this, %scenegraph)
{
%this.isColliding = 0;
%this.spawn();
}
function objectClass::onCollision(%this, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
%this.isColliding = 1;
}
function objectClass::spawn(%this)
{
//set initial position
%this.setPosition(getRandom(-37.5, 37.5), getRandom(-27.5, 27.5));
if (%this.isColliding == 1)
{
echo("respawning");
//send back to top of function?
%this.spawn();
}
}Thanks.
About the author
#2
08/04/2009 (9:50 am)
I might be missing something but can't you just do this? I haven't tested the code below but I don't see any reason why it wouldn't work.function objectClass::onLevelLoaded(%this, %scenegraph)
{
%this.spawn();
}
function objectClass::onCollision(%this, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
%this.spawn();
}
function objectClass::spawn(%this)
{
//set initial position
%this.setPosition(getRandom(-37.5, 37.5), getRandom(-27.5, 27.5));
}
#3
Nate - thanks & unfortunately I have other things happening when the player collides with this object. These are sort of the "power pellets" I want to set in the level before it starts. But maybe I can use your idea and just specify if there's a specific collision? If it's colliding with itself do this and if it's colliding with the player do that.
08/04/2009 (10:28 am)
Tom - thanks I'll look into those functions tonight.Nate - thanks & unfortunately I have other things happening when the player collides with this object. These are sort of the "power pellets" I want to set in the level before it starts. But maybe I can use your idea and just specify if there's a specific collision? If it's colliding with itself do this and if it's colliding with the player do that.
Associate Tom Eastman (Eastbeast314)
Hope that helps :)