Game Development Community

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?

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.

#1
08/03/2009 (9:04 pm)
I'm not sure that would work. If you spawn and then set the position before checking isColliding, you're not going to get the onCollision in the middle of that. They happen one at a time. You can cast a collision, which is probably what you want to do. See castCollision or pickRadius or something like that. You could also call spawn from onCollision if you disable spawning once it's been done properly once.

Hope that helps :)
#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
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.