Bullet Detection

Blitz3D Forums/Blitz3D Beginners Area/Bullet Detection

Happy Llama(Posted 2012) [#1]
I'm about ready to start making a top down shooter and I have a quick question. How would you check if a bullet hit a target? I know that you would check if the gun was facing the thing your shooting at but I don't know how you would achieve this effect. Any help would be greatly appreciated! :)


RemiD(Posted 2012) [#2]
You can set low tris colliders as childs of your high tris meshes that can be targets.

Then,
You can use LinePick() from the barrel of your gun to forward, and then check if a collider target has been hit.
Or
You can create a projectile with a collider sphere around it and move it from the barrel of your gun to forward, and then check if a there was a collision between the collider sphere and a collider target.

From my tests, with the same low tris colliders, with collisions it is 4 times faster. (on my current pc)

Last edited 2012


RemiD(Posted 2012) [#3]
Oh and if you use one of these techniques, you want to hide the colliders meshes with EntityAlpha(Mesh,0).


Kryzon(Posted 2012) [#4]
Could you specify if you want 2D or 3D top-down shooter?


_PJ_(Posted 2012) [#5]
-oops, RemiD said the same thing, I just didn't read it >.<

Last edited 2012


Happy Llama(Posted 2012) [#6]
3D top down preferably, but I guess 2D would work too. Which is easier?


Yue(Posted 2012) [#7]
2D commands to work "imageRectOverlap" and "ImageRectCollide"

Sample blitz3d

; Create new empty graphic to store our circle in
gfxCircle=CreateImage(50,50)

; Draw the circle image
; Set drawing operations to point to our new empty graphic
SetBuffer ImageBuffer(gfxCircle)
Color 255,0,0
; Note the extra space between the circle and the edge of the graphic
Oval 10,10,30,30,1

; Let's not forget to put the drawing buffer back!
SetBuffer BackBuffer()
Color 0,0,255

; Locate our box to a random, visible screen location
hotX=Rnd(50,610)
hotY=Rnd(50,430)
hotW=Rnd(20,100)
hotH=Rnd(20,100)

; Repeat the loop until we've had a collision
Repeat 
; Attach our mouse to the circle to move it
circleX=MouseX()
circleY=MouseY()
; Standard double buffer technique; clear screen first
Cls
; Draw our rectangle
Rect hotX,hotY,hotW,hotH,0
DrawImage gfxCircle,circleX,circleY
; Standard double buffer technique; flip after all drawing is done
Flip
; We test the locations of our rectangle area and circle to see if they have  pixel collided
Until ImageRectCollide (gfxCircle,circleX,circleY,0,hotX,hotY,hotW,hotH)

; Loop is over, we must've collided!
Text 0,0, "WE'VE HAD A COLLISION! PRESS A MOUSE BUTTON"
; Can't see the text until we flip ..
Flip
; Wait for a mouse click
WaitMouse()
; End our graphics mode
EndGraphics


Last edited 2012


Kryzon(Posted 2012) [#8]
3D top down preferably, but I guess 2D would work too. Which is easier?

You're gonna have difficulties\learning curve with either, so pick the one which suits your original game concept best.
It's your call on this one.


Happy Llama(Posted 2012) [#9]
Just another quick question. Do market FPS games actually have a "bullet" shoot out of the gun when fired to detect the collision (I assume not) or do they just check if a target is in front of the gun when it is shot?

Last edited 2012


Matty(Posted 2012) [#10]
That depends on the type of gun...I'd guess a rocket or grenade would most likely use a point object of some sort to represent its location since it can bounce around all over the place,,,I'd say a sniper rifle wouldn't...but there are lots of different ways of doing things...


RemiD(Posted 2012) [#11]
I guess that in a game where we can see the projectile moving, the bullet has a collider (for example some weapons in Unreal Tournament or Quake 3) whereas for weapons where we can't see the bullet (for example in rainbow six) the bullet hit or lack of hit is calculated with linepick.

What is interesting is that in some games (as in UT or Quake 3) you can guess where the bot is going and shoot the bullet not directly at him but when he continues to move, it will hit him. You can't do that with linepick, you need a collider.

I'd say it is more appropriate to use a collider when the bullet is slow enough that you can see it, and to use linepick when the bullet is too fast to be seen.

Last edited 2012


_PJ_(Posted 2012) [#12]
Just another quick question. Do market FPS games actually have a "bullet" shoot out of the gun when fired to detect the collision (I assume not) or do they just check if a target is in front of the gun when it is shot?



For ballistic weapons with extremely fast projectiles then the bullet speed is way too fast for reliably simulating with code, and bullet objects typically way too small. Most marketed games will use techniques such as LinePick or CameraPick to identify the actual target, then add a randomisation factor dependant on the distance to the actual location hit.

If slower moving projectiles or parabolic trajectories etc. are considered, then a 3D sprite or mesh can be made to follow approximate newtonian paths. For 3D games, these objects will move according to gravity and inertia as well as any self-propellant motive forces.

Generally, speaking I would consider the speed of motion, the duration of 'lifetime' and number of potential objects that may be needed and use this information toi decide whether it's too much CPU/GPU workload compared to what's actually gooing to be seen.

In 2D, the approach is different, but theory the same. The trajectory will need to be figured out depending on the facing of the 'gun'.
For fast projectiles, since there's no camerapick or linepicks in 2D, some form of iteration may be necessary to know what is actually hit. For slower projectiles, sprites (read 2D images), can be used, and as Yue says , the "ImageRectOverlap" and "ImageRectCollide" commands will help with this.


mv333(Posted 2012) [#13]
Yue, you need to edit your code, because the rectangle is off screen.