Mouse picking outside terrain [SOLVED]

Hey guys

I am currently developing an RTS, but I'm having a bit of a problem.

I need people to be able to zoom in on the location of the mouse. Now, when I hover the mouse over the terrain map and I zoom in, I can quite easily make it zoom in on the location of the mouse, through the use of ray casting. So far everything is fine.

But when the mouse is location outside the terrain, I also need to be able to know at which x- and z-coordinates the mouse is located. I was thinking about using ray casting on the plane with y-coordinate being 0, but I don't know how I could achieve this.

Can anyone help me out here please?

Thanks in advance


Well, you can always do zooming by modifying the field of view in the projection matrix. That's how a lot of games do the scope effect for a sniper rifle, that way you don't care if the ray is intersecting the terrain or not.

Though, what exactly would you be zooming to without a terrain being there? If it's something else, like a 3D model you could do picking on that too, or have caps on how far you can zoom in.

Well, if the mouse is located outside the terrain, I would let the player zoom in on the terrain trimesh located closest to the mouse. This can easily be achieved by doing the following:

if(mouseLocation.x > maxX)

   mouseLocation.x = maxX;

Same for the other 3 bounds.

Because of the height difference, the other coordinate will be located a little bit off, but that's no big deal.

What I was actually thinking of to solve this problem, is to find the coordinates of the intersection of my ray (from the mouse into the flield) with the xz-plane located at y = 0. But does anyone know how I can do this?

After some research, I found out that some math was enough to achieve this. Problem solved :slight_smile:

The solution:

Define a plane (with normal N being the vector (0, 1, 0) in my case) and a vector from the start point of the ray to the end point of the ray. Find the intersection of the plane and the vector and problem solved.

I found out that jME has nothing to do with this, except for the ray itself. It was a pure mathematical problem.