Ooops
January 23, 2021, 04:23:29 AM

Author Topic: [bb] Break the Glass by Jeppe Nielsen [ 1+ years ago ]  (Read 609 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Break the Glass by Jeppe Nielsen [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Break the Glass
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Shoot glass with the mouse.

Code :
Code: BlitzBasic
  1. ;Break glass example, by Jeppe Nielsen 2003
  2.  
  3. Type glass
  4.  
  5. Field e
  6.  
  7. Field s
  8.  
  9. Field w#,h#
  10.  
  11. Field sx#,sy#
  12.  
  13. Field broken
  14.  
  15. End Type
  16.  
  17. Type glasspiece
  18.  
  19. Field p
  20.  
  21. Field pivot[3]
  22.  
  23. Field x#,y#,z#
  24.  
  25. Field vx#,vy#,vz#
  26.  
  27. Field pitch#,yaw#,roll#
  28.  
  29. Field g.glass
  30.  
  31. Field index
  32.  
  33. End Type
  34.  
  35.  
  36. Const gravity#=0.004
  37.  
  38. Graphics3D 800,600,16,2
  39.  
  40. pivot=CreatePivot()
  41. camera=CreateCamera(pivot)
  42.  
  43. CameraClsColor camera,255,255,255
  44.  
  45. MoveEntity camera,0,1,-4
  46.  
  47. ;cube=CreateCube()
  48. ;PositionEntity cube,0,0,0
  49. ;FlipMesh cube
  50.  
  51. light=CreateLight(1)
  52. RotateEntity light,-10,40,0
  53.  
  54. g.glass=glassnew(2,2,20,20)
  55. PositionEntity ge,0,0,-1
  56. g.glass=glassnew(2,2,20,20)
  57. PositionEntity ge,0,0,1
  58. g.glass=glassnew(2,2,20,20)
  59. PositionEntity ge,0,0,-1
  60. TurnEntity ge,0,90,0
  61. g.glass=glassnew(2,2,20,20)
  62. PositionEntity ge,2,0,-1
  63. TurnEntity ge,0,90,0
  64.  
  65. Color 0,0,0
  66.  
  67. Repeat
  68.  
  69. TurnEntity pivot,0,1,0
  70.  
  71.  
  72.  
  73.  
  74.  
  75. If MouseHit(1)>0
  76.  
  77. p=CameraPick(camera,MouseX(),MouseY())
  78. g.glass=Last glass
  79. While g<>Null
  80. If ge=p
  81. glassbreak(g)
  82. Exit
  83. EndIf
  84. g=Before g
  85. Wend
  86.  
  87.  
  88. EndIf
  89.  
  90. glassupdate()
  91.  
  92.  
  93. RenderWorld()
  94. mx=MouseX()
  95. my=MouseY()
  96.  
  97. Text 400,20,"SHOOT the glass cube :)",1
  98.  
  99. Oval mx-30,my-30,60,60,0
  100. Plot mx,my
  101.  
  102.  
  103. Flip
  104.  
  105. Until KeyDown(1)
  106.  
  107. End
  108.  
  109.  
  110.  
  111. Function glassnew.glass(wid#,hei#,secx,secy,parent=0)
  112.  
  113. g.glass=New glass
  114.  
  115. gw#=wid#
  116. gh#=hei#
  117.  
  118.  
  119. ge=CreateMesh(parent)
  120.  
  121. gs=CreateSurface(ge)
  122.  
  123. dx#=1/Float(secx)
  124. dy#=1/Float(secy)
  125.  
  126. gsx#=dx#
  127. gsy#=dy#
  128.  
  129. y#=0
  130. For yy=1 To secy
  131. x#=0
  132. For xx=1 To secx
  133.  
  134. gp.glasspiece=New glasspiece
  135. gpg=g
  136.  
  137. gpindex=AddVertex(gs,x#,y#,0,x#,y#,0)
  138. AddVertex(gs,x#+dx#,y#,0,x#+dx#,y#,0)
  139. AddVertex(gs,x#,y#+dy#,0,x#,y#+dy#,0)
  140. AddVertex(gs,x#+dx#,y#+dy#,0,x#+dx#,y#+dy#,0)
  141.  
  142. AddTriangle gs,gpindex,gpindex+3,gpindex+1
  143. AddTriangle gs,gpindex,gpindex+3,gpindex+2
  144.  
  145. x#=x#+dx#
  146. Next
  147. y#=y#+dy#
  148. Next
  149.  
  150. ScaleMesh ge,wid#,hei#,1
  151.  
  152.  
  153. EntityFX ge,16
  154. EntityPickMode ge,2
  155. EntityColor ge,0,0,255
  156. EntityAlpha ge,0.5
  157.  
  158. Return g
  159.  
  160. End Function
  161.  
  162. Function glassbreak(g.glass)
  163. If groken=1 Then Return
  164. groken=1
  165.  
  166. For gp.glasspiece=Each glasspiece
  167. If gpg=g
  168.  
  169. gpp=CreatePivot()
  170.  
  171. gpx#=(VertexX(gs,gpindex)+VertexX(gs,gpindex+3))/2
  172. gpy#=(VertexY(gs,gpindex)+VertexY(gs,gpindex+3))/2
  173. gpz#=(VertexZ(gs,gpindex)+VertexZ(gs,gpindex+3))/2
  174. gpvx#=Rnd(-.02,.02)
  175. gpvy#=Rnd(-.02,.02)
  176. gpvz#=Rnd(-.02,.02)
  177. gppitch#=Rnd(-2,2)
  178. gpyaw#=Rnd(-2,2)
  179. gp
  180. oll#=Rnd(-2,2)
  181.  
  182. PositionEntity gpp,gpx,gpy,gpz
  183.  
  184. For n=0 To 3
  185. gppivot[n]=CreatePivot(gpp)
  186. PositionEntity gppivot[n],VertexX(gs,gpindex+n),VertexY(gs,gpindex+n),VertexZ(gs,gpindex+n),1
  187. Next
  188.  
  189. EndIf
  190. Next
  191.  
  192. End Function
  193.  
  194. Function glassupdate()
  195.  
  196. For g.glass=Each glass
  197. If groken=1
  198.  
  199. For gp.glasspiece=Each glasspiece
  200. If gpg=g
  201.  
  202. gpvy=gpvy-gravity#
  203.  
  204. gpx=gpx+gpvx
  205. gpy=gpy+gpvy
  206. gpz=gpz+gpvz
  207.  
  208. PositionEntity gpp,gpx+gpgsx#/2,gpy+gpgsy#/2,gpz,1
  209.  
  210. TurnEntity gpp,gppitch,gpyaw,gp
  211. oll
  212.  
  213. For n=0 To 3
  214. VertexCoords gpgs,gpindex+n,EntityX(gppivot[n],1),EntityY(gppivot[n],1),EntityZ(gppivot[n],1)
  215. Next
  216.  
  217. If gpy<-10 Then glassdelete(g)
  218.  
  219.  
  220. EndIf
  221. Next
  222.  
  223.  
  224.  
  225. EndIf
  226. Next
  227.  
  228. End Function
  229.  
  230. Function glassdelete(g.glass)
  231.  
  232. For gp.glasspiece=Each glasspiece
  233. If gpg=g
  234.  
  235. For n=0 To 3
  236. FreeEntity gppivot[n]
  237. Next
  238.  
  239. FreeEntity gpp
  240.  
  241. Delete gp
  242.  
  243. EndIf
  244. Next
  245.  
  246. FreeEntity ge
  247.  
  248. Delete g
  249.  
  250. End Function


Comments :


n8r2k(Posted 1+ years ago)

 try this for fun. If you press 1 you get a new glass box
Code: [Select]
; ID: 727
; Author: Jeppe Nielsen
; Date: 2003-06-24 19:34:01
; Title: Break the Glass
; Description: Shoot glass with the mouse.

;Break glass example, by Jeppe Nielsen 2003

Type glass

Field e

Field s

Field w#,h#

Field sx#,sy#

Field broken

End Type

Type glasspiece

Field p

Field pivot[3]

Field x#,y#,z#

Field vx#,vy#,vz#

Field pitch#,yaw#,roll#

Field g.glass

Field index

End Type


Const gravity#=0.004

Graphics3D 800,600,16,2

pivot=CreatePivot()
camera=CreateCamera(pivot)

CameraClsColor camera,255,255,255

MoveEntity camera,0,1,-4

;cube=CreateCube()
;PositionEntity cube,0,0,0
;FlipMesh cube

light=CreateLight(1)
RotateEntity light,-10,40,0
.label
g.glass=glassnew(2,2,20,20)
PositionEntity ge,0,0,-1
g.glass=glassnew(2,2,20,20)
PositionEntity ge,0,0,1
g.glass=glassnew(2,2,20,20)
PositionEntity ge,0,0,-1
TurnEntity ge,0,90,0
g.glass=glassnew(2,2,20,20)
PositionEntity ge,2,0,-1
TurnEntity ge,0,90,0

Color 0,0,0

Repeat

TurnEntity pivot,0,1,0





If MouseHit(1)>0

p=CameraPick(camera,MouseX(),MouseY())
g.glass=Last glass
While g<>Null
If ge=p
glassbreak(g)
Exit
EndIf
g=Before g
Wend


EndIf

glassupdate()


RenderWorld()
mx=MouseX()
my=MouseY()

Text 400,20,"SHOOT the glass cube :)",1

Oval mx-30,my-30,60,60,0
Plot mx,my


Flip
If KeyHit(2)
Goto label
EndIf
Until KeyDown(1)

End



Function glassnew.glass(wid#,hei#,secx,secy,parent=0)

g.glass=New glass

gw#=wid#
gh#=hei#


ge=CreateMesh(parent)

gs=CreateSurface(ge)

dx#=1/Float(secx)
dy#=1/Float(secy)

gsx#=dx#
gsy#=dy#

y#=0
For yy=1 To secy
x#=0
For xx=1 To secx

gp.glasspiece=New glasspiece
gpg=g

gpindex=AddVertex(gs,x#,y#,0,x#,y#,0)
AddVertex(gs,x#+dx#,y#,0,x#+dx#,y#,0)
AddVertex(gs,x#,y#+dy#,0,x#,y#+dy#,0)
AddVertex(gs,x#+dx#,y#+dy#,0,x#+dx#,y#+dy#,0)

AddTriangle gs,gpindex,gpindex+3,gpindex+1
AddTriangle gs,gpindex,gpindex+3,gpindex+2

x#=x#+dx#
Next
y#=y#+dy#
Next

ScaleMesh ge,wid#,hei#,1


EntityFX ge,16
EntityPickMode ge,2
EntityColor ge,0,0,255
EntityAlpha ge,0.5

Return g

End Function

Function glassbreak(g.glass)
If groken=1 Then Return
groken=1

For gp.glasspiece=Each glasspiece
If gpg=g

gpp=CreatePivot()

gpx#=(VertexX(gs,gpindex)+VertexX(gs,gpindex+3))/2
gpy#=(VertexY(gs,gpindex)+VertexY(gs,gpindex+3))/2
gpz#=(VertexZ(gs,gpindex)+VertexZ(gs,gpindex+3))/2
gpvx#=Rnd(-.02,.02)
gpvy#=Rnd(-.02,.02)
gpvz#=Rnd(-.02,.02)
gppitch#=Rnd(-2,2)
gpyaw#=Rnd(-2,2)
gpoll#=Rnd(-2,2)

PositionEntity gpp,gpx,gpy,gpz

For n=0 To 3
gppivot[n]=CreatePivot(gpp)
PositionEntity gppivot[n],VertexX(gs,gpindex+n),VertexY(gs,gpindex+n),VertexZ(gs,gpindex+n),1
Next

EndIf
Next

End Function

Function glassupdate()

For g.glass=Each glass
If groken=1

For gp.glasspiece=Each glasspiece
If gpg=g

gpvy=gpvy-gravity#

gpx=gpx+gpvx
gpy=gpy+gpvy
gpz=gpz+gpvz

PositionEntity gpp,gpx+gpgsx#/2,gpy+gpgsy#/2,gpz,1

TurnEntity gpp,gppitch,gpyaw,gpoll

For n=0 To 3
VertexCoords gpgs,gpindex+n,EntityX(gppivot[n],1),EntityY(gppivot[n],1),EntityZ(gppivot[n],1)
Next

If gpy<-10 Then glassdelete(g)


EndIf
Next



EndIf
Next

End Function

Function glassdelete(g.glass)

For gp.glasspiece=Each glasspiece
If gpg=g

For n=0 To 3
FreeEntity gppivot[n]
Next

FreeEntity gpp

Delete gp

EndIf
Next

FreeEntity ge

Delete g

End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal