Ooops
January 15, 2021, 06:36:35 PM

Author Topic: [bb] Using LinePick to do sphere-to-sphere collisions by Ken Lynch [ 1+ years ago ]  (Read 1298 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Using LinePick to do sphere-to-sphere collisions
Author : Ken Lynch
Posted : 1+ years ago

Description : A visual example of how the LinePick command works and how you could use it to do sphere-to-sphere collisions with a bit more control than using Blitz's collision system.

Hope someone finds it useful.


Code :
Code: BlitzBasic
  1. ;
  2. ; LinePick sphere-to-sphere collision
  3. ;
  4.  
  5. Graphics3D 800, 600
  6.  
  7. light = CreateLight()
  8.  
  9. pivot = CreatePivot()
  10.  
  11. camera = CreateCamera(pivot)
  12. PositionEntity camera, 0, 10, 0
  13. RotateEntity camera, 90, 0, 0
  14.  
  15. ;
  16. ; Player
  17. ;
  18. player = CreateSphere()
  19. player_speed# = 0.1
  20. PositionEntity player, 0, 0, 5
  21. EntityPickMode player, 1
  22.  
  23. ;
  24. ; Sphere
  25. ;
  26. sphere = CreateSphere()
  27. velocity# = 2
  28. radius# = 1
  29. sph = CreateCylinder()
  30. EntityParent sph, sphere
  31. ScaleEntity sph, 0.1, 1, 0.1
  32. EntityColor sph, 255, 0, 0
  33. PositionEntity sph, 0, 0, 1
  34. AlignToVector sph, 0, 0, 1, 2
  35.  
  36. ;
  37. ; Sphere to show where collision occurs
  38. ;
  39. collision = CreateSphere()
  40. PositionEntity collision, 0, 0, 2
  41. EntityColor collision, 0, 0, 255
  42. EntityAlpha collision, 0.5
  43. EntityFX collision, 1
  44.  
  45. ;
  46. ; Cylinder to show normal vector
  47. ;
  48. normal = CreateCylinder()
  49. ScaleEntity normal, 0.1, 1, 0.1
  50. EntityColor normal, 255, 0, 0
  51. HideEntity normal
  52.  
  53. Repeat
  54.         ;
  55.         ; Up key
  56.         ;
  57.         If KeyDown(200) Then MoveEntity player, 0, 0, player_speed
  58.  
  59.         ;
  60.         ; Down key
  61.         ;
  62.         If KeyDown(208) Then MoveEntity player, 0, 0, -player_speed
  63.  
  64.         ;
  65.         ; Left key
  66.         ;
  67.         If KeyDown(203) Then MoveEntity player, -player_speed, 0, 0
  68.  
  69.         ;
  70.         ; Right key
  71.         ;
  72.         If KeyDown(205) Then MoveEntity player, player_speed, 0, 0
  73.        
  74.         ;
  75.         ; Rotate sphere
  76.         ;
  77.         If KeyDown(44) Then TurnEntity sphere, 0, 1, 0
  78.         If KeyDown(45) Then TurnEntity sphere, 0, -1, 0
  79.        
  80.         ;
  81.         ; Increase/decrease velocity
  82.         ;
  83.         If KeyHit(12) Then velocity = velocity - 0.1
  84.         If KeyHit(13) Then velocity = velocity + 0.1
  85.        
  86.         ;
  87.         ; Increase/decrease radius
  88.         ;
  89.         If KeyHit(26) Then radius = radius - 0.1
  90.         If KeyHit(27) Then radius = radius + 0.1
  91.         ScaleEntity sphere, radius, radius, radius
  92.        
  93.         ;
  94.         ; Transform velocity to global coordinates
  95.         ;
  96.         TFormVector 0, 0, velocity, sphere, 0
  97.        
  98.         ;
  99.         ; Do the line pick
  100.         ;
  101.         If LinePick(0, 0, 0, TFormedX(), TFormedY(), TFormedZ(), radius) > 0 Then
  102.                 ;
  103.                 ; Show normal vector
  104.                 ;
  105.                 ShowEntity normal
  106.                 ;
  107.                 ; Move it to the picked position
  108.                 ;
  109.                 PositionEntity normal, PickedX(), PickedY(), PickedZ()
  110.                 ;
  111.                 ; Align it to the picked vector
  112.                 ;
  113.                 AlignToVector normal, PickedNX(), PickedNY(), PickedNZ(), 2
  114.                 ;
  115.                 ; Show collision sphere
  116.                 ;
  117.                 ShowEntity collision
  118.                 ;
  119.                 ; Move collision sphere
  120.                 ;
  121.                 PositionEntity collision, PickedX() + radius * PickedNX(), PickedY() + radius * PickedNY(), PickedZ() + radius * PickedNZ()
  122.                 ;
  123.                 ; Scale to radius
  124.                 ;
  125.                 ScaleEntity collision, radius, radius, radius
  126.         Else
  127.                 ;
  128.                 ; Nothing was picked
  129.                 ;
  130.                 HideEntity normal
  131.                 HideEntity collision
  132.         End If
  133.  
  134.         RenderWorld
  135.        
  136.         Locate 0, 0
  137.         Print "Velocity = "+velocity
  138.         Print "Radius   = "+radius
  139.         Print
  140.         Print "Use cursor keys to move player"
  141.         Print "Use z/x to rotate sphere left/right
  142.         Print "Use -/= to decrease/increase velocity"
  143.         Print "Use [/] to decrease/increase radius"
  144.        
  145.         Flip
  146. Until KeyHit(1)


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal