Ooops
January 16, 2021, 09:58:47 PM

Author Topic: How to delete a 3d bullet when it gets a certain range?  (Read 2999 times)

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
How to delete a 3d bullet when it gets a certain range?
« on: October 07, 2018, 12:10:08 AM »
Is there some other command to remove/delete the bullet in the below code when it gets to a certain range so I don't end up inundated with
bullets off in the distance?.  :-[

Code: BlitzBasic
  1. Graphics3D 1024,768,32,0
  2. SeedRnd MilliSecs() camera = CreateCamera() light = CreateLight () HidePointer
  3. Type bullet
  4. Field shot
  5. End Type
  6. Type enemy
  7. Field ecube
  8. End Type
  9.  
  10. player=CreateCube() ScaleEntity player,1,3,1 PositionEntity player,512,1,2
  11. PositionEntity camera,512,5,-10 EntityParent camera,player
  12.  
  13. ground=CreatePlane() PositionEntity ground,0,0,0 ScaleEntity ground,2000,1,2000 EntityColor ground,120,200,120
  14. site=CreateCube() ScaleEntity site,.2,.2,.2 PositionEntity site,512,1,10 EntityColor site,255,0,0 EntityParent site,player
  15. enemy=CreateCube() ScaleEntity enemy,1,3,1 PositionEntity enemy,500,1,30 EntityColor enemy,255,0,0
  16.  
  17. ;--------------------------------- ;Main Game Loop ;------
  18.  
  19. While Not KeyHit (1)
  20.  
  21. If KeyDown(17) MoveEntity player,0,0,+.5
  22. If KeyDown(31) MoveEntity player,0,0,-.5
  23. If KeyDown(32) TurnEntity player,0,-1,0
  24. If KeyDown(30) TurnEntity player,0,1,0
  25.  
  26. ;shoot
  27. If MouseHit(1)
  28. b.bullet=New bullet
  29. b\shot=CreateSphere()
  30. PositionEntity b\shot,EntityX(player),EntityY(player),EntityZ(player)
  31. PointEntity b\shot,site
  32. EndIf
  33.  
  34. ;move bullet
  35. For b.bullet=Each bullet
  36. MoveEntity b\shot,0,0,1.5
  37. ;If EntityDistance (player,b\shot)>50 FreeEntity b\shot                      <<<< WHY DOESN'T THIS WORK, or is there another way to do it???
  38. Next
  39.  
  40. ;enemy face player
  41. PointEntity (enemy,player)
  42. If EntityDistance (enemy,player)>5 MoveEntity enemy,0,0,.01
  43.  
  44. RenderWorld
  45. Text 10,10,"WASD to move Mouse 1 or SPACE to fire"
  46.  
  47. Flip
  48. Wend
  49. End
  50.  

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #1 on: October 07, 2018, 01:06:39 AM »
I thought of making a large invisible sphere around the player and destroying the cube on collision, but surely there is a better way.

I THINK FreeEntity is working, but then it crashes as it goes to move a bullet that is no longer existing..any ideas guys?. 

Offline Matty

  • Hero Member
  • *****
  • Posts: 1332
    • MattiesGames
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #2 on: October 07, 2018, 02:57:19 AM »
Store a lifetime value in the bullet type and set as non zero initially.  Deduct 1 each frame and when lifetime is zero remove the bullet.
I eat cheese in the trees when its eight degrees

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #3 on: October 07, 2018, 03:28:01 AM »
I added a lifetime value as below

Type bullet
Field shot
Field lifetime=40
End Type

and then added the countdown on the timer as below

;move bullet
For b.bullet=Each bullet
MoveEntity b\shot,0,0,1.5          <<< But when I run it I get  "Entity does not exist" for this line....
lifetime=lifetime-1
If lifetime=0 FreeEntity b\shot
Next
If lifetime=0 lifetime=40


Offline Matty

  • Hero Member
  • *****
  • Posts: 1332
    • MattiesGames
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #4 on: October 07, 2018, 03:43:58 AM »
After you freeentity you should delete the bullet....otherwise the handle will point to an entity that no longer exists.
I eat cheese in the trees when its eight degrees

Offline Matty

  • Hero Member
  • *****
  • Posts: 1332
    • MattiesGames
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #5 on: October 07, 2018, 03:44:39 AM »
Actually your code is quite wrong.  I dont think you understand what i suggested.
I eat cheese in the trees when its eight degrees

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #6 on: October 07, 2018, 03:52:29 AM »
Thank you for your help, I am not good at this, I have gone through the command reference and the only way I can see to delete an entity is the freeentity command and when I try to freeentity on the bullet I get an "illegal type conversion" error. I'm used to gamemaker where they have a destroy() command, I have looked for delete, destroy, kill (kidding)... no luck so far, I know I am close tho.

Offline Matty

  • Hero Member
  • *****
  • Posts: 1332
    • MattiesGames
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #7 on: October 07, 2018, 04:06:54 AM »
;shoot
;modifying your code im on mobile so not formatted right
If MouseHit(1)
b.bullet=New bullet
b\shot=CreateSphere()
b\lifetime = 40 ;set it here
PositionEntity b\shot,EntityX(player),EntityY(player),EntityZ(player)
PointEntity b\shot,site
EndIf

;move bullet
For b.bullet=Each bullet
b\lifetime = b\lifetime - 1
MoveEntity b\shot,0,0,1.5
;If EntityDistance (player,b\shot)>50 FreeEntity b\shot                      <<<< WHY DOESN'T THIS WORK, or is there another way to do it???
If b\lifetime < 1 then
 Freeentity b\shot
 Delete b
Endif
Next
I eat cheese in the trees when its eight degrees

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #8 on: October 07, 2018, 05:38:02 AM »
God, I was miles off. THANK YOU!. Now I can get on with it cheers!.

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2587
  • I mostly code at night... Mostly.
    • SyntaxBomb - Indie Coders
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #9 on: October 07, 2018, 05:53:56 AM »
Your original code works if you change :

Code: [Select]
If EntityDistance (player,b\shot)>50 FreeEntity b\shot

To

Code: [Select]
If EntityDistance (player,b\shot)>50 Then
FreeEntity b\shot
Delete b
EndIf

But overall I agree with Matty that initially a bullet should have a lifetime countdown. The EntityDistance part could be used as a basic collider with other objects.
Mac mini ( 2018 / 2020 ), 3 GHz 6-Core Intel Core i5, 16 GB 2667 MHz DDR4, 1TB NVMe, eGPU Radeon Pro 580 8 GB, LG Ultragear 27GL83A-B 27 Inch
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time...

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #10 on: October 07, 2018, 05:17:09 PM »
Thanks Qube, that alternative you gave me will also come in VERY handy for the collisions I am sure.
You guys have put me on the right track now, I'm having a blast  ;D

Offline Madjack

  • Jr. Member
  • **
  • Posts: 66
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #11 on: October 08, 2018, 05:41:47 AM »
Don't know if this applies here, but one gotcha I came up against in Blitz3d was that sometimes an entity would be involved in a collision and deleted in the same frame. This could be a problem as my code handling collisions would then crash as the collision entity had already been deleted.

If you have simple code, you could of course just handle collisions before any entities get deleted.
But in this instance I simply made the entity invisible and non-collidable, added a deletion flag to it and deleted it in the following cycle.

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #12 on: October 08, 2018, 08:27:55 PM »
Bahh... collisions...
The below code needs no assets to run, you can shoot kill the enemy (which re-spawns) and that works great. My question is..about 3/4 the way down on the code I have attempted to kill the bullets when they hit a tree..but they ~snake~ around the tree instead and won't die!!. I used the same code to kill the bullets which works good I also tried messing around with entitydistance as well, but no luck.

I just saw what MadJack wrote, and am thinking this is a possible alternative...but is there a better/correct way to accomplish this?.


Code: BlitzBasic
  1. Graphics3D 512,512,32,0
  2. SeedRnd MilliSecs()
  3. camera = CreateCamera()
  4. light = CreateLight ()
  5. HidePointer
  6.  
  7. ;TYPES
  8. Type bullet
  9. Field shot
  10. Field lifetime
  11. End Type
  12.  
  13. ;Collision types
  14. type_player=1
  15. type_enemy=2
  16. type_tree=3
  17. type_bullet=4
  18.  
  19. ;player
  20. player=CreateCube()
  21. ScaleEntity player,1,3,1
  22. PositionEntity player,512,1,2
  23. EntityRadius player,2
  24. EntityType player,type_player
  25. ;enemy
  26. ex=50+Rnd(100) ey=1 ez=50+Rnd(100)
  27. enemy=CreateCube()
  28. ScaleEntity enemy,1,3,1
  29. PositionEntity enemy,ex,1,ey
  30. EntityColor enemy,255,0,0
  31. EntityRadius enemy,2
  32. EntityType enemy,type_enemy
  33. ;camera
  34. PositionEntity camera,512,15,-20
  35. EntityParent camera,player
  36.  
  37. ;ground
  38. ground=CreatePlane()
  39. PositionEntity ground,0,0,0
  40. ScaleEntity ground,2000,1,2000
  41. EntityColor ground,120,200,120
  42. EntityType ground,type_ground
  43.  
  44. ;TREES
  45. For d=1 To 300
  46. tree=CreateCube()
  47. ScaleEntity tree,2,35,2
  48. EntityColor tree,140,100+Rnd(50),100
  49. leaves=CreateCone()
  50. ScaleEntity leaves,10+Rnd(15),25+Rnd(25),10+Rnd(15)
  51. EntityColor leaves,100,180+Rnd(55),120
  52. tx=Rnd(-2000,2000)
  53. tz=Rnd(-2000,2000)
  54. PositionEntity tree,tx,1,tz
  55. PositionEntity leaves,tx,50,tz
  56. EntityType tree,type_tree
  57. EntityRadius tree,.1
  58. Next
  59. ;----------------------- ;Main Game Loop ---------------
  60. While Not KeyHit (1)
  61.  
  62. ;CONTROLS
  63. If KeyDown(17) MoveEntity player,0,0,+.5
  64. If KeyDown(31) MoveEntity player,0,0,-.5
  65. If KeyDown(32) TurnEntity player,0,-1,0
  66. If KeyDown(30) TurnEntity player,0,1,0
  67. ;shoot
  68. If KeyDown(57) ;If MouseHit(1)
  69. b.bullet=New bullet
  70. b\shot=CreateSphere() EntityColor b\shot,255,0,0
  71. b\lifetime = 120 ;set range here
  72. PositionEntity b\shot,EntityX(player),EntityY(player),EntityZ(player)
  73. RotateEntity b\shot,EntityPitch(player),EntityYaw(player),EntityRoll(player)
  74. EntityType b\shot,type_bullet
  75. EntityRadius b\shot,.1
  76. EndIf
  77.  
  78. ;move bullet
  79. For b.bullet=Each bullet
  80. b\lifetime = b\lifetime - 1
  81. MoveEntity b\shot,0,0,2
  82. If EntityDistance (b\shot,enemy)<1 Then
  83. b\lifetime=0;reset enemy
  84. MoveEntity enemy,500,1,30      
  85. EndIf
  86. Next
  87.  
  88. ;kill bullet on tree collision                   <<<<<<<<<<<<<< Same code as "Kill Bullet"
  89. For b.bullet=Each bullet                      ;                         But the bullets won't delete!!.
  90. If EntityDistance(b\shot,tree)<1 Then  
  91. FreeEntity b\shot
  92. Delete b
  93. EndIf
  94.  
  95. ;Kill Bullet
  96. If b\lifetime < 1 Then
  97. FreeEntity b\shot
  98. Delete b
  99. EndIf
  100. Next
  101.  
  102. ;enemy face player
  103. PointEntity (enemy,player)
  104. If EntityDistance(enemy,player)>5 MoveEntity enemy,0,0,.5
  105.  
  106. ;COLLISIONS
  107. Collisions type_player, type_enemy,2,1
  108. Collisions type_player, type_tree,2,2
  109. Collisions type_bullet, type_enemy,2,2
  110. Collisions type_enemy,  type_tree,2,2
  111. Collisions type_bullet, type_tree,2,2
  112.  
  113. UpdateWorld
  114. RenderWorld
  115. Text 10,10,"WASD to move & SPACE to fire"
  116.  
  117. Flip
  118. Wend
  119. End
  120.  

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1128
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #13 on: October 13, 2018, 07:15:51 PM »
Quote
This could be a problem as my code handling collisions would then crash as the collision entity had already been deleted.
i know that you have finished a quite impressive game (Tank Universal), but this is bad practice to consider entities which have been "freed"...

One way to prevent such a crash is, after you have "freed" an entity, set its handle to 0
Then before you consider an entity to update it, check if its handle is different than 0
No more crash...
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1128
Re: How to delete a 3d bullet when it gets a certain range?
« Reply #14 on: October 13, 2018, 07:17:28 PM »
@phodiS>>your code contains several mistakes / bad ways of doing things... (with Blitz3d)
i will post a "better" version and explain why i do this and that...
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal