Problem with collisions between player & geometry (jme 3)

I’m playing with the HelloCollision tutorial and running into a problem whenever I try to add collisions to a box I’ve created. I’ve tried a couple different ways and the collisions largely work but if you walk too close to the box sometimes you can see into it. I have this code currently:



[java]

Box box2 = new Box(Vector3f.ZERO, 2f, 2f, 2f);

Geometry grassBox = new Geometry(“Box2”, box2);



Material mat_grass = new Material(assetManager, “Common/MatDefs/Misc/SimpleTextured.j3md”);

mat_grass.setTexture(“m_ColorMap”,

assetManager.loadTexture(“Textures/grass2.jpg”));

grassBox.setMaterial(mat_grass);



Node grass = new Node(“grass”);

grass.attachChild(grassBox);



CompoundCollisionShape boxShape =

CollisionShapeFactory.createMeshCompoundShape(grass);



grassNode = new PhysicsNode(grass, boxShape, 0);

grassNode.setLocalTranslation(new Vector3f(20f, 5f, 5f));



rootNode.attachChild(grassNode);

bulletAppState.getPhysicsSpace().add(grassNode);

[/java]



I also tried it this way and it had the same problems::

physGrass = new PhysicsNode(grassBox, new BoxCollisionShape(new Vector3f(2f, 2f, 2f)), 0);

physGrass.setLocalTranslation(new Vector3f(20f, 5f, 5f));





I don’t need to do any advanced stuff with the box, I just want it to act like the “landscape” in the example. How would I go about doing this?

Edit: Sorry, misread your post/code… The fact that you can see into the box probably has to do with the camera and the frustum settings of it and not with the collision.

I left some stuff out to keep it short, but there were other issues that made me think it’s not the camera. When I remove the “landscape” altogether and create a large box to walk on, I couldn’t always jump off the box. I’d press the space bar and nothing would happen. Also the rest of the “landscape”, the houses, etc. work perfect with the camera. It’s just the box in the middle that I could see through.

Anyone? Am I doing something wrong or is this a bug with JME?

I’d say post a small test case here together with what’s wrong with it so we could have a look. The info you give is just a bit too shallow.

Sorry if I add my question to this thread, instead of reply yours, but I have the same problem!



my source: http://pastebin.com/mcjWYghh



In this app the first-person player can touch the “levelUp box” and in that moment a text “would” appear over the screen telling that appened a collision.



But I have 2 problem:

  1. Even if player and box seem to realize they’re in touch [they are booth physics-thing], the app can’t recognize the contact and so anything is displayed onto the screen or output terminal. Where did I commit errors? How can I fix them?
  2. Everything in the scene is shacking. Maybe other problem with CollisionShape? Why?



    thanks!



    r.



    ps: I think it would be great a section in this forum for each kind of games, eg: on for FPS, one for car racing and so on. In that manner we can resume problems, tips and tutorial in the same place. Isn’t it?



    pps: Sorry for my english, but I’m italian [nothing further!]
durandal said:
I'd say post a small test case here together with what's wrong with it so we could have a look. The info you give is just a bit too shallow.


ok, this is just a simple variation of the HelloCollision tutoral. It creates a green rectangle to walk on, a red square & a monkey head. If you don't have the monkey head model you can just delete that section.

[java]
package jme3test.helloworld;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.bullet.nodes.PhysicsCharacterNode;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Spatial;
import com.jme3.math.Vector3f;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.Node;



public class TestWorld extends SimpleApplication implements ActionListener {

private BulletAppState bulletAppState;
private Spatial sceneModel;
private PhysicsNode physland;
private PhysicsNode physicsRed;
private PhysicsNode physbox;
private PhysicsNode monkeyNode;
private PhysicsNode wallFinal;
private PhysicsCharacterNode player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;

public static void main(String[] args) {
TestWorld app = new TestWorld();
app.start();
}

public void simpleInitApp() {
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);

// We re-use the flyby camera for rotation, while positioning is handled by physics
viewPort.setBackgroundColor(new ColorRGBA(0.7f,0.8f,1f,1f));
flyCam.setMoveSpeed(100);
setupKeys();

// We add a light so we see the scene
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White.clone().multLocal(2)); // bright white light
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalize());
rootNode.addLight(dl);


