Explode.bb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;Example of exploding an entity
  2. ;David Bird
  3. ;[email protected]
  4. ;15/6/01 (beta 2.24)
  5. Graphics3D 640,480
  6. SetBuffer BackBuffer()
  7. piv=CreatePivot()
  8. cam=CreateCamera(piv)
  9. PositionEntity cam,0,0,-5
  10. mesh=CreateSphere(10)
  11. tex=LoadTexture("tex0.bmp")
  12. ScaleTexture tex,.5,.5
  13. EntityTexture mesh,tex
  14. While Not KeyDown(1)
  15. If KeyHit(57) Then Create_Explosion(mesh,tex) ;Space Bar to produce explosion
  16. If KeyDown(203) TurnEntity piv,0,-1,0 ;Arrow keys to
  17. If KeyDown(205) TurnEntity piv,0,+1,0 ;swing camera
  18. If KeyDown(200) TurnEntity piv,1,0,0 ;round the object
  19. If KeyDown(208) TurnEntity piv,-1,0,0 ;
  20. Update_Particles()
  21. UpdateWorld
  22. RenderWorld
  23. Text 0,0,"Hit <space> to blow up the sphere!"
  24. Flip
  25. Wend
  26. ;
  27. ;Freeup memory used in demo
  28. Free_Particles()
  29. FreeEntity mesh
  30. FreeEntity cam
  31. EndGraphics
  32. End
  33. Type particle
  34. Field ent
  35. Field surf
  36. Field dx#
  37. Field dy#
  38. Field dz#
  39. Field cnt
  40. End Type
  41. Function Update_Particles()
  42. ;Each particle is a triangle
  43. ;as there cnt is reduced and become less than
  44. ;100 the particles alpha is reduced
  45. ;to slowerly fade to nothing.
  46. For a.particle=Each particle
  47. TranslateEntity a\ent,a\dx,a\dy,a\dz
  48. TurnEntity a\ent,4*a\dx,4*a\dy,2
  49. a\cnt=a\cnt-1
  50. If a\cnt<100 Then
  51. EntityAlpha a\ent,Float(a\cnt)/100.0
  52. End If
  53. If a\cnt<1 Then
  54. FreeEntity a\ent
  55. Delete a
  56. End If
  57. Next
  58. End Function
  59. Function Free_Particles()
  60. For a.particle=Each particle
  61. If a\ent Then FreeEntity a\ent
  62. Delete a
  63. Next
  64. End Function
  65. Function Create_Explosion(mesh,texture)
  66. surf=GetSurface(mesh,CountSurfaces(mesh)) ;Get the surface from mesh
  67. nt=CountTriangles(surf) ;How many triangles are in there.
  68. For cnt=0 To nt-1 Step 2
  69. a.particle=New particle
  70. a\ent=CreateMesh()
  71. a\surf=CreateSurface(a\ent)
  72. ind1=TriangleVertex(surf,cnt,0);get the verices 0
  73. ind2=TriangleVertex(surf,cnt,1); ~ ~ --~-- 1
  74. ind3=TriangleVertex(surf,cnt,2); ~ ~ --~-- 2
  75. x#=VertexX(surf,ind1) ;retrieve vertex index 0 for this triangle
  76. y#=VertexY(surf,ind1) ;going tro use this for particle velocity
  77. z#=VertexZ(surf,ind1)
  78. a\dx=x*(Rnd(.015,.035)) ;reduce velocity by a random number in x
  79. a\dy=y*(Rnd(.015,.035)) ;reduce velocity by a random number in y
  80. a\dz=z*(Rnd(.015,.035)) ;reduce velocity by a random number in z
  81. ;Now recreate this tri as an entity
  82. AddVertex(a\surf,VertexX(surf,ind1)-x,VertexY(surf,ind1)-y,VertexZ(surf,ind1)-z,VertexU(surf,ind1),VertexV(surf,ind1))
  83. VertexColor a\surf,0,VertexRed(surf,ind1),VertexGreen(surf,ind1),VertexBlue(surf,ind1)
  84. AddVertex(a\surf,VertexX(surf,ind2)-x,VertexY(surf,ind2)-y,VertexZ(surf,ind2)-z,VertexU(surf,ind2),VertexV(surf,ind2))
  85. VertexColor a\surf,0,VertexRed(surf,ind2),VertexGreen(surf,ind2),VertexBlue(surf,ind2)
  86. AddVertex(a\surf,VertexX(surf,ind3)-x,VertexY(surf,ind3)-y,VertexZ(surf,ind3)-z,VertexU(surf,ind3),VertexV(surf,ind3))
  87. VertexColor a\surf,0,VertexRed(surf,ind3),VertexGreen(surf,ind3),VertexBlue(surf,ind3)
  88. ;Now add the triangle to the entity
  89. ;double sided
  90. AddTriangle(a\surf,0,1,2)
  91. AddTriangle(a\surf,0,2,1)
  92. PositionEntity a\ent,x,y,z
  93. a\cnt=Rnd(150,200)
  94. EntityTexture a\ent,texture
  95. Next
  96. End Function