Ooops
November 25, 2020, 05:56:12 AM

Author Topic: [bb] Dynamic Collision Lib by simonh [ 1+ years ago ]  (Read 701 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Dynamic Collision Lib by simonh [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Dynamic Collision Lib
Author : simonh
Posted : 1+ years ago

Description : Blitz's collision system is normally limited to using only dynamic sphere -> static poly collisions, however with this lib you can use dynamic sphere -> dymanic poly collisions, useful for lifts etc.

For the purposes of the code archives submission below the example and lib are combined, however you can download the files in their original from <a href="http://www.si-design.co.uk/stuff/dcol_lib.zip" target="_blank">here[/url].

You are free to use this source code how you like, however a credit in any programs that use it would be appreciated.

Limitations:
Only works with spheres as source entities
Only works if the source entity is colliding with one dynamic entity at once


Code :
Code: BlitzBasic
  1. ; Dynamic Collisions Example
  2. ; --------------------------
  3.  
  4. ;Include "dcol_lib.bb" ; must be used at start of program
  5.  
  6. Graphics3D 640,480
  7. SetBuffer BackBuffer()
  8.  
  9. cam=CreateCamera() : PositionEntity cam,0,12,-10 : RotateEntity cam,30,0,0
  10. light=CreateLight()
  11.  
  12. ; Create source entity
  13. sphere=CreateSphere() : EntityColor sphere,0,0,255 : PositionEntity sphere,0,5,0
  14.  
  15. ; Give sphere a collision type for collisions with non-dynamic objects, i.e. the ground in this case
  16. EntityType sphere,1 : EntityRadius sphere,1
  17.  
  18. ; Create ground - this won't be moving and so will not be part of the dynamic collision system.
  19. ; It will be part of the normal collision system however so apply collision type to it and enable collisions with sphere.
  20. plane=CreatePlane() : EntityColor plane,0,255,0
  21. EntityType plane,2
  22.  
  23. ; Set up normal sliding collisions between sphere and plane
  24. Collisions 1,2,2,2
  25.  
  26. ; NOW THE DYNAMIC STUFF - *** Important comments highlighted by stars ***
  27.  
  28. ; Create first destination entity - platform 1
  29. platform1=CreateCylinder(16) : ScaleEntity platform1,2,6,2 : EntityColor platform1,255,255,0
  30. PositionEntity platform1,-10,-8,8
  31.  
  32. ; Create second destination entity - platform 2
  33. platform2=CreateCube() : ScaleEntity platform2,4,4,4 : EntityColor platform2,255,0,0
  34. PositionEntity platform2,10,0,8
  35.  
  36. src_type=3 ; assign a collision type for source collision entity (sphere)
  37. des_type=4 ; assign a collision type for destination collision entity (platforms)
  38.  
  39. ; *** Set dynamic collision pairs ***
  40. DCO_SetPair(sphere,platform1,src_type,des_type,1.0) ; parameters - src_entity,des_entity,src_type,des_type,src_radius=1.0
  41. DCO_SetPair(sphere,platform2,src_type,des_type,1.0)
  42.  
  43. ; *** Enable collisions for dynamic pairs ***
  44. Collisions src_type,des_type,2,2
  45.  
  46. While Not KeyDown(1)
  47.  
  48.         ; Control sphere
  49.         sph_x#=0
  50.         sph_z#=0
  51.         If KeyDown(203)=True Then sph_x=-.4
  52.         If KeyDown(205)=True Then sph_x=.4
  53.         If KeyDown(208)=True Then sph_z=-.4
  54.         If KeyDown(200)=True Then sph_z=.4
  55.  
  56.         ; Apply gravity to sphere
  57.         MoveEntity sphere,0,-.5,0
  58.  
  59.         ; Make our platforms dynamic - move them!
  60.         MoveEntity platform1,0,Sin(angle#)*0.1,0 : angle#=angle#+1
  61.         TurnEntity platform2,0,1,0
  62.         TranslateEntity platform2,0,0,Sin(angle#)*0.1
  63.         MoveEntity sphere,sph_x,sph_y,sph_z
  64.  
  65.         ; *** Call DCO_Update functions, with UpdateWorld inbetween them ***
  66.         DCO_UpdateA()
  67.         UpdateWorld 0
  68.         DCO_UpdateB()
  69.        
  70.         ; *** Usual UpdateWorld call ***
  71.         UpdateWorld
  72.        
  73.         ; *** For best results, try experimenting with the order of the above four function calls ***
  74.        
  75.         RenderWorld
  76.        
  77.         ; Output dynamic collision information - the dynamic collision system works best if you use these values to implement
  78.         ; proper bouncing physics between source entity and destination entity
  79.         Text 0,0,"EntityCollided: "+DCO_EntityCollided()
  80.         Text 0,20,"CollisionNX: "+DCO_CollisionNX#()
  81.         Text 0,40,"CollisionNY: "+DCO_CollisionNY#()
  82.         Text 0,60,"CollisionNZ: "+DCO_CollisionNZ#()
  83.        
  84.         Flip
  85.  
  86. Wend
  87.  
  88. End
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. ; Dynamic collision lib by Simon Harrison (si@si-design.co.uk)
  100.  
  101. ; You are free to use this source code how you like, however a credit in any programs that use it would be appreciated.
  102.  
  103. ; Limitations:
  104. ; Only works with spheres as source entities
  105. ; Only works if the source entity is colliding with one dynamic entity at once
  106.  
  107. ; Functions:
  108. ; DCO_SetPair(src_ent,des_ent,src_type,des_type,src_rad=1.0)
  109. ; DCO_UpdateA()
  110. ; DCO_UpdateB()
  111. ; DCO_EntityCollided()
  112. ; DCO_CollisionNX()
  113. ; DCO_CollisionNY()
  114. ; DCO_CollisionNZ()
  115.  
  116. ; * See included example for demonstration of system.
  117.  
  118. Global DCO_pair_pos
  119. Global DCO_ent
  120. Global DCO_nx#
  121. Global DCO_ny#
  122. Global DCO_nz#
  123.  
  124. Type DCO_pair
  125.  
  126.         Field src_ent,src_col,src_type,src_rad
  127.         Field des_ent,des_col,des_type
  128.        
  129. End Type
  130.  
  131. Function DCO_SetPair(src_ent,des_ent,src_type,des_type,src_rad=1.0)
  132.  
  133.         c.DCO_pair=New DCO_pair
  134.        
  135.         csrc_ent=src_ent
  136.         csrc_type=src_type
  137.         csrc_rad=src_rad
  138.        
  139.         cdes_ent=des_ent
  140.         cdes_type=des_type
  141.        
  142.         csrc_col=CreatePivot() : EntityRadius csrc_col,src_rad
  143.         cdes_col=CopyEntity(des_ent)
  144.         EntityAlpha cdes_col,0
  145.        
  146.         DCO_pair_pos=DCO_pair_pos+(MeshWidth(cdes_col)*2)
  147.         DCO_pair_pos=DCO_pair_pos+1000
  148.         PositionEntity cdes_col,DCO_pair_pos,DCO_pair_pos,0
  149.        
  150.         EntityType csrc_col,src_type
  151.         EntityType cdes_col,des_type
  152.        
  153.         ResetEntity csrc_col
  154.         ResetEntity cdes_col
  155.        
  156. End Function
  157.  
  158. Function DCO_UpdateA()
  159.  
  160.         Local dx#,dy#,dz#,pivo,pivv
  161.  
  162.         For c.DCO_pair=Each DCO_pair
  163.  
  164.                 dx#=EntityX#(csrc_ent)-EntityX#(cdes_ent)
  165.                 dy#=EntityY#(csrc_ent)-EntityY#(cdes_ent)
  166.                 dz#=EntityZ#(csrc_ent)-EntityZ#(cdes_ent)
  167.  
  168.                 pivo=CreatePivot()
  169.                 pivv=CreatePivot(pivo)
  170.                
  171.                 PositionEntity pivo,EntityX#(cdes_col),EntityY#(cdes_col),EntityZ#(cdes_col)
  172.                
  173.                 RotateEntity pivo,0,0,0
  174.                
  175.                 PositionEntity pivv,dx#,dy#,dz#,False
  176.  
  177.                 RotateEntity pivo,-EntityPitch#(cdes_ent),-EntityYaw#(cdes_ent),-EntityRoll#(cdes_ent)
  178.  
  179.                 PositionEntity csrc_col,EntityX#(pivv,True),EntityY#(pivv,True),EntityZ#(pivv,True)
  180.                
  181.                 FreeEntity pivo
  182.                
  183.         Next
  184.  
  185. End Function
  186.  
  187. Function DCO_UpdateB()
  188.  
  189.         Local col,dx#,dy#,dz#,dx2#,dy2#,dz2#,ddx#=0,ddy#=0,ddz#=0,pivo,pivv,pivn
  190.        
  191.         DCO_ent=0
  192.         For c.DCO_pair=Each DCO_pair
  193.                                
  194.                 col=EntityCollided(csrc_col,cdes_type)
  195.                
  196.                 If col=cdes_col
  197.        
  198.                         DCO_ent=cdes_ent
  199.        
  200.                         dx#=EntityX#(csrc_ent)-EntityX#(cdes_ent)
  201.                         dy#=EntityY#(csrc_ent)-EntityY#(cdes_ent)
  202.                         dz#=EntityZ#(csrc_ent)-EntityZ#(cdes_ent)
  203.                
  204.                         dx2#=EntityX#(csrc_col)-EntityX#(cdes_col)
  205.                         dy2#=EntityY#(csrc_col)-EntityY#(cdes_col)
  206.                         dz2#=EntityZ#(csrc_col)-EntityZ#(cdes_col)
  207.        
  208.                         pivo=CreatePivot()
  209.                         pivv=CreatePivot(pivo)
  210.                         pivn=CreatePivot(pivo) ; piv used to translate collsion normals
  211.                        
  212.                         PositionEntity pivo,0,0,0
  213.        
  214.                         RotateEntity pivo,-EntityPitch#(cdes_ent),-EntityYaw#(cdes_ent),-EntityRoll#(cdes_ent)
  215.                        
  216.                         PositionEntity pivv,dx2#,dy2#,dz2#,True
  217.                         PositionEntity pivn,CollisionNX#(csrc_col,1),CollisionNY#(csrc_col,1),CollisionNZ#(csrc_col,1),True
  218.                        
  219.                         RotateEntity pivo,0,0,0
  220.                        
  221.                         ; platform normals
  222.                         DCO_nx#=EntityX#(pivn)
  223.                         DCO_ny#=EntityY#(pivn)
  224.                         DCO_nz#=EntityZ#(pivn)
  225.                        
  226.                         dx2#=EntityX#(pivv)
  227.                         dy2#=EntityY#(pivv)
  228.                         dz2#=EntityZ#(pivv)
  229.        
  230.                         ddx#=dx2#-dx#
  231.                         ddy#=dy2#-dy#
  232.                         ddz#=dz2#-dz#
  233.                                                        
  234.                         FreeEntity pivo
  235.                
  236.                         TranslateEntity csrc_ent,ddx#,ddy#,ddz#
  237.                
  238.                         ;Exit
  239.                
  240.                 EndIf
  241.                                
  242.         Next
  243.  
  244. End Function
  245.  
  246. Function DCO_EntityCollided()
  247.  
  248.         Return DCO_ent
  249.  
  250. End Function
  251.  
  252. Function DCO_CollisionNX#()
  253.  
  254.         Return DCO_nx#
  255.  
  256. End Function
  257.  
  258. Function DCO_CollisionNY#()
  259.  
  260.         Return DCO_ny#
  261.  
  262. End Function
  263.  
  264. Function DCO_CollisionNZ#()
  265.  
  266.         Return DCO_nz#
  267.  
  268. End Function


Comments :


daaan(Posted 1+ years ago)

 Very nice! Thank you for sharing this.


RifRaf(Posted 1+ years ago)

 nice ty


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal