Posted: 6th Mar 2003 22:10
A lot of people seem to be having trouble with this, so here is a basic code snippet for a rolling ball on a matrix (taken from a test piece for a very old project):



+ Code Snippet
rem ZoomRacer (Tentative Title)
rem Prerelease Alpha 1

rem ##INTRO##
SYNC ON : SYNC RATE 0
Hide Mouse

`Set the global variables
False=0
True=1



playerSize#=50
`Sets the fastest speed at which the ball can rotate
`Current Max = approx. 8
playerRotationSpeedCap#=10
playerSpeed#=10
playerRotationSpeed#=2
acceleration#=0
accelerationcap#=1

`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=60
baseTimer=Timer()
updatePlayerTimer=baseTimer



CreateWorld()
CreateBall()


`Main Program Loop
Do

 `Check the timer to see if the player needs updating
 baseTimer=Timer()
 if updatePlayerTimer  0
      acceleration# = acceleration# - 0.02
   Else
      acceleration = 0
   EndIf
 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#*acceleration#
 playerVectorX#=sin(playerRotateY#)*playerVectorLength#
 playerVectorZ#=cos(playerRotateY#)*playerVectorLength#
 If Object Position Y(1)  Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)
      playerVectorY#=Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)

   endif
 Endif

 `fatalerror(str$(CurveValue(Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#),Object Position Y(1),acceleration#)))
 `Get the maximum angle of rotation along any axis
 playerMaxRotation#=abs(playerVectorLength#)*((playerSize#*3.14159)/360)
   If playerMaxRotation# > playerRotationSpeedCap#
      playerMaxRotation# = playerRotationSpeedCap#
   EndIf


 `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
 ` change acceleration
   If acceleration# > 0
      acceleration# = acceleration# - 0.01
   Else
      acceleration# = 0
   EndIf
 `Update the player timer
 updatePlayerTimer=baseTimer+(1000/updatePlayerInterval)
Return




Function CreateBall()
   `Load Ball Image
      Load Bitmap "Data\Images\ball1.bmp", 1
      Get Image 2,0,0,256,256
      Delete Bitmap 1
   `Make the Player sphere & set position
      Make Object Sphere 1,50
      Texture Object 1,2
      Ghost Object On 1
      Position Object 1,1000,(playerSize/2)+Get Ground Height(1,1000,1000),1000
      Load Image "Data\Images\pt1.bmp",3
EndFunction

Function CreateWorld()

   `Load floor image
      Load Bitmap "Data\Images\floor1.bmp",1
      Get Image 1, 0,0,256,256
      Delete Bitmap 1

   `Make a Matrix
      Make Matrix 1,2000,2000,11,11
      Position Matrix 1,0,0,0
      Prepare Matrix Texture 1,1,2,2
      Set Matrix Height 1,5,5,100
      Set Matrix Height 1,5,6,100

      update matrix 1
EndFunction
Posted: 7th Mar 2003 21:31
I get an error on this line in both db and dbpro all patched to current levels.

if updatePlayerTimer 0
Posted: 7th Mar 2003 22:16
Woops! - Sorry, here is corrected:

+ Code Snippet
rem ZoomRacer (Tentative Title)
rem Prerelease Alpha 1

rem ##INTRO##
SYNC ON : SYNC RATE 0
Hide Mouse

`Set the global variables
False=0
True=1



playerSize#=50
`Sets the fastest speed at which the ball can rotate
`Current Max = approx. 8
playerRotationSpeedCap#=10
playerSpeed#=10
playerRotationSpeed#=2
acceleration#=0
accelerationcap#=1

`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=60
baseTimer=Timer()
updatePlayerTimer=baseTimer



CreateWorld()
CreateBall()


`Main Program Loop
Do

 `Check the timer to see if the player needs updating
 baseTimer=Timer()
 if updatePlayerTimer = 0
      acceleration# = acceleration# - 0.02
   Else
      acceleration = 0
   EndIf
 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#*acceleration#
 playerVectorX#=sin(playerRotateY#)*playerVectorLength#
 playerVectorZ#=cos(playerRotateY#)*playerVectorLength#
 If Object Position Y(1)  Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)
      playerVectorY#=Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)

   endif
 Endif

 `fatalerror(str$(CurveValue(Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#),Object Position Y(1),acceleration#)))
 `Get the maximum angle of rotation along any axis
 playerMaxRotation#=abs(playerVectorLength#)*((playerSize#*3.14159)/360)
   If playerMaxRotation# > playerRotationSpeedCap#
      playerMaxRotation# = playerRotationSpeedCap#
   EndIf


 `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
 ` change acceleration
   If acceleration# > 0
      acceleration# = acceleration# - 0.01
   Else
      acceleration# = 0
   EndIf
 `Update the player timer
 updatePlayerTimer=baseTimer+(1000/updatePlayerInterval)
Return




Function CreateBall()
   `Load Ball Image
      Load Bitmap "Data\Images\ball1.bmp", 1
      Get Image 2,0,0,256,256
      Delete Bitmap 1
   `Make the Player sphere & set position
      Make Object Sphere 1,50
      Texture Object 1,2
      Ghost Object On 1
      Position Object 1,1000,(playerSize/2)+Get Ground Height(1,1000,1000),1000
      Load Image "Data\Images\pt1.bmp",3
EndFunction

Function CreateWorld()

   `Load floor image
      Load Bitmap "Data\Images\floor1.bmp",1
      Get Image 1, 0,0,256,256
      Delete Bitmap 1

   `Make a Matrix
      Make Matrix 1,2000,2000,11,11
      Position Matrix 1,0,0,0
      Prepare Matrix Texture 1,1,2,2
      Set Matrix Height 1,5,5,100
      Set Matrix Height 1,5,6,100

      update matrix 1
EndFunction
Posted: 7th Mar 2003 22:44
No dice: It failed in both programs again
#100013:Command line out of place at line 51
Try again
Posted: 7th Mar 2003 22:47
It seems your having troubles getting the ball rolling on this program. pun intended.
Posted: 8th Mar 2003 0:13
Dammit! - Lets try pasting it again:

+ Code Snippet
rem ZoomRacer (Tentative Title)
rem Prerelease Alpha 1

rem ##INTRO##
SYNC ON : SYNC RATE 0
Hide Mouse

`Set the global variables
False=0
True=1



playerSize#=50
`Sets the fastest speed at which the ball can rotate
`Current Max = approx. 8
playerRotationSpeedCap#=10
playerSpeed#=10
playerRotationSpeed#=2
acceleration#=0
accelerationcap#=1

`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=60
baseTimer=Timer()
updatePlayerTimer=baseTimer



CreateWorld()
CreateBall()


`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() )
 text 20,20, str$(acceleration#)
 text 20,40, str$(playerMaxRotation#)

 `Update the screen
 Sync

Loop

`Player update subroutine
UpdatePlayer:

 playerSpeed#=20

 `Handle the player input
 If Upkey()=1
   If acceleration# < accelerationcap#
      acceleration# = acceleration# + 0.05
   Else
      acceleration# = 1.01
   EndIf
  `playerSpeed#=playerSpeed#+10

 Endif

 If Downkey()=1
 ` playerSpeed#=playerSpeed#-10
   If acceleration > 0
      acceleration# = acceleration# - 0.02
   Else
      acceleration = 0
   EndIf
 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#*acceleration#
 playerVectorX#=sin(playerRotateY#)*playerVectorLength#
 playerVectorZ#=cos(playerRotateY#)*playerVectorLength#
 If Object Position Y(1) < Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)
   playerVectorY#=Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)
 else
   if Object Position Y(1) > Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)
      playerVectorY#=Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#)

   endif
 Endif

 `fatalerror(str$(CurveValue(Get Ground Height(1,playerX#+playerVectorX#,playerZ#+playerVectorZ#),Object Position Y(1),acceleration#)))
 `Get the maximum angle of rotation along any axis
 playerMaxRotation#=abs(playerVectorLength#)*((playerSize#*3.14159)/360)
   If playerMaxRotation# > playerRotationSpeedCap#
      playerMaxRotation# = playerRotationSpeedCap#
   EndIf


 `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
 ` change acceleration
   If acceleration# > 0
      acceleration# = acceleration# - 0.01
   Else
      acceleration# = 0
   EndIf
 `Update the player timer
 updatePlayerTimer=baseTimer+(1000/updatePlayerInterval)
Return




Function CreateBall()
   `Load Ball Image
      Load Bitmap "Data\Images\ball1.bmp", 1
      Get Image 2,0,0,256,256
      Delete Bitmap 1
   `Make the Player sphere & set position
      Make Object Sphere 1,50
      Texture Object 1,2
      Ghost Object On 1
      Position Object 1,1000,(playerSize/2)+Get Ground Height(1,1000,1000),1000
      Load Image "Data\Images\pt1.bmp",3
EndFunction

Function CreateWorld()

   `Load floor image
      Load Bitmap "Data\Images\floor1.bmp",1
      Get Image 1, 0,0,256,256
      Delete Bitmap 1

   `Make a Matrix
      Make Matrix 1,2000,2000,11,11
      Position Matrix 1,0,0,0
      Prepare Matrix Texture 1,1,2,2
      Set Matrix Height 1,5,5,100
      Set Matrix Height 1,5,6,100

      update matrix 1
EndFunction
Posted: 8th Mar 2003 0:14
Now it pasted OK - Where is a damn working edit button when you need one?
Posted: 27th Mar 2003 9:32
These physics don't work in DBPro. For me anyways. When you turn, the ball rolls, but not at the correct angles.

-Kensupen