//create land
Box box1 = new Box(Vector3f.ZERO, 50f, 2f, 50f);
Geometry landBox = new Geometry("Box1", box1);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
mat1.setColor("m_Color", ColorRGBA.Green);
landBox.setMaterial(mat1);
physland = new PhysicsNode(landBox, new BoxCollisionShape(new Vector3f( 50f, 2f, 50f)), 0);
rootNode.attachChild(physland);
bulletAppState.getPhysicsSpace().add(physland);

//create box
Box box2 = new Box(Vector3f.ZERO, 5f, 5f, 5f);
Geometry redBox = new Geometry("Box2", box2);
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
mat2.setColor("m_Color", ColorRGBA.Red);
redBox.setMaterial(mat2);
physbox = new PhysicsNode(redBox, new BoxCollisionShape(new Vector3f( 5f, 5f, 5f)), 0);
physbox.setLocalTranslation(new Vector3f(0f, 5f, 0f));
rootNode.attachChild(physbox);
bulletAppState.getPhysicsSpace().add(physbox);

//create monkey head
Spatial bumpy = (Spatial) assetManager.loadModel("Models/MonkeyHead/MonkeyHead.mesh.xml");
bumpy.setLocalScale(2f);
CompoundCollisionShape monkeyShape =
CollisionShapeFactory.createMeshCompoundShape((Node) bumpy);
monkeyNode = new PhysicsNode(bumpy, monkeyShape, 0);
monkeyNode.setLocalTranslation(new Vector3f(0f, 5f, 15f));
rootNode.attachChild(monkeyNode);
bulletAppState.getPhysicsSpace().add(monkeyNode);


//create player
player = new PhysicsCharacterNode(new CapsuleCollisionShape(1.5f, 6f, 1), .05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setLocalTranslation(new Vector3f(20, 30, 0));
rootNode.attachChild(player);
bulletAppState.getPhysicsSpace().add(player);




}

/** We over-write some navigational key mappings here, so we can
* add physics-controlled walking and jumping: */
private void setupKeys() {
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Lefts");
inputManager.addListener(this, "Rights");
inputManager.addListener(this, "Ups");
inputManager.addListener(this, "Downs");
inputManager.addListener(this, "Jumps");
}

/** 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 value, float tpf) {
if (binding.equals("Lefts")) {
if (value) { left = true; } else { left = false; }
} else if (binding.equals("Rights")) {
if (value) { right = true; } else { right = false; }
} else if (binding.equals("Ups")) {
if (value) { up = true; } else { up = false; }
} else if (binding.equals("Downs")) {
if (value) { down = true; } else { down = false; }
} else if (binding.equals("Jumps")) {
player.jump();
}
}

/**
* This is the main event loop--walking happens here.
* We check in which direction the player is walking by interpreting
* the camera direction forward (camDir) and to the side (camLeft).
* The setWalkDirection() command is what lets a physics-controlled player walk.
* We also make sure here that the camera moves with player.
*/
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().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.getLocalTranslation());
}
}

[/java]

2 problems: you frequently can't jump & if you walk up to the red square you can usually see into it. The monkey head doesn't have these problems. I suspect if the monkey head was a cube it still would work fine. I don't have a cube model to test this out though.

Seems you are sinking a bit into the floor box. That’s probably why you can’t jump either.I got a bit better results by using the CollisionShapeFactory in stead of the BoxCollisionShape for that too. Don’t ask me why though - I have had problems with sinking through an object too when gravity/fall speed is too large or the colision shape fits between the mesh. (might want to try to stand on a smaller box).

Physics definately has some issues still.

I tried that test and I have more success using the sphere collision shape. The shaking is gone, I can’t view into the box, and I can jump always.