CollisionShapes for scenes created with TerraMonkey

Hi all, :slight_smile:

first of all: You did a great job with the terrain editor. Especially the textures^^



My problem: Everytime I try to create a CollisionShape for a scene with a terrain, that I’ve created in the terrain editor, I get this warning:

java.lang.IndexOutOfBoundsException: 8465
at java.nio.DirectIntBufferU.get(DirectIntBufferU.java:212)
at com.jme3.scene.mesh.IndexIntBuffer.get(IndexIntBuffer.java:52)
at com.jme3.bullet.util.Converter.convert(Converter.java:248)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(MeshCollisionShape.java:70)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.(MeshCollisionShape.java:65)
at com.jme3.bullet.util.CollisionShapeFactory.createSingleMeshShape(CollisionShapeFactory.java:174)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:65)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:62)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:62)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:85)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshCompoundShape(CollisionShapeFactory.java:94)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshShape(CollisionShapeFactory.java:134)

(The index of the IndexOfBoundException ist of course always a different one for each scene ;))

I load the scene with this line:
[java]Spatial sceneModel = assetManager.loadModel(scenePath);[/java]

That seem's to work, because there's no error. ;) But, when I try this, the programm exits with the upper error:
[java]CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(sceneModel);[/java]

I hope, you can help me.

Thanks,
destro :)

I have this in my code and it works:



[java]CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);[/java]



Try that out?





-NomNom

I’ve tried this, but it still doesn’t work. :frowning:

The mesh seems to consist not only of triangles. Only triangulized meshes can be used. Also, it should not be a subspatial of a terrain node.

Hm… I just created a flat terrain. And it consists only of triangles.

The terrain is at the moment the only child of the scene node. But even when I add it to a subnode (SceneNode->Node->Terrain), I still get this error.

(The same error appears at every terrain, also at a hill terrain or one created out of a heightmap)



Any other ideas? (Btw, I have the newest version using the nighlty SVN, if that’s important)



EDIT: The code works with an empty scene - The only problem is the terrain node…



EDIT2: (OffTopic) I see that you’re not displaying *.dds-Images in the TextureChooser in TerraMonkey. I once wanted to display *.dds-Files in Java and after half a year^^, I finally found and edited something, that worked. If you want to, I can give you my code to display *.dds-Images in Java. (If you have problems with this, otherwise sorry for OffTopic :wink:)

When you create the terrain in TerraMonkey, right-click the base terrain node and select “AddControl”->“Static RigidBody”. Then, for any other models added, use the same method for static or use the SceneComposer tools for dynamic shapes that you want to save in the scene. Works fine for me.

destroflyer said:
I see that you're not displaying *.dds-Images in the TextureChooser in TerraMonkey. I once wanted to display *.dds-Files in Java and after half a year^^, I finally found and edited something, that worked. If you want to, I can give you my code to display *.dds-Images in Java. (If you have problems with this, otherwise sorry for OffTopic ;))

Oh, forgot to answer to that :) Awesome! Would love to add the code to the texture selector.
When you create the terrain in TerraMonkey, right-click the base terrain node and select “AddControl”->”Static RigidBody”. Then, for any other models added, use the same method for static or use the SceneComposer tools for dynamic shapes that you want to save in the scene. Works fine for me.

Still the same error. :(
_________________

Oh, forgot to answer to that :) Awesome! Would love to add the code to the texture selector.


*.dds-Images in Java
First of all, you have to import the file DDSUtils.jar into your project. I've uploaded it here for you:
http://uploaded.to/file/80yi64df
(OMG, I just remember how long I searched for that :D)

After that, add that line to the imports of your *.java-Class: (Import the DDS-thing)
[java]import Model.DDSImageFile;[/java]

After that, you can use this code to get the content of a *.dds-File as a BufferedImage (Or as an ImageIcon, that's easier to display ;)):
[java]try{
String path = "myImage.dds";
File file = new File(path);
DDSImageFile ddsImageFile = new DDSImageFile(file);
BufferedImage bufferedImage = ddsImageFile.getData();
ImageIcon imgicon = new ImageIcon(bufferedImage);
}catch(Exception ex){
ex.printStackTrace();
}[/java]
(The DDSImageFile-Constructor can throw an exception, so you have to try-catch the function ;))

Maybe you also have to import these classes, if you don't have yet:
[java]import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.ImageIcon;[/java]

Have fun and luck to display the images! :)

Greez, destro
2 Likes

Thanks! I will look into adding this.

Yeeeaaah, I finally did it. :smiley: Thanks again!

But somehow, there’s still a bug in the terrain editor - If I change the name of the terrain node and klick “Save”, it actually doesn’t save the new name. :wink:

Added in svn, works fine in the texture browser :slight_smile: The skybox panel doesn’t yet use it tho…

Thanks again.

Lol^^ I was 8s faster. :smiley:

Anyway, I’m happy to hear, that it works. :slight_smile:



EDIT: Nothing to thank, you guys just did an icredible job - I’m used to many frameworks, but nothing and really NOTHING can reach this level of perfection. :wink: Great job again! I’m happy that I could help a little bit to improve jMonkeyEngine. :wink:

Hm… there’s still a problem - Is there a way to scale the terrain?

I’ve changed the “LocalScale” of the SceneNode in the SceneComposer to [3.0 3.0 3.0], but when I load it into the application, it still appears small and not scaled.

I can scale it directly in the code (setLocalScale), but then the CollisionShape doesn’t match anymore…

Regenerate the collision shape after resizing…?

Nope, doesn’t work - I’ve already tried. :frowning:



EDIT: Ok, it works now - The problem is, that the CollisionShape doesn’t regard the scale of the scene node. If I scale the TERRAIN node by (3|3|3) and regenerate the Static RigidBody, it works fine. :slight_smile:

Yeah, the CollisionShape is using the local version of the spatial its generated from and detaches it from the parent for generation so it gets no offset by a parents location.

hi, just a question that’s been on my mind for a while now: it’s said that for collisionmeshshape a mesh should contain only triangles. Is there a way to fake Mode.Triangles for jBullet and still draw from Mode.TriangleStrips?

For terrain theres the HeightfieldCollisionShape, its generated by the factory when supplied a Terrain spatial and much more effective than triangles.

normen said:
For terrain theres the HeightfieldCollisionShape, its generated by the factory when supplied a Terrain spatial and much more effective than triangles.


To be precise, the factory creates HeightfieldCollisionShape for TerrainQuads. Anyways I moved that one line to my code, supplying the constructor with my heightmap data and scale value, but it does not seem to do the trick.

So now I'm doing the following, can you correct me, what I'm doing wrong?

[java]
float[] heightMap = this.getHeightMap();
this.shape = new HeightfieldCollisionShape(heightMap, new Vector3f(this.getScale(), 1, this.getScale()));
this.terrain.addControl(new RigidBodyControl(this.shape, 0));
this.physics.getPhysicsSpace().add(this.terrain.getControl(RigidBodyControl.class));
[/java]

if I call

[java]
float[] heightMap = this.getHeightMap();
this.shape = new HeightfieldCollisionShape(heightMap, new Vector3f(this.getScale(), 1, this.getScale()));
this.terrain.addControl(new RigidBodyControl(0));
this.physics.getPhysicsSpace().add(this.terrain.getControl(RigidBodyControl.class));
[/java]

new RigidBodyControl without the collisionshape, then it works, but creates mesh collisionshape as far as I can see. (mainly because I haven't passed the heightfield data to the constructor)

Uh, what doesn’t wok about it? Both ways should give you a heightfield collision shape if its a TerrainQuad, otherwise it would crash.