I used the tutorial physics, the only thing i changed is the scenemodel. I changed it into solids because i made a minecraft like world with all kinds of blocks. Those blocks must be solid. The map works correctly but when i start the game i fall trough all the blocks. How can i take care so i dont fall through the blocks.
Here is my whole code:
[java]
package main;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
/**
-
test
-
@author normenhansen
*/
public class Main extends SimpleApplication implements ActionListener {public Node solids;
public Spatial cube8;
public ColorRGBA blueSky = new ColorRGBA(109/255f, 213/255f, 1f, 1f);
public int x = 0;
public int z = 0;private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();public static void main(String[] args) {
Main app = new Main();
app.start();
}@Override
public void simpleInitApp() {
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
solids = new Node(“Solid”);viewPort.setBackgroundColor(blueSky); flyCam.setMoveSpeed(50f); setUpKeys(); initLight(); CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) solids); landscape = new RigidBodyControl(sceneShape, 0); solids.addControl(landscape); CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1); player = new CharacterControl(capsuleShape, 0.05f); player.setJumpSpeed(20); player.setFallSpeed(100); player.setGravity(60); player.setPhysicsLocation(new Vector3f(20, 50, -20)); rootNode.attachChild(solids); draw(); bulletAppState.getPhysicsSpace().add(landscape); bulletAppState.getPhysicsSpace().add(player);
}
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}public void initLight() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
//DirectionalLight sun = new DirectionalLight();
//sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
//sun.setColor(ColorRGBA.White);
//rootNode.addLight(sun);
/** A white, spot light source. */
PointLight lamp = new PointLight();
lamp.setPosition(new Vector3f(64f, 80f, -64f));
lamp.setColor(ColorRGBA.White);
rootNode.addLight(lamp);
}public void draw() {
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture(
“Textures/hill.png”);
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
heightmap.load();
for (float value: heightmap.getHeightMap()) {value = Math.round(value / 12) * 2; if (value <= 10) { solids.attachChild(makeBlock(x, value, z, "Textures/sand.png")); } else if (value >= 27) { solids.attachChild(makeBlock(x, value, z, "Textures/stone.png")); } else { //solids.attachChild(makeBlock8(x, value, z)); solids.attachChild(makeBlock(x, value, z, "Textures/grass.png")); } x += 2; if (x >= 128) { x = 0; z -= 2; } }
}
public Geometry makeBlock(float x, float y, float z, String texture) {
Box box = new Box(new Vector3f(x, y, z), 1f, 1f, 1f);
Geometry cube = new Geometry(“grassBlock”, box);
Material mat1 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
mat1.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
mat1.setTexture(“DiffuseMap”, assetManager.loadTexture(texture));
cube.setMaterial(mat1);
return cube;
}public Spatial makeBlock8(float x, float y, float z) {
cube8 = assetManager.loadModel(“Models/box/box.j3o”);
cube8.setLocalTranslation(new Vector3f(x, y, z));
Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
mat.setTexture(“DiffuseMap”, assetManager.loadTexture(“Textures/tex_grass.png”));
cube8.setMaterial(mat);
cube8.scale(1f, 1f, 1f);
return cube8;
}private void setUpKeys() {
inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
}
/** These are our custom actions triggered by key presses.
- We do not walk yet, we just keep track of the direction the user pressed. */
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals(“Left”)) {
left = isPressed;
} else if (binding.equals(“Right”)) {
right= isPressed;
} else if (binding.equals(“Up”)) {
up = isPressed;
} else if (binding.equals(“Down”)) {
down = isPressed;
} else if (binding.equals(“Jump”)) {
if (isPressed) { player.jump(); }
}
}
}
[/java]