Update Rotate Camera in 3D Around Object by Maleck25th May 2004 23:55
|
---|
Summary Updated code based on Philip Young's camera rotation example. This rotates camera view around a position (initially 0,0,0) view mouse controls. Description ********************************************************************* Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com `********************************************************************* `*** Camera Rotation Code Around 3D Object with Mouse Controls *** `*** Based on code from Philip Young in codebase *** `*** Modified by Eric Lenhart 5-2004 Please use freely with credit *** `*** *** `*** Controls have been modified to use mouse controls instead *** `*** arrows *** `********************************************************************* `*** Controls: *** `Hold right mouse button to rotate `Use scroll to zoom `Both mouse buttons enables scroll also `While rotating, cursor will wrap around screen `Holding right mouse and shift will move camera focus point along X and Y (still needs work) `Space bar recenters camera sync on `*** Set display settings *** SCREENX = 1024 SCREENY = 768 set display mode SCREENX,SCREENY,32 `*** Create objects to view *** Make Object cube 1, 3 Make object cylinder 2, 2 Make object cube 3, 3 position object 1, 0, 0, 0 position object 2, 0, 2.5, 0 Position object 3, 0, 5, 0 `*** Set object collision for camera *** automatic camera collision 0, .25, 1 `*** position camera variables *** x# = 0 y# = 0 z# = -400 `*** point and position camera variables *** pcx = 0 pcy = 0 pcz = 0 position camera x#, y#, z# point camera 0, 0, 0, 0 bearing# = 20 azimuth# = 20 distance# = 30 `*** Setup last camera variables *** LASTMOUSEX = MOUSEX() LASTMOUSEY = MOUSEY() LASTMOUSEZ = MOUSEZ() do `*** show the mouse *** show mouse `*** Reset model to center if SPACE pressed *** IF SCANCODE() = 57 pcx = 0 pcy = 0 pcz = 0 point camera 0, 0, 0, 0 ENDIF `*** Read current mouse coordinates *** CURRENTMOUSEX = MOUSEX() CURRENTMOUSEY = MOUSEY() CURRENTMOUSEZ = MOUSEZ() `****************************************************** If MOUSECLICK() = 2 `*** Wrap mouse around screen if right mouse button clicked *** IF MOUSEX() = 0 Position mouse SCREENX - 2, MOUSEY() ENDIF IF MOUSEX() = SCREENX - 1 position mouse 1, MOUSEY() ENDIF IF MOUSEY() = 0 Position mouse MOUSEX(), SCREENY - 2 ENDIF IF MOUSEY() = SCREENY - 1 position mouse MOUSEX(), 1 ENDIF `*** Move view of object if holding down shift and right mouse *** IF SCANCODE() = 42 IF CURRENTMOUSEX > LASTMOUSEX pcx = pcx + 1 ENDIF IF CURRENTMOUSEX < LASTMOUSEX pcx = pcx - 1 ENDIF IF CURRENTMOUSEY > LASTMOUSEY pcy = pcy + 1 ENDIF IF CURRENTMOUSEY < LASTMOUSEY pcy = pcy - 1 ENDIF ENDIF `*** Right mouse button clicked and shift not down - rotate camera *** if CURRENTMOUSEX < LASTMOUSEX AND SCANCODE() <> 42 bearing# = wrapvalue(bearing# + 3) endif if CURRENTMOUSEX > LASTMOUSEX AND SCANCODE() <> 42 bearing# = wrapvalue(bearing# - 3) endif if CURRENTMOUSEY < LASTMOUSEY AND SCANCODE() <> 42 and azimuth# < 179 azimuth# = wrapvalue(azimuth# + 3) endif if CURRENTMOUSEY > LASTMOUSEY AND SCANCODE() <> 42 AND azimuth# > 2 azimuth# = wrapvalue(azimuth# - 3) endif ENDIF `*** Zoom with mouse wheel *** IF CURRENTMOUSEZ < LASTMOUSEZ distance# = distance# + 1 ENDIF IF CURRENTMOUSEZ > LASTMOUSEZ distance# = distance# - 1 ENDIF `*** Zoom by holdign both mouse buttons with cursor wrap *** IF MOUSECLICK() = 3 HIDE MOUSE `*** wrap mouse during zoom *** IF MOUSEX() = 0 Position mouse SCREENX - 2, MOUSEY() ENDIF IF MOUSEX() = SCREENX - 1 position mouse 1, MOUSEY() ENDIF IF MOUSEY() = 0 Position mouse MOUSEX(), SCREENY - 2 ENDIF IF MOUSEY() = SCREENY - 1 position mouse MOUSEX(), 1 ENDIF `*** Perform zoom *** IF CURRENTMOUSEY < LASTMOUSEY distance# = distance# - .2 ENDIF IF CURRENTMOUSEY > LASTMOUSEY distance# = distance# + .2 ENDIF ENDIF `*** Set current mouse position as last mouse position *** LASTMOUSEX = MOUSEX() LASTMOUSEY = MOUSEY() LASTMOUSEZ = MOUSEZ() `*** Calculate camera position *** x# = distance# * sin(azimuth#) * cos(bearing#) z# = distance# * sin(azimuth#) * sin(bearing#) y# = distance# * cos(azimuth#) `*** Position and point camera *** position camera x# + pcx, y# + pcy, z# point camera 0, pcx, pcy, pcz `*** Display various stats *** set cursor 0, 0 print "x: ";x#;" y: ";y#;" z: ";z# print "bearing: ";bearing# print "azimuth: ";azimuth# print "distance: ";distance# print "scancode: ";SCANCODE() print "mosue x: ";mousex() print "mouse y: ";mousey() print "mouse z: ";mousez() print "fps: ";screen fps() sync loop |