# Coordinates of the intersection of camera frustum and spatial

Hi!

I’m just playing around with the JME and trying to implement a simple game with a static camera.

I have a large Terrain Quad, the camera is set to a specific position and is not movable.

Now I want to know at which x-Coordinate the planes of the camera frustum cross my terrain quad (for a specific z-Coordinate).

I already found out that I can check if a spatial is inside the camera fructum via 1 FrustumIntersect intersect = cam.contains(spatial.getWorldBound());

But I want the specific coordinates!?

Any hints?

You can get the distance of the frustum using cam.getFrustumFar(). With that and the location of the camera you can calculate where it crosses.

May I ask what you need that point for?

I want to move spatials on the x-Axis through the screen.

The spatials should not suddenly appear on the screen, the should move from the outside of the frustum inside the cameras view.

With the coordinates I would know where I can initially place the spatials so that they are outside the frustum, but near the border of the frustum.

I didn’t quite get your suggestion with the getFrustumFar() Method… Could you tell me some details?

Frustum far gets you the distance from the camera to the far point. So if your camera is at 0,0,0 and your frustumFar value is at 100, and you are looking down the +X axis, then the far point is at 100,0,0

I’m pretty sure the frustum culling will make the spatials suddenly appear when they cross the threshold. If you want them to pass through a surface, you will probably want a quad there that they move through.

Look at the mouse picking tutorial.

Use that to cast a ray out to your terrain from the far left of your screen and find the intersection point you are looking for…

I’m sorry,I think I still don’t get your solutions.

Maybe its best if I describe my game a little better:

I’m looking with a static camera down the Z-Axis. Now my spatials should cross the scenery on the X-Axis. It’s kind of like those old “Moorhuhn” shooting games…

All the spatials move on the same z-coordinate, so its basically a 2D Game.

What I want are the X-Coordinates where the camera frustum intersects with the Z-Coordinate where I move all my spatials.

I think I understood the basic functionality of Rays as described in the HelloPicking Tutorial, but I can’t see a way to use it in my scenario.

http://dl.dropbox.com/u/230524/Untitled.png

Maybe the picture helps…

Take the angle of the direction where the camera is facing (down the Z axis). Ask the camera for the frustum angle. Divide that by two and you get the angle from the center where the camera is looking to the right or left edge of the frustum triangle.

http://i.imgur.com/bEQJ0.png

Solve for L2. That is the distance from the center line you are looking down.

Or fire a ray from the camera, down that angle and intersect it with the “plane” you have. Take the x coordinate of the result.

Look at the mouse picking code.

Now pretend someone clicked a mouse at the very left of the screen and 2/3rds of the way down.

That ray will shoot out along your frustrum and hit the ground at some point.

Check the intersection point for the ground - anything you spawn left of there will be off screen, it will come on screen as it passes that point.

If you use that picked z co-ordinate as well then that will find a distance appropriate to the screen size being used.

To move closer then draw a line between the picked point and the camera and follow it. To move further away do the same in the other direction.

And because I’m a trig nerd and killing time before dinner:

The breakdown should be: (based on sploreg’s diagram)

L2 / L1 = tan(a)

So:

L2 = tan(a) * L1

And tan(a) will be ‘constant’ for your frustum settings… so you only need to calculate that once, really.

Allright, now I got it…