Mouse in BaseGame

Can someone tell me how I can add the mouse in BaseGame ?? I write it in SimpleGame but when i try to do it in BaseGame it doesn't work. TIA everyone!

MouseInput.get().setCursorVisible(true);



Try searching the forums in the future as this one has been talked about a LOT. ;)

Thats not what I want. In SimpleGame I use the code:



 protected void InitMouse() {
     am = new AbsoluteMouse("The Mouse", display.getWidth(), display
             .getHeight());
           TextureState ts = display.getRenderer().createTextureState();
                 URL cursorLoc = HelloMousePick.class.getClassLoader().getResource(
                         "mission/cursor.png" );
                 Texture t = TextureManager.loadTexture(cursorLoc, Texture.MM_LINEAR,
             Texture.FM_LINEAR);
           ts.setTexture(t);
           am.setRenderState(ts);

           AlphaState as = display.getRenderer().createAlphaState();
           as.setBlendEnabled(true);
           as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
           as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
           as.setTestEnabled(true);
           as.setTestFunction(AlphaState.TF_GREATER);
           am.setRenderState(as);

           am.setLocalTranslation(new Vector3f(display.getWidth() / 2, display
             .getHeight() / 2, 0));
                 am.registerWithInputHandler( input );   
                 rootNode.attachChild(am);
                 pr = new BoundingPickResults();
                 (( FirstPersonHandler ) input ).getMouseLookHandler().setEnabled( false );
 }



and I has the moouse pointer which was the cursor.png picture. When I transfer it to BaseGame it doesn't work anymore. What am I doing wrong ??

u dont wanna use absolutemouse, its slow and lengthy in terms of lines of code.



just use hardware mouse, which darkfrog told u earlier.

and set the cursor of the hardware mouse~



it does the same thing but faster and simpler. :wink:

you can do that…


   private void initCursor() {
      URL file = SomeClass.class.getClassLoader().getResource("cursor.png");
      Texture texture = TextureManager.loadTexture(file, Texture.FM_LINEAR, Texture.FM_LINEAR);
      texture.setWrap(Texture.WM_BCLAMP_S_BCLAMP_T);
      TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
      ts.setTexture(texture);
      am.setRenderState(ts);
      
      AlphaState alpha = display.getRenderer().createAlphaState();
      alpha.setBlendEnabled(true);
      alpha.setSrcFunction(AlphaState.SB_SRC_ALPHA);
      //alpha.setDstFunction(AlphaState.DB_ONE);
      alpha.setTestEnabled(true);
      alpha.setTestFunction(AlphaState.TF_GREATER);
      alpha.setEnabled(true);
      
      am.setRenderQueueMode(Renderer.QUEUE_ORTHO);
      
      am.setCullMode(SceneElement.CULL_NEVER);
      am.setRenderState(ts);
      am.setRenderState(alpha);
      am.setLocalScale(new Vector3f(1, 1, 1));
      
      //am.setSpeed(0.5f);
      
      cursor = new Node("Cursor");
      cursor.attachChild(am);
      
      rootNode.attachChild(cursor);
   }



OR you can set the hardware cursor to an image


URL someUrl = SomeClass.class.getClassLoader().getResource("cursor.png");
MouseInput.get().setHardwareCursor(someUrl);

THX a lot. I have one more question: How I can check, what object I clicked on ?? Get a pointer to this object.

aderal said:

THX a lot. I have one more question: How I can check, what object I clicked on ?? Get a pointer to this object.


u need to create a ray from the point where ur mouse clicked.

then do a ray collision check.

u might wannt check out the mousePick tutorial or search on the forum.

Can u give me a code ?? I use the hardware mouse, I create it with code:


URL someUrl = SomeClass.class.getClassLoader().getResource("cursor.png");
MouseInput.get().setHardwareCursor(someUrl);

i can give u my code, and u can probably modify it to suit ur game :smiley:



