EntityX.bb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ; EntityX / EntityY / EntityZ example.
  2. ; Escape quits, other keys move or pause the display.
  3. Const width = 640, height = 480
  4. Const KEY_ESC = 1, KEY_LEFT = 203, KEY_RIGHT = 205
  5. Graphics3D 640, 480
  6. AmbientLight 50, 50, 50
  7. Global isMoving = False ; used to pause/resume movement
  8. Global count ; how many updates have been done
  9. ; Set up a camera, light and three entities...
  10. cam = CreateCamera()
  11. PositionEntity cam, 0, 2, -50
  12. CameraZoom cam, 4
  13. lt = CreateLight() : TurnEntity lt, 30, 40, 0
  14. Global oSphere, pCone, cSphere
  15. oSphere = CreateSphere()
  16. EntityColor oSphere, 250, 50, 0 ; Orange = Origin, parent of cone
  17. pCone = CreateCone( 8, True, oSphere) ; will be a parent of small sphere
  18. ScaleEntity pCone, .8, 2.0, .8
  19. PositionEntity pCone, 8, 0, 0
  20. EntityColor pCone, 255, 255, 0
  21. cSphere = CreateSphere( 8, pCone ) ; child of the cone
  22. EntityColor cSphere, 150, 150, 0
  23. ScaleEntity cSphere, .4/.8, .4/2.0, .4/.8 ; try commenting out this line
  24. PositionEntity cSphere, 0, 2, 0 ; above parent
  25. ; ... and we are ready run.
  26. While Not KeyDown( KEY_ESC )
  27. UpdateEverything
  28. RenderWorld
  29. ShowInfo
  30. Flip
  31. Wend
  32. End
  33. Function UpdateEverything( )
  34. ; Nothing moves relative to its parent, so local coordinates are constant.
  35. ; Try uncommenting the PositionEntity command to change this.
  36. If GetKey() Then isMoving = Not isMoving
  37. If isMoving
  38. TurnEntity oSphere, 0, .5, 0
  39. TurnEntity pCone, .2, 0, 0
  40. count = count + 1
  41. a# = count Mod 360
  42. ; PositionEntity cSphere, 0, 2 + Sin( a ), 0 ; experiment with this
  43. End If
  44. End Function
  45. Function ShowInfo( ) ; global and local coordinates for all entities
  46. Local x$, y$, z$
  47. Color 255, 255, 255
  48. Text 185, 20, "Global"
  49. Text 495, 20, "Local"
  50. Color 250, 50, 0
  51. Text 20, 50, "oSphere: " + XYZ( oSphere, True )
  52. Text 400, 50, XYZ( oSphere, False )
  53. Color 255, 255, 0
  54. Text 20, 75, " pCone: " + XYZ( pCone, True )
  55. Text 400, 75, XYZ( pCone, False )
  56. Color 150, 150, 0
  57. Text 20, 100, "cSphere: " + XYZ( cSphere, True )
  58. Text 400, 100, XYZ( cSphere, False )
  59. End Function
  60. ; ******************************************************************
  61. ; These two functions just format the text display.
  62. ; Without them there are too many numbers crowding the screen.
  63. Function Round#( x#, m# ) ; returns x rounded to multiple of m
  64. If m < 0.0 Then m = -m
  65. s# = Sgn( x )
  66. If x < 0.0 Then x = -x
  67. diff# = x Mod m
  68. If diff < .5 * m
  69. Return ( x - diff ) * s
  70. Else
  71. Return ( m + x - diff ) * s
  72. End If
  73. End Function
  74. Function XYZ$( entity, globalFlag )
  75. ex# = Round( EntityX( entity, globalFlag ), .001 )
  76. ey# = Round( EntityY( entity, globalFlag ), .001 )
  77. ez# = Round( EntityZ( entity, globalFlag ), .001 )
  78. Return RSet( ex, 8 ) + RSet( ey, 8 ) + RSet( ez, 8 )
  79. End Function