We're actually doing this in our Globall Project.
I took this from the libraries and edited it. Its very crude, and it isnt plug-n-play, but it gives you an idea on how to do it.
In our game, we have the ability to do straifing along with forward and reverse. I took this out for simplicity.
;===Basic setup of variables
MouseSpeed# = .2
Accel# = 2
;===This is how the hiearchy of the pivots are created
MainPlayer = createpivot()
CamPivot1 = createpivot(MainPlayer)
CamPivot2 = createpivot(CamPivot1)
BallModel = createsphere(8,MainPlayer)
Camera = CreateCamera()
positionentity(Camera,0,0,-10)
entityparent(Camera,CamPivot2)
;====Taken from my player movement function (library) and modified a bit for ease of reading===
;====Rotate camera to mouse position
;Get Mouse position then reset Screen Mouse
MOUX = (graphicswidth()/2) - mousex()
MOUY = Mousey() - (graphicsheight()/2)
Movemouse(graphicswidth()/2,graphicsheight()/2)
;===Calculate New Look Angles
Player\LookX# = Player\LookX# + (float#(MOUX) * MouseSpeed#)
Player\LookY# = Player\LookY# + (float#(MOUY) * MouseSpeed#)
if Player\LookX# > 359 then Player\LookX# = Player\LookX# - 359
if Player\LookX# < 0 then Player\LookX# = 359 + Player\LookX#
if Player\LookY# < -88 then Player\LookY# = -88
if Player\LookY# > 88 then Player\LookY# = 88
;===Rotate Camera to the Looks
Rotateentity(Player\CamPivot1,0,Player\LookX#,0)
Rotateentity(Player\CamPivot2,Player\LookY#,0,0)
;===Do Movement
;Calculate Correct 3d Angle of movement
VelRot# = 359 - KCS_CurveValue#(Player\LookX#)
ZMod# = cos(VelRot#)
XMod# = sin(VelRot#)
;If you want the ball to go in reverse, simply subtract 180 from the velrot and re-run it back through KCS_CurveValue()
if keydown(Arrow_Up) then moveentity(Player\MainPlayer,XMod# * Accel#,0,ZMod#*Accel#)
;====Do ball roll
;reset turnentity
RotateEntity Player\BallModel,0,0,0
Width# = 360 / (MeshWidth#(Player\BallModel) * 3.14)
;Turn it according to distance traveled
Player\BallRollX# = Player\VelZ# * Width#
Player\BallRollZ# = -Player\VelX# * Width#
TurnEntity Player\BallModel,Player\BallRollX#,0,Player\BallRollZ#
;Reset the coordinates of the verticies
RotateMesh Player\BallModel,EntityPitch(Player\BallModel),EntityYaw(Player\BallModel),EntityRoll(Player\BallModel)
;====End of Library extraction
function KCS_CurveValue#(KCS_Angle#)
if KCS_Angle# > 359 then KCS_Angle# = KCS_Angle# - 359
if KCS_Angle# < 0 then KCS_Angle# = 359 + KCS_Angle#
return KCS_Angle#
end function
|