Camera Control Function
Created By RUCCUS
Note: The old thread title didn't suit the function, seeing as the function serves multiple camera controls and the old thread was entitled 3rd Person View Function, the old thread will be locked: http://forum.thegamecreators.com/?m=forum_view&t=63718&b=6
Use this function in your games for fast and easy camera control. It allows you to set a wide variety of variables to suit your needs. All you need to do is include the function in your program and call it once and it will handle everything else; just follow the simple instructions at the end of the post.
Great for any camera view you can think of, there's a sample at the bottom of this post containing 5 different views made using the function; 1st person view, 2nd person view, 3rd person view flying style, 3rd person view walking style, and 3rd person view map editor style w/ scroll button zooming.
There's a lot of parameters but after using it for a while you'll get used to it and adding it will become a cinch.
Features:
Easy implementation
Mouse/Camera rotation option
Mouse/Object rotation option
Camera rotation limits
Camera rotation speed
Customizable camera positioning and tilting
Easy zooming implementation
Syntax:
ThirdPersonView(Object, Dummy, Mesh, X, Y, Z, Tilt, Speed, DumX, DumY, ObjX, ObjY, MaxUp, MaxDown, MaxLeft, MaxRight)
Breakdown:
Object: The Object you wish to have 3rd Person View applied to.
Dummy: A "Dummy" object used for camera control. Specify this object number as an object not created in the game yet, the function will create the object using the dummy number.
Mesh: An uncreated mesh number that the function can use to add limbs with.
X: X offset for the camera; how far left or right of the user the camera will be.
Y: Y offset for the camera; how high or low the camera will be.
Z: Z offset for the camera; how close or far from the object the camera will be.
XTilt: The angle offset for the camera on the x axis, how far left or right of the object the camera should point.
YTilt: The angle offset for the camera on the y axis, how far above or below the object the camera should point.
ZTilt: The angle offset for the camera on the z axis, how far infront or behind the object the camera should point.
Speed: The speed at which the mouse rotates the camera and object. It's recommended setting this to a decimal (.1 works good). Setting this to 0 will cancel out the mouse rotation code, incase you don't want to use the mouse.
DumX: Set this parameter to 1 if you wish the mouse to rotate the camera around the x axis of the object.
DumY: Set this parameter to 1 if you wish the mouse to rotate the camera around the y axis of the object.
ObjX: Set this parameter to 1 if you wish the mouse to rotate the object around it's x axis as the mouse moves.
ObjY: Set this parameter to 1 if you wish the mouse to rotate the object around it's y axis as the mouse moves.
MaxUp: The Max upward rotation of the object/camera. Setting this to 0 will give no limits. Set this to a negative number.
MaxDown: The Max downward rotation of the object/camera. Setting this to 0 will give no limits. Set this to a positive number.
MaxRight: The Max rotation to the right the object/camera can go. Setting this to 0 will give no limits. Set this to a negative number.
MaxLeft: The Max rotation to the left the object/camera can go. Setting this to 0 will give no limits. Set this to a positive number.
Function Source Code:
FUNCTION CameraControl(Object, Dummy, Mesh, X#, Y#, Z#, XTilt#,YTilt#,ZTilt#, Speed#, DumX, DumY, ObjX, ObjY, MaxUp#, MaxDown#, MaxLeft#, MaxRight#)
IF OBJECT EXIST(Dummy)=0
DIM CamX#(0)
DIM CamY#(0)
MAKE OBJECT TRIANGLE Dummy, 0,0,0,0,0,0,0,0,0
MAKE MESH FROM OBJECT Mesh, Dummy
ADD LIMB Dummy, 1, Mesh
DELETE MESH Mesh
ENDIF
IF OBJECT EXIST(Dummy)=1
OFFSET LIMB Dummy, 1, X#, Y#, Z#
IF Speed#=0
IF X#=0 AND Y#=0 AND Z#=0
POSITION CAMERA OBJECT POSITION X(Object),OBJECT POSITION Y(Object),OBJECT POSITION Z(Object)
POINT CAMERA OBJECT POSITION X(Object), OBJECT POSITION Y(Object), OBJECT POSITION Z(Object)
ENDIF
ENDIF
IF X#>0 OR Y#>0 OR Z#>0
POSITION OBJECT Dummy, OBJECT POSITION X(Object),OBJECT POSITION Y(Object), OBJECT POSITION Z(Object)
POSITION CAMERA LIMB POSITION X(Dummy, 1),LIMB POSITION Y(Dummy, 1),LIMB POSITION Z(Dummy, Object)
POINT CAMERA OBJECT POSITION X(Object)+XTilt#, OBJECT POSITION Y(Object)+YTilt#, OBJECT POSITION Z(Object)+ZTilt#
ENDIF
IF Speed#>0
IF X#=0 AND Y#=0 AND Z#=0
POSITION CAMERA OBJECT POSITION X(Object),OBJECT POSITION Y(Object),OBJECT POSITION Z(Object)
XROTATE CAMERA CamX#(0)
YROTATE CAMERA CamY#(0)
ENDIF
CamY#(0)=CamY#(0)+MOUSEMOVEX()*Speed#
CamX#(0)=CamX#(0)+MOUSEMOVEY()*Speed#
IF MaxUp#>0 OR MaxUp#<0
IF CamX#(0)<MaxUp# THEN CamX#(0)=MaxUp#
ENDIF
IF MaxDown#>0 OR MaxDown#<0
IF CamX#(0)>MaxDown# THEN CamX#(0)=MaxDown#
ENDIF
IF MaxLeft#>0 OR MaxLeft#<0
IF CamY#(0)<MaxLeft# THEN CamY#(0)=MaxLeft#
ENDIF
IF MaxRight#>0 OR MaxRight#<0
IF CamY#>MaxRight# THEN CamY#=MaxRight#
ENDIF
IF DumX=1
XROTATE OBJECT Dummy,CamX#(0)
ENDIF
IF DumY=1
YROTATE OBJECT Dummy,CamY#(0)
ENDIF
IF ObjX=1
XROTATE OBJECT Object, CamX#(0)
ENDIF
IF ObjY=1
YROTATE OBJECT Object, CamY#(0)
ENDIF
ENDIF
ENDIF
ENDFUNCTION CameraControl
Sample Code:
Here's some sample code on how to use the function. Alter it to experiment before implementing it into your game, press buttons 1 - 5 to switch between the views included in the program.
SYNC ON:SYNC RATE 0:AUTOCAM OFF:HIDE MOUSE
MAKE OBJECT BOX 1,30,30,30
MAKE OBJECT BOX 2,50,50,50
POSITION OBJECT 2,0,0,100
ZOOM#=-300
MODE=1
DO
` Display some text so we know what buttons do what
TEXT 0,0,"Camera Control Function"
TEXT 0,50,"[1] = 3rd Person View | Flying Style"
TEXT 0,65,"[2] = 3rd Person View | Walking Style"
TEXT 0,80,"[3] = 3rd Person View | Map Editor Style w/ Scroll Zoom"
TEXT 0,95,"[4] = 2nd Person View"
TEXT 0,110,"[5] = 1st Person View"
`Mode Definition
FOR KEY=2 TO 6
IF KEYSTATE(KEY)=1 THEN MODE=KEY-1
NEXT KEY
`3rd Person View | Flying Style
IF MODE=1
CameraControl(1,3,1,0,200,-300,0,100,0,.1,1,1,1,1,-70,30,0,0)
ENDIF
`3rd Person View | Walking Style
IF MODE=2
CameraControl(1,3,1,0,200,-300,0,100,0,.1,1,1,0,1,-70,30,0,0)
ENDIF
`3rd Person View | Map Editor Style w/ Scroll Zoom
IF MODE=3
INC ZOOM#,MOUSEMOVEZ()*.1
CameraControl(1,3,1,0,200,ZOOM#,0,0,0,.1,1,1,0,0,-70,30,0,0)
ENDIF
`2nd Person View
IF MODE=4
CameraControl(1,3,1,0,200,-20,0,0,0,.1,0,1,0,1,0,0,0,0)
ENDIF
`1st Person View
IF MODE=5
CameraControl(1,3,1,0,0,0,0,0,0,.1,1,1,1,1,0,0,0,0)
ENDIF
`Some basic controls
IF KEYSTATE(17)=1 OR UPKEY()=1 THEN MOVE OBJECT 1,1
IF KEYSTATE(31)=1 OR DOWNKEY()=1 THEN MOVE OBJECT 1,-1
IF KEYSTATE(30)=1 OR LEFTKEY()=1 THEN MOVE OBJECT LEFT 1,1
IF KEYSTATE(32)=1 OR RIGHTKEY()=1 THEN MOVE OBJECT RIGHT 1,1
SYNC
LOOP
FUNCTION CameraControl(Object, Dummy, Mesh, X#, Y#, Z#, XTilt#,YTilt#,ZTilt#, Speed#, DumX, DumY, ObjX, ObjY, MaxUp#, MaxDown#, MaxLeft#, MaxRight#)
IF OBJECT EXIST(Dummy)=0
DIM CamX#(0)
DIM CamY#(0)
MAKE OBJECT TRIANGLE Dummy, 0,0,0,0,0,0,0,0,0
MAKE MESH FROM OBJECT Mesh, Dummy
ADD LIMB Dummy, 1, Mesh
DELETE MESH Mesh
ENDIF
IF OBJECT EXIST(Dummy)=1
OFFSET LIMB Dummy, 1, X#, Y#, Z#
IF Speed#=0
IF X#=0 AND Y#=0 AND Z#=0
POSITION CAMERA OBJECT POSITION X(Object),OBJECT POSITION Y(Object),OBJECT POSITION Z(Object)
POINT CAMERA OBJECT POSITION X(Object), OBJECT POSITION Y(Object), OBJECT POSITION Z(Object)
ENDIF
ENDIF
IF X#>0 OR Y#>0 OR Z#>0
POSITION OBJECT Dummy, OBJECT POSITION X(Object),OBJECT POSITION Y(Object), OBJECT POSITION Z(Object)
POSITION CAMERA LIMB POSITION X(Dummy, 1),LIMB POSITION Y(Dummy, 1),LIMB POSITION Z(Dummy, Object)
POINT CAMERA OBJECT POSITION X(Object)+XTilt#, OBJECT POSITION Y(Object)+YTilt#, OBJECT POSITION Z(Object)+ZTilt#
ENDIF
IF Speed#>0
IF X#=0 AND Y#=0 AND Z#=0
POSITION CAMERA OBJECT POSITION X(Object),OBJECT POSITION Y(Object),OBJECT POSITION Z(Object)
XROTATE CAMERA CamX#(0)
YROTATE CAMERA CamY#(0)
ENDIF
CamY#(0)=CamY#(0)+MOUSEMOVEX()*Speed#
CamX#(0)=CamX#(0)+MOUSEMOVEY()*Speed#
IF MaxUp#>0 OR MaxUp#<0
IF CamX#(0)<MaxUp# THEN CamX#(0)=MaxUp#
ENDIF
IF MaxDown#>0 OR MaxDown#<0
IF CamX#(0)>MaxDown# THEN CamX#(0)=MaxDown#
ENDIF
IF MaxLeft#>0 OR MaxLeft#<0
IF CamY#(0)<MaxLeft# THEN CamY#(0)=MaxLeft#
ENDIF
IF MaxRight#>0 OR MaxRight#<0
IF CamY#>MaxRight# THEN CamY#=MaxRight#
ENDIF
IF DumX=1
XROTATE OBJECT Dummy,CamX#(0)
ENDIF
IF DumY=1
YROTATE OBJECT Dummy,CamY#(0)
ENDIF
IF ObjX=1
XROTATE OBJECT Object, CamX#(0)
ENDIF
IF ObjY=1
YROTATE OBJECT Object, CamY#(0)
ENDIF
ENDIF
ENDIF
ENDFUNCTION CameraControl
Add-Ons
Feel free to suggest any add-on features for the function. Critisism is encouraged, questions are welcomed. If the suggestion sounds good or the bug is legitimate I'll correct/add it as soon as possible.
Other than that, Enjoy