this will help u find the point the mouse hit


   /**
    * <This is a utility method>.
    * Find the intersection of the calling object and the object with given name in the direction
    * defined by the given Ray. The returned value is in the player's current island's local coordinate
    * system.
    * If the target cannot be found or the ray does not hit the target, return null.
    * @param ray The ray defines the direction of searching.
    * @param targetName The name of the model which is being looked for.
    * @return A Vector3f which defines the intersection point.
    */
   protected static Vector3f findBounding(Ray ray, String targetName) {
      Vector3f intersection = new Vector3f();
      PickResults results = new TrianglePickResults();

      // Use the ray to find all the models on the player's island which are hit by the ray.
      results.clear();
      player.getIsland().findPick(ray, results);
      
      // Find the target model in the results.
      boolean found = false;
      boolean hit = false;
      for(int i = 0; i < results.getNumber() && found == false; i++)
      {
         TriMesh model = (TriMesh)results.getPickData(i).getTargetMesh().getParentGeom();
         if(model.getName().equalsIgnoreCase(targetName))
         {
            found = true;
            // Find the intersection where the ray hits the target's boundingBox in world
            // coordinate system.
            Vector3f[] vertices = new Vector3f[3];
            for(int j = 0; j < model.getTriangleCount() && hit == false; j++)
            {
               model.getTriangle(j, vertices);
               hit = ray.intersectWhere(
                     vertices[0].addLocal(model.getWorldTranslation()),
                        vertices[1].addLocal(model.getWorldTranslation()),
                           vertices[2].addLocal(model.getWorldTranslation()), intersection);
            }
         }
      }
      // If there is an intersection found, translate coordinate system.
      if(found == true && hit == true)
      {
         // Translate the worldCoordinates into the player's island local coordinate system.
         intersection.subtractLocal(player.getIsland().getWorldTranslation());
      }
      // If the target cannot be found or the ray does not hit the target, return null.
      if(found == false || hit == false)
      {
         return null;
      }
      // Return the intersection.
      return intersection;
   }



this is the mouse listener


import com.jme.input.MouseInputListener;
import com.jme.math.Vector2f;

/**
 * MouseListener handles all the mouseInput actions. And with each mouse action, a particular
 * game action is performed.
 *
 * @author Neakor
 *
 */
public class MouseListener implements MouseInputListener{
   // Screen Coordinates.
   private Vector2f mouseScreenCoords;
   
   /**
    * Constructor of MouseListener.
    */
   public MouseListener() {

   }

   /**
    * This method gets called when ever a button action happends.
    * @param button The button index.
    * @param pressed True indicates pressed down, false indicates released. Holding a button down
    *               without motion counts as one click at the initial clicking position.
    * @param x The x screen coordinate.
    * @param y The y screen coordinate.
    */
   public void onButton(int button, boolean pressed, int x, int y) {
      // If the left button was pressed, pass the screen position to Player's move method.
      if(button == 0 && pressed == true)
      {
         Vector2f mouseScreenCoords = new Vector2f(x, y);
         this.mouseScreenCoords = mouseScreenCoords;
      }
   }

   public void onMove(int xDelta, int yDelta, int newX, int newY) {

   }

   public void onWheel(int wheelDelta, int x, int y) {
      
   }
   
   public Vector2f getScreenCoords() {
      return this.mouseScreenCoords;
   }
   
}



this part of the method create the ray.


         Vector3f mouseWorldCoords = display.getWorldCoordinates(mouseScreenCoords, 0);
         Vector3f mouseWorldCoords2 = display.getWorldCoordinates(mouseScreenCoords, 1);
         Vector3f direction = mouseWorldCoords2.subtractLocal(mouseWorldCoords).normalizeLocal();
         Ray mouseRay = new Ray(mouseWorldCoords, direction);
         String target = this.island.getName();

         Vector3f intersection = Lounge.findBounding(mouseRay, target);

Ok I solve the problem, thx one more time for your posts an help. :slight_smile:

np :smiley: