forked from adamredwoods/minib3d-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcollision.monkey
1485 lines (944 loc) · 35.1 KB
/
tcollision.monkey
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Import minib3d
Import minib3d.math.vector
Import minib3d.math.geom
Import minib3d.math.matrix
'' will need to port over a collision library specifically for monkey xplatform
Const MAX_TYPES=100
'collision methods
Const COLLISION_METHOD_SPHERE:Int=1
Const COLLISION_METHOD_POLYGON:Int=2
Const COLLISION_METHOD_BOX:Int=3
Const COLLISION_METHOD_AABB:Int=4
'collision actions
''-- damping (water)? -- sticking?
Const COLLISION_RESPONSE_NONE:Int=0
Const COLLISION_RESPONSE_STOP:Int=1
Const COLLISION_RESPONSE_SLIDE:Int=2
Const COLLISION_RESPONSE_SLIDEXZ:Int=3
Const COLLISION_EPSILON:Float=0.001
Class TCollision
Field type:Int
Field impact:TCollisionImpact[]
''collision, pick
Field radius_x#=1.0,radius_y#=1.0
Field box_x#=-1.0,box_y#=-1.0,box_z#=-1.0,box_w#=2.0,box_h#=2.0,box_d#=2.0
Method New()
End
Method Copy:TCollision()
Local cc:TCollision = New TCollision
cc.type = type
cc.radius_x = radius_x
cc.radius_y = radius_y
cc.box_x = box_x
cc.box_y = box_y
cc.box_z = box_z
cc.box_w = box_w
cc.box_h = box_h
cc.box_d = box_d
Return cc
End
End
Class TCollisionImpact
Field x#,y#,z#
Field nx#,ny#,nz#
Field time#
Field ent:TEntity
Field surf:Int
Field tri:Int
End
Class TCollisionPair
Global list:List<TCollisionPair> =New List<TCollisionPair>
Global ent_lists:List<TEntity>[MAX_TYPES]
Field src_type:Int
Field des_type:Int
Field col_method=0
Field response=0
Method New()
End
Method Delete()
End
'' a better way to create tcollision lists
Method ListAddLast(ent:TEntity, type_no:Int)
If ent_lists[type_no]=Null Then ent_lists[type_no] = New List<TEntity> ' create new list is one doesn't exist
ent_lists[type_no].AddLast(ent)
End
Method ListRemove(ent:TEntity, type_no:Int)
ent_lists[type_no].RemoveEach(ent)
End
Function Collisions(src_no:Int, dest_no:Int, method_no:Int, response_no:Int=0)
Local col:TCollisionPair=New TCollisionPair
col.src_type=src_no
col.des_type=dest_no
col.col_method=method_no
col.response=response_no
' check to see if same collision pair already exists
For Local col2:TCollisionPair=Eachin list
If col2.src_type=col.src_type
If col2.des_type=col.des_type
' overwrite old method and response values
col2.col_method=col.col_method
col2.response=col.response
Return
Endif
Endif
Next
list.AddLast(col)
End
Function Clear()
list.Clear()
End
End
Class CollisionInfo
Field dv:Vector = New Vector
Field sv:Vector = New Vector
Field radii:Vector = New Vector
Field panic:Vector = New Vector
Field mesh_coll:MeshCollider
Field radius:Float, inv_y_scale:Float, y_scale:Float
Field n_hit:Int
Field planes:Plane[] = [New Plane, New Plane, New Plane, New Plane]
Field col_points:Vector[] = [New Vector, New Vector, New Vector, New Vector]
Field coll_method:Int
Field coll_line:Line = New Line()
Field tf_line:Line = New Line()
Field dir:Vector = New Vector
Field tform:TransformMat=New TransformMat
Field y_tform:TransformMat = New TransformMat
'Field oform:TransformMat=New TransformMat ''original ent mat
Field renew:Matrix = New Matrix
Field renew2:Matrix = New Matrix
Field renew3:Matrix = New Matrix
Field tf_offset:Vector = New Vector
Field inv_scale:Vector = New Vector
''for triangle collisions
Field tf_scale:Vector = New Vector
Field tf_radius:Vector = New Vector ''scaled radius for sphere in local mesh space
Field tf_box:Box = New Box()
Field td:Float
Field td_xz:Float
Field hits:Int
Field dst_radius:Float
Field ax:Float
Field ay:Float
Field az:Float
Field bx:Float
Field by:Float
Field bz:Float
'' globals
Const MAX_HITS:Int=10
Const EPSILON=0.000001 '' a small value
'' per update globals
Global vec_a:Vector = New Vector(0.0,0.0,0.0)
Global vec_b:Vector = New Vector(0.0,0.0,0.0)
Global vec_c:Vector = New Vector(0.0,0.0,0.0)
'Local vec_radius:Vector = New Vector(0.0,0.0,0.0)
Global vec_i:Vector = New Vector(0.0,0.0,0.0)
Global vec_j:Vector = New Vector(0.0,0.0,0.0)
Global vec_k:Vector = New Vector(0.0,0.0,0.0)
Global t_mat:Matrix
Global vec_v:Vector = New Vector(0.0,0.0,0.0)
Global test_vec:Vector = New Vector
Global test_dot:Vector = New Vector
Global testx:Float, testy:Float, testz:Float
Global testx2:Float, testy2:Float, testz2:Float
Global testline:Line = New Line()
Global trigger:Int=0
Method New()
t_mat = New Matrix()
tform = New TransformMat()
End
Method UpdateRay:Int(sv2:Vector,dv2:Vector,radii2:Vector)
t_mat = New Matrix()
tform = New TransformMat()
dv.Overwrite(dv2)
sv.Overwrite(sv2)
panic.Overwrite(sv2)
radii.Overwrite(radii2)
radius = radii.x
planes[0].Update(0.0,0.0,0.0, 0)
planes[1].Update(0.0,0.0,0.0, 0)
planes[2].Update(0.0,0.0,0.0, 0)
planes[3].Update(0.0,0.0,0.0, 0)
col_points[0].Update(0.0,0.0,0.0)
col_points[1].Update(0.0,0.0,0.0)
col_points[2].Update(0.0,0.0,0.0)
col_points[3].Update(0.0,0.0,0.0)
y_scale=1.0
inv_y_scale=1.0
y_tform.m.grid[1][1]=1.0 ''mat.j.y = mat.grid[1][1]
If( radii.x <> radii.y )
y_scale=radius/radii.y
y_tform.m.grid[1][1]=radius/radii.y
inv_y_scale=1.0/y_scale
sv.y *= y_scale
dv.y *= y_scale
Endif
coll_line.Update( sv.x, sv.y, sv.z, dv.x-sv.x, dv.y-sv.y, dv.z-sv.z )
dir.Update((coll_line.d.x),(coll_line.d.y),(coll_line.d.z))
dir = dir.Normalize()
td=coll_line.d.Length()
tf_radius = tf_scale.Multiply(radius)
tf_line.Update(coll_line.o.x,coll_line.o.y,coll_line.o.z, coll_line.d.x,coll_line.d.y,coll_line.d.z)
td_xz = New Vector( coll_line.d.x,0,coll_line.d.z ).Length()
''
hits=0
dst_radius=1.0
ax=1.0
ay=1.0
az=1.0
bx=1.0
by=1.0
bz=1.0
End
Method UpdateShape (ent:TEntity)
dst_radius =ent.collision.radius_x
ax =ent.collision.box_x
ay =ent.collision.box_y
az =ent.collision.box_z
bx =ent.collision.box_x+ent.collision.box_w
by =ent.collision.box_y+ent.collision.box_h
bz =ent.collision.box_z+ent.collision.box_d
End
Function UpdateCollisions()
'DebugLog "Update Collisions"
Local coll_obj:CollisionObject
Local col_info:CollisionInfo= New CollisionInfo()
' loop through collision setup list, containing pairs of src entities and des entities to be check for collisions
For Local i:Int=0 Until MAX_TYPES
' if no entities exist of src_type then do not check for collisions
If TCollisionPair.ent_lists[i]=Null Then Continue
' loop through src entities
For Local ent:TEntity=Eachin TCollisionPair.ent_lists[i]
ent.no_collisions=0
' if src entity is hidden or it's parent is hidden then do not check for collision
If ent.Hidden()=True Then Continue
ent.collision.impact =New TCollisionImpact[0]
vec_a.Update(ent.mat.grid[3][0],ent.mat.grid[3][1],-ent.mat.grid[3][2]) ''line dest
vec_b.Update(ent.old_x,ent.old_y,ent.old_z) ''line source
vec_c.Update(ent.collision.radius_x,ent.collision.radius_y,ent.collision.radius_x)
''make collision line
col_info.UpdateRay(vec_b,vec_a,vec_c)
Local response:Int
Repeat
Local hit:Int =0
Local ent2_hit:TEntity=Null
coll_obj = New CollisionObject
For Local col_pair:TCollisionPair=Eachin TCollisionPair.list
If col_pair.src_type = i
' if no entities exist of des_type then do not check for collisions
If TCollisionPair.ent_lists[col_pair.des_type]=Null Then Continue
' loop through entities2 that are paired with src entities
For Local ent2:TEntity=Eachin TCollisionPair.ent_lists[col_pair.des_type]
' if entity2 is hidden or it's parent is hidden then do not check for collision
If ent2.Hidden()=True Then Continue
If ent=ent2 Then Continue ' if src ent is same as entity2 then do not check for collision
If QuickCheck(ent,ent2)=False Then Continue ' quick check to see if entities are colliding
col_info.CollisionSetup(ent2, col_pair.col_method, coll_obj)
hit = col_info.CollisionDetect(coll_obj)
If hit
ent2_hit=ent2
response=col_pair.response
Endif
Next
Endif
Next
If ent2_hit<>Null
ent.no_collisions=ent.no_collisions+1
Local i=ent.no_collisions-1
ent.collision.impact=ent.collision.impact.Resize(i+1)
ent.collision.impact[i]=New TCollisionImpact
ent.collision.impact[i].x=coll_obj.col_coords.x
ent.collision.impact[i].y=coll_obj.col_coords.y
ent.collision.impact[i].z=coll_obj.col_coords.z
ent.collision.impact[i].nx=coll_obj.normal.x
ent.collision.impact[i].ny=coll_obj.normal.y
ent.collision.impact[i].nz=coll_obj.normal.z
ent.collision.impact[i].ent=ent2_hit
If TMesh(ent2_hit)<>Null
ent.collision.impact[i].surf=coll_obj.surface '& $0000ffff
Else
ent.collision.impact[i].surf=0
Endif
''get the real tri index (byte packed in surface) --not anymore
ent.collision.impact[i].tri=coll_obj.index '((coll_obj.surface & $ffff0000) Shr 16)
''exit on no hits
If col_info.CollisionResponse(coll_obj, response)=False Then Exit
Else
Exit
Endif
Forever
Local hits:Int =col_info.CollisionFinal(coll_obj, response)
If hits
ent.PositionEntity(coll_obj.col_pos.x,coll_obj.col_pos.y,coll_obj.col_pos.z,True)
Endif
ent.old_x=ent.mat.grid[3][0]
ent.old_y=ent.mat.grid[3][1]
ent.old_z=-ent.mat.grid[3][2]
Next
Next
End
''
'' line is in world space. need to change line to local mesh space. watch out for parented global scale versus local scale
''
Method CollisionSetup(ent:TEntity, c_method:Int, coll_obj:CollisionObject)
'Print "setup----"+ent.classname
coll_method = c_method
''remove global scaling from matrix
Local ex:Float = 1.0/ent.gsx'*ent.gsx)
Local ey:Float = 1.0/ent.gsy'*ent.gsy)
Local ez:Float = 1.0/ent.gsz'*ent.gsz)
inv_scale.Overwrite(ex,ey,ez)
tform.m.grid[0] = [ent.mat.grid[0][0]*ex,ent.mat.grid[1][0]*ey,-ent.mat.grid[2][0]*ez, 0.0]
tform.m.grid[1] = [ent.mat.grid[0][1]*ex,ent.mat.grid[1][1]*ey,-ent.mat.grid[2][1]*ez, 0.0]
tform.m.grid[2] = [-ent.mat.grid[0][2]*ex,-ent.mat.grid[1][2]*ey,ent.mat.grid[2][2]*ez, 0.0]
renew2 = tform.m.Inverse()
If( y_scale<>1.0 )
tform = y_tform.Multiply(tform)
Endif
' if pick mode is sphere or box then update collision info object to include entity radius/box info
If coll_method=COLLISION_METHOD_BOX
CollisionBoxSetup(ent, radius, coll_obj)
Elseif If coll_method=COLLISION_METHOD_SPHERE
CollisionSphereSetup(ent, radius, coll_obj)
Elseif coll_method=COLLISION_METHOD_POLYGON
CollisionTriangleSetup(ent, radius, coll_obj)
Elseif coll_method=COLLISION_METHOD_AABB
CollisionAABBSetup(ent, radius, coll_obj)
Endif
End
Method CollisionSphereSetup(ent:TEntity, radius:Float, coll_obj:CollisionObject)
tf_offset = New Vector(ent.mat.grid[3][0],ent.mat.grid[3][1],-ent.mat.grid[3][2] )
UpdateShape(ent)
coll_obj.time = 1.0
End
Method CollisionBoxSetup(ent:TEntity, radius:Float, coll_obj:CollisionObject)
tf_offset.Overwrite(-ent.mat.grid[3][0],-ent.mat.grid[3][1],ent.mat.grid[3][2] )
tf_line = tform.m.Multiply(coll_line.Add(tf_offset) )
tf_scale.Overwrite(ent.gsx, ent.gsy, ent.gsz)
UpdateShape(ent)
coll_obj.time = tf_line.d.Add(tf_radius).DistanceSquared()+radius
End
Method CollisionAABBSetup(ent:TEntity, radius:Float, coll_obj:CollisionObject)
tf_line = New Line(coll_line.o,coll_line.d)
UpdateShape(ent)
coll_obj.time = tf_line.d.Add(tf_radius).DistanceSquared()+radius
End
Method CollisionTriangleSetup(ent:TEntity, radius:Float, coll_obj:CollisionObject)
tf_offset.Overwrite(-ent.mat.grid[3][0],-ent.mat.grid[3][1],ent.mat.grid[3][2] )
tf_line = tform.m.Multiply(coll_line.Add(tf_offset) )
'tf_scale.Overwrite(1.0/ent.gsx, 1.0/ent.gsy, 1.0/ent.gsz)
tf_scale.Overwrite(ent.gsx, ent.gsy, ent.gsz)
tf_radius.Overwrite(radius, radius, radius)
Local mesh:TMesh = TMesh(ent)
If mesh<>Null
mesh_coll = mesh.col_tree.CreateMeshTree(mesh) ' create collision tree for mesh if necessary
Endif
'tf_scale = tform.m.Multiply(tf_scale)
mesh_coll.tf_scale = tf_scale
'Local radoffset:Vector = tf_line.d.Normalize().Multiply(radius+0.001) ''MUST HAVE r+r??
tf_box.Update(tf_line.o)
tf_box.Update(tf_line.d)'.Add(radoffset))
tf_box.Expand(radius) 'tf_radius)
'Local ex# = 1.0/ent.gsx
'Local ey# = 1.0/ent.gsy
'Local ez# = 1.0/ent.gsz
tf_box.a.Multiply(inv_scale); tf_box.b.Multiply(inv_scale)
If radius = 0.0
''setup ray for pick
tf_radius.x = 0.0
mesh_coll.RayBoxSetup( New Line( tf_line.o.Multiply(inv_scale) , tf_line.d.Multiply(inv_scale)) ) ''may need new line with radoffset
Endif
coll_obj.time = tf_line.d.Add(tf_radius).DistanceSquared()+radius ''dist squ
If radius = 0.0 Then coll_obj.time = 1.0 ''raytriangle
End
Method CollisionDetect:Int(coll_obj:CollisionObject)
Local res:Int=0
Select ( coll_method )
Case COLLISION_METHOD_SPHERE
res = coll_obj.SphereCollide( coll_line,radius,tf_offset,dst_radius )
If res
coll_obj.col_coords=coll_line.Multiply(coll_obj.time)
RegisterHitPlane ( coll_obj.col_coords, coll_obj.normal)
Endif
radius = 0.0
Case COLLISION_METHOD_POLYGON
If mesh_coll.CollideAABB( tf_box, tf_radius, coll_obj, mesh_coll.tree )
res = mesh_coll.TriNodeCollide( tf_box, tf_line, tf_radius, coll_obj, tf_scale )
''adjust normal to original ent. matrix
If res 'And coll_obj.time < 99999.0
coll_obj.normal = renew2.Multiply(coll_obj.normal).Normalize()
''convert col_coords back to world space
coll_obj.col_coords = renew2.Multiply(coll_obj.col_coords ).Subtract(tf_offset)
RegisterHitPlane ( coll_obj.col_coords, coll_obj.normal)
Endif
Endif
Case COLLISION_METHOD_BOX
Local box:Box= New Box(New Vector(ax,ay,az), New Vector(bx,by,bz))
If( coll_obj.SphereBox( tf_line, radius, box, tf_scale ) ) ''BoxCollide( ~t*line,radius,box )
coll_obj.normal=renew2.Multiply(coll_obj.normal) '.Normalize()
'coll_obj.normal=coll_obj.normal
res = True
'coll_obj.col_coords=coll_line.Multiply(coll_obj.time)'.Subtract(tf_offset)
coll_obj.col_coords = renew2.Multiply(coll_obj.col_coords ).Subtract(tf_offset)
RegisterHitPlane ( coll_obj.col_coords, coll_obj.normal)
Endif
'radius = 0.0
'' *** TO DO ***
Case COLLISION_METHOD_AABB
Local box_target:Box= New Box(New Vector(ax,ay,az), New Vector(bx,by,bz))
'Local box:Box= New Box(New Vector(-radius,-radius,-radius), New Vector(radius,radius,radius))
Local line_box:Box = New Box(tf_line)
line_box.Expand(radius+radius)
res= coll_obj.AABBCollide(tf_line, line_box, box_target)
If res
'coll_obj.normal=
'coll_obj.col_coords=coll_line.Multiply(coll_obj.time)
RegisterHitPlane ( coll_obj.col_coords, coll_obj.normal)
Endif
radius = 0.0
End
'If res
'CollisionInfo.test_vec = coll_obj.col_coords.Copy()
'CreateTestLine(coll_obj.col_coords, coll_obj.col_coords.Add(coll_obj.normal.Multiply(1.0)), 100,0,100 )
'Endif
Return res
End
'' Physics
Method RegisterHitPlane(coords:Vector, norm:Vector)
hits +=1
If hits>3 Or hits<1 Then Return
n_hit = hits-1
planes[n_hit] = New Plane( coords, norm)
col_points[n_hit] = coords.Copy()
End
Method CollisionResponse:Int(coll:CollisionObject,response:Int)
''notes: needed a copy for dv and sv when assigning, but not so with local vectors
'' let's not worry about time here, and work only with collision point
If( hits>=MAX_HITS ) Return False
Local new_coords:Vector
''create correct normal response
If ( response = COLLISION_RESPONSE_SLIDEXZ )
coll.normal.y = 0.0'-coll_line.o.y
coll.normal = coll.normal.Normalize()
Endif
new_coords=coll.col_coords.Add(coll.normal.Multiply(radius))
'new_coords = coll.col_coords.Copy() 'coll_line.o.Add(coll_line.d.Multiply(coll.time))
new_coords.y *= y_scale
'coll.col_coords=new_coords
Local coll_plane:Plane = New Plane( new_coords,coll.normal )
coll_plane.d -= COLLISION_EPSILON
'Print "coll_time "+coll.time+" res:"+response+" colnorm:"+coll.normal.ToString+" rad:"+radius
sv.Overwrite(new_coords)
If ( response=COLLISION_RESPONSE_STOP )
dv.Overwrite(sv)
Return False
Endif
''find nearest point on plane To dest
Local nv:Vector =coll_plane.Nearest( dv )
'CreateTestLine(new_coords, nv, 200,255,200 )
'' multiple plane hits
If( n_hit>0 )
If( n_hit=1 )
'Print ""
'Print ""
'Print ":: n_hit "+n_hit+" "+planes[0].Distance(nv)+" "+Abs( planes[0].n.Dot( coll_plane.n ))
Local plane_norms# = planes[0].n.Dot( coll_plane.n )
If( planes[0].Distance(nv)> radius )
n_hit=0
Elseif plane_norms < -0.999
''SQUISHED! planes are parallel
hits=MAX_HITS
dv.Overwrite(sv)
Return False
Elseif( Abs( plane_norms ) < 1.0-EPSILON )
nv=coll_plane.Intersect( planes[0] ).Nearest( dv )
'Print ":: n_hit1 "+nv
'CreateTestLine(nv, nv.Add(pn.Multiply(1.0)), 0,100,0 )
Endif
Else
If( planes[0].Distance(nv)> radius And planes[1].Distance(nv)> radius )
'Print ":: n_hit2 "+n_hit
'dv=nv
n_hit=0
Else
dv.Overwrite(sv)
Return False
Endif
Endif
n_hit =0
'test_dot.Overwrite(nv)
'Print " cv "+cv.ToString()
'CreateTestLine(cv, cv.Add(nn), 200,0,200 )
Endif
'CollisionInfo.trigger+=1
Local dd:Vector = nv.Subtract(sv)
'Print "dd "+dd.ToString+" sv "+sv.ToString+" nv "+nv.ToString
''going behind initial direction? really necessary?
'If( dd.Dot( dir )<=0 )
'Print "***** behind"
'dv.Overwrite(sv)
'Return False
'Endif
'' -- apparently, NV gives a correct response
Local d:Float=dd.Dot(dd)
If d< EPSILON Then dv.Overwrite(sv); Return False
If( response = COLLISION_RESPONSE_SLIDE )
Elseif( response = COLLISION_RESPONSE_SLIDEXZ )
nv.Overwrite( nv.x, coll_line.o.y, nv.z)
Endif
'' since we are going back to test more collisions, need to update the coll_line
dv=nv
coll_line.o.Overwrite(sv)
coll_line.d.Overwrite(dv.Subtract(sv))'dd) '' length, from o
'Print "final dv :"+dv.ToString() '+" dd:"+dd.ToString()
Return True
End
Method CollisionFinal:Bool(coll:CollisionObject, response:Int)
If (response = COLLISION_RESPONSE_NONE) Then Return False
If( hits )
If( hits<MAX_HITS )
dv.y *= inv_y_scale
coll.col_pos=dv.Copy()
Else
coll.col_pos=panic.Copy()
Endif
Return True
Endif
Return False
End
' perform quick check to see whether it is possible that ent and ent 2 are intersecting
Function QuickCheck:Bool(ent:TEntity,ent2:TEntity)
' check to see if src ent has moved since last update - if not, no intersection
If ent.old_x=ent.EntityX(True) And ent.old_y=ent.EntityY(True) And ent.old_z=ent.EntityZ(True)
Return False
Endif
Return True
End
End
Class CollisionObject
Const ONETHIRD:Float = 1.0/3.0
Global BOXQUADS:Int[]=[
2,3,1,0,
3,7,5,1,
7,6,4,5,
6,2,0,4,
6,7,3,2,
0,1,5,4 ]
Field time:Float = 2.0
Field normal:Vector = New Vector()
Field surface:Int=0
Field index:Int=0
Field coll_u:Float ''uv coords, only polygon coll
Field coll_v:Float
Field rad2:Float
Field tform:TransformMat
Field col_coords:Vector = New Vector()
Field col_normal:Vector = New Vector()
Field col_time:Int
Field col_surface:Int
Field col_index:Int
Field col_pos:Vector = New Vector()
Global test2:Vector = New Vector()
Global test3:Vector = New Vector()
Global renew:Matrix
Global scale2:Vector = New Vector()
Global tf_offset:Vector = New Vector()
Global trip:Int=0
Private
Field int_vec:Vector = New Vector() ''temp intersection vec
Public
Method Update:Int( line:Line,t:Float, n:Vector, u#=0.0, v#=0.0, intersect:Vector=Null )
'' If( t<0 || t>time ) Return False ''--need to keep negative time
If( t>time ) Return 0 ''need the closest intersection
Local p:Plane
If intersect
'' define col_coords here, to manage triangle-plane collisions properly
'' col_coords is closest point to triangle plane (in triangle space)
col_coords = intersect 'tplane.Nearest( line.o.Add(line.d.Multiply(t)) )
p = New Plane(col_coords,n) 'tplane
Else
p = New Plane(line.Multiply(t),n)
If ( (p.n.Dot(line.o) + p.d ) < -COLLISION_EPSILON ) Return 0 ''starts behind plane
Endif
If( p.n.Dot( line.d )> -COLLISION_EPSILON ) Return 0 ''moving away from plane
'If( p.Distance(line.o)< -COLLISION_EPSILON ) Return 0
'If ( (p.n.Dot(line.o) + p.d ) < -COLLISION_EPSILON ) Return 0 ''starts behind plane
'Print "< UPDATE "+t
time=t
normal=n
coll_u = u
coll_v = v
Return 1
End
Method AABBCollide:Int( li:Line, box:Box, target:Box)
Local n:Vector = New Vector()
Local r1:Float, r2:Float, r3:Float, r4:Float, r5:Float, r6:Float, t:Float
If box.b.x<target.b.x Then r1 = box.b.x Else r1 = target.b.x
If box.a.x>target.a.x Then r2 = box.a.x Else r2 = target.a.x
If box.b.y<target.b.y Then r3 = box.b.y Else r3 = target.b.y
If box.a.y>target.a.y Then r4 = box.a.y Else r4 = target.a.y
If box.b.z<target.b.z Then r5 = box.b.z Else r5 = target.b.z
If box.a.z>target.a.z Then r6 = box.a.z Else r6 = target.a.z
If Not ((r1>=r2) And (r3>=r4) And (r5>=r6)) Return 0
Local target_center:Vector = New Vector( (target.a.x+target.b.x)*0.5, (target.a.y+target.b.y)*0.5, (target.a.z+target.b.z)*0.5)
t = li.o.DistanceSquared( target_center)
Return Update( li,t, n )
End
Method SphereCollide:Int ( line:Line,radius:Float,dest:Vector,dest_radius:Float )
radius += dest_radius
Local l:Line = New Line( line.o.Subtract(dest) ,line.d )
Local a:Float=l.d.Dot(l.d)
If a<0.001 Then Return 0
Local b:Float=l.o.Dot(l.d)*2.0
Local c:Float=l.o.Dot(l.o)-radius*radius
Local d:Float=b*b-4.0*a*c
If( d<0 ) Return 0
Local sd:Float = Sqrt(d)
'Local q:Float
'If b<0 Then q = (-b-sd) * 0.5 Else q = (-b+sd)*0.5
Local inva:Float = 1.0/a
Local t1:Float=(-b+sd)*0.5*inva '(-b+sd)/(2.0*a) 'q/a
Local t2:Float=(-b-sd)*0.5*inva '(-b-sd)/(2.0*a) 'c/q
Local t:Float
If t1<t2 Then t=t1 Else t=t2
If( t>time ) Return 0
Return Update( line,t,(l.Multiply(t).Normalize()) )
End