Randomly Generating Object3D's

Started by Albareth, January 15, 2004, 04:45:23 AM

Previous topic - Next topic

Albareth

Is it possible to randomly generate an Object3D, or use them in an array?  I need an infinitely possible amount of ships that can be created, each capable of being individual...  is this possible?

EgonOlsen

The amount of objects that may belong to a world is unlimited (in theory...in practise, memory usage and performance will limit it of course). However, letting the engine determine which objects are visible among 1,000s of them is not a good idea performance wise. Maybe it's possible for you to use a pre-processing stage based on the logic of your game to determine what may be visible and what isn't (divide the space into sectors or whatever...). With this information, you can either set some objects to visible/invisible as needed or remove them from the collection of objects if not needed and add them again if needed. The second solution may not be a good idea if it happens a lot, because removing them is not that cheap.
Note that you have to call build() only once on an object. It's not required to be called everytime you add it.
How much is "infinitely" in your case?

Albareth

Well, the style of the enemy creation is kind of like those 2D shooters, like galaga and galaxian.  I need to develop a system for randomly generating enemies that just move straight down.  It could be possible to have hundreds of enemies flying past the main ship...  I also need a system to destroy the enemy ships after they fly past the 480*1024 edge of the screen.  These ships are created in runtime and they are continously moving.  I have absolutely no clue on how to create this sort of system...

EgonOlsen

Creating them at runtime may not be a good idea as it is quite costly to call build() on an object. It may be an option if the objects are quite simple though. I think it's better to add them at the beginning/setup of the level, i.e. when you know that you'll never use more than 100 objects of type x and maybe 50 of type y. Just create the maximum amount of objects for each type (try to use cloneObject() for this as it will save you lots of memory if the object's meshes are all the same), build them and add them to the world (remove the formerly generated objects for the previous level) and maybe to a pool/manager that you have to write yourself to manage the state of the objects (the pool can as well do the creation of the objects...that's an even better way of doing it). If you need a new object of type x at position a/b, ask the manager for it. Similar to a connection pool for accessing a database. This demo http://www.jpct.net/demos does something similar in a very small scale with the moving bubbles.
However, i hope you don't want to do a collision detection between all these objects, because when using 100 objects at time, this will result in something like 100*99 collision detections per step, which is a bit heavy...

Hope this helps somehow.

Albareth

It helps somewhat...  I need to move all of these ships/objects foreward continuously and test to see if they hit the player character...

EgonOlsen

Testing the ships for collision with the player should be fine. The demo i mentioned above does the same between the bubbles and the alien. It displays approx. 100 bubbles.

Albareth

Thanks...  Well, I hope this works now...

Albareth

How are those bubbles initialized?  Are they created using an array?  If so, can you release the code for their creation?

EgonOlsen

Not sure if this will help you much, because the bubbles are managed in a reeeeaaalll simple way, but anyway:

for (int i=0; i<NUMBER_OF_BOXES; i++) {
        Object3D box=Primitives.getSphere(4,1);
        box.calcTextureWrapSpherical();
        box.setTexture("wood");
        box.setTransparency(0);
        box.setEnvmapped(Object3D.ENVMAP_ENABLED);
        box.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
        box.setOrigin(new SimpleVector(140,0,-117));
        setBox(box, i);
        box.translate((float)Math.random()*-140, 0, (float) Math.random()*117);
        theWorld.addObject(box);
        boxes[i]=box;
}


And the code for setBox():

private void setBox(Object3D box, int i) {
     float x=15f-30f*(float) Math.random();
     float y=20f-40f*(float) Math.random();
     float z=15f-30f*(float) Math.random();
     box.setTranslationMatrix(new Matrix());
     box.translate(new SimpleVector(x, y, z));
     float trXn=1.5f+2f*(float) Math.random();
     float trZn=1.1f+2f*(float) Math.random();
     trX[i]=trXn/2f;
     trZ[i]=trZn/2f;
     lifeTime[i]=0;
     box.rotateX(x);
     box.rotateY(y);
     box.rotateZ(z);
}


The bubbles are stored in the array named boxes (they were boxes in the first version... :wink: ) and their current translation in X and Z direction in trX and trZ. In addition, the lifetime of each bubble is stored in the lifeTime[]-array. But this is ugly code written to serve a single purpose. It would be better to wrap this information into an object (or maybe to extend Object3D, which sadly isn't possible in 0.97 but will be possible in 0.98 because i'll get rid of the useless final-stuff) and manage these objects by a simple Manager-class. If you don't want to write such a wrapper class and go with the array-approach, the Manager is still a good idea because it can hide this fact from the rest of the code.

Hope this helps.

Albareth

Thank Helge...

This should help somewhat...