TGC Codebase Backup



Third Person Mouse Look by Dave J

4th Sep 2004 7:57
Summary

A simple function that rotates the camera/player in the third person by the amount the mouse is moved.



Description

This function accepts various arguments that will rotate the camera and object appropriately for a thid person camera type system. The first 3 parameters are the X, Y, Z coordinates of the object/player. The next parameter specifies how far the camera should be from the object/player and the last 2 parameters are simply the amount the mouse has moved, I left these as arguments though as you may wish to modify how fast the camera rotates, so for example, the MouseMoveX() command could be halved so it rotates at only half the speed.

A sample program is included on how to use the function, the small cube is the 'player' and the large cube is just some 'scenery' which acts as a point of reference.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Global Variables
Global hAngle# As Integer
Global vAngle# As Integer

`Set Sync, Load Objects
Sync Rate 0
Sync On

`Create the Main Character
Make Object Cube 1, 100

`Only do this if your character is rotated incorrectly (like mine)
Rotate Object 1, Object Angle X(1), Object Angle Y(1) + 180, Object Angle Z(1)
Fix Object Pivot 1

`Load some 'scenery' so we know it's working
Make Object Box 2, 200, 200, 200
Position Object 2, Object Position X(1) + 400, Object Position Y(1), Object Position Z(1)

Do

   `-Control Camera-
   `PARAMETERS:
   `Your Characters X Position, Char Y Position, Char Z Position, How far outwards the camera should be,
   `how high the camera should be (what the mouses Y is), how fast the cam moves around (how much the user moves their mouse), which camera
   MouseLook(Object Position X(1), Object Position Y(1), Object Position Z(1), 500.0, MouseMoveY(), MouseMoveX())


   Sync

Loop

Function MouseLook(xTarget, yTarget, zTarget, hDist#, vDist#, Speed#)

   `Continue from where we last were
   hAngle# = hAngle# + Speed#
   vAngle# = vAngle# + vDist#

   `Tricky part: Make a 'circular' camera movement
   xPos# = xTarget + cos(hAngle#) * hDist#
   yPos# = yTarget + vAngle#
   zPos# = zTarget + sin(hAngle#) * hDist#

   `Position Camera in it's new place and point it at our main character
   Position Camera xPos#, yPos#, zPos#
   Point Camera xTarget, yTarget, zTarget

   `Usually you'll want the camera just a tad higher then the char, so do that now
   Position Camera xPos#, yPos# + 200, zPos#

   `Point our Main Character in the same direction as our camera
   Rotate Object 1, Object Angle X(1), Camera Angle Y(), Object Angle Z(1)

EndFunction