TGC Codebase Backup



Camera Positioning Relative to an Object by Kira Vakaan

19th Jan 2009 19:59
Summary

Use this function to position and rotate a camera relative to an object's position and rotation.



Description

Syntax:
CameraFollowObject(Camera,Object,FollowX#,FollowY#,FollowZ#,XAng#,Yang#,ZAng#)

Camera - The number of the camera to reposition
Object - The number of the object to reposition the camera around
FollowX#, FollowY#, FollowZ# - The new X, Y, and Z positions of the camera relative to the object's position and rotation
XAng#, YAng#, ZAng# - The new X, Y, and Z angles of the camera relative to the object's rotation



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function CameraFollowObject(Camera,Object,FollowX#,FollowY#,FollowZ#,XAng#,Yang#,ZAng#)
   xpos#=object position x(Object)+(FollowZ#*cos(object angle x(Object))+FollowY#*sin(object angle x(Object)))*sin(object angle y(Object))+FollowX#*cos(object angle y(Object))
   ypos#=object position y(Object)-FollowZ#*sin(object angle x(Object))+FollowY#*cos(object angle x(Object))
   zpos#=object position z(Object)+(FollowZ#*cos(object angle x(Object))+FollowY#*sin(object angle x(Object)))*cos(object angle y(Object))-FollowX#*sin(object angle y(Object))
   position camera Camera,xpos#,ypos#,zpos#
   set camera to object orientation Camera,Object
   pitch camera down Camera,XAng#
   turn camera right Camera,YAng#
   roll camera left Camera,ZAng#
endfunction