Hi,
I want create an example for my problem with TrianglePickResults but I can't even manage to get a simple application up and running.
I copied the mouse input tutorial to get the framework and planned to add the relevant code once that runs.
The application starts, however the mouse input does not work (neither does key input when I copied the key input example).
I even can't close the window by clicking on X so I'm not sure if the problem really originates from JME.
I'm starting the app from IntelliJ Idea btw.
Please see the following code and tell me if it should run (or does on your systems):
public class TestTrianglePicking extends SimpleGame {
// This will be my mouse
// This will be my mouse
AbsoluteMouse am;
// This will be he box in the middle
Box b;
PickResults pr;
public static void main(String[] args) {
TestTrianglePicking app = new TestTrianglePicking();
app.setConfigShowMode(ConfigShowMode.AlwaysShow);
app.start();
}
protected void simpleInitGame() {
input.setEnabled(true);
// Create a new mouse. Restrict its movements to the display screen.
am = new AbsoluteMouse("The Mouse", display.getWidth(), display
.getHeight());
// Get a picture for my mouse.
TextureState ts = display.getRenderer().createTextureState();
URL cursorLoc = TestTrianglePicking.class.getClassLoader().getResource(
"textures/arrow_up_blue.png" ); //this texture is from my system, so provide an own here
Texture t = TextureManager.loadTexture(cursorLoc, Texture.MinificationFilter.NearestNeighborNoMipMaps,
Texture.MagnificationFilter.Bilinear);
ts.setTexture(t);
am.setRenderState(ts);
// Make the mouse's background blend with what's already there
BlendState as = display.getRenderer().createBlendState();
as.setBlendEnabled(true);
as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
as.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
as.setTestEnabled(true);
as.setTestFunction(BlendState.TestFunction.GreaterThan);
am.setRenderState(as);
// Get the mouse input device and assign it to the AbsoluteMouse
// Move the mouse to the middle of the screen to start with
am.setLocalTranslation(new Vector3f(display.getWidth() / 2, display
.getHeight() / 2, 0));
// Assign the mouse to an input handler
am.registerWithInputHandler( input );
// Create the box in the middle. Give it a bounds
b = new Box("My Box", new Vector3f(-1, -1, -1), new Vector3f(1, 1, 1));
b.setModelBound(new BoundingBox() );
b.updateModelBound();
// Attach Children
rootNode.attachChild(b);
rootNode.attachChild(am);
// Remove all the lightstates so we can see the per-vertex colors
lightState.detachAll();
b.setLightCombineMode( Spatial.LightCombineMode.Off );
pr = new BoundingPickResults();
((FirstPersonHandler) input ).getMouseLookHandler().setEnabled( false );
}
// This is called every frame. Do changing of values here.
protected void simpleUpdate() {
// Get the mouse input device from the jME mouse
// Is button 0 down? Button 0 is left click
if (MouseInput.get().isButtonDown(0)) {
Vector2f screenPos = new Vector2f();
// Get the position that the mouse is pointing to
screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
// Get the world location of that X,Y value
Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
System.out.println( worldCoords.toString() );
// Create a ray starting from the camera, and going in the direction
// of the mouse's location
Ray mouseRay = new Ray(worldCoords, worldCoords2
.subtractLocal(worldCoords).normalizeLocal());
// Does the mouse's ray intersect the box's world bounds?
pr.clear();
rootNode.findPick(mouseRay, pr);
for (int i = 0; i < pr.getNumber(); i++) {
pr.getPickData(i).getTargetMesh().setRandomColors();
}
}
}
}