Collision Detection for bullets
Blitz3D Forums/Blitz3D Beginners Area/Collision Detection for bullets
| ||
I'm making a (VERY) simple FPS, and I just figured out how to make the player shoot bullets. Now, I want to be able to shoot a bullet at an enemy, and have that enemy die. The only problem is, I don't know how to do collision detection between the bullet and enemy. Could someone help me? |
| ||
try something like this if entitycollided(enemy, bullets) then enemyhealth#=enemyhealth#-(5?) endif I use a different collision type for my shots to make everything simpler |
| ||
I tried that already; the bullet passes through the enemy, but has no effect on the enemy. |
| ||
I use NGC for Blitz, saves a lot of programming, and works well. |
| ||
You might want to just line pick and see what the bullet hits and just not use the bullet for collision detection its what I usually do. |
| ||
I have no idea how to do entity picking. I tried the blitz help file things, but I still don't get it. Could you post some code or something? Thanks. |
| ||
Use LinePick, correct. Use the current position and the position from the frame before and use those for the linepick command. radius is the one of the bullet. you just need to make the objects that can collide pickable, thats it. |
| ||
So I would do this, right? ------------------------- If LinePick(bulletx , bullety , bulletz , lastbulletx , lastbullety, lastbulletz , bulletradius) = enemymodelhandle then delete enemy delete bullet End If |
| ||
i dont know how to use linepicks, but it sounds like the error in your current collision-detection system is that you bullet could be traveling too fast, and as a result, between frame 1 and frame 2, it goes right through the guy! |
| ||
Blitz collisions prevent penetration to the bullet can travel as fas as you like. For the linepick , you don't have to worry about last position. Assuming your bullet is pointing in the direction it's firing, you can use something like .. tformvector 0,0,BulletSpeed, Bullet, 0 If LinePick(bulletx , bullety , bulletz , tformedx , tformedy , tformedz , bulletradius) = enemymodelhandle delete enemy delete bullet else moveentity bullet, 0,0,bulletspeed endif |
| ||
Did you remember to put a collision command at the begining of the code? |
| ||
Remember, the second x,y,z co-ords in the linepick are NOT the end co-ords, they are distances from the start x,y,z co-ords. |
| ||
I tried that LinePick thing, and it still doesn't work. Is there anything else about linepicks I should know able. |
| ||
if you don't like linepicks you could do a camerapick and check the z value to see if fits in your bullet range. |
| ||
CameraPick cam,400,300 p_e=pickedentity() p_x=PickedX() p_y=PickedY() p_z=PickedZ() if p_z<100 and p_e=enemyhandle then freeentity enemy |
| ||
heres some an example thats completely done using camerapickGraphics3D 800,600,16,2 ;set up graphics ;create a box to shoot Local box = CreateCube() PositionEntity box,0,0,5 EntityPickMode box,2 ;set its pick geo ;create a camera Local cam = CreateCamera() HidePointer(); hide the mouse While Not KeyHit(1) If MouseHit(1) Then If CameraPick(cam,MouseX(),MouseY()) = box Then ;check if the user shot the box Text 400,300,"YOU SHOT THE BOX" Flip Delay 1000 Else Text 400,300,"YOU MISSED THE BOX" Flip Delay 1000 EndIf EndIf ;update and render stuff UpdateWorld() RenderWorld() ;draw a pointer Oval MouseX()-6,MouseY()-6,12,12,False Line MouseX(),MouseY()-7,MouseX(),MouseY()+7 Line MouseX()-7,MouseY(),MouseX()+7,MouseY() Flip Wend |