Exact physic representation of aTriMesh

Hi,



if I have a TriMesh and want to have exact physics howcan I do this?

I tried to create a boudningbox and deformit using computefrombatchesbut it does not affect the result in any way.

Is there a easy and fast way to do this which can be done during game?



BoundingBox box=new BoundingBox();

        ArrayList<TriangleBatch> bachties = new ArrayList<TriangleBatch>();

        for (int i=0;i<(((TriMesh) ((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0))).getBatchCount();i++)

        bachties.add(((TriMesh) ((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0)).getBatch(i));

       

        box.computeFromBatches(bachties);

        box.

        ghostNode.setModelBound(box);

        //dynNode[11].setModelBound(box);

        dynNode[11].updateModelBound();



I have loaded a jME file and got the trianglebatches.

Am I on the totally wrong way or is it maybe not possible to do this for more then 40 models effectivly?


Sue said:

Am I on the totally wrong way?

Yes :)

Simply create a physics node, call createMesh, and call copyFrom on the resulting PhysicsMesh.
Or you can call generateGeometry( true ) to generate geometries for a whole node with trimesh accuracy.

If you set useTrimesh = true in TestBasicRepresentation you can see trimesh collision in action.

PS. Beware: trimeshes are quite slow, obviously, as they can be really complex. Additionally ODE is not that stable with using dynamic trimeshes.

When I use the genGeometry(boolean) it really slows down, but when i use the code below everything starts flying around!

Today is not my brightest day so can you please enlighten me?

Do I have to use another dynamicNode as Child of dynNode[11] and add to this one the physicsMesh?



PhysicsMesh bound=dynNode[11].createMesh("ghostbound");

        bound.copyFrom(((TriMesh) ((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0)));

        ghostNode.setLocalScale(0.30f);

    staticNode.attachChild(dynNode[11]);

dynNode[11].generatePhysicsGeometry();

        dynNode[11].computeMass();

  1. You should never attach physics nodes to each other (if detected this results in an exception). Physics nodes (static and/or dynamic) should always be siblings regarding scenegraph hierarchy.
  2. Don't create a physics mesh yourself and call generateGeometry. You should either create the physics collision geometries yourself or call generate geometries.
  3. Scale your mesh/node before copying the mesh data - see javadoc  (btw I'm not sure scaling is working with trimesh at all, try without scale first)

Ok, but ih the tutorial you are attaching different physicNodes to each other like a dyn Node to a static one and a dynamic to another one togetthis strange ball with buckle.

If I do it this way and not use the trianglecollision it is even slower and somehowthe model does not match anymore the physics representation.

I used both ways of scaling with the same result. I even used an unscaled model but this one is so heavy it penetrates the floor ;) 

The penetration of other objects and their bounceyness can be adjusted using materials right?



dynNode[11].attachChild( ghostNode );

        System.out.println(((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0).getName()+" "+((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0).getType());

        PhysicsMesh bound=dynNode[11].createMesh("ghostbound");

        bound.copyFrom(((TriMesh) ((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0)));

        bound.setLocalScale(0.30f);

        staticNode.attachChild(dynNode[11]);

        ghostNode.setLocalScale(0.30f);

//dynNode[11].generatePhysicsGeometry();

        dynNode[11].computeMass();

Sue said:

Ok, but ih the tutorial you are attaching different physicNodes to each other like a dyn Node to a static one and a dynamic to another one togetthis strange ball with buckle.

No, you misinterpreted the code. A geometry is attached not a physics node.

Yes, materials could be used. Or you can simply set the mass instead of computing it.

Is it possible to encapsulate stuff or should physic nodes always be siblings?

Like a staticNode containing a model which contains some dynNodes which contain models?

Or should it be not designed that way?

ANother problem if I am not using a BoundingBox orSphere my Model starts to sink through the floor. I applied to the surrounding floor which is in a staticNode some material and to my Model and nothing changes.

And the Model and everything nover stops moving around.



rootNode.attachChild(staticNode);

room=(Node) ((Node) roomNode);

staticNode.attachChild(room);

staticNode.setMaterial(Material.GRANITE);

room.setLocalScale(20f);

room.updateModelBound();

staticNode.generatePhysicsGeometry();

System.out.println("here");



for (int i = 0; i < dynNode.length; i++) {

dynNode= getPhysicsSpace().createDynamicNode();



}



dynNode[0].attachChild( skullNode);

        //  skullNode.setLocalTranslation(new Vector3f(5,5,0));

        skullNode.setLocalScale(0.20f);

        skullNode.updateModelBound();

        rootNode.attachChild( dynNode[0] );

        dynNode[0].generatePhysicsGeometry();

        dynNode[0].computeMass();

       

     

        for (int i=0;i< bubbleNode.length; i++){

        dynNode[i+1].attachChild( bubbleNode );

        bubbleNode.setLocalScale(0.20f);

            bubbleNode.setModelBound(new BoundingSphere());

            bubbleNode.updateModelBound();

            rootNode.attachChild(dynNode[i+1]);

    dynNode[i+1].generatePhysicsGeometry();

            dynNode[i+1].computeMass();

        }

        dynNode[11].attachChild( ghostNode );

        //ghostNode.setLocalScale(0.30f);

        System.out.println(((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0).getName()+" "+((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0).getType());

        //PhysicsMesh bound=dynNode[11].createMesh("ghostbound");

        //TriMesh boundy=((TriMesh) ((Node)((Node)ghostNode.getChild(0)).getChild(0)).getChild(0));

        //boundy.setLocalScale(0.3f);

        //bound.copyFrom(boundy);

        //dynNode[11].setLocalScale(0.3f);

        rootNode.attachChild(dynNode[11]);

        //ghostNode.setLocalScale(0.30f);

dynNode[11].generatePhysicsGeometry(true);

Material ma=new Material();

ma.setDensity(10f);

dynNode[11].setMaterial(Material.GRANITE);

        dynNode[11].setMass(1f);

dynNode[11].updateCollisionTree();

       

       

        rootNode.updateCollisionTree();

//staticNode.generatePhysicsGeometry();



        showPhysics = true;

        rootNode.setCullMode(Node.CULL_DYNAMIC);

Sue said:

Is it possible to encapsulate stuff or should physic nodes always be siblings?

yes, always.

re that sink-problem: probably your winding is flipped? Check if you can see your objects normally when you apply a cullstate with CS_BACK. Is your ground also a trimesh? Probably ODE does not handle this correctly then.

It is a room and everything there is a trimesh because I imported it via collada.

Oh, that will be way too slow. You'll have to wait some processor generations before you can simply compute all TriMeshes. You will have to approximate most of the dynamic things with some spheres, boxes, capsules etc. Also walls should be approximated by boxes.

Hmmm so setting around every wall and the floor a boundingbox should work?

As I have seen it collada creates a new Node which carries a Node which caries the textures and Trimeshes of myimported Model so setting around every trimesh a BouningBox should resolf the problems ODE has with Trimeshes?

I'm not sure we are talking about the same things. You have to use physics primitives - e.g. a PhysicsBox - to approximate your meshes. ODE does not see any trimesh data then, so it's obviously not slowed down by too much geometry data. But BoundingBox is primarily a thing for culling (and picking currently). These boudings are used by generateGeometry( false ) to generate physics geometries from it. If you want to do the latter using BoundingBoxes would be ok. But as there are no bounding capsules of bounding cylinders, etc., you cannot use all physics primitives this way.

Ok i got it :slight_smile:

It is still not working as I want it.

If I use these basic objects everything works quite well except for the fact that I am able to drag big objects through small spaces in my walls.

But if I use PhysicsMesh and copy the geometry from my  little complex object then my object tilts to the heavier side and penetrates the floor.

Is there a way to adjust the physicsboxes the way boundingboxes do?