Ive created a floor(using mesh),
but there are alwats few places on the mesh( not on the edges but somewhere inside) where collision dossent occur .(results.size==0)
and i cant figure out why. Maybe you can find out what im doing wrong
Hope its not too long to help me , but i cant make it shorter if i want to show how its all done.
I believe the mesh is good , and thats my problem is somewhere in the collisions detection code (and maybe the ray building)
This is where i set up camera and build the mesh:
[java]
public void simpleInitApp() {
flyCam.setEnabled(false);
inputManager.setCursorVisible(true);
camNode=new CameraNode(โCamโ, cam);
camNode.setLocalTranslation(new Vector3f(10f,20f,15f));
camNode.lookAt(new Vector3f(10f,0f,-10f) , Vector3f.UNIT_Y);
rootNode.attachChild(camNode);
rootNode.attachChild(floor);
buildMesh();
}
[/java]
This is the buildMesh method :
[java]
private void buildMesh() {
Mesh mesh = new Mesh();
int xCount=20;
int zCount=20;
float width=1f;
int totalSquers=xCount*zCount;
FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(4*totalSquers);
FloatBuffer texBuffer = BufferUtils.createVector2Buffer(4*totalSquers);
IntBuffer indexBuffer = BufferUtils.createIntBuffer(3*4*totalSquers);
for (float i=0;i<xCount;i++){
for (float j=0;j<zCount;j++){
float basex=i*width;
float basez=(-zCount+j+1);
vertexBuffer.put(basex).put(0).put(basez);
vertexBuffer.put(basex+width).put(0).put(basez);
vertexBuffer.put(basex).put(0).put(basez-width);
vertexBuffer.put(basex+width).put(0).put(basez-width);
int[] indices = new int[] {
2,0,1,
1,3,2
};
for ( int k = 0; k < indices.length; k++ ) {
indices[k] += (i*zCount + j)*4;
}
indexBuffer.put(indices);
}
}
mesh.setBuffer(VertexBuffer.Type.Position,3,vertexBuffer);
mesh.setBuffer(VertexBuffer.Type.TexCoord,2,texBuffer);
mesh.setBuffer(VertexBuffer.Type.Index,3,indexBuffer);
mesh.setStatic();
mesh.updateBound();
geo = new Geometry("OurMesh", mesh);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geo.setMaterial(mat);
floor.attachChild(geo);
}
[/java]
This is the update loop:
[java]
public void simpleUpdate(float tpf) {
CollisionResults results=check_for_collisions(floor);
//if mouse is outside of floor
if (results.size()==0){
System.out.println("Im in trackable plane out side the floor - " + new Date());
System.out.println(โ");
}
else{
System.out.println(โIm in trackable plane in side the floor - " + new Date());
System.out.println(results.getClosestCollision().getGeometry().getName()+โ - "+ results.getClosestCollision().getContactPoint().toString());
System.out.println("โ);
}
}
[/java]
And this is the check_for_collisions method ( same as shown in the tutorials)
[java]
private CollisionResults check_for_collisions(Node checked_node) {
// Reset results list.
CollisionResults results = new CollisionResults();
// Convert screen click to 3d position
//Vector2f click2d = inputManager.getCursorPosition();
Vector2f click2d = inputManager.getCursorPosition();
System.out.println(click2d);
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
System.out.println(click3d);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 1f).subtractLocal(click3d).normalizeLocal();
// Aim the ray from the clicked spot forwards.
Ray ray = new Ray(click3d, direction);
// Collect intersections between ray and checked_node in results list.
checked_node.collideWith(ray, results);
return results;
}
[/java]