Author Topic: mouse controlled objects  (Read 5752 times)

Offline drounal

  • byte
  • *
  • Posts: 16
    • View Profile
mouse controlled objects
« on: May 17, 2006, 11:31:56 pm »
Hey, another issue I need to work out.
how can I select object on the map using mouse1 (click and/or selection) and tell a given object to go where I click with mouse2 ?
thx in advance

Offline manumoi

  • long
  • ***
  • Posts: 121
    • View Profile
    • http://www.iro.umontreal.ca/~blanchae
mouse controlled objects
« Reply #1 on: May 18, 2006, 07:07:31 am »
hello, to select an element in a 3D environment, you can do something like that
Code: [Select]

  public Object3D pickingObject(int mouseX, int mouseY){
SimpleVector ray=Interact2D.reproject2D3D(camera, buffer, mouseX, mouseY);
  int[] res=Interact2D.pickPolygon(theWorld.getVisibilityList(), ray);
if (res!=null) {
   return theWorld.getObject(Interact2D.getObjectID(res));  
}
    return null;
    }

where mouseX and mouseY can be obtained by the mouseEvent. The mouseEvent also contains information on which button triggered it.

To make elements move from a point to another, you have to obtain the position of your starting point and your ending point in order to use a pathfinding algorithm. In general a 3D environment is linked with a 2D array that contains useful information for this algorithm (pathfinding can also be done in X dimensions but it s more and more complex).

You can for example add a method to your element class like this one:
Code: [Select]

public ArrayList doPathfinding(Dimension endingCoordinates){ // make a  raycast on a mouse event more or less like in the previous method in order to obtain the ending coordinates
     Dimension startingCoordinates = selectedElement.get2DPosition(); // this method should return the current position of your element in the 2D map
     return processPath(startingCoordinates, endingCoordinates); // method where you apply your pathfinding algorithm. The ArrayList obtained contains the different node of the path.
}


This should work for non dynamic pathfinding (meaning that you don t have a dynamic environment i.e. an environment where elements that could "collide/affect the path" are moveable).

Doing dynamic pathfinding is far more complex in order to be efficient.

Hope this helps


Manu

Offline drounal

  • byte
  • *
  • Posts: 16
    • View Profile
mouse controlled objects
« Reply #2 on: May 18, 2006, 09:49:36 am »
indeed it does, thanks a lot !

one more question by the way, using the first code u gave me. the PickingObject method returns an Object3D; how can I make it return the "Cube" object that Object3D belongs to ?
("Cube" is here made with 5 Object3D)

Offline drounal

  • byte
  • *
  • Posts: 16
    • View Profile
mouse controlled objects
« Reply #3 on: May 18, 2006, 10:02:21 am »
Oh ! one more thing.

If I have an array filled with structure "cube" on one hand and "cube" returned by the PickingObject method for example; how do I get the number of the "cube" in the array ?
(see what I mean ?)

Louis

Offline drounal

  • byte
  • *
  • Posts: 16
    • View Profile
mouse controlled objects
« Reply #4 on: May 18, 2006, 10:35:11 am »
when trying to access the camera (in the CarTest class), I get :
non-static camera cannot be referenced from a static context
how do u think I solve this ? (nothing appears to be static in the MouseMapper appart from BUTTON1 and BUTTON2)

Offline manumoi

  • long
  • ***
  • Posts: 121
    • View Profile
    • http://www.iro.umontreal.ca/~blanchae
mouse controlled objects
« Reply #5 on: May 18, 2006, 04:53:33 pm »
hello the only static part of CarTest is the main method. So unless you make a call to camera inside this method, you should not have this exception. If that is the case, you just have to keep the reference of your instance of carTest and retrieve your camera or the method where the camera is called through this instance.

ex:

Code: [Select]

public static void main(String[] args) {
      CarTest c = new CarTest(args);
      c.MethodThatUsesCamera();
   }

If the object3D that is retrieved is already a cube, you just have to cast it
Code: [Select]
return (Cube) theWorld.getObject(Interact2D.getObjectID(res));
(if it s not you may have a ClassCastException.)

You can also use the instanceof keyword to confirm that it is a cube
Code: [Select]
Object3D o = theWorld.getObject(Interact2D.getObjectID(res));
if (o instanceof Cube)
      return (Cube) o;
return null;


Manu

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
mouse controlled objects
« Reply #6 on: May 18, 2006, 05:29:10 pm »
Or, if "Cube" is just a composition of Object3Ds and can't be returned directly (or, like in the car example, it is an Object3D itself but contains others), maybe this helps: Write an interface CubePart like so:

Code: [Select]

public interface CubePart {
     Cube getMaster();
}


Make all your parts of Cube (and maybe Cube itself) implement it and return the  instance of Cube in all of them (has to be stored in the sub object, which is not very nice, but works...). Then you can do:


Code: [Select]

Cube cube=((SubObject3D) theWorld.getObject(Interact2D.getObjectID(res))).getMaster();


I suggest not to code against Cube, but against an Interface "Entity" or similar, so that this method can handle more than just instances of Cubes, but to keep the example simple, i restricted it to Cube here.

Offline drounal

  • byte
  • *
  • Posts: 16
    • View Profile
mouse controlled objects
« Reply #7 on: May 18, 2006, 09:16:27 pm »
thx for all your proposals. any idea concerning the number of a given cube in a cube array ? If I have the cube can I find the number easily ?
thx again and in advance

Offline manumoi

  • long
  • ***
  • Posts: 121
    • View Profile
    • http://www.iro.umontreal.ca/~blanchae
mouse controlled objects
« Reply #8 on: May 19, 2006, 09:06:27 pm »
Hello,

If i correctly understand your question (and i m not sure of that  :? ) you just have to use the getID() method, given the fact that each Cube is an Object3D

Manu