Rotating the Camera Around an Object (Free-Look)
By RUCCUS of RUCCUS.net
I've seen this asked a few times and realized I'd done it a while back without realizing it could help, so here's how to make the mouse or arrow keys rotate around an object and use the scroll wheel/arrow keys to zoom in and out.
`FREE-LOOK WITH MOUSE
`Create by RUCCUS of RUCCUS.net
`This snippet is designed to show you how to accomplish using the
`mouse or arrow keys to rotate around and zoom into/out of an
`object; much like those found in popular map editors and
`modelers.
`Teh Codez
`Standard Program Setup
SYNC ON:SYNC RATE 0:HIDE MOUSE:AUTOCAM OFF
`Define main variables
OFFSET#=-300`Starting limb offset
MINZOOM#=-100`Closest zoom extent
MAXZOOM#=-3000`Farthest zoom extent
VIEW_OBJECT#=1`Object to be viewed
`Make an object to view
MAKE OBJECT CUBE 1,10
`Make a dummy object
MAKE OBJECT TRIANGLE 2,0,0,0,0,0,0,0,0,0
`Make a mesh from the object
MAKE MESH FROM OBJECT 1,2
`Add a limb onto the object using the mesh we just made
ADD LIMB 2,1,1
`Offset the limb 700 units away
OFFSET LIMB 2,1,0,0,OFFSET#
`Hide the limb for lag reduction
HIDE LIMB 2,1
`Hide the dummy object for lag reduction
HIDE OBJECT 2
`Start program loop
DO
`Position the dummy object at the object to be looked at
POSITION OBJECT 2,OBJECT POSITION X(VIEW_OBJECT#),OBJECT POSITION Y(VIEW_OBJECT#),OBJECT POSITION Z(VIEW_OBJECT#)
`Position the camera at the dummy object's limb
POSITION CAMERA LIMB POSITION X(2,1),LIMB POSITION Y(2,1),LIMB POSITION Z(2,1)
`Point the camera at the object to be looked at
POINT CAMERA OBJECT POSITION X(VIEW_OBJECT#),OBJECT POSITION Y(VIEW_OBJECT#),OBJECT POSITION Z(VIEW_OBJECT#)
`Use the mouse wheel (mousemovez) to zoom in/out by inc/decreasing
`the offset
IF OFFSET#<=-100 AND OFFSET#>=-3000
INC OFFSET#,MOUSEMOVEZ()
`Alternate controls: arrow key zoom
IF KEYSTATE(13)=1 THEN INC OFFSET#,1
IF KEYSTATE(12)=1 THEN DEC OFFSET#,1
ENDIF
`Add limitations to the zooming, change these ass needed
IF OFFSET#<MAXZOOM# THEN OFFSET#=MAXZOOM#
IF OFFSET#>MINZOOM# THEN OFFSET#=MINZOOM#
`Offset the dummy object's limb at the current offset# variable
OFFSET LIMB 2, 1, 0,0,OFFSET#
`Get the mouse movement speeds, multiply them by .1 for less
`jumpyness
CAMY#=CAMY#+MOUSEMOVEX()*.1
CAMX#=CAMX#+MOUSEMOVEY()*.1
`Add limitations to the rotation angles, change these as needed
IF CAMX#>45 THEN CAMX#=45
IF CAMX#>315 THEN CAMX#=315
IF CAMX#<-45 THEN CAMX#=-45
IF CAMX#<-315 THEN CAMX#=-315
`Yrotate/Xrotate the object by the stores mouse speeds
IF RIGHTKEY()=0 AND LEFTKEY()=0
YROTATE OBJECT 1,CAMY#
XROTATE OBJECT 1,CAMX#
ENDIF
`Alertane control: leftkey rotation
IF LEFTKEY()=1
YROTATE OBJECT 2,WRAPVALUE(OBJECT ANGLE Y(2)+-.1)
ENDIF
`Alternate control: rightkey rotation
IF RIGHTKEY()=1
YROTATE OBJECT 2,WRAPVALUE(OBJECT ANGLE Y(2)+.1)
ENDIF
`Alternate control: upkey rotation
IF UPKEY()=1
XROTATE OBJECT 2,WRAPVALUE(OBJECT ANGLE X(2)+.1)
ENDIF
`Alternate control: downkey rotation
IF DOWNKEY()=1
XROTATE OBJECT 2,WRAPVALUE(OBJECT ANGLE X(2)+-.1)
ENDIF
`Refresh the screen
SYNC
`End main program loop
LOOP
Breakdown: Basically you make a dummy object, a triangle with 0,0,0,0,0,0,0,0,0 dimensions for minimal amount of lag. Then add a limb to that object and offset it away from it a bit. Then you can do this:
Zooming: Using the mousemovez() command we can get the mouse wheel movement speed, using that speed we can increase or decrease the offset of the dummy object's limb. Increasing or decreasing the offset will have a zooming effect since wherever the limb goes the camera goes.
Rotating: Using the mousemovex() and mousemovey() commands we can yrotate and xrotate the dummy object with these values. Rotating the dummy object will in turn move it's limb with it, and since once agan the camera goes where the limb goes, a rotation effect is given.
I added in arrowkey controls aswell since some people might need that:
Upkey = Zoom in
Downkey = Zoom out
Leftkey = Rotate left
Rightkey = Rotate right
+ = Zoom in
- = Zoom out
Editing it to fit your needs:
To change the default distance of the camera, change the
OFFSET# variable.
To change the max distance able to zoom out, change the
MAX_ZOOM# variable.
To change the max distance able to zoom in, change the
MIN_ZOOM# variable.
To change the object being viewed, change the
VIEW_OBJECT# variable
Thats basically it, if you need help understanding it just ask and Ill go into more detail but it's pretty simple for the novice to understand.
Hope it helps,
RUCCUS.
Current Projects: SHADE - Game Maker | Mecho - TGC Puzzle Entry | Halo Physics Engine | COLD - Polygonal Collision Detection