page7.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <font face="verdana">
  2. <table border="1" width="100%" bordercolor="#FFFFFF">
  3. <tr>
  4. <td width="61%" bgcolor="#6097C9"><font face="verdana">
  5. <h1 align="left"><font color="#FFFFFF">&nbsp;Collision Detection</font></h1>
  6. <p align="left"><b>&nbsp;Aim</b>: To detect a collision between 2 objects<br>
  7. &nbsp;<b>Skill level</b>: Intermediate<br>
  8. &nbsp;<b>Files Needed</b>: collision.bb</font>
  9. <p>&nbsp;</td>
  10. <td width="5%">&nbsp;</td>
  11. <td width="34%">
  12. <p align="center"><font face="verdana">
  13. <img src="9c.jpg" alt="Movement" width="167" height="127" align="right">
  14. </font>
  15. </p>
  16. </td>
  17. </tr>
  18. </table>
  19. <p>
  20. In this example I shall be using 2 ready-made entity objects, a Sphere and a
  21. Box.</p>
  22. <p>
  23. Hopefully The program will display a message once the sphere comes into contact
  24. with the box. Run the program first to fully understand what is happening before
  25. delving into the sourcecode below.</p>
  26. <table border="1" width="86%" bordercolor="#FFFFFF">
  27. <tr>
  28. <td width="14%">&nbsp;</td>
  29. <td width="72%" bordercolor="#000000" bgcolor="#C0C0C0">&nbsp;<br>
  30. <font face="arial">
  31. &nbsp; Graphics3D 800,600<br>
  32. <br>
  33. &nbsp; Const CUBE_COL=1<br>
  34. &nbsp; Const SPHERE_COL=2<br>
  35. <br>
  36. &nbsp; SetBuffer BackBuffer()<br>
  37. <br>
  38. &nbsp; camera=CreateCamera()<br>
  39. &nbsp; CameraViewport camera,0,0,800,600<br>
  40. &nbsp; PositionEntity camera,0,0,-5<br>
  41. <br>
  42. &nbsp; light=CreatLight()<br>
  43. <br>
  44. &nbsp; cube=CreateCube()<br>
  45. &nbsp; PositionEntity cube,-5,0,5<br>
  46. &nbsp; EntityColor cube,70,80,190<br>
  47. &nbsp; EntityType cube,CUBE_COL<br>
  48. <br>
  49. &nbsp; sphere=CreateSphere(12)<br>
  50. &nbsp; PositionEntity sphere,5,0,5<br>
  51. &nbsp; EntityColor sphere,170,80,90<br>
  52. &nbsp; EntityType sphere,SPHERE_COL<br>
  53. <br>
  54. &nbsp; Collisions SPHERE_COL,CUBE_COL,3,1<br>
  55. <br>
  56. &nbsp; While Not KeyHit(1)<br>
  57. <br>
  58. &nbsp; MoveEntity sphere,-0.02,0,0<br>
  59. <br>
  60. &nbsp; UpdateWorld<br>
  61. &nbsp; RenderWorld<br>
  62. <br>
  63. &nbsp; If EntityCollided(sphere,CUBE_COL) Then<br>
  64. &nbsp; Text 370,80,"Collided !!!"<br>
  65. &nbsp; EndIf<br>
  66. <br>
  67. &nbsp; Text 335,500,"Collision Detection"<br>
  68. &nbsp; Flip<br>
  69. <br>
  70. &nbsp; Wend<br>
  71. <br>
  72. &nbsp; End<br><br>
  73. </font>
  74. </font></td>
  75. <td width="14%">&nbsp;</td>
  76. </tr>
  77. </table>
  78. <p>
  79. Being able to check for collisions is perhaps the most major part of any game. After all, without collision detection
  80. what's to stop Mario falling through the floor, or the bullets from Max Payne's
  81. gun doing its damage. We need it - whether we want it or not !. </p>
  82. <p>
  83. If you look carefully through the above example you will&nbsp;notice quite a few
  84. new commands. Let's briefly run through the entire program before we look at
  85. these. </p>
  86. <p>
  87. Firstly we create 2 objects - a Cube and a Sphere.., then we setup the collision
  88. so that B3D will check these objects every time the UPDATEWORLD instruction is
  89. called. </p>
  90. <p>
  91. Slowly we move the Sphere towards the Cube, until they collide.. after that, we
  92. print up the collision message on the screen to signal that we have collided. </p>
  93. <p>
  94. So what do these new instructions do: </p>
  95. <table border="0" width="100%" bgcolor="#6097C9">
  96. <tr>
  97. <td width="100%">
  98. <b><font face="Arial">Const CUBE_COL=1<br>
  99. Const SPHERE_COL=2</font></b></td>
  100. </tr>
  101. </table>
  102. <font face="verdana">
  103. <p>
  104. Ok, its not an instruction, but just something I've strung together for this
  105. example. When programming always try and make things as easy as possible - It
  106. will certainly help when it comes to debugging. (working out any problems)</p>
  107. <p>
  108. The Collision instructions we have to setup rely on variables, but rather than
  109. just using numbers - I'll use the CONSTANT variables I've setup to represent
  110. them.</p>
  111. <p>
  112. So in the above two lines, I can use the variable names CUBE_COL every time I
  113. want to use a 1 - and SPHERE_COL instead of 2. Although of course I could just
  114. use the numbers with the instructions, as you'll see it will help us out.</p>
  115. </font>
  116. <table border="0" width="100%" bgcolor="#6097C9">
  117. <tr>
  118. <td width="100%">
  119. <b><font face="Arial">EntityType cube,CUBE_COL</font></b></td>
  120. </tr>
  121. </table>
  122. <font face="verdana">
  123. <p>
  124. After we've setup the basic entity object, we need to setup a collision variable
  125. for it. We do this by assigning a number to the entity, As you can see to do
  126. this we use the ENTITYTYPE command. Here I've set the entity cube to have a
  127. value of 1. (remember the CONST variable is set to 1)</p>
  128. <table border="0" width="100%" bgcolor="#6097C9">
  129. <tr>
  130. <td width="100%">
  131. <b><font face="Arial">EntityType </font>
  132. </font>
  133. <font face="Arial">sphere,SPHERE_COL</font></b></td>
  134. </tr>
  135. </table>
  136. <font face="verdana"><p>As before, we setup the sphere collision variable to
  137. have a value of 2.</p>
  138. <p><b>AN IMPORTANT NOTE TO REMEMBER !</b></p>
  139. <p>Every entity <b>DOES NOT</b> have to have a separate collision variable
  140. number. For example say we created a 3d maze game, that had 10 objects for the
  141. sides of the maze. We would want to check if we have collided with a wall, it wouldn't
  142. matter which wall.. just a wall. So every wall object(entity) would have the
  143. same collision variable.<br>
  144. I would use the code:<br>
  145. <br>
  146. Const WALL=1<br>
  147. EntityType wall1,WALL<br>
  148. EntityType wall2,WALL... etc</p>
  149. <p>Later on when we check for a collision we would just say, is there a
  150. collision with the wall ?. Nice and Easy isnt it ?</p>
  151. </font>
  152. <table border="0" width="100%" bgcolor="#6097C9">
  153. <tr>
  154. <td width="100%">
  155. <b><font face="Arial">Collisions SPHERE_COL,CUBE_COL,3,1</font></b></td>
  156. </tr>
  157. </table>
  158. <font face="verdana">
  159. <p>
  160. Now the fun begins, this is the main instruction that informs B3D which objects
  161. to check for collisions and what action it should take.</p>
  162. <p>
  163. The first part of the line &quot;<b>Collisions SPHERE_COL,CUBE_COL</b>,3,1&quot;,
  164. is saying that we want a check to take place between the collision markers 1 and
  165. 2. (Remember the Sphere is 1 and the Cube is 2)</p>
  166. <p>
  167. If we had more entities with the same collision marker value, then of course
  168. these too would be included.</p>
  169. <p>&quot;Collisions SPHERE_COL,CUBE_COL,<b>3</b>,1&quot; - The first value (3</font>),
  170. <font face="verdana">represents the type of collision that we want B3D to
  171. perform, in this case we are using mode '3' - which is a Sphere-to-Box
  172. collision.</p>
  173. <p>
  174. B3D has 3 different types of collisions we can perform, these are:</p>
  175. <table border="0" width="100%" bgcolor="#FFFF00">
  176. <tr>
  177. <td width="100%">
  178. <i>1 = Sphere-to-Sphere<br>
  179. 2 = Sphere-to-Polygon<br>
  180. 3 = Sphere-to-Box</i></td>
  181. </tr>
  182. </table>
  183. <p>Now we come to the last value, &quot;Collisions SPHERE_COL,CUBE_COL,3,<b>1</b>&quot;.
  184. This is the response value, it signals what B3D should do when a collision has
  185. taken place. I used the value 1 which is used for a dead stop. (when it collides
  186. with something, don't let it move any closer to it)</p>
  187. <p>
  188. As before there are 3 mode types we can use:</p>
  189. <table border="0" width="100%" bgcolor="#FFFF00">
  190. <tr>
  191. <td width="100%">
  192. <i>1 = Stop<br>
  193. 2 = Slide1 - Full sliding collision<br>
  194. 3 = Slide2 - Takes into consideration the angle of slopes</i></td>
  195. </tr>
  196. </table>
  197. <p>Even though in my program I am moving the sphere into the box with the
  198. MOVEENTITY command, when it collides (because I've used the STOP mode).. the
  199. entity will NOT move through it.</p>
  200. <p>We have one more command to look at, that's the collision check instruction
  201. itself &quot;EntityCollided&quot;</font><font face="arial">.</font></p>
  202. <table border="0" width="100%" bgcolor="#6097C9">
  203. <tr>
  204. <td width="100%">
  205. <font face="Arial">If
  206. <b> EntityCollided(sphere,CUBE_COL) </b> then</font><font face="arial"><br>
  207. Text 370,80,"Collided !!!"<br>
  208. EndIf
  209. </font></td>
  210. </tr>
  211. </table>
  212. <font face="verdana"><p>As you can can probably guess, this instruction (imbedded
  213. in an IF statement) - is checking the entity SPHERE for a collision with the
  214. collision marker 1 (the cube).</p>
  215. <p>If it has collided then print the message to signal a collision !.</p>
  216. <p>Ok, we have now run through the entire program - but did it make much sense
  217. to you ?, To begin with I couldn't grasp it at all !. The best way to understand the various collision
  218. instructions is to experiment yourself with the different mode settings.
  219. Eventually (if you haven't understood fully by now).. you will realize just how
  220. easy and powerful they can be.</p>
  221. <p align="center"><font color="#808080">Tutorial 8 - <a href="page8.html">Vertexes</a></font></p>
  222. </font>