# Adding max boundaries to cameraview

Hi,

I'm trying to set a maximum view area to my camera but so far I didnt have much luck. The player should only be allowed to "look arround" in that area.

I did the following:

With cam.getDirection I found out the following values.

minX=-0.38f

A hint or approach how to possibly solve my problem is also ok!

sounds like the other camera angles are not getting the message…  You might try setting left to an appropriate value (cross direction and your world up)

Solved it!

You more or less put me on the right track.

Apparantly when you change directions the cam.setLeft should be changed as well. For instance if I look to the left and run my method the setLeft would also require a Z greater or smaller than 0 (dependant how you set up your world). In my case I just randomly picked cam.setLeft(new Vector3f(-0.7f, 0.0f, 0.3f)); which stopped my camera from doing barrelrolls.

Thanks It seems I spoke to soon unfortunatly.

Because I was unsatisfied with the way I calculated the Left I used the following line to make sure it was done correctly:

``` cam.setLeft(cam.getUp().cross(cam.getDirection())); ```

However this line assumes that cam.getUp is a constant. This however, isn't the case. It seems that whenever the camera tilts forward (facing "down") the Up also changes. Using the default Vector3f(0,1,0) makes the camera roll arround again eventually.

I'm beginning to wonder if my approach is correct and if I shouldn't be thinking of solving this in a different kind of way.
How are bounding area's normally solved in games?

cam.setLeft(cam.getUp().cross(cam.getDirection()));

This is not assuming the UP direction is constant, it is only assuming it is perpendicular to the facing direction (and both normalized)

duenez said:

cam.setLeft(cam.getUp().cross(cam.getDirection()));

This is not assuming the UP direction is constant, it is only assuming it is perpendicular to the facing direction (and both normalized)

but since Up is defined as (0,1,0) it is pretty much invalid. What would be a good way to calculate Up when facing direction changes?

Well, the way Camera.lookAt does it is:

• Set facing direction

• Set up vector to Y_UP (0,1,0)

• Compute left as a normalized facing cross up

• Compute up vector as normalized facing cross left

This seems redundant (since you set up twice), but it actually works as long as facing is normalized.

Sorry it's not really making sense to me how this would fix my problem.

My problem is that my UP-vector changes whenever the direction my camera is facing isnt the defaul (0,0-1) (which occurs when you move into a bound and get bounced back). When I apply (0,1,0) to my Up-vector when the facing direction isnt the default (0,0,-1), viewing arround gets messy. So I somehow need to calculate the UP for any given viewDirection.

Maybe I'm approaching the problem from the wrong angle, isn't there something like a "setMaxRotationDegreesXAxis" for the Camera?

Have you tried to do what I told you?

Use the code below, and see if it solves it (I assume direction, left and up are already defined)

```         up.set(Vector3f.UNIT_Y).normalizeLocal();         left.set(up).crossLocal(direction).normalizeLocal();         if (left.equals(Vector3f.ZERO)) {             if (direction.x != 0) {                 left.set(direction.y, -direction.x, 0f);             } else {                 left.set(0f, direction.z, -direction.y);             }         }         up.set(direction).crossLocal(left).normalizeLocal(); ```

Whoa! Amazing, it works! Though I have absolutely no idea what you did exactly. Im guessing my poor vector math is to blame? Perhaps you could explain to me what you did exactly?

Thanks! Darklord said:

Whoa! Amazing, it works! :P

Well, what did you expect? it is Math...  :D

Actually, that method of computing the up, left and facing directions is a little bit of a cheat.

It takes the facing direction, and assumes that UNIT_Y is your desired up direction, so it defines the facing-up plane with those two vectors. Note that these two vectors do not cross at 90 deg. (or PI/2 radians  ;)) angle, but for the matter of defining the left direction it suffices.

That is precisely what does next, it computes the left direction with that plane and your original facing direction. This creates a left vector that is perpendicular to the plane (and thus to facing and up). However, remember that our up direction was just a guess, but was good enough to compute the left one.

Finally, we correct the up direction with the two that we have correct up to this point (facing and left), and that is it.

Hope it helps.  XD

i'm pretty much lost when it comes to reading other people's code (i'm the "i can imagine this 9dimensional marriage optimization algorithm in my mind and convert it to code"-type)

if i use this code, will my camera stop at 0,1,0 and 0,-1,0 instead of turning upside down if i move my mouse forward/backbard?

No, not really, but with this code you can easily impose that constraint. What this might do is keep a consistent y-up direction, so if you go more in the up direction after facing up already (depending on your input manager) then it will just rotate the camera to point to the back of what originally was. (Assuming you never look exactly up)

To limit your camera operation you need to override the input handler calculating the y-up direction with your facing direction (assuming they are normalized, a simple dot product suffices). A facing dot up would yield -1 if you are facing directly down, and 1 if you are facing directly up, so you can set your limits to -0.9 to 0.9 for instance.

Hope it helps