Hello, I am new to Jmonkey, trying to making a simple game to detect 2 box’s collision, the 1st box is controlled by player with a default location Vector3f(0, 0, 0), the other one is placed in Vector3f(7, 1, 7). I also created a BoundingBox at Vector3f(7, 1, 7) for the 2nd box.
But collision seems always triggered when player at Vector3f(0, 0, 0)
here is my code:
import java.security.SecureRandom;
import java.util.ArrayList;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.bounding.BoundingBox;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
public class geme_test_coll extends SimpleApplication {
public static void main(String[] args){
geme_test_coll app = new geme_test_coll();
app.start();
}
BoundingBox pillerBounding = new BoundingBox();
Geometry player;
ChaseCamera chaseCam;
BoundingBox playerBounding = new BoundingBox();
Geometry g = new Geometry();
SecureRandom sr = new SecureRandom();
@Override
public void simpleInitApp() {
assetManager.registerLocator("town", FileLocator.class);
Box box1 = new Box(0.5f, 0.5f, 0.5f);
player = new Geometry("player", box1);
player.setLocalTranslation(new Vector3f(0, 0, 0));
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Green);
player.setMaterial(mat1);
player.setModelBound(playerBounding);
player.updateModelBound();
rootNode.attachChild(player);
flyCam.setEnabled(false);
chaseCam = new ChaseCamera(cam, player);
chaseCam.setSmoothMotion(true);
Box floor = new Box(100, 0.1f, 100);
Geometry floor_geo = new Geometry("floor", floor);
Material floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey tk = new TextureKey("metal-tiles-dirt.jpg");
tk.setGenerateMips(true);
Texture floor_tx = assetManager.loadTexture(tk);
floor_tx.setWrap(WrapMode.Repeat);
floor_mat.setTexture("ColorMap", floor_tx);
floor_geo.setMaterial(floor_mat);
floor_geo.setLocalTranslation(0, -0.1f, 0);
rootNode.attachChild(floor_geo);
floor.scaleTextureCoordinates(new Vector2f(100f, 100f));
int rnd = sr.nextInt(4);
Box piller = new Box(0.5f, 1 + rnd, 0.5f);
g = new Geometry("piller", piller);
Material m = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
m.setColor("Color", ColorRGBA.randomColor());
g.setMaterial(m);
g.setLocalTranslation(new Vector3f(7, 1, 7));
rootNode.attachChild(g);
pillerBounding = new BoundingBox(new Vector3f(7, 1, 7), 0.5f, 1 + rnd, 0.5f);
g.setModelBound(pillerBounding);
g.updateModelBound();
movementManager();
}
private void movementManager() {
inputManager.addMapping("M_L", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("M_R", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("M_F", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("M_B", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("ROT", new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(al, "M_L", "M_R", "M_F", "M_B", "ROT");
}
private AnalogListener al = new AnalogListener() {
@Override
public void onAnalog(String cmd, float value, float tpf) {
player.updateGeometricState();
Vector3f playerPosition = player.getLocalTranslation();
float spd = (float)((float)speed * 4);
if(cmd.equals("M_L")) { player.setLocalTranslation(playerPosition.x - value * spd, playerPosition.y, playerPosition.z); }
if(cmd.equals("M_R")) { player.setLocalTranslation(playerPosition.x + value * spd, playerPosition.y, playerPosition.z); }
if(cmd.equals("M_F")) { player.setLocalTranslation(playerPosition.x, playerPosition.y, playerPosition.z + value * spd); }
if(cmd.equals("M_B")) { player.setLocalTranslation(playerPosition.x, playerPosition.y, playerPosition.z - value * spd); }
if(cmd.equals("ROT")) { player.rotate(0, 0 + value * spd, 0); }
collisionDetect();
}
};
private void collisionDetect() {
Geometry a = player;
BoundingBox b = pillerBounding;
CollisionResults results = new CollisionResults();
a.collideWith(b, results);
System.out.println("Number of Collisions between" +
a.getName()+ " and piller " + results.size());
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
System.out.println("What was hit? " + closest.getGeometry().getName() );
System.out.println("Where was it hit? " + closest.getContactPoint() );
System.out.println("Distance? " + closest.getDistance() );
}
}
}
Output when player is at <0, 0, 0>
Number of Collisions between player and piller 10
What was hit? player
Where was it hit? null
Distance? 0.0
the Number has a range about from 6 to 10
Output when player is outside <0, 0, 0>
Number of Collisions between player and piller 0
can anyone help me? thanks!