10 Replies Latest reply on Mar 23, 2007 5:13 AM by Nickname_is_none

# camera rotation limits not right

I have a camera rotating left/right/up/down, I am trying to limit the rotation to 150 degrees in any direction.

I have this code working for the left/right:

if gPanLeft then pCamera.rotate(0,1,0)
pCamera.transform.rotation.z = max(-150, min(150,pCamera.transform.rotation.z))

I'm using the same code for up/down (using the x axis), but the camera stops about 50 degrees in the up direction and about 210 in the downwards direction.

I thought I might have a problem with the starting angle of my camera (as saved from 3DS Max) and I have tried using different angles to compensate, but they don't seem to do much. I have also checked to see if my camera collision is stopping the rotation, but it isn't.

Can anyone help?
• ###### 1. Re: camera rotation limits not right
Hello,

I think the problem is that we can't rely on the camera's rotation vector to
detect extents of movement, as the y axis can flip -180 if the camera is
rotated a certain amount, this will screw up your max rotation checking and
make the camera appear to jam.

I think if you work with seperate properties for each axis of rotation, edit
and check against the properties rather than the camera's transform, then
apply them to the camera rotation vector in one go this should fix the
problem. Some pseudo code to give the idea...
-------------------------------
property pCamera,pCamRotX,pCamRotY,pCamRotZ

on beginSprite
pCamera = sprite(the currentSpriteNum).camera
pCamRotX = 0
pCamRotY = 0
pCamRotZ = 0
end beginSprite
------------------------------
on exitFrame
if gPanLeft AND pCamRotY < 150 then
pCamRotY = pCamRotY + 1
end if
pCamera.transform.rotation = vector(pCamRotX,pCamRotY,pCamRotZ)
end exitFrame
-------------------------------

Hope this may help

Richard Smith
www.funpods.com

• ###### 2. Re: camera rotation limits not right
Thanks for the response, and for shedding some light on what the problem might be.

I'll give that code a test and let you know if it works. I'll have to play around with it as my camera slowly swings back to it's origin when the mouse button is released.

Thanks.
• ###### 3. Re: camera rotation limits not right
Right, not having much luck.

I understand the priniciple of this, but I can't intergrate it with the rest of my code. I keep getting a script error: "variable used before assigned a value", when I set my properties on a beginsprite.

In the beginsprite script, as well as the script you described above, I also:
_initiate properties of the pSprite, p3Dmember, pCamera
_create the camera's bounding sphere
_make the sphere a child of the camera
_register events for camera collision detection
_origin
_ the movement script above

For the rest of my lingo, I have the camera coliision script, script to move the camera back to it's origin, and the movement script that you also mentioned above.

Any clues as to the holes in my wafer-thin lingo skills?
• ###### 4. Re: camera rotation limits not right
Hi,

Can you get the rotation limit script to work on its own (just get the
camera rotating left and right with limits)? I can't really help with all
the other scripts without seeing them and knowing what you are trying to
achieve. Do all the scripts you mention work correctly before you added the
rotation limit script?

You need to narrow down where that 'variable used before assigned a value'
error is coming from, usually a typo for a property name, or failure to add
a property to the property declaration at the top of the script, or failure
to initialize a variable before using it's value in a statement.

Be aware that the register for collision lingo functions are very unreliable
and slow, and I personally never use it. A radial distance check and ray
casting are usually better options.

Maybe if you describe your project in detail I may be able to suggest
something.

Cheers :)

Richard Smith

• ###### 5. Re: camera rotation limits not right
I did get the script working by itself, then I didnt....it seems to be causing problems when it reaches it's 150 limit then stops the other directions working.

Using you're script above, to then move right, I have used:
if gPanRight AND pCamRotY < 150 then
pCamRotY = pCamRotY - 1
end if

I thought that the degree restriction should then be -150, but that stops it working. I have the same issues for the Up/Down.

As for the actual project, it is a effectively a scope camera in a tunnel that I want pivot around (like a worm).
• ###### 6. Re: camera rotation limits not right
Hi there,

> Using you're script above, to then move right, I have used:
> if gPanRight AND pCamRotY < 150 then
> pCamRotY = pCamRotY - 1
> end if
>
> I thought that the degree restriction should then be -150, but that stops
> it
> working. I have the same issues for the Up/Down.

OK, it seems to be you have a simple logic error. This is the type of thing
you need...
----------------------
if gPanRight AND pCamRotY < 150 then
pCamRotY = pCamRotY+1
end if

if gPanLeft AND pCamRotY > -150 then
pCamRotY = pCamRotY-1
end if
-----------------------

Note the use of 'greater than' with the negative value comparison. Use this
pattern for the X and Y axis, Z is usually left alone. If the camera works,
but the keys seem reversed, just switch the plus and minus signs in the
'body' of each if statement.

Please note I will not be around for the next 4 days (in case you think I'm
ignoring posts). Cheers :)

Richard Smith

• ###### 7. Re: camera rotation limits not right
Ahhhhhh, must've been the only thing I didn't try!!! Thanks.

It seems to working well, thanks for your help.

I'm going to try and add the other bits (zooming, and an automatic slow 'return to origin').
• ###### 8. Re: camera rotation limits not right
Ok, the next step that I can't get working is to make the camera slowly move back to it's origin if none of the movement buttons are pressed. So far I have...

if not (gPanLeft or gPanRight or gPanUp or gPanDown or gZoomIn or gZoomOut) then pCamera.transform.interpolateTo(pOriginalTransform,1)

Rather than using pCamera, should I be using the pCamRotX, pCamRotY, pCamRotZ route? Or is there a totally different/better way that I should be doing this?
• ###### 9. Re: camera rotation limits not right
Hi there,

if you just want to return the rotation back to (0,0,0) use this sort of
thing...
------------------------------
if NOT gPanLeft AND NOT gPanRight AND NOT gPanUp AND NOT gPanDown AND NOT
gZoomIn AND NOT gZoomOut then
pCamRotX = pCamRotX * 0.95
pCamRotY = pCamRotY * 0.95
pCamRotZ = pCamRotZ * 0.95
pCamera.transform.rotation = vector(pCamRotX,pCamRotY,pCamRotZ)
end if
-------------------------------
Check out how the if logic differed from yours to see why it was locking up.

if the origin you want to return pCamera to is world postion (0,0,0) then
just do this...
-----------
pCamera.worldPosition = pCamera.worldPosition * 0.95
-----------
quick and dirty, but gives nice smooth motion return.

Sorry I took so long to reply, been away.

Good luck

Richard Smith

• ###### 10. Re: camera rotation limits not right
Brilliant! I've also re-alligned my initial 3DS model to accomodate a 0,0,0 allignment (it was originally 90,0,0), and it's working perfectly.

I've got some other amendments to make to my models, but my next step is to add a button to stop all movement except the zoom. So I might be back to this post, but in the meantime, thanks for your help Mr Smith!!