So, I have a simple program that involves a square room made up of 4 boxes of 10,4,1.
Inside this room I have a small cube that i want to translate along a simple vector, and have it turn around when it hits the wall, and repeat. I accomplish this by calculating the distance from the cube to the approaching wall using ray casting, and reverse when some close distance is reached.
My code works with my scale set to 1, but when I set it to anything higher I get strange behavior.
Jmonkey seems to think my wall geometries are much further away then they āappearā to be. and thus will pass right through it and will start backtracking from further behind it.
Can anyone tell me why this is and possibly how to fix it so that when my rootNode is scaled to any value my cube will behave as I described?
[java]
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
int scale = 1;
Spatial s;
Vector3f dir;
public void simpleInitApp() {
//Create floor
flyCam.setMoveSpeed(20);
Box floor = new Box(10, 0.05f, 10);
Geometry geom = new Geometry("Box", floor);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Gray);
geom.setMaterial(mat);
geom.setLocalTranslation(10, -3.0f, 10);
rootNode.attachChild(geom);
Box wall;
Spatial Wall;
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
mat.setColor("Color", ColorRGBA.Brown);
for(int i=0; i<4; i++){
if(i==0){
wall = new Box(10,4,1);
Wall = new Geometry("w1", wall);
Wall.setMaterial(mat);
Wall.move(10,0,0);
}
else if(i==1){
wall = new Box(1,4,10);
Wall = new Geometry("w2", wall);
Wall.setMaterial(mat);
Wall.move(0,0,10);
}
else if(i==2){
wall = new Box(10,4,1);
Wall = new Geometry("w3", wall);
Wall.setMaterial(mat);
Wall.move(10,0,20);
}
else if(i==3){
wall = new Box(1,4,10);
Wall = new Geometry("w4", wall);
Wall.setMaterial(mat);
Wall.move(20,0,10);
}
else {
Wall = new Geometry();//to avoid warnings
}
rootNode.attachChild(Wall);
}
dir = new Vector3f(1,0,0);//initial direction
Box b = new Box(1f,1f,1f);
s = new Geometry("oscillator", b);
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
mat.setColor("Color", ColorRGBA.Red);
s.setMaterial(mat);
s.setLocalTranslation(5,-2f,5);
rootNode.attachChild(s);
rootNode.scale(scale);
}
@Override
public void simpleUpdate(float tpf) {
Ray r = new Ray(s.getLocalTranslation(),dir);
s.move(dir.mult(0.01f));//slow it down.
CollisionResults results = new CollisionResults();
rootNode.collideWith(r, results);
if(results.size() > 0){
//if distance < some threshold reverse direction vector.
float dist = results.getClosestCollision().getDistance();
System.out.println(dist);//for debug
if(dist < 1){
dir = dir.negate();
}
//for debug
System.out.println(results.getClosestCollision().getGeometry());
}
}
}
[/java]
When you run my code you will see what I mean.
If you change my global āscaleā value to anything higher than 1, you can watch the console as it outputs the distance from where it thinks the far side wall is, and itās clearly not where it is displayed.