Page 1 of 2

How to check if a bullet will hit a target

Posted: Mon Jun 07, 2021 5:33 pm
by Gunroar:Cannon()
So I want to do something where the camera slows down as the player is about to get a kill, but then I don't know of a way to check if the bullet they shot is on path to hitting the target(another player). I could calculate if the hit is actually a kill but does anyone know how to check if a bullet will hit it's target, even if not immediately shot.

Re: How to check if a bullet will hit a target

Posted: Mon Jun 07, 2021 5:41 pm
by darkfrei
Make a trace (two traces?) for the bullet: if it's short then it was a collision.
https://love2d.org/forums/viewtopic.php?f=14&t=89815

Re: How to check if a bullet will hit a target

Posted: Mon Jun 07, 2021 10:52 pm
by togFox
Vectors? I use to hate vectors but now they are essential tools.

Determine if vectors intersect and if so then when (in seconds from 'now'). When bullet reaches (intersect - x seconds) then slow down.

Re: How to check if a bullet will hit a target

Posted: Mon Jun 07, 2021 11:14 pm
by knorke
is the bullet a "hitscan" weapon that instantly reaches the target? (like Railgun in Quake)
Or does the projectile actually fly through the air some time? (like Rocketlauncher in Quake)

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 12:33 am
by darkfrei
togFox wrote: Mon Jun 07, 2021 10:52 pm Vectors? I use to hate vectors but now they are essential tools.

Determine if vectors intersect and if so then when (in seconds from 'now'). When bullet reaches (intersect - x seconds) then slow down.
If the bullet has speed 10 pixels per dt and the ray length is lower than 10 pixels then you hit it after less than dt, it means "in this tick", or set a flag, that it must be hit in the next tick.
Maybe safe if the target was moved from this line.

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 5:48 am
by Gunroar:Cannon()
knorke wrote: Mon Jun 07, 2021 11:14 pm is the bullet a "hitscan" weapon that instantly reaches the target? (like Railgun in Quake)
Or does the projectile actually fly through the air some time? (like Rocketlauncher in Quake)
Projectile that flies ...
togFox wrote: Mon Jun 07, 2021 10:52 pm Vectors? I use(d) to hate vectors but now they are essential tools.

Determine if vectors intersect and if so then when (in seconds from 'now'). When bullet reaches (intersect - x seconds) then slow down.
darkfrei wrote: Tue Jun 08, 2021 12:33 am
If the bullet has speed 10 pixels per dt and the ray length is lower than 10 pixels then you hit it after less than dt, it means "in this tick", or set a flag, that it must be hit in the next tick.
Maybe safe if the target was moved from this line.
Thnx, vectors do seem like the answers :awesome: I'll give it a try!

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 7:07 am
by togFox
If you end up writing a function then I wouldn't mind getting a hold of that for my code library of useful functions.

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 7:29 am
by knorke
Maybe I miss something but how does the ray-tracing check if bullet & target not only cross paths but also collide "in time"?
Their paths may cross but they could still miss each other if they are not at the intersection point at the same time. It would require a second calcuation, comparing their time to the intersection.
/edit: Ah, that is would togFox meant.

Anyway, another solution might be to simply run the physics a few steps into the future.

Code: Select all

simBullet=bullet
simPlayer=player
for i = 1,500 do --run 500 physic steps into future to check for possible hits
simBullet.x=simBullet.x+simBullet.speedX
simBullet.x=simBullet.x+simBullet.speedY
simPlayer.x=simPlayer.x+simPlayer.speedX
simPlayer.y=simPlayer.y+simPlayer.speedY
  if collision (walls, simBullet) then
    --bullet will a hit a wall before reaching player, stop prediction
    break
  end
  if collision (simPlayer, simBullet) then
  ...bullet will hit the player...
  end
end
It is maybe a bit more CPU-intensive but would also work for more complex physics, like a player moving non-linear (falling down and accelerated by gravity) or bullets bouncing of walls.
One could add some checks, for example if the bullet is moving into the wrong direction then there is no need to run the simulation.

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 7:46 am
by togFox
If you want to be super crude about it, just measure the pixels between the bullet and target(s) and if close then slow down the camera. Sometimes it's easy to over-engineer the solution.

Re: How to check if a bullet will hit a target

Posted: Tue Jun 08, 2021 5:41 pm
by pgimeno
Are players in fixed positions? if not, the movements of the player may cause the prediction to fail.

You can however store some frames and replay them after the hit.