mouse controlled objects

Started by drounal, May 17, 2006, 11:31:56 PM

Previous topic - Next topic

drounal

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

manumoi

hello, to select an element in a 3D environment, you can do something like that

 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:

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

drounal

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)

drounal

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

drounal

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)

manumoi

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:


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
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
Object3D o = theWorld.getObject(Interact2D.getObjectID(res));
if (o instanceof Cube)
     return (Cube) o;
return null;


Manu

EgonOlsen

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:


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:



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.

drounal

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

manumoi

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