July 28, 2021, 05:37:56

Author Topic: [bb] Snap to grid movement by Zethrax [ 1+ years ago ]  (Read 775 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Snap to grid movement by Zethrax [ 1+ years ago ]
« on: June 29, 2017, 00:28:39 »
Title : Snap to grid movement
Author : Zethrax
Posted : 1+ years ago

Description : Demonstrates camerapicking a point on a grid and moving an object so that movement is snapped to the size of the grid cell

Code :
Code: BlitzBasic
  1. Global G_grid_cell_size# = 1.0
  2.  
  3. Graphics3D 800, 600, 0, 2
  4.  
  5. Global G_camera = CreateCamera()
  6. CameraZoom G_camera, 1.6
  7. TurnEntity G_camera, 90.0, 0.0, 0.0
  8. MoveEntity G_camera, 0.0, 0.0, -20.0
  9.  
  10. Global G_light = CreateLight()
  11.  
  12. Global G_cube = CreateCube()
  13. ScaleMesh G_cube, 0.5, 0.5, 0.5
  14. PositionMesh G_cube, 0.5, 0.5, 0.5
  15. UpdateNormals G_cube
  16.  
  17. Global G_grid = CreateGrid()
  18. EntityPickMode G_grid, 2
  19.  
  20. Global G_picked_entity
  21.  
  22. SetBuffer BackBuffer()
  23.  
  24. Repeat
  25.  
  26.         G_picked_entity = CameraPick( G_camera, MouseX(), MouseY() )
  27.        
  28.         If G_picked_entity
  29.        
  30.                 x# = Floor( PickedX() / G_grid_cell_size# ) * G_grid_cell_size#
  31.                 z# = Floor( PickedZ() / G_grid_cell_size# ) * G_grid_cell_size#        
  32.                 PositionEntity G_cube, x#, PickedY(), z#
  33.        
  34.         EndIf
  35.        
  36.         UpdateWorld
  37.         RenderWorld
  38.         Flip
  39.         Delay( 1 )
  40.  
  41. Until KeyHit( 1 )
  42.  
  43. End
  44.  
  45.  
  46.  
  47. Function CreateGrid()
  48.         ; -- Create grid texture.
  49.         Local i, x, y
  50.         Local grid_2d_tex = CreateTexture ( 256, 256, 11 )
  51.         SetBuffer TextureBuffer ( grid_2d_tex )
  52.         For i = 0 To 4
  53.                 Rect i, i, 256 - i - i, 256 - i - i, False
  54.         Next
  55.         For y = 5 To 250
  56.                 For x = 5 To 250
  57.                         WritePixel x, y, 0
  58.                 Next
  59.         Next
  60.         ;^^^^^^
  61.         ; -- Create 2D grid.
  62.         Local grid_2D = CreatePlane ()
  63.         EntityTexture grid_2D, grid_2d_tex
  64.         EntityFX grid_2D, 9
  65.         ;^^^^^^
  66.         Return grid_2D
  67. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal