Ooops
November 25, 2020, 07:48:31 AM

Author Topic: [bb] Avoid_entity function by Nate the Great [ 1+ years ago ]  (Read 576 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Avoid_entity function
Author : Nate the Great
Posted : 1+ years ago

Description : This is another version of the yaw to entity code that allows for avoiding as well as attraction!

The cone goes to the white box and avoids the red box.  I haven't found any glitches in the avoid_entity function yet.  Please tell me if you do.


Code :
Code: BlitzBasic
  1. ; The number of logic updates per second.
  2. Const C_LOGIC_FREQUENCY = 50
  3.  
  4. ; The interval (in milliseconds) between logic updates.
  5. Const C_LOGIC_INTERVAL# = 1000.0 / C_LOGIC_FREQUENCY ; Unused.
  6.  
  7. Const C_SMART_MISSILE_TEST_DISTANCE# = 10.0
  8. Const C_SMART_MISSILE_TEST_ANGLE# = 60.0
  9. Const C_SMART_MISSILE_NO_TURN_DISTANCE# = 15.0
  10.  
  11. Global timer = CreateTimer( C_LOGIC_FREQUENCY )
  12.  
  13. Graphics3D 800, 600, 0, 2
  14. SetBuffer BackBuffer()
  15.  
  16. SeedRnd MilliSecs()
  17.  
  18. Global cam = CreateCamera()
  19. CameraZoom cam, 1.6
  20. MoveEntity cam, 0.0, 60.0, 0.0
  21. TurnEntity cam, 90.0, 0.0, 0.0
  22.  
  23. Global light = CreateLight()
  24.  
  25. Global cone = CreateCone()
  26. RotateMesh cone, 90.0, 0.0, 0.0
  27. UpdateNormals cone
  28. PositionEntity cone, 0.0, 0.0, -20.0
  29.  
  30. Global cube = CreateCube()
  31. Global bcube = CreateCube()
  32. EntityColor bcube,255,0,0
  33. UpdateNormals cube
  34. ;PositionEntity cube, 40.0, 0.0, 20.0
  35. PositionEntity cube, Rnd( -5.0, 5.0 ), 0.0, Rnd( -5.0, 5.0 )
  36. Repeat
  37.         PositionEntity bcube, Rnd( -15.0, 15.0 ), 0.0, Rnd( -15.0, 15.0 )
  38. Until(EntityDistance(cube,bcube) > 8)
  39.  
  40.  
  41. Global dist#
  42.  
  43. While Not KeyHit( 1 )
  44.        
  45.         dist# = EntityDistance( cone, cube )
  46.        
  47.         If allow_turn
  48.                 If dist# < C_SMART_MISSILE_TEST_DISTANCE#
  49.                         If Abs( DeltaYaw( cone, cube ) ) > C_SMART_MISSILE_TEST_ANGLE#
  50.                                 allow_turn = False
  51.                         EndIf
  52.                 EndIf
  53.         Else
  54.                 If dist# > C_SMART_MISSILE_NO_TURN_DISTANCE# Then allow_turn = True
  55.         EndIf
  56.        
  57.         If Not avoid_entity( cone, bcube, 3.0, 0.5 ) Then
  58.                 If allow_turn Then YawToEntity cone, cube, 3.0
  59.         EndIf
  60.        
  61.         MoveEntity cone, 0.0, 0.0, 0.5
  62.        
  63.         If dist# < 2.0
  64.                 PositionEntity cube, Rnd( -15.0, 15.0 ), 0.0, Rnd( -15.0, 15.0 )
  65.                 Repeat
  66.                         PositionEntity bcube, Rnd( -15.0, 15.0 ), 0.0, Rnd( -15.0, 15.0 )
  67.                 Until(EntityDistance(cube,bcube) > 8)
  68.         EndIf
  69.        
  70.         UpdateWorld
  71.         RenderWorld
  72.         Flip
  73.        
  74.         WaitTimer( timer )
  75.        
  76. Wend
  77.  
  78. End
  79.  
  80.  
  81. Function YawToEntity( src_entity, dest_entity, rate# )
  82. ; Turns 'src_entity' to point at 'dest_entity' at the rotation rate specified by 'rate#'.
  83.  
  84.         Local target_yaw# = DeltaYaw( src_entity, dest_entity )
  85.        
  86.         ; If the required correction amount is less than the correction amount to be applied...
  87.         If Abs( target_yaw# ) < rate#
  88.                 ; Point 'src_entity' directly at 'dest_entity' to prevent jittering.
  89.                 TurnEntity src_entity, 0.0, target_yaw#, 0.0
  90.         Else
  91.                 ; Turn 'src_entity' gradually towards 'dest_entity'.
  92.                 TurnEntity src_entity, 0.0, rate# * Sgn( target_yaw# ), 0.0
  93.         EndIf
  94. End Function
  95.  
  96.  
  97. Function avoid_entity( scr_entity, dest_entity, rate#, spd# )
  98.  
  99.         If EntityDistance(scr_entity,dest_entity) < 8 Then
  100.                 MoveEntity scr_entity,0,0,spd#
  101.                 dist1# = EntityDistance(scr_entity,dest_entity)
  102.                 MoveEntity scr_entity,0,0,-spd#
  103.                 TurnEntity scr_entity,0,rate#,0
  104.                 MoveEntity scr_entity,0,0,spd#
  105.                 dist2# = EntityDistance(scr_entity,dest_entity)
  106.                 MoveEntity scr_entity,0,0,-spd#
  107.                 TurnEntity scr_entity,0,-2*rate#,0
  108.                 MoveEntity scr_entity,0,0,spd#
  109.                 dist3# = EntityDistance(scr_entity,dest_entity)
  110.                 MoveEntity scr_entity,0,0,-spd#
  111.                
  112.                 If dist1# > dist2# And dist1# > dist3# Then TurnEntity scr_entity,0,rate#,0
  113.                 If dist2# > dist3# And dist2# > dist1# Then TurnEntity scr_entity,0,2*rate#,0
  114.                
  115.                 Return True
  116.         Else
  117.                 Return False
  118.         EndIf
  119.  
  120. End Function


Comments :


_33(Posted 1+ years ago)

 How do I implement this in the code?  What is the lib, and what is the example code?


Nate the Great(Posted 1+ years ago)

 >what is the lib?What do you mean?I just added the avoid_entity function to the YawtoEntity program submitted a month ago.  The avoid_entity function simply allows the missile to avoid things it doesn't want to hit.Just copy and paste this into B3d and it should work.


_33(Posted 1+ years ago)

 ok cool thanks


Mortiis(Posted 1+ years ago)

 I can actually use it for a space shooter project of mine. Thanks I appreciate it.


Knight #51(Posted 1+ years ago)

 wow. nice code


Nate the Great(Posted 1+ years ago)

 This is two months old.  I had forgotten about it but thanks anyway. :)P.S.  I am using a version of this in my verlet physics engine which I am turning into a game  :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal