This is some pretty simple code, but I spent the afternoon making it as I'm learning DBPro, and thought I would share it since I couldn't find a plain, simple example of doing this.
What this code does:
Allows positioning the camera around a 3d point, using mouseXYZ to control rotation, angle, and zoom. Limits Y rotation to prevent going over 90 degrees. Z is also limited to zooming between min and max values.
If you've ever played games like Prince of Persia or Assassin's Creed, this will feel very familar to you.
Should be easy enough to add to your own project, and is as well commented as I could come up with.
Code:
rem camera rotates around a point
rem Camera Code by Gawain Doell, also known as 300happy on the Game Creator Forums
rem If you use this code, please give credit. If you find this useful, I'd love to hear
rem from you. My email is: smileygames@prodigy.net
remstart
the camera is positioned along a 2d vector of Distance*Height. This 2d vector is rotated by
the x angle rotation value to allow the camera to swing around the player.
In short, think of it as the camera is mounted to a giant sphere around the player, and can
move along the sphere using it's X rotation and Y position.
Values needed:
camXangle - the x angle rotation of camera around the player
camHeight - the height of the camera
camDist - the distance to place the camera away from the player object
camCDist - the recalculated distance, based on camDist - the absolute value of camHeight.
This formula means that the further away from 0 camHeight is, the closer the camera will
be moved to the object. This keeps the camera on a sphere around the object. Without
this calculation, the camera would always be the same distance on the xyz grid, which
isn't what we need because that would allow the player to "zoom out" when the camera
was really high angled or low angled.
camMaxDist - the maximum distance the player can zoom out
camMinDist - the minimum distance the player can zoom in
px, py, and pz are simply the x, y, and z position of the object you wish to follow.
remend
camXangle = 0
camHeight = 0
camDist = 300
camCDist = 0
camMaxDist = 500
camMinDist = 50
rem main loop
do
rem control camera through mouse position
camXangle = camXangle + ( mousemovex()) rem Add the amount the mouse has moved to the camX angle
if camXangle < -360 then camXangle = 360 rem correct for over 360 values to keep numbers sane
if camXangle > 360 then camXangle = -360
rem for the Y angle movement, we multiply it by the distance / 90. This means as
rem the player zooms out, they can move the camera further up and down, preventing
rem a "slowdown" effect of increasing the distance/angle ratio
camHeight = camHeight + ( mousemovey() * ( (camDist / 90) + 1))
if camHeight > camDist-1 then camHeight = camDist-1 rem correct for over 360 values to keep numbers sane
if camHeight < -camDist+1 then camHeight = -camDist+1
camDist = camDist + (mousemovez()/2) rem Add mouse z to camDist to allow zoom in and out.
if camDist < camMinDist then camDist = camMinDist rem Keep the camDist value above camMinDist.
if camDist > camMaxDist then camDist = camMaxDist rem Keep camDist value below camMaxDist.
rem We calculate the actual distance by placing the camera at the distance, then subtracting
rem the absolute value of the height. In layman's terms, the further up or down the camera is,
rem the closer the camera will be to the player. This keeps the vector between the camera and
rem the player the same distance, despite the change of height, and gives us the "sphere"
rem movement around the player.
camCDist = camDist - abs(camHeight)
rem Now we just plug in both the player position, and our calculated X angle, Distance, and Height.
rem If you want to adjust "smoothness" of movement, just set the 800.0 value to something else.
rem A value of 1.0 will give instant feedback as the camera will teleport to the position over
rem one frame, but will be jerky.
set camera to follow px, py, pz,camXangle,camCDist,camHeight,800.0,0
rem Now point the camera at the player. The Set camera to Follow doesn't do this, so by setting
rem it here, we can be sure the camera will always center our player in the screen.
point camera px, py, pz
rem draw the screen
sync
rem loop the code
loop