Hello,
I’ve run into an issue with JBullet rayTest.
rayTest seems to inconsistently return no result on GhostControls, depending on ray origin it seems?
I was able to reproduce the thing in a small test app, here is the code:
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsRayTestResult;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.Arrow;
import java.util.ArrayList;
public class HelloJME3 extends SimpleApplication {
private BulletAppState bulletAppState;
public static void main(String[] args){
HelloJME3 app = new HelloJME3();
app.start(); // start the game
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
this.bulletAppState.setDebugEnabled(true);
Node node = new Node("test");
this.getRootNode().attachChild(node);
debugArrow(node, new Vector3f(),new Vector3f(0,1,0), ColorRGBA.Green);
debugArrow(node, new Vector3f(),new Vector3f(1,0,0), ColorRGBA.Red);
debugArrow(node, new Vector3f(),new Vector3f(0,0,1), ColorRGBA.Blue);
Vector3f end = new Vector3f(20,0,0);
genBlock(this,end,new Vector3f(1,1,1));
Vector3f origin = new Vector3f(0,0,0);
this.rayCast(origin, end.subtract(origin).normalize(),25);
}
private void debugArrow(Node node,Vector3f origin, Vector3f end, ColorRGBA color) {
Arrow arrow = new Arrow(end);
Spatial debug = new Geometry("arrow", arrow);
Material matArrow = getAssetManager().loadMaterial("materials/debugMat.j3m");
matArrow.setColor("Color", color);
debug.setMaterial(matArrow);
node.attachChild(debug);
debug.setLocalTranslation(origin);
}
private void rayCast(Vector3f origin, Vector3f dir, float length) {
ArrayList<PhysicsRayTestResult> tmpRes = new ArrayList<>();
Vector3f end = origin.add(dir.mult(length));
try {
getPhysicSpace().rayTest(origin.clone(), end.clone(), tmpRes);
} catch (IllegalArgumentException e) {
System.err.println("origin: " + origin + " dir: " + dir + " length: " + length + " results: " + tmpRes);
throw new RuntimeException(e);
}
if (bulletAppState.isDebugEnabled()) {
debugArrow(getRootNode(),origin, end, (tmpRes.size() > 0 ? ColorRGBA.Orange : ColorRGBA.Red));
}
}
private void genBlock(HelloJME3 gameEng,Vector3f pos,Vector3f extend) {
Node node2 = new Node("test");
GhostControl control = new GhostControl(new BoxCollisionShape(extend));
control.setCollisionGroup(0);
node2.addControl(control);
node2.setLocalTranslation(pos);
gameEng.getPhysicSpace().add(control);
gameEng.getRootNode().attachChild(node2);
}
private PhysicsSpace getPhysicSpace() {
return bulletAppState.getPhysicsSpace();
}
}
for example the rayTest will return no result if the origin is 0,0,0 but will work fine with -5,0,0
which should (obviously ?) not change anything
If anyone as an idea of what I’m doing wrong I’d be grateful^^