1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
| Public Class Form1
Dim m_univ As New Universe
Dim bDrawCircle As Boolean
Dim m_v_max As Single = 5
Private Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
Dim shape As New Shape
Dim r As RectangleF
r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
Rnd() * PictureBox1.ClientSize.Height, 20, 20)
'r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
' 100, 20, 20)
shape.rect() = r
shape.velocity = New Windows.Vector(Rnd() * m_v_max, Rnd() * m_v_max)
'shape.velocity = New Windows.Vector(Rnd() * m_v_max, 0)
Dim drawObj As IDrawShape
Select Case ComboBox1.SelectedIndex
Case 0
drawObj = New DrawShapeCircle
shape.ShapeType = 1
Case 1
drawObj = New DrawShapeRect
shape.ShapeType = 2
Case 2
drawObj = New DrawShapeSphere("ball3d.png")
shape.ShapeType = 1
End Select
shape.DrawObj = drawObj
Dim opts As IDrawShapeOpts
opts = TryCast(drawObj, IDrawShapeOpts)
If (opts IsNot Nothing) Then
opts.pen = Pens.Brown
opts.brush = Brushes.Yellow
End If
m_univ.AddShape(shape)
Refresh()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
m_univ.Draw(e)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m_univ.DoTimeStep()
Refresh()
End Sub
Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
'Dim r As Rectangle = PictureBox1.ClientRectangle
m_univ.BoundingBox = PictureBox1.ClientRectangle 'New RectangleF(r.X, r.Y, r.Width, r.Height)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ComboBox1.SelectedIndex = 0
Dim testCircle As ICollisionTest = New CollisionTestCircle
m_univ.CollisionTest(1, 1) = testCircle
Dim actionDestory As ICollisionAction = New CollisionActionDestory
m_univ.CollisionAction(1, 1) = actionDestory
End Sub
End Class
Public Class CollisionTestCircle
Implements ICollisionTest
Dim v As New Windows.Vector
Public Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean Implements ICollisionTest.IsCollided
Dim r1 As RectangleF = shape1.rect
Dim r2 As RectangleF = shape2.rect
v.X = (r1.Left + r1.Right) / 2 - (r2.Left + r2.Right) / 2
v.Y = (r1.Top + r1.Bottom) / 2 - (r2.Top + r2.Bottom) / 2
Return (v.Length < (r1.Width / 2 + r2.Width / 2))
End Function
End Class
Public Class CollisionActionDestory
Implements ICollisionAction
Public Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape) Implements ICollisionAction.CollideShapes
shape1.Universe.DelShape(shape1)
shape2.Universe.DelShape(shape2)
End Sub
End Class
Public Class DrawShapeOpts
Implements IDrawShapeOpts
Dim m_pen As Pen = Pens.Black
Dim m_br As Brush
Public Property brush() As System.Drawing.Brush Implements IDrawShapeOpts.brush
Get
Return m_br
End Get
Set(ByVal value As System.Drawing.Brush)
m_br = value
End Set
End Property
Public Property pen() As System.Drawing.Pen Implements IDrawShapeOpts.pen
Get
Return m_pen
End Get
Set(ByVal value As System.Drawing.Pen)
m_pen = value
End Set
End Property
End Class
Public Class DrawShapeCircle
Inherits DrawShapeOpts
Implements IDrawShape
Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
If brush() IsNot Nothing Then
e.Graphics.FillEllipse(brush(), shape.rect)
End If
If pen() IsNot Nothing Then
e.Graphics.DrawEllipse(pen(), shape.rect())
End If
'e.Graphics.DrawEllipse(Pens.Black, shape.rect())
End Sub
End Class
Public Class DrawShapeRect
Inherits DrawShapeOpts
Implements IDrawShape
Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
e.Graphics.DrawRectangle(Pens.Black, shape.rect.X, shape.rect.Y, _
shape.rect.Width, shape.rect.Height)
End Sub
End Class
Public Class DrawShapeSphere
Implements IDrawShape
Dim m_pic As System.Drawing.Bitmap
Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
e.Graphics.DrawImage(m_pic, shape.rect)
End Sub
Sub New(ByVal file As String)
m_pic = New System.Drawing.Bitmap(file)
End Sub
End Class
Public Interface IDrawShape
Sub DrawShape(ByVal shape As Shape, ByVal e As PaintEventArgs)
End Interface
Public Interface IDrawShapeOpts
Property pen() As Pen
Property brush() As Brush
End Interface
Public Interface IMoveShape
Sub MoveShape(ByVal shape As Shape)
End Interface
Public Interface ICollisionTest
Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean
End Interface
Public Interface ICollisionAction
Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape)
End Interface
Public Class MoveShapeReflect
Implements IMoveShape
Dim pt As New Windows.Vector
Public Sub MoveShape(ByVal shape As Shape) Implements IMoveShape.MoveShape
Dim r As RectangleF = shape.rect()
pt.X = shape.velocity.X
pt.Y = shape.velocity.Y
r.Offset(pt.X, pt.Y)
Dim box As RectangleF = shape.Universe.BoundingBox
If r.Left < box.Left Then
r.X = box.Left
pt.X = -pt.X
ElseIf r.Right > box.Right Then
r.X = box.Right - r.Width
pt.X = -pt.X
End If
If r.Top < box.Top Then
r.Y = box.Top
pt.Y = -pt.Y
ElseIf r.Bottom > box.Bottom Then
r.Y = box.Bottom - r.Height
pt.Y = -pt.Y
End If
shape.rect = r
shape.velocity = pt
End Sub
End Class
Public Class Shape
Dim m_rect As New RectangleF
Dim m_drawObj As IDrawShape
Dim m_velocity As Windows.Vector
Dim m_moveObj As IMoveShape = New MoveShapeReflect
Dim m_univ As Universe
Dim m_bCollided As Boolean
Dim m_type As Integer
Dim m_brushOld As Brush = Brushes.Red
Property DrawObj() As IDrawShape
Get
Return m_drawObj
End Get
Set(ByVal value As IDrawShape)
m_drawObj = value
End Set
End Property
Property rect() As RectangleF
Get
Return m_rect
End Get
Set(ByVal value As RectangleF)
m_rect = value
End Set
End Property
Public Sub DrawShape(ByVal e As PaintEventArgs)
If m_drawObj IsNot Nothing Then
m_drawObj.DrawShape(Me, e)
End If
End Sub
Public Property velocity() As Windows.Vector
Get
Return m_velocity
End Get
Set(ByVal value As Windows.Vector)
m_velocity = value
End Set
End Property
Public Sub MoveShape()
If m_moveObj IsNot Nothing Then
m_moveObj.MoveShape(Me)
End If
End Sub
Property MoveObj() As IMoveShape
Get
Return m_moveObj
End Get
Set(ByVal value As IMoveShape)
m_moveObj = value
End Set
End Property
Property Universe() As Universe
Get
Return m_univ
End Get
Set(ByVal value As Universe)
m_univ = value
End Set
End Property
Property Collided() As Boolean
Get
Return m_bCollided
End Get
Set(ByVal value As Boolean)
If m_bCollided <> value Then
Dim opts As IDrawShapeOpts = TryCast(m_drawObj, IDrawShapeOpts)
If opts IsNot Nothing Then
Dim br As Brush = opts.brush
opts.brush = m_brushOld
m_brushOld = br
End If
End If
m_bCollided = value
End Set
End Property
Property ShapeType() As Integer
Get
Return m_type
End Get
Set(ByVal value As Integer)
m_type = value
End Set
End Property
End Class
Public Class Universe
Dim m_shapes As New List(Of Shape)
Dim m_box As RectangleF
Dim m_shAdd As New List(Of Shape)
Dim m_shDel As New List(Of Shape)
Dim m_bEditEnabled As Boolean = True
Const nShapeTypes As Integer = 10
Dim m_clnTest(nShapeTypes, nShapeTypes) As ICollisionTest
Dim m_clnAction(nShapeTypes, nShapeTypes) As ICollisionAction
ReadOnly Property Shapes() As List(Of Shape)
Get
Return m_shapes
End Get
End Property
Public Sub AddShape(ByVal shape As Shape)
If m_bEditEnabled Then
shape.Universe = Me
m_shapes.Add(shape)
Else
m_shAdd.Add(shape)
End If
End Sub
Public Sub DelShape(ByVal shape As Shape)
If m_bEditEnabled Then
m_shapes.Remove(shape)
Else
m_shDel.Add(shape)
End If
End Sub
Public Sub Draw(ByVal e As PaintEventArgs)
For i As Integer = 0 To m_shapes.Count - 1
'e.Graphics.DrawEllipse(Pens.Black, m_shapes(i).rect())
m_shapes(i).DrawShape(e)
Next
End Sub
Public Sub DoTimeStep()
m_bEditEnabled = False
For i As Integer = 0 To m_shapes.Count - 1
m_shapes(i).MoveShape()
m_shapes(i).Collided = False
Next
Dim sh1 As Shape, sh2 As Shape
'Dim r1 As RectangleF, r2 As RectangleF
For i As Integer = 0 To m_shapes.Count - 1
sh1 = m_shapes(i)
For j As Integer = i + 1 To m_shapes.Count - 1
sh2 = m_shapes(j)
If Not sh1.rect.IntersectsWith(sh2.rect) Then
Continue For
End If
If m_clnTest(sh1.ShapeType, sh2.ShapeType) Is Nothing Then
Continue For
End If
If m_clnTest(sh1.ShapeType, sh2.ShapeType).IsCollided(sh1, sh2) Then
If m_clnAction(sh1.ShapeType, sh2.ShapeType) IsNot Nothing Then
m_clnAction(sh1.ShapeType, sh2.ShapeType).CollideShapes(sh1, sh2)
End If
sh1.Collided = True
sh2.Collided = True
End If
Next
Next
m_bEditEnabled = True
For i = 0 To m_shAdd.Count - 1
AddShape(m_shAdd(i))
Next
For i = 0 To m_shDel.Count - 1
DelShape(m_shDel(i))
Next
m_shAdd.Clear()
m_shDel.Clear()
End Sub
Property BoundingBox() As RectangleF
Get
Return m_box
End Get
Set(ByVal value As RectangleF)
m_box = value
End Set
End Property
Property CollisionTest(ByVal i As Integer, ByVal j As Integer) As ICollisionTest
Get
Return m_clnTest(i, j)
End Get
Set(ByVal value As ICollisionTest)
m_clnTest(i, j) = value
End Set
End Property
Property CollisionAction(ByVal i As Integer, ByVal j As Integer) As ICollisionAction
Get
Return m_clnAction(i, j)
End Get
Set(ByVal value As ICollisionAction)
m_clnAction(i, j) = value
End Set
End Property
End Class |