Absolute Mouse and the 3D World

I am busy coding an RTS type of game. What I have is the game floor, which is just a large, flat, Box object at the origin.



Now, as in all RTS's you have to have the ability to place buildings on the ground. Now, I have an Absolute Mouse and I need to use this to place buildings. I want to place a building where the Absolute Mouse is on the ground, but as I'm using AbsoluteMouse.getX(), i'm getting the mouse's position relative to screen so it places the building all over the place in the game world.



How do I make it place the building where my mouse is pointing on the screen?



Thanks in advance

You do picking by casting a ray from the screen location. Look for TestPicking and TestTerrainPicking in the source jmetest package.

Cheers,

Normen

There is a good tutorial for this in HelloMousePicking in beginner tutorials section .

Ok, so I followed the HelloMousePick tut,



The thing is, I want to know at what world co-ordinate the Ray hit the 'ground' or box object. I wonder if there is perhaps a method for this sort of thing?



The reason I ask is that I'm really quite confused by all this.



The code I have so far

         // 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);
         // 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();
         scene.findPick(mouseRay, pr);


That was copied straight from the tut. How do I extract the location at which the ray hit the 'ground'?

Thanks and sorry for the newbness.

scene.findPick(mouseRay, pr); <- this line goes through all children of the scene, looks for geometrys that would collide with your ray and stores that info in the supplied PickResult object.

Cheers,

Normen

Well, there is only one bit of geometry in the sceneā€¦I need to know at what point it struck the geometry.



Any way to do this?

You can do it like this :


 TrianglePickResults pr = new TrianglePickResults();
            pr.setCheckDistance(true);

            tb.calculatePick(mouseRay, pr);
            Vector3f intersectionPoint;
             intersectionPoint = ((TrianglePickData) pr.getPickData(0)).getIntersectionPoint();



tb is the ground as you said , and mouseRay is the ray originating from the mouse pointer

For those of you using jME3 its a bit different i believe.



If you want to find the contact point on the terrain where your ray hits use :



    pt = results.getCollision(0).getContactPoint();



Remember, results is a collection - your ray may hit 3 or 4 different objects on its travels, so its usually safer to loop over all the results and inspect them individually - checking if one of them is the terrain!

Thanks very much!



To hadi, that worked perfectly, so thank you very much.



To renegadeandy, unfortunately I'm not using jME 3, so its of no use to me, however anyone who does and is looking for this is sure to benefit so thanks.