How to check if a bullet will hit a target
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Gunroar:Cannon()
- Party member
- Posts: 1141
- Joined: Thu Dec 10, 2020 1:57 am
How to check if a bullet will hit a target
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
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
https://love2d.org/forums/viewtopic.php?f=14&t=89815
Re: How to check if a bullet will hit a target
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.
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:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Re: How to check if a bullet will hit a target
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)
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
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.
- Gunroar:Cannon()
- Party member
- Posts: 1141
- Joined: Thu Dec 10, 2020 1:57 am
Re: How to check if a bullet will hit a target
Projectile that flies ...
Thnx, vectors do seem like the answers I'll give it a try!
Re: How to check if a bullet will hit a target
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:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Re: How to check if a bullet will hit a target
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.
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.
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
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
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:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Re: How to check if a bullet will hit a target
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.
You can however store some frames and replay them after the hit.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 0 guests