Author Topic: Object3D.addChild();  (Read 7450 times)

Offline Bart

  • byte
  • *
  • Posts: 11
    • View Profile
    • http://home.deds.nl/~svg
Object3D.addChild();
« on: October 03, 2003, 10:01:39 pm »
Hello,

I have some code similar to this:

Code: [Select]
levelObj = Object3D.createDummyObj();
levelObj.addChild(Box1);
theWorld.addObject(levelObj);


This doesn't seem to work.

But if i add Box1 to the world. Everything works fine. Do you know what the problem is?
o java or not to java

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Object3D.addChild();
« Reply #1 on: October 04, 2003, 02:05:49 am »
Yes, a dummy object should never be added to the World. You can do it though, but it doesn't make much sense. The basic processing goes like this:

Iterate through all visible object in the world, calculate the object's transformation matrix by recursing through its parents, transform the object using the generated matrix, render the object.

If you don't add the box-object to the world, nothing will be rendered at all. The dummy object lends its transformations to the box-object. It doesn't cause the box object to be rendered. jPCT doesn't care if an object has childs, it just looks at the parent(s) for getting the transformation hierarchie. Maybe it's clearer if you use box1.addParent(levelObj) instead.

Dummy objects serve a single purpose: They lend their transformations to their childs. That's usefull for connecting objects transformation-wise. They are not something like nodes of a scenegraph, as jPCT isn't a fullblown scenegraph engine...it just takes some ideas from the scenegraph concept.

Hope this helps.

Edit: here's another thread dealing with object hierarchies: http://www.jpct.net/forum/viewtopic.php?t=53

Offline Bart

  • byte
  • *
  • Posts: 11
    • View Profile
    • http://home.deds.nl/~svg
Object3D.addChild();
« Reply #2 on: October 04, 2003, 11:09:30 am »
Thanks for your help. mergeObjects is the method i need (i want to add an octree). The following code throws this exception:

Exception occurred during event dispatching:
java.lang.StackOverflowError
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren
   at com/threed/jpct/OcTree.createChildren

Code: [Select]
public void loadLevel(String level)
{
int y = 0;
int z = 0;
boolean onPlatform = false;
levelObj = new Object3D(0);
try{
URL url = new URL(getCodeBase(),level);
InputStream inStream = url.openStream();
BufferedReader dataStream =new BufferedReader(new InputStreamReader(inStream));
String inLine = null;
while((inLine = dataStream.readLine()) != null)
{
if(!inLine.equals(";"))
{
int x = 0;
while(x < inLine.length())
{
if(inLine.charAt(x) == '#')
{
//This command adds a blue block
Object3D Box1 = block.cloneObject();
if(!onPlatform)
Box1.translate(x*4,y*4,z*4);
else
Box1.translate(x*4,(y*4)-0.5f,z*4);
levelObj = Object3D.mergeObjects(levelObj,Box1); //add the block to the level
}
else
if(inLine.charAt(x) == 'R')
{
//This command moves the red sphere
sphere1.translate(x+5,y-1.5f,z-2); //move the sphere
camera.setPosition(x+5,y-1.5f,z-10); //move the camera
}
x++;
}
z++;
}
else
{
//go to the next platform or to the 2nd layer of the current platform
if(onPlatform)
onPlatform = false;
else
onPlatform = true;
z = 0;
if(!onPlatform)
y--;
}
}
}
catch(Exception e){System.out.println("Exception while loading the level: " + e);}
levelObj.createTriangleStrips(2);
OcTree tree = new OcTree(levelObj,12,OcTree.MODE_OPTIMIZED);
levelObj.setOcTree(tree);
theWorld.addObject(levelObj);
theWorld.buildAllObjects();
}


I don't know what is wrong.
o java or not to java

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Object3D.addChild();
« Reply #3 on: October 04, 2003, 12:25:17 pm »
You are trying to create the octree using a maxPoly-value of 12. The OcTree is build using recursion and if the recursion gets too deep, the stack will overflow. This is what happens to you here. I suggest that you use a maxPoly-value larger than 12 (try 100 for example). 12 seems to be very low even if the VM wouldn't puke on it. An OcTree with too few polygons in each leaf isn't efficient any more. Take the fps-demo as an example: It's using a value of 100. If i'm using 10, the tree gets so big, that traversing it every frame costs more time than it saves and the frames/sec are going down by around 20%.
Play around with maxPoly to find a good (and working  :wink: ) value. I think i'll add a "break if recursion gets too deep"-option into jPCT to avoid this problem in the future...i planned to do this anyway.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Object3D.addChild();
« Reply #4 on: October 04, 2003, 12:55:25 pm »
After thinking some more about it and doing some tests, i discovered that there could be another problem: Imagine a maxPoly of 10 and 11 polygons that are exactly the same (same size, same orientation, same transformation, i.e. lying onto each other)...in that case, the spacial subdivision will subdivide and subdivide to get the polycount of the node down to 10 or lower but that will never happen (because jPCT doesn't split polygons to fit into the octree). This is not the only situation where this may happen (but it's simple to understand using this example), so maybe this is what happens to you.
I'm not quite sure what to do against this, i.e. how to detect this case. In your case, i would still say: play around with maxPoly...it should fix the problem. I'll also add a maxDepth to the constructor for the next version that takes care of the recursion not going too deep in these cases...but i'll also try if it's possible to avoid the problem at all.

Offline Bart

  • byte
  • *
  • Posts: 11
    • View Profile
    • http://home.deds.nl/~svg
Object3D.addChild();
« Reply #5 on: October 04, 2003, 04:26:48 pm »
I've changed the code and everything works fine now :D . Thanks for your help.
o java or not to java