Hey chaps,
I cannot see any mouse examples in JME3 and AbsoluteMouse doesnt appear to exist.
Does anyone have a very simple example of a mouse which is not tied to the camera in JME3?
Thanks
Andy
I wrote one today with a friend of mine, sources are at my friends computer so i cant give an example right now, but its simply to do.
You only have to set cursorVisible of inputManager to false, adding a shape in the gui node (shape in bucket gui) and adding a "MouseMoveListener" to move the shape.
You can use RawInputListener to get the exact position of the mouse on the window, and like Lutherion you can make the cursor visible.
Changing the cursor icon isn't possible at this point, but it is planned for the future.
Ah ok - and restricting the mouse's movement within the window bounds must be done manually?
Right, but it's also easy, left down is 0,0 and right up is cam.width,cam.height, if i remember right - relativ to gui node.
Here is some code, if you want to pick 3d objects with your mouse.
// 1. Reset results list.
CollisionResults results = new CollisionResults();
// 2. Aim the ray from cam loc to cursor position.
Vector3f worldCoordinates = cam.getWorldCoordinates(new Vector2f(mousepositionx, mousepositiony), 0);
Vector3f worldCoordinates2 = cam.getWorldCoordinates(new Vector2f(mousepositionx, mousepositiony), 1);
Ray ray = new Ray(worldCoordinates, worldCoordinates2.subtractLocal(worldCoordinates).normalizeLocal());
// 3. Collect intersections between Ray and Shootables in results list.
mapfieldmeshes.collideWith(ray, results);
System.out.println("
Collisions? " + results.size() + "
");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
}
Thanks for that code snippet - will proove useful.
Anyhow - Where exactly do i restrict the mouse's movement scope - cos its too late when the event is fired i.e when the mouse is moved to the top right - i get the evt but i dont have access to the mouse object itself so how do i stop it from being able to move anymore?
Cheers,
Andy
The "MouseListener" moves your'e mousepointer shape, right?- it has to! So there you have access. I did control the mouse position after translation and correct the translation if necessary.
Em, not getting you this is my mouse listener:
public class MouseInputManager implements RawInputListener{
public void onJoyAxisEvent(JoyAxisEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void onJoyButtonEvent(JoyButtonEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void onMouseMotionEvent(MouseMotionEvent evt) {
System.out.println("Mouse moved x:"+evt.getX()+" Y:"+evt.getY());
}
public void onMouseButtonEvent(MouseButtonEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void onKeyEvent(KeyInputEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
How does what your saying fit into that?
Where do you move your "own" mousepointer shape? The hardware mouse is not visible right?
The listener must have access to your shape
I dont have my own shape.
I do
inputManager.setCursorVisible(true);
The hardware mouse is visible and movement events are correctly notified.
And thats the lot.
Lutherion said:So you didnt read the first posts properly. You have to make the mouse invisible and fake the mousepointer with a shape in the gui-node.
I wrote one today with a friend of mine, sources are at my friends computer so i cant give an example right now, but its simply to do.
You only have to set cursorVisible of inputManager to false, adding a shape in the gui node (shape in bucket gui) and adding a "MouseMoveListener" to move the shape.
I presumed you had made a typo…
what is this gui-node you speak of? Got an example?
The gui node is the "root node" of the gui, normally rendered in orthogonal mode.
SimpleApplication(SA) of jme3 has a protected gui node, so if you extend SA, you can access the gui node in your app, otherwise look at the source of SA to make your own.
You can add a Geometry in that node:
SimpleApplication.this.guiNode.attachChild(geometry); geometry is (in that case) your faked mouse pointer. This geometry has to be translated on mouse movement and is used for further controls like mouse picking and so on.
For orthogonal rendering it has to be set: geometry.setQueueBucket(Bucket.Gui); to render orthogonal.
I c,
so I have made a geometry as follows:
Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);
Geometry blue = new Geometry("Box", box1);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
mat1.setColor("m_Color", ColorRGBA.Blue);
blue.setMaterial(mat1);
blue.setQueueBucket(Bucket.Gui);
this.guiNode.attachChild(blue);
and now on mouse move i do :
public void onMouseMotionEvent(MouseMotionEvent evt) {
System.out.println("Mouse moved x:"+evt.getX()+" Y:"+evt.getY());
mouse.setLocalTranslation(new Vector3f(evt.getX(),evt.getY(),1));
}
But i dont see my blue box anywhere...?
Could deal with the box, try to take a Quad(10,10) on x,y= 10 and z=0 without mouse movement for the first to see if the quad is there.
Right that worked - nice.
So i suppose I need an image of a mouse pointer, or a model of some sort.
Cheers,
Andy
Yes, normally you would choose an image with transparency to make the pointer looks nice.
Alright so I have now designed my mouse.png file
So I have never had to actually load and display an image before. Do I need to load it in as a texture or something and texture a blank quad?
Confused.
Please explain!
Yes, load it as a texture and add it to your quad(parent geometry) with transparency mode:
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
TextureKey key = new TextureKey(nameOfTexture, true);//Locator must be set to root directory
texture = getAssetManager().loadTexture(key);