December 03, 2020, 08:30:05 PM

Author Topic: [bb] capsule collider using one pivot + several ellipsoids + collidables by RemiD [ 11 months ago ]  (Read 751 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : capsule collider using one pivot + several ellipsoids + collidables
Author : RemiD
Posted : 11 months ago

Description : for characters, for vehicles, using Blitz3d collision system

Code :
Code: BlitzBasic
  1. Graphics3D(800,600,32,2)
  2.  
  3. SeedRnd(MilliSecs())
  4.  
  5. Origine = CreateCube()
  6. ScaleMesh(Origine,0.01/2,0.01/2,0.01/2)
  7. EntityColor(Origine,255,000,000)
  8.  
  9. ;Input
  10. Const CUp% = 1
  11. Const CToDown% = 2
  12. Const CDown% = 3
  13. Const CToUp% = 4
  14.  
  15. Global MPX% = 0
  16. Global MPY% = 0
  17. Global MXDiff# = 0
  18. Global MYDiff# = 0
  19.  
  20. Global MZOld% = 0
  21. Global MZCur% = 0
  22. Global MWheel% = 0
  23.  
  24. Global Camera = CreateCamera()
  25. CameraRange(Camera,0.1,100)
  26.  
  27. Global TerrainRenderer
  28. Global TerrainCollidable
  29.  
  30. BuildTerrain()
  31.  
  32. BuildingsMaxCount% = 100
  33. Global BuildingsCount%
  34. ;Dim BuildingHeight#(BuildingsMaxCount)
  35. Dim BuildingRenderer(BuildingsMaxCount)
  36. Dim BuildingCollidable(BuildingsMaxCount)
  37.  
  38. BuildBuildings()
  39.  
  40. PlatformsMaxCount% = 100
  41. Global PlatformsCount%
  42. Dim PlatformRenderer(PlatformsMaxCount)
  43. Dim PlatformCollidable(PlatformsMaxCount)
  44.  
  45. BuildPlatforms()
  46.  
  47. CharactersMaxCount% = 10
  48. Global CharactersCount%
  49. Dim CharacterFeet(CharactersMaxCount)
  50. Dim CharacterEyes(CharactersMaxCount)
  51. Dim CharacterRenderer(CharactersMaxCount)
  52. Dim CharacterCollider(CharactersMaxCount,10)
  53. Dim DebugCharacterCollider(CharactersMaxCount,10)
  54. Dim CharacterState%(CharactersMaxCount)
  55. Dim CharacterTarget(CharactersMaxCount)
  56.  
  57. BuildCharacters()
  58.  
  59. Global PlayerId%
  60.  
  61. PlayerId = Rand(1,10)
  62. SetEntityAsFree(Camera)
  63. PositionRotateEntityLikeOtherEntity(Camera,CharacterEyes(PlayerId))
  64. MoveEntity(Camera,0,0,-3)
  65. SetEntityAsChildOfOtherEntity(Camera,CharacterEyes(PlayerId))
  66. RotateEntity(CharacterEyes(PlayerId),22.5,0,0)
  67.  
  68. ;PositionEntity(Camera,0,0,-3)
  69.  
  70. DLight = CreateLight(1)
  71. LightColor(DLight,200,200,200)
  72. PositionEntity(DLight,-1000,1000,-1000)
  73. RotateEntity(DLight,45,-45,0)
  74.  
  75. AmbientLight(050,050,050)
  76.  
  77. Global CollidedCollidable%
  78. Global CollidedName$
  79. Global CollidedKind$
  80. Global CollidedId%
  81.  
  82. Const GroupTerrainsR% = 1
  83. Const GroupBuildingsR% = 2
  84. Const GroupPlatformsR% = 3
  85. Const GroupCharactersER% = 100
  86.  
  87. Global UpdateCollidersTime%
  88. Global UpdateCollisionsTime%
  89.  
  90. Main()
  91.  
  92. EndGraphics()
  93.  
  94. End()
  95.  
  96. Function PositionRotateEntityLikeOtherEntity(Entity,OtherEntity)
  97.  PositionEntity(Entity,EntityX(OtherEntity,True),EntityY(OtherEntity,True),EntityZ(OtherEntity,True))
  98.  RotateEntity(Entity,EntityPitch(OtherEntity,True),EntityYaw(OtherEntity,True),EntityRoll(OtherEntity,True))
  99. End Function
  100.  
  101. Function PositionEntityLikeOtherEntity(Entity,OtherEntity)
  102.  PositionEntity(Entity,EntityX(OtherEntity,True),EntityY(OtherEntity,True),EntityZ(OtherEntity,True))
  103. End Function
  104.  
  105. Function RotateEntityLikeOtherEntity(Entity,OtherEntity)
  106.  RotateEntity(Entity,EntityPitch(OtherEntity,True),EntityYaw(OtherEntity,True),EntityRoll(OtherEntity,True))
  107. End Function
  108.  
  109. Function SetEntityAsChildOfOtherEntity(Entity,OtherEntity)
  110.  EntityParent(Entity,OtherEntity,True)
  111. End Function
  112.  
  113. Function SetEntityAsFree(Entity)
  114.  EntityParent(Entity,0)
  115. End Function
  116.  
  117. Function Distance2D#(PAX#,PAZ#,PBX#,PBZ#)
  118.  VX# = PBX - PAX
  119.  VZ# = PBZ - PAZ
  120.  Distance2D# = Sqr((VX*VX)+(VZ*VZ))
  121.  Return Distance2D
  122. End Function
  123.  
  124. Function Distance3D#(PAX#,PAY#,PAZ#,PBX#,PBY#,PBZ#)
  125.  VX# = PBX - PAX
  126.  VY# = PBY - PAY
  127.  VZ# = PBZ - PAZ
  128.  Distance3D# = Sqr((VX*VX)+(VY*VY)+(VZ*VZ))
  129.  Return Distance3D
  130. End Function
  131.  
  132. Function BuildTerrain()
  133.  
  134.  TerrainRenderer = CreateTerrain(128)
  135.  For VX% = 0 To 128-1
  136.   For VZ% = 0 To 128-1
  137.    HeightRGB% = Rand(030,033)
  138.    Height# = 1.0/255*HeightRGB
  139.    ModifyTerrain(TerrainRenderer,VX,VZ,Height,False)
  140.   Next
  141.  Next
  142.  ScaleEntity(TerrainRenderer,1,25.5,1)
  143.  TerrainDetail(TerrainRenderer,4000,True)
  144.  TerrainShading(TerrainRenderer,True)
  145.  PositionEntity(TerrainRenderer,0,0,0)
  146.  EntityColor(TerrainRenderer,000,100,000)
  147.  NameEntity(TerrainRenderer,"TER"+Str(1))
  148.  
  149. End Function
  150.  
  151. Function BuildBuildings()
  152.  
  153.  For i% = 1 To 100
  154.   BuildingsCount = BuildingsCount + 1
  155.   BId% = BuildingsCount
  156.   TWidth# = Rnd(1,5)
  157.   TDepth# = Rnd(1,5)
  158.   THeight# = Rnd(0.25,15)
  159.   BuildingRenderer(BId) = CreateMesh()
  160.   TPart = CreateCube()
  161.   ScaleMesh(TPart,TWidth/2,0.3/2,TDepth/2)
  162.   PositionMesh(TPart,0,-0.15,0)
  163.   AddMesh(TPart,BuildingRenderer(BId))
  164.   FreeEntity(TPart)
  165.   TPart = CreateCube()
  166.   ScaleMesh(TPart,TWidth/2,THeight/2,TDepth/2)
  167.   PositionMesh(TPart,0,THeight/2,0)
  168.   AddMesh(TPart,BuildingRenderer(BId))
  169.   FreeEntity(TPart)
  170.   EntityColor(BuildingRenderer(BId),100,100,100)
  171.   BuildingCollidable(BId) = CopyMesh(BuildingRenderer(BId))
  172.   EntityAlpha(BuildingCollidable(BId),0.25)
  173.   NameEntity(BuildingCollidable(BId),"BUI"+Str(BId))
  174.   .LineChooseBuildingPosition
  175.   TX# = Rnd(1+TWidth/2,100-1-TWidth/2)
  176.   TY# = 3.3
  177.   TZ# = Rnd(1+TDepth/2,100-1-TDepth/2)
  178.   PositionEntity(BuildingCollidable(BId),TX,TY,TZ)
  179.   For OBId% = 1 To BuildingsCount
  180.    If(OBId <> BId)
  181.     If(MeshesIntersect(BuildingCollidable(BId),BuildingCollidable(OBId))=True)
  182.      Goto LineChooseBuildingPosition
  183.     EndIf
  184.    EndIf
  185.   Next
  186.   PositionEntity(BuildingRenderer(BId),TX,TY,TZ)
  187.  Next
  188. End Function
  189.  
  190. Function BuildPlatforms()
  191.  
  192.  For i% = 1 To 100
  193.   PlatformsCount = PlatformsCount + 1
  194.   PId % = PlatformsCount
  195.   TWidth# = Rnd(0.5,3)
  196.   TDepth# = Rnd(0.5,3)
  197.   PlatformRenderer(PId) = CreateMesh()
  198.   TPart = CreateCube()
  199.   ScaleMesh(TPart,TWidth/2,0.1/2,TDepth/2)
  200.   PositionMesh(TPart,0,0.05,0)
  201.   AddMesh(TPart,PlatformRenderer(PId))
  202.   FreeEntity(TPart)
  203.   EntityColor(PlatformRenderer(PId),100,100,100)
  204.   PlatformCollidable(PId) = CopyMesh(PlatformRenderer(PId))
  205.   EntityAlpha(PlatformCollidable(PId),0.25)
  206.   NameEntity(PlatformCollidable(PId),"PLA"+Str(PId))
  207.   .LineChoosePlatformPosition
  208.   TX# = Rand(1+TWidth/2,100-1-TWidth/2)
  209.   TY# = Rnd(3.3,10)
  210.   TZ# = Rand(1+TDepth/2,100-1-TDepth/2)
  211.   PositionEntity(PlatformCollidable(PId),TX,TY,TZ)
  212.   For BId% = 1 To BuildingsCount
  213.    If(MeshesIntersect(PlatformCollidable(PId),BuildingCollidable(BId))=True)
  214.     Goto LineChoosePlatformPosition
  215.    EndIf
  216.   Next
  217.   For OPId% = 1 To PlatformsCount
  218.    If(OPId <> PId)
  219.     If(MeshesIntersect(PlatformCollidable(PId),PlatformCollidable(OPId))=True)
  220.      Goto LineChoosePlatformPosition
  221.     EndIf
  222.    EndIf
  223.   Next
  224.   PositionEntity(PlatformRenderer(PId),TX,TY,TZ)
  225.  Next
  226.  
  227. End Function
  228.  
  229. Function BuildCharacters()
  230.  
  231.  For i% = 1 To 10
  232.   CharactersCount = CharactersCount + 1
  233.   CId% = CharactersCount
  234.  
  235.   R% = Rand(050,250)
  236.   G% = Rand(050,250)
  237.   B% = Rand(050,250)
  238.  
  239.   ;Feet
  240.   CharacterFeet(CId) = CreatePivot()
  241.   PositionEntity(CharacterFeet(CId),0,0,0)
  242.  
  243.   ;Eyes
  244.   CharacterEyes(CId) = CreatePivot()
  245.   PositionRotateEntityLikeOtherEntity(CharacterEyes(CId),CharacterFeet(CId))
  246.   SetEntityAsChildOfOtherEntity(CharacterEyes(CId),CharacterFeet(CId))
  247.   MoveEntity(CharacterEyes(CId),0,1.6,0.15)
  248.   DebugCharacterEyes = CreateCube()
  249.   ScaleMesh(DebugCharacterEyes,0.14/2,0.04/2,0.08/2)
  250.   PositionMesh(DebugCharacterEyes,0,0,0.04)
  251.   PositionRotateEntityLikeOtherEntity(DebugCharacterEyes,CharacterEyes(CId))
  252.   SetEntityAsChildOfOtherEntity(DebugCharacterEyes,CharacterEyes(CId))
  253.   EntityColor(DebugCharacterEyes,R,G,B)
  254.  
  255.   ;Mesh
  256.   CharacterRenderer(CId) = CreateCube()
  257.   ScaleMesh(CharacterRenderer(CId),0.5/2,1.7/2,0.25/2)
  258.   PositionMesh(CharacterRenderer(CId),0,1.7/2,0)
  259.   PositionRotateEntityLikeOtherEntity(CharacterRenderer(CId),CharacterFeet(CId))
  260.   SetEntityAsChildOfOtherEntity(CharacterRenderer(CId),CharacterFeet(CId))
  261.   EntityColor(CharacterRenderer(CId),R,G,B)
  262.  
  263.   ;DebugColliderEs
  264.   For ii% = 1 To 10
  265.    DebugCharacterCollider(CId,ii%) = CreateSphere(8)
  266.    ScaleMesh(DebugCharacterCollider(CId,ii%),0.25,0.25,0.25)
  267.    EntityAlpha(DebugCharacterCollider(CId,ii%),0.5)
  268.   Next
  269.  
  270.   ;ColliderEs
  271.   For EId% = 1 To 6
  272.    VY# = 0+(EId*0.25)
  273.    CharacterCollider(CId,EId) = CreatePivot()  
  274.    PositionRotateEntityLikeOtherEntity(CharacterCollider(CId,EId),CharacterFeet(CId))
  275.    MoveEntity(CharacterCollider(CId,EId),0,VY,0)
  276.    SetEntityAsChildOfOtherEntity(CharacterCollider(CId,EId),CharacterFeet(CId))
  277.    PositionRotateEntityLikeOtherEntity(DebugCharacterCollider(CId,EId),CharacterFeet(CId))
  278.    MoveEntity(DebugCharacterCollider(CId,EId),0,VY,0)
  279.    SetEntityAsChildOfOtherEntity(DebugCharacterCollider(CId,EId),CharacterFeet(CId))
  280.    NameEntity(CharacterCollider(CId,EId),"CHA"+Str(CId))  
  281.   Next
  282.  
  283.   ;Target
  284.   CharacterTarget(CId) = CreatePivot()
  285.   DebugCharacterTarget = CreateCylinder(8)
  286.   ScaleMesh(DebugCharacterTarget,0.5/2,0.001/2,0.5/2)
  287.   PositionRotateEntityLikeOtherEntity(DebugCharacterTarget,CharacterTarget(CId))
  288.   SetEntityAsChildOfOtherEntity(DebugCharacterTarget,CharacterTarget(CId))
  289.   EntityColor(DebugCharacterTarget,R,G,B)
  290.  
  291.   PositionEntity(CharacterFeet(CId),32.0/(12)*i,3.3+0.001,0.5)
  292.   PositionEntityLikeOtherEntity(CharacterTarget(CId),CharacterFeet(CId))
  293.  
  294.  Next
  295.  
  296. End Function
  297.  
  298. Function Main()
  299.  
  300.  While(KeyDown(1)<>1)
  301.  
  302.   GetInputvBasic()
  303.  
  304.   If(KeyHit(3)>0)
  305.    PlayerId = PlayerId + 1
  306.    If(PlayerId > 10)
  307.     PlayerId = 1
  308.    EndIf
  309.    SetEntityAsFree(Camera)
  310.    PositionRotateEntityLikeOtherEntity(Camera,CharacterEyes(PlayerId))
  311.    SetEntityAsChildOfOtherEntity(Camera,CharacterEyes(PlayerId))
  312.    MoveEntity(Camera,0,0,-3)
  313.    RotateEntity(CharacterEyes(PlayerId),22.5,0,0)
  314.   EndIf
  315.  
  316.   UpdateColliders()
  317.  
  318.   UpdatePlayerBC()
  319.  
  320.   UpdateBotsBC()
  321.  
  322.   UpdateCollisionsStart% = MilliSecs()
  323.   UpdateWorld(0)
  324.   UpdateCollisionsTime% = MilliSecs() - UpdateCollisionsStart
  325.  
  326.   UpdatePlayerAC()
  327.  
  328.   UpdateBotsAC()
  329.  
  330.   If(KeyDown(2)=1)
  331.    WireFrame(True)
  332.   Else
  333.    WireFrame(False)
  334.   EndIf
  335.  
  336.   SetBuffer(BackBuffer())
  337.   RenderWorld()
  338.  
  339.   Text(0,0,"Triangles = "+TrisRendered())
  340.   Text(0,20,"FPS = "+Str(FPS))
  341.   Text(0,40,"CollidedCollidable = "+Str(CollidedCollidable))
  342.   Text(0,60,"CollidedName = "+CollidedName)
  343.   Text(0,80,"CollidedKind = "+CollidedKind)
  344.   Text(0,100,"CollidedId = "+Str(CollidedId))
  345.   Text(0,260,"UpdateCollidersTime = "+Str(UpdateCollidersTime))
  346.   Text(0,280,"UpdateCollisionsTime = "+Str(UpdateCollisionsTime))
  347.  
  348.   Flip(1)
  349.  
  350.  Wend
  351.  
  352. End Function
  353.  
  354. Function GetInputvBasic()
  355.  
  356.  ;MouseWheel
  357.  MWheel = 0
  358.  MZCur = MouseZ()
  359.  If(MZCur > MZOld)
  360.   MWheel = + 1
  361.   MZOld = MZCur
  362.   ;DebugLog("MWheel = "+MWheel)
  363.  ElseIf(MZCur < MZOld)
  364.   MWheel = - 1
  365.   MZOld = MZCur
  366.   ;DebugLog("MWheel = "+MWheel)
  367.  EndIf
  368.  
  369.  ;Mouse picker properties
  370.  MXDiff = MouseXSpeed()  
  371.  MYDiff = MouseYSpeed()
  372.  MPX = MouseX()
  373.  MPY = MouseY()
  374.  
  375. End Function
  376.  
  377. Function UpdateColliders()
  378.  
  379.  UpdateCollidersStart% = MilliSecs()
  380.  
  381.  ;Delete Colliders
  382.  For CId% = 1 To CharactersCount
  383.   For EId% = 1 To 6
  384.    SetEntityAsFree(CharacterCollider(CId,EId))
  385.    SetEntityAsFree(DebugCharacterCollider(CId,EId))
  386.    FreeEntity(CharacterCollider(CId,EId))
  387.    CharacterCollider(CId,EId) = 0
  388.   Next
  389.  Next
  390.  
  391.  ;Recreate Colliders
  392.  For CId% = 1 To CharactersCount
  393.   ;ColliderEs
  394.   For EId% = 1 To 6
  395.    VY# = 0+(EId*0.25)
  396.    CharacterCollider(CId,EId) = CreatePivot()  
  397.    PositionRotateEntityLikeOtherEntity(CharacterCollider(CId,EId),CharacterFeet(CId))
  398.    MoveEntity(CharacterCollider(CId,EId),0,VY,0)
  399.    SetEntityAsChildOfOtherEntity(CharacterCollider(CId,EId),CharacterFeet(CId))
  400.    PositionRotateEntityLikeOtherEntity(DebugCharacterCollider(CId,EId),CharacterFeet(CId))
  401.    MoveEntity(DebugCharacterCollider(CId,EId),0,VY,0)
  402.    SetEntityAsChildOfOtherEntity(DebugCharacterCollider(CId,EId),CharacterFeet(CId))
  403.    NameEntity(CharacterCollider(CId,EId),"CHA"+Str(CId))  
  404.   Next
  405.  Next
  406.  
  407.  ;give each collider and each collidable a collision group
  408.  EntityType(TerrainRenderer,GroupTerrainsR)
  409.  For BId% = 1 To BuildingsCount
  410.   EntityType(BuildingCollidable(BId),GroupBuildingsR)
  411.  Next
  412.  For PId% = 1 To PlatformsCount
  413.   EntityType(PlatformCollidable(PId),GroupPlatformsR)
  414.  Next
  415.  For CId% = 1 To CharactersCount
  416.   For EId% = 1 To 6
  417.    EntityRadius(CharacterCollider(CId,EId),0.25)
  418.    EntityType(CharacterCollider(CId,EId),GroupCharactersER+CId)
  419.    NameEntity(CharacterCollider(CId,EId),"CHA"+Str(CId))
  420.   Next
  421.  Next
  422.  
  423.  ClearCollisions()
  424.  ;define collision detection and response between different collision groups
  425.  For CId% = 1 To CharactersCount
  426.   Collisions(GroupCharactersER+CId,GroupTerrainsR,2,1)
  427.   Collisions(GroupCharactersER+CId,GroupBuildingsR,2,1)
  428.   Collisions(GroupCharactersER+CId,GroupPlatformsR,2,1)
  429.   For OCId% = 1 To CharactersCount
  430.    If(OCId <> CId)
  431.     Collisions(GroupCharactersER+CId,GroupCharactersER+OCId,1,1)
  432.    EndIf
  433.   Next
  434.  Next
  435.  
  436.  UpdateCollidersTime = MilliSecs() - UpdateCollidersStart
  437.  
  438. End Function
  439.  
  440. Function UpdatePlayerBC()
  441.  
  442.  Id% = PlayerId
  443.  If(KeyDown(30)=1)
  444.   TurnEntity(CharacterFeet(Id),0,1.5,0)
  445.  ElseIf(KeyDown(32)=1)
  446.   TurnEntity(CharacterFeet(Id),0,-1.5,0)
  447.  EndIf
  448.  If(KeyDown(17)=1)
  449.   MoveEntity(CharacterFeet(Id),0,0,0.1)
  450.  ElseIf(KeyDown(31)=1)
  451.   MoveEntity(CharacterFeet(Id),0,0,-0.1)
  452.  EndIf
  453.  If(KeyDown(16)=1)
  454.   MoveEntity(CharacterFeet(Id),0,-0.1,0)
  455.  ElseIf(KeyDown(18)=1)
  456.   MoveEntity(CharacterFeet(Id),0,0.1,0)
  457.  EndIf
  458.  If(KeyDown(44)=1)
  459.   MoveEntity(CharacterFeet(Id),-0.1,0,0)
  460.  ElseIf(KeyDown(45)=1)
  461.   MoveEntity(CharacterFeet(Id),0.1,0,0)
  462.  EndIf
  463.  
  464. End Function
  465.  
  466. Function UpdatePlayerAC()
  467.  
  468.  Id% = PlayerId
  469.  For EId% = 1 To 6
  470.   CC% = CountCollisions(CharacterCollider(Id,EId))
  471.   If(CC% > 0)
  472.    CollidedCollidable = CollisionEntity(CharacterCollider(Id,EId),1)
  473.    If(CollidedCollidable <> 0)
  474.     TName$ = EntityName(CollidedCollidable)
  475.     TKind$ = Left(TName,3)
  476.     TId% = Right(TName,Len(TName)-3)
  477.     VY# = 0+(EId*0.25)
  478.     TFormPoint(0,-VY,0,CharacterCollider(Id,EId),0)
  479.     PositionEntity(CharacterFeet(Id),TFormedX(),TFormedY(),TFormedZ())
  480.     Goto LineEndCheckCollisions
  481.    EndIf
  482.   EndIf
  483.  Next
  484.  .LineEndCheckCollisions
  485.  
  486. End Function
  487.  
  488. Function UpdateBotsBC()
  489.  
  490.  For Id% = 1 To CharactersCount
  491.   If(Id <> PlayerId)
  492.  
  493.   EndIf
  494.  Next
  495.  
  496. End Function
  497.  
  498. Function UpdateBotsAC()
  499.  
  500.  For Id% = 1 To CharactersCount
  501.   If(Id <> PlayerId)
  502.  
  503.   EndIf
  504.  Next
  505.  
  506. End Function


Comments :


RemiD(Posted 11 months ago)

 I will update this code in the future so that it has better controls, possible actions, but for now this may help some of you understand what i mean by "several ellipsoids childs of one pivot and turn move the pivot".


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal