MORE CODE | ||||||||||||
DEEP BLUE'S ROLLING CODE |
||||||||||||
Here's a nice piece of code contributed by a member of the Dark Basic Forum. Notice the clever use of trig. functions to determine the ball's direction of movement. The formula for the circumference of a circle is adapted to calculate the distance the ball moves based on the degrees it rotates. |
||||||||||||
View the original thread | ||||||||||||
`Use the arrow keys to move/rotate the ball `Author: Deep Blue `Set Display Mode 800,600,16 Sync On Sync Rate 0 Hide Mouse `Set the global variables False=0 True=1 playerSize#=50 playerSpeed#=0 playerRotationSpeed#=2 `Following line can be set to True/False to allow player to rotate while not moving playerRotatesWhileStill=True playerRotateX#=0 playerRotateY#=0 playerRotateZ#=0 `Make a timer to update the player every x per/sec updatePlayerInterval=40 baseTimer=Timer() updatePlayerTimer=baseTimer `Make Object Texture 64*64 Red/White Ink Rgb(255,255,255),1 Box 0,0,63,31 Ink Rgb(255,0,0),1 Box 0,32,63,63 Get Image 1,0,0,63,63 `Reset Ink to White Ink Rgb(255,255,255),1 `Make a Matrix Make Matrix 1,2000,2000,20,20 Position Matrix 1,0,0,0 `Put a bump on the Matrix Set Matrix Height 1,9,10,50 Set Matrix Height 1,10,10,50 Set Matrix Height 1,11,10,50 update matrix 1 `Make the Player sphere & set position Make Object Sphere 1,50 Texture Object 1,1 Position Object 1,1000,(playerSize/2)+Get Ground Height(1,1000,1000),1000 `Main Program Loop Do `Check the timer to see if the player needs updating baseTimer=Timer() if updatePlayerTimer < baseTimer Then Gosub UpdatePlayer `Update the camera Set Camera to follow playerX#, playerY#, playerZ#, playerRotateY#, playerSize#*4, playerSize#*4, 1, 0 Point camera playerX#,playerSize#/2,playerZ# `Display the fps text 8,0,"fps: " + str$(screen fps() ) `Update the screen Sync Loop `Player update subroutine UpdatePlayer: playerSpeed#=0 `Handle the player input If Upkey()=1 playerSpeed#=playerSpeed#+5 Endif If Downkey()=1 playerSpeed#=playerSpeed#-5 Endif If Leftkey()=1 if playerRotatesWhileStill=True then YRotate Object 1,WrapValue(0-playerRotationSpeed#) playerRotateY#=WrapValue(playerRotateY#-playerRotationSpeed#) Endif If Rightkey()=1 if playerRotatesWhileStill=True then YRotate Object 1,WrapValue(playerRotationSpeed#) playerRotateY#=WrapValue(playerRotateY#+playerRotationSpeed#) Endif `Get the player object's current location playerX# = Object position X(1) playerY# = Object position Y(1) playerZ# = Object position Z(1) `Get the player object's current rotation playerAngleX# = Object angle X(1) playerAngleY# = Object angle Y(1) playerAngleZ# = Object angle Z(1) `Calculate the required movement vector for the player object playerVectorLength#=playerSpeed# playerVectorX#=sin(playerRotateY#)*playerVectorLength# playerVectorZ#=cos(playerRotateY#)*playerVectorLength# playerVectorY#=Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#) `Get the maximum angle of rotation along any axis playerMaxRotation#=abs(playerVectorLength#)*((playerSize#*3.14159)/360) `Position the player object, then from it's new position calculate the required rotation Position object 1,playerX#+playerVectorX#,playerY#,playerZ# playerX# = Object position X(1) XRotate object 1,WrapValue(playerVectorLength#*(playerMaxRotation#*(cos(playerAngleY#-playerRotateY#)*abs(cos(playerAngleY#-playerRotateY#))))) Position object 1,playerX#,playerY#,playerZ#+playerVectorZ# playerZ# = Object position Z(1) ZRotate object 1,WrapValue(playerVectorLength#*(playerMaxRotation#*(cos((playerAngleY#-playerRotateY#)-90)*abs(cos((playerAngleY#-playerRotateY#)-90))))) Position object 1,playerX#,playerVectorY#+(playerSize#/2),playerZ# playerY# = Object position Y(1) `fix the objects pivot fix object pivot 1 `Update the player timer updatePlayerTimer=baseTimer+(1000/updatePlayerInterval) Return |