Game Development Community

dev|Pro Game Development Curriculum

Largest object position inheritance for Prefabs

by Konrad Kiss · 11/29/2010 (2:30 pm) · 12 comments


Our game places prefabs procedurally (say that fast!) and we often use prefabs as a way to create several instances of one object. Placing prefabs on the fly was giving us unnecessary headaches which this resource addresses.

This one object is usually the largest object which already has a place in the world. Such as a room or a building. The engine uses the center of the selection to position the prefab while it is probably more efficient to use the position of the largest object - which lets you position the object without having to poke into the prefab and extract its offset.

In gui/worldEditor/worldEditor.cpp change everything marked by the // >>> and // <<< comments.
// SimGroup we collect prefab objects into.
   SimGroup *group = new SimGroup();
   group->registerObject();

   // >>>
   // a small change to make prefabs use the center of the object with the largest bounding box
   SimObject *obj = NULL;
   SceneObject *sObj = NULL;

   Box3F largestBounds = Box3F::Zero;
   SceneObject* largest = NULL;
   for ( S32 i = 0; i < found.size(); i++ )
   {      
      obj = found[i];
      sObj = dynamic_cast< SceneObject* >( obj );
      if ( sObj )
      {
         if (largestBounds.getExtents().len() < sObj->getWorldBox().getExtents().len()) {
            largestBounds = sObj->getWorldBox();
            largest = sObj;
         }
      }
   }

   // Transform from World to Prefab space.
   MatrixF fabMat(true);
   fabMat.setPosition( largest ? largest->getPosition() : mSelected->getCentroid() );
   fabMat.inverse();

   MatrixF objMat;
   // <<<

   for ( S32 i = 0; i < found.size(); i++ )
   {

That's all there is to it. Next up - sometime next week I think - some optimized C++ AI pathfinding code based on Gabriel Notman's excellent resource. I just need some time to extract it from our game. ;)

@konradkiss
KonradKiss @ GitHub
konradkiss.com

#1
11/29/2010 (5:16 pm)
Nice solution. Didn't occur to me to use the largest object as the centre... I had been defining a central object and over complicating things -- thanks Konrad!
#2
11/29/2010 (7:40 pm)
Thanks Michael. I hope you find it useful. I haven't seen that many people using prefabs, but replacing existing models with prefabs in a level now at least won't require people to reposition all the prefabs.

Edit: that is if they are just manually edited from ie TSStatics.
#3
11/30/2010 (7:38 am)
Cool stuff Konrad, and looking forward to that pathfinding resource :)
#4
11/30/2010 (9:26 am)
Nice ! How do i procedurally place buildings etc. in my mission? Using triggers?
#5
11/30/2010 (4:54 pm)
@Gareth: Thanks! Yeah, I'm on it, just lots of things to shove aside first. :)

@Daniel: Well, that's up to you.. it could be as simple as
for (%i=0;%i<3;%i++) {
   %pos = %i*10 SPC 0 SPC 100;
   %mynewobject = new TSStatic() {
      shapeFile = "art/myshape.dts";
      position = %pos;
   };
}
... or something like that.

#6
11/30/2010 (4:55 pm)
...continued...

Procedural placement is the process during which the program itself decides what should go where. Manual placement on the other hand happens either at design time (via an editor) or within the game (as in placing a building in an RTS - which is more like what you meant I think).

I'm not sure though how you would want to use triggers to place buildings unless you meant culling shapes that are far away to the player, or culling stuff inside/outside a building.
#7
12/01/2010 (6:39 am)
ty Konrad
#8
12/01/2010 (7:09 am)
Nice resource, and I will be very pleased to see the next one!
#9
12/01/2010 (10:11 am)
Very nice!
This code can be combined with the vehicle blocker if you wish to restrict the generation in some areas.
#10
12/02/2010 (1:21 pm)
@Konrad, I think it would be really slick to see it combined with a perfect maze generator where the pieces were programatically placed and always resulted in a traversable maze with only one solution from beginning to end.

I did this one in TGB and you could potentially swap out where I set the tiles with your code to place your objects - or use the objects from your dungeon creator... hmmm....
#11
12/02/2010 (2:00 pm)
Nice stuff as always :-)
#12
12/15/2010 (1:09 pm)
i actually looked into a 3d perfect maze generator a few years ago for TGE, at that time the programming and even the concepts were giving me trouble, maybe i should see if i can find my old code and try again :)