Ooops
October 28, 2020, 06:00:31 AM

### Author Topic: [bb] basic collisions example by RemiD [ 1+ years ago ]  (Read 1308 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] basic collisions example by RemiD [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : basic collisions example
Author : RemiD
Posted : 1+ years ago

Description : turningmoving colliders (ellipsoid) -> static collidables (mesh) | turningmoving colliders (ellipsoid) <-> turningmoving colliders (ellipsoid)

Code :
Code: BlitzBasic
1. ;Here is an example of how to use the collision system and how to manage the turningmoving colliders and the static collidables
2. ;collidables = static meshes
3. ;colliders = turningmoving ellipsoids
4. ;turningmoving colliders (ellipsoid) -> static collidables (mesh) | turningmoving colliders (ellipsoid) <-> turningmoving colliders (ellipsoid)
5. ;Z,S,Q,D to turn move the first ellipsoid collider
6. ;UP,DOWN,LEFT,RIGHT to turn move the second ellipsoid collider
7.
8. Graphics3D(640,480,32,2)
9.
10. SeedRnd(MilliSecs())
11.
12. Camera = CreateCamera()
13. CameraRange(Camera,0.1,100)
14. CameraClsColor(Camera,000,000,000)
15.
16. ;create position rotate the statics collidables (meshes)
17. Global StaticsCount%
18. Dim Static(10)
19.
20. For n% = 1 To 10
21.  StaticsCount = StaticsCount + 1
22.  I% = StaticsCount
23.  C% = Rand(1,2)
24.  If( C = 1 )
25.   Static(I) = CreateCube()
26.  Else If( C = 2 )
27.   Static(I) = CreateCylinder(8)
28.  EndIf
29.  ScaleMesh(Static(I),0.5/2,0.5/2,0.5/2)
30.  PositionMesh(Static(I),0,0.5/2,0)
31.  .LineChooseStaticPosition
32.  PositionEntity(Static(I),Rnd(-5,5),0,Rnd(-5,5))
33.  ;is there another static at this position ?
34.  For OI% = 1 To StaticsCount
35.   If(OI <> I)
36.    If(EntityDistance(Static(I),Static(OI)) < 0.36+0.36)
37.     Goto LineChooseStaticPosition
38.    EndIf
39.   EndIf
40.  Next
41. Next
42.
43. ;create position rotate the turningmovings colliders (ellipsoids)
44. Global TurningMovingsCount%
45. Dim TurningMoving(10)
46.
47. For n% = 1 To 2
48.  TurningMovingsCount = TurningMovingsCount + 1
49.  I% = TurningMovingsCount
50.  TurningMoving(I) = CreateSphere(16)
51.  ScaleMesh(TurningMoving(I),0.5/2,0.5/2,0.5/2)
52.  DebugTurningMovingDirection = CreateCube()
53.  ScaleMesh(DebugTurningMovingDirection,0.01/2,0.01/2,1.0/2)
54.  PositionMesh(DebugTurningMovingDirection,0,0,1.0/2)
55.  PositionEntity(DebugTurningMovingDirection,EntityX(TurningMoving(I),True),EntityY(TurningMoving(I),True),EntityZ(TurningMoving(I),True))
56.  RotateEntity(DebugTurningMovingDirection,EntityPitch(TurningMoving(I),True),EntityYaw(TurningMoving(I),True),EntityRoll(TurningMoving(I),True))
57.  EntityParent(DebugTurningMovingDirection,TurningMoving(I),True)
58.  .LineChooseTurningMovingPosition
59.  PositionEntity(TurningMoving(I),Rnd(-5,5),0.25,Rnd(-5,5))
60.  ;is there a static at this position ?
61.  For OI% = 1 To StaticsCount
62.   If(EntityDistance(TurningMoving(I),Static(OI)) < 0.25+0.36)
63.    Goto LineChooseTurningMovingPosition
64.   EndIf
65.  Next
66.  ;is there another turningmoving at this position ?
67.  For OI% = 1 To StaticsCount
68.   If(OI <> I)
69.    If(EntityDistance(Static(I),Static(OI)) < 0.25+0.25)
70.     Goto LineChooseTurningMovingPosition
71.    EndIf
72.   EndIf
73.  Next
74.  RotateEntity(TurningMoving(I),0,Rnd(-180,180),0)
75. Next
76.
77. PositionEntity(Camera,0,7.5,-7.5)
78. RotateEntity(Camera,45,0,0)
79.
80. ;put each static collidable in a collision group
81. For I% = 1 To StaticsCount
82.  EntityType(Static(I),1) ;group 1
83. Next
84.
85. ;put each turningmoving collider in a collision group
86. For I% = 1 To TurningMovingsCount
88.  EntityType(TurningMoving(I),2) ;group 2
89. Next
90.
91. ;configure the collision detection and response between the different collisions groups
92. Collisions(2,1,2,1) ;collision group 2 -> group 1, ellipsoid to mesh, stop
93. Collisions(2,2,1,1) ;collision group 2 -> group 2, ellipsoid to ellipsoid, stop
94.
95. While(KeyDown(1)=0)
96.
97.  ;turn move turningmoving 1
98.  If(KeyDown(30)=1)
99.   TurnEntity(TurningMoving(1),0,1.5,0)
100.  ElseIf(KeyDown(32)=1)
101.   TurnEntity(TurningMoving(1),0,-1.5,0)
102.  EndIf
103.  If(KeyDown(17)=1)
104.   MoveEntity(TurningMoving(1),0,0,0.05)
105.  ElseIf(KeyDown(31)=1)
106.   MoveEntity(TurningMoving(1),0,0,-0.05)
107.  EndIf
108.
109.  ;turn move turningmoving 2
110.  If(KeyDown(203)=1)
111.   TurnEntity(TurningMoving(2),0,1.5,0)
112.  ElseIf(KeyDown(205)=1)
113.   TurnEntity(TurningMoving(2),0,-1.5,0)
114.  EndIf
115.  If(KeyDown(200)=1)
116.   MoveEntity(TurningMoving(2),0,0,0.05)
117.  ElseIf(KeyDown(208)=1)
118.   MoveEntity(TurningMoving(2),0,0,-0.05)
119.  EndIf
120.
121.  ;detect collisions update colliders depending on the "response"
122.  UpdateWorld()
123.
124.  For I% = 1 To StaticsCount
125.   EntityColor(Static(I),255,255,255)
126.  Next
127.  For I% = 1 To TurningMovingsCount
128.   EntityColor(TurningMoving(I),255,255,255)
129.  Next
130.
131.  ;for each turningmoving collider
132.  For I% = 1 To TurningMovingsCount
133.   ;check if a collision happened between this turningmoving collider and a static collidable or another turningmoving collider
134.   CC% = CountCollisions(TurningMoving(I))
135.   ;if yes
136.   If(CC > 0)
137.    ;retrieve the collided with CollisionEntity(Collider,1) ;note : in most cases there is only one collision but if CountCollisions(Collider) is superior to 1, you can analyze the others collisions
138.    ;retrieve the collision point with CollisionX(Collider,1) CollisionY(Collider,1) CollisionZ(Collider,1) ;same note as above
139.    ;retrieve the collision normal with CollisionNX(Collider,1) CollisionNY(Collider,1) CollisionNZ(Collider,1) ;same note as above
140.    ;update the entities as you want...
141.    EntityColor(TurningMoving(I),255,000,000) ;turningmoving collider (ellipsoid)
142.    EntityColor(CollisionEntity(TurningMoving(I),1),125,000,000) ;static collidable (mesh) or another turningmoving collider (ellipsoid)
143.   EndIf
144.  Next
145.
146.  SetBuffer(BackBuffer())
147.  RenderWorld()
148.
149.  Flip(1)
150.
151. Wend
152.
153. End()