Custom CollisionShape

spatial.addControl(myControl)?

It still doesn’t appear.



Full code:

[java]SphereCollisionShape capsuleShapes = new SphereCollisionShape(2f);



CompoundCollisionShape compound = new CompoundCollisionShape();

compound.addChildShape(capsuleShapes, new Vector3f(0,1,0), Matrix3f.ZERO);

RigidBodyControl rigidControla = new RigidBodyControl(compound, 0);

rigidControla.setPhysicsLocation(new Vector3f(20,5,0));

bulletAppState.getPhysicsSpace().add(rigidControla);

green.addControl(rigidControla);[/java]

did you attach it to the rootnode?

That was the problem. I was detaching the spatial because it was too big and was afraid it’d hide the shape. Thanks for your help normen. Happy New Year.

Sub issue:



I am having a problem similar to memonick. When using…

[java]HullCollisionShape shape = CollisionShapeFactory.createDynamicMeshShape(someModel);[/java]

…the object that the function is returning is a “CollisionShape” which is the super class of “HullCollisionShape”. I checked the online jme3 javadoc as well as the source and in both documented cases they return only “CollisionShape”. I tried casting “CollisionShape” to “HullCollsionShape” without success…

[java]HullCollisionShape shape = (HullCollisionShape) CollisionShapeFactory.createDynamicMeshShape(someModel);[/java]

…is this an error in jMonkeyEngine SDK 3.0beta source or am I using it wrong?



Please advise!

I tried that too, with the same result you got. I ended up using CompoundCollisionShapes.

Guys, HullCollisionShape also has a constructor, you do not need the factory, its only when you want to create compound shapes.

Thanks for the reply back normen,



I did try…

[java]HullCollisionShape shape = new HullCollisionShape(meshObject);[/java]

…but I am confused on what a “Mesh” object is. In other words I was expecting to use something like…

[java]HullCollisionShape shape = new HullCollisionShape(spatialObject);[/java]

…I am unaware of how to load a “Spatial” as a “Mesh”. How can I load a “Spatial” to retrieve it as a “Mesh” object?



I apologize for my ignorance, thanks for your patience normen. jme3 is the best engine since struggling with Ogre lol. Your team has done a fine job with this engine!

I think I see it now. The “Mesh” object is a collection of vertices that someone has to build from the “Spatial” object.

[java]HullCollisionShape shape = new HullCollisionShape(float userDefinedVertices);[/java]

So I need to write my own function to get the vertices for the object because jme3 doesn’t automatically assign vertex points from the spatial. Something like…

[java]HullCollisionShape shape = new HullCollisionShape(gameObject.getHullVertices());



public class GameObject

{



public float[] getHullVertices()

{



return vertices;

}

}[/java]

…would work because I am defining the float vertices on my own; jme3 does not calculate hull vertices on its own.



Does this idea look correct?

geometry.getMesh() :roll:

“Spatials” do not have…

[java]Spatial.getMesh();[/java]

…to speak of. So I looked at the help file and this is what it came up with…

[java]// Load any model

Node model = (Node) assetManager.loadModel("Models/myCharacterModel.mesh.xml");

rootNode.attachChild(model);

// Create a appropriate physical shape for it

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

CharacterControl character_phys = new CharacterControl(capsuleShape, 0.01f);

// Attach physical properties to model and PhysicsSpace

model.addControl(character_phys);

bulletAppState.getPhysicsSpace().add(character_phys);[/java]

…This doesn’t work at all. Even substituting “CapsuleCollisionShape” with “HullCollisionShape” does not work. What we are looking to do is…

  1. Spatial myModel = assetmanager.loadmodel(“Models/somekindofmodel.mesh.xml”);
  2. Create a hullcollision shape based off of the myModel spatial.
  3. Add it to the bulletphysicsappwhatever so we can get “accurate” collisions.

    We are missing instructions for step #2, could you please fill in the gaps?

Look at the line 143 http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/bullet/TestHoveringTank.java

If you wanna know how it creates the hull shape internally then look at this http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/bullet-common/com/jme3/bullet/util/CollisionShapeFactory.java at line 184.

Don’t be afraid to check the source code.

A Spatial is either a Node or a Geometry, refresh your java/coding and scene graph knowledge.

I tried loading a Spatial as Geometry:

[java]playerSpatial= (Geometry)assetManager.loadModel("Models/Plane001/Plane001.j3o");[/java]



But the following error came:



SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry

I give up. For the player Character, I will use CompoundCollisionShape and for opponents (and obstacles/scenes), I will use:

[java]CollisionShape sceneShape2 = CollisionShapeFactory.createMeshShape(obstacle);

f = new RigidBodyControl(sceneShape2, 0);

obstacle.addControl(f);

bulletAppState.getPhysicsSpace().add(f);

rigidBodyControl.setMass(1f);

f.setKinematic(true);[/java]



It works well.

playerSpatial= (Geometry)assetManager.loadModel("Models/Plane001/Plane001.j3o");


Lol. Sounds like you don't know what's polymorphism at all.

java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry


One geometry is a Spatial, but a spatial is not necessarily a geometry. Why didn't you use the helper method from CollisionShapeFactory for creating dynamic shapes as I said in my last post? You can pass your loaded spatial as param and the method will care the geometries for you. Don't be stubborn guys ;). This topic is being useless.

Well thanks for the ad hominem. People just don’t spell it out in plain English to the rest of us as we are not arrogant and just down to earth simpletons. I think since we are able to get this far with the documentation being scattered at best, it shows we have tried things and are willing to work through the problems. The forums are here to ask questions and troubleshoot. As an example the tutorials don’t even go over how to nest main() methods to create classes, I did that on my own making an abstract MainEntity class and deriving subclasses from that.



So…

[java]CollisionShape mordor = CollisionShapeFactory.createDynamicMeshShape(Spatial || Node);[/java]



…will return a HullCollisionShape. And again, thank you guys for answering our questions.



:o PROBLEM SOLVED TO ALL WHO THREAD HERE. SEE EXAMPLE ABOVE^

1 Like

What’s ’ || Node’?

He just wanted to mean that the param can be any spatial object, either a node or a geometry. Keep in mind that a spatial is an abstract class, then it can be a geometry or a node or a particle emitter… That’s the createDynamicMeshShape() checks if the spatial is a node or a geometry:



[java]

if(spatial instanceof Node)



if(spatial instanceof Geometry)



[/java]

That worked like a charm gentlemen! Now onto controls…lol j/k :smiley: