December 03, 2020, 08:23:52 PM

Author Topic: [bb] Enemy chasing with collision by Jeppe Nielsen [ 1+ years ago ]  (Read 692 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Enemy chasing with collision
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Shows how enemies can chase a target, without sliding into same position. No blitz collisions used, only math.

Code :
Code: BlitzBasic
  1. Graphics3D 800,600,16,2
  2.  
  3. cam=CreateCamera()
  4. RotateEntity cam,90,0,0
  5. PositionEntity cam,0,50,0
  6.  
  7. ;create player entity
  8. player=CreateSphere()
  9. EntityFX player,1
  10.  
  11. ;create 10 randomly positioned creatures, with random velocity aswell
  12. For n=1 To 10
  13.         creaturenew(Rnd(-20,20),0,Rnd(-20,20),Rnd(0.1,0.5))
  14. Next
  15.  
  16. Repeat
  17.        
  18.         ;make the creatures home in on player
  19.         creatureupdate(EntityX(player),EntityY(player),EntityZ(player))
  20.        
  21.         ;control player with mouse
  22.         PositionEntity player,MouseX()/10-30,0,-MouseY()/10+30
  23.        
  24.         RenderWorld
  25.        
  26.         Text 400,0,"Control player with mouse",1,0
  27.         Text 400,20,"Creatures will not slide into each other",1,0
  28.        
  29.        
  30.        
  31.         Flip
  32.        
  33.        
  34. Until KeyDown(1)
  35. End
  36.  
  37. Type creature
  38.  
  39. Field e ;entity
  40.  
  41. Field vel# ;velocity
  42.  
  43. End Type
  44.  
  45. Function CreatureNew(x#,y#,z#,vel#)
  46.        
  47.         c.creature=New creature
  48.        
  49.         ce=CreateCube()
  50.        
  51.         cvel=vel
  52.        
  53.         PositionEntity ce,x,y,z
  54.        
  55. End Function
  56.  
  57. Function CreatureUpdate(x#,y#,z#,size#=2) ;size is 2 here as a created cube is 2 units wide
  58.        
  59.         For c.creature=Each creature
  60.        
  61.                 ;vector to x,y,z
  62.                
  63.                 dx#=(x-EntityX(ce))
  64.                 dy#=(y-EntityY(ce))
  65.                 dz#=(z-EntityZ(ce))
  66.                
  67.                 ;length of vector
  68.                
  69.                 l#=Sqr(dx*dx+dy*dy+dz*dz)
  70.                
  71.                 ;make the vector a unit vector, length = 1
  72.                
  73.                 dx=dx/l
  74.                 dy=dy/l
  75.                 dz=dz/l
  76.                
  77.                 TranslateEntity ce,dx*cvel,dy*cvel,dz*cvel
  78.                
  79.                 For cc.creature=Each creature
  80.                        
  81.                         ;do not test against it self
  82.                         If cc<>c
  83.                        
  84.                                 ;vector to another enemy
  85.                                 dx#=(EntityX(cce)-EntityX(ce))
  86.                                 dy#=(EntityY(cce)-EntityY(ce))
  87.                                 dz#=(EntityZ(cce)-EntityZ(ce))
  88.                                
  89.                                 ;length of vector
  90.                                
  91.                                 l#=Sqr(dx*dx+dy*dy+dz*dz)
  92.                                
  93.                                
  94.                                 ;enemies collide if they er within range:
  95.                                
  96.                                 If l<size
  97.                                        
  98.                                         ;make the vector a unit vector, length = 1
  99.                                        
  100.                                         dx=dx/l
  101.                                         dy=dy/l
  102.                                         dz=dz/l
  103.                                        
  104.                                         TranslateEntity ce,-dx*cvel,-dy*cvel,-dz*cvel
  105.                                        
  106.                                 EndIf
  107.                                        
  108.                         EndIf
  109.                        
  110.                 Next
  111.                
  112.         Next
  113.        
  114. End Function


Comments :


puki(Posted 1+ years ago)

 I like that "Jeppe" - reminds me of "Ross C's" one.I can imagine the little white sphere shouting "Chase me, Chase me.".


Rob Farley(Posted 1+ years ago)

 That's really cool, feels like gauntlet!


Zach3D(Posted 1+ years ago)

 You stole this someone, it was already posted a long time ago


Damien Sturdy(Posted 1+ years ago)

 <div class="quote"> You stole this someone, it was already posted a long time ago </div>Dipstick :P Look at the date of the original post! LOL :)


Grey Alien(Posted 1+ years ago)

 lol. "oops"


Leiden(Posted 1+ years ago)

 Haha Grey Alien ;)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal