Hi,
I’ve been going through the jmonkey tutorials and I’m trying to add in physics (collision detection) to the tutorial “HelloTerrain”. So far I’ve managed to create a player (encapsulate the flycam) and add in gravity, the problem is i can’t seem to get collision detection between the terrain and the player so my player keeps falling through the mesh itself. I’m now only stuck on an error that i can’t seem to fix, here is the code i’m having trouble with so far:
[java]
import com.bulletphysics.collision.broadphase.Dbvt.Node;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsCharacter;
import com.jme3.bullet.util.CollisionShapeFactory;
public class Main extends SimpleApplication implements ActionListener {
private TerrainQuad terrain;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture(
“Textures/Terrain/splat/mountains512.png”);
heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), false, true, 0), 1f);
heightmap.load();
int patchSize = 65;
terrain = new TerrainQuad(“my terrain”, patchSize, 513, heightmap.getHeightMap());
com.jme3.bullet.collision.shapes.CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(terrain);
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(2f, 1f, 2f);
Node terrain_holder = new Node();
rootNode.attachChild(terrain);
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape(terrain.getSpatial());
terrain.updateModelBound();
}
}[/java]
Any help or guidance to solve this problem will be very appreciative,
cheers!
Theres a test class for exactly that.
i belive you forgot to add the collision shape to the bulletappstate:
bulletAppState.getPhysicsSpace().add(sceneShape);
also, why are you doing this? :
CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(terrain.getSpatial());
seems pointless to me… maybe you were trying to debug or something?
and terrain_holder is not used at all…
finally, i belive normen is talking about this tutorial:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_collision
hope this helps…
cheers…
I’ve got it working! thanks dixil, much appreciated!!
cheers lads