How to check if a bullet will hit a target

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

How to check if a bullet will hit a target

Post 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.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

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

Post 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)
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

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

Post 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!
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

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

Post 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.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests