I have a bunch of boxes, and when I get close to them (but not inside) the camera can occasionally see inside one of them. This is what it should look like -http://imgur.com/cGRE8l&Gf6L8 , and this is what it looks like when the camera sees inside the box -http://imgur.com/cGRE8&Gf6L8l . I had a similar problem in jME2, and I fixed it by adding a ZBufferState to my object - is there something similar I need to do in jME3?
Thank you!
And here’s my code…
[java]import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.bounding.BoundingBox;
import com.jme3.math.ColorRGBA;
import com.jme3.light.PointLight;
import com.jme3.scene.Node;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.RenderState.FaceCullMode;
public class JME extends SimpleApplication implements AnalogListener {
private float camHeight = 0f;
private Vector3f prevCamLocation = new Vector3f ();
private ArrayList<BoundingBox> wallBoundingBoxes = new ArrayList<BoundingBox> ();
private boolean left=false, right=false, up=false, down=false;
public static void main(String[] args){
JME app = new JME();
app.start();
}
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed (10f);
setupKeys ();
Vector3f boxVector = new Vector3f (0,0,0);
Node boxes = new Node ();
Box b;
Geometry geom;
Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);
mat.setColor (“m_Color”, ColorRGBA.Blue);
mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
Box floorBox = new Box (new Vector3f (0,-.5f,0), -100f, 0f, 100f);
Geometry floorGeom = new Geometry (“Floor”, floorBox);
Material floorMat = new Material (assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);
floorMat.setColor (“m_Color”, ColorRGBA.White);
floorGeom.setMaterial (floorMat);
rootNode.attachChild (floorGeom);
try {
BufferedImage image = ImageIO.read(new File(“test.bmp”));
System.out.println (“BEGIN READ IMAGE”);
for (int z=0; z < image.getHeight (); z++) {
for (int x=0; x < image.getWidth (); x++) {
if (image.getRGB (x, z) == -16777216) {
boxVector.x = x;
boxVector.z = z;
b = new Box (boxVector, .5f,.5f,.5f);
geom = new Geometry (“Box” + x + “,” + z, b);
geom.setMaterial (mat);
System.out.print (image.getRGB (x, z)+ “t”);
boxes.attachChild (geom);
wallBoundingBoxes.add (new BoundingBox (boxVector, .5f, .5f, .5f));
}
}
System.out.println ();
}
System.out.println (“END READ IMAGE”);
}
catch (IOException exc) {}
rootNode.attachChild (boxes);
}
private void setupKeys() {
System.out.println (context.getKeyInput ());
inputManager.deleteMapping (“FLYCAM_Forward”);
inputManager.deleteMapping (“FLYCAM_Backward”);
inputManager.deleteMapping (“FLYCAM_StrafeLeft”);
inputManager.deleteMapping (“FLYCAM_StrafeRight”);
inputManager.addMapping(“Lefts”, new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Rights”, new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Forward”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Backward”, new KeyTrigger(KeyInput.KEY_S));
inputManager.addListener(this, “Lefts”);
inputManager.addListener(this, “Rights”);
inputManager.addListener(this, “Forward”);
inputManager.addListener(this, “Backward”);
}
private void moveCamera(float value, boolean sideways){
Vector3f vel = new Vector3f();
Vector3f pos = cam.getLocation().clone();
boolean collision = false;
if (sideways){
cam.getLeft(vel);
}else{
cam.getDirection(vel);
}
vel.y = camHeight;
vel.multLocal(value * 10f);
pos.addLocal(vel);
BoundingBox playerBox = new BoundingBox (pos, .1f, .1f, .1f);
for (int i=0; i<wallBoundingBoxes.size (); i++) {
if (playerBox.intersects (wallBoundingBoxes.get (i))) {
collision = true;
break;
}
}
if (!collision)
cam.setLocation (pos);
}
public void onAnalog(String name, float value, float tpf) {
if (name.equals(“Lefts”)) {
moveCamera (value, true);
} else if (name.equals(“Rights”)) {
moveCamera (-value, true);
} else if (name.equals(“Forward”)) {
moveCamera (value, false);
} else if (name.equals(“Backward”)) {
moveCamera (-value, false);
}
}
}[/java]
You can change the cameras near frustum value to have objects being cut off later/at closer range.
If you’re using physics makes sure the collision sphere radius for the camera is higher than the near plane value
I tried changing the near frustum value but I still had the same problem. Also, I’m not using any physics.
Does it maybe have something to do with my “floor”? When I set it to be slightly below the walls (instead of touching them), then I don’t get that effect - but then it looks like this when I look down: http://imgur.com/DAtAo&9fxg8 . When I’m not looking down, it looks right, like this - http://imgur.com/DAtAo&9fxg8l .
e: It seems like if I extend the box down into the floor then everything looks ok. It’s not perfect, but I’ll play around with it for a bit and see if I can get it.