[gameprogrammer] Re: How to lead off a bullet?
- From: "Alan Wolfe" <alan.wolfe@xxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Wed, 10 Jan 2007 10:50:54 -0800
Wow, this list never ceases to amaze (:
Thank you David and Leighton, I'm going to play with both solutions
and see what I can figure out, maybe even some kind of hybrid which
does an aproximation like you were talking about David.
Thanks again (:
Alan
On 1/9/07, Leighton Haynes <dayta@xxxxxxxxxxxxxxxxx> wrote:
You're looking for the time 't' when the distance between the enemy
position and the player's predicted position equals the bullet distance
at time 't'.
Assume enemy position at time zero is ex,ey
Assume player position at time zero is px,py
Bullet speed equals bs
Player speed equals ps
Normalised player direction is dx,dy
Player position at time t, pos(t) = px + dx*t, py + dy*t
Vector from enemy position vec(t) = px + dx*t - ex, py+dy*t - ey
Distance from enemy position dis(t) = sqrt((px + dx * t -ex)^2 +
(py+dy*t-ey)^2)
Bullet distance bdis(t) = bs * t
equating dis(t) and bdis(t):
bs * t = sqrt((px + dx * t -ex)^2 +(py+dy*t-ey)^2)
You should simplify this equation by squaring both sides and multiplying
out the squared terms, then grouping them. This will simplify to a
quadratic equation of the form:
0 = a*t^2 + b*t + c
the solution to which is t = (-b +- sqrt(b^2 - 4ac))/2a
This gives two times, pick the most appropriate one.
Leighton...
If it's in 3d, do the
On Tue, Jan 09, 2007 at 09:48:17PM -0800, Alan Wolfe wrote:
> Hey guys,
>
> I'm trying to program something in a game of mine but am not having much
> luck.
>
> Basically what i'm trying to do is make an enemy be able to shoot a
> bullet that leads off on the player so that if the player doesnt
> change course it will hit them.
>
> I started working it out on paper but haven't gotten very far ):
>
> As far as i see it (i might be wrong) i know these things:
>
> *Player's current position
> *Player's direction
> *Player's speed
>
> *Bullet's current position (where the enemy is)
> *Bullet's speed (all bullets go the same speed)
>
> and i'm trying to find...
>
> *Bullet's direction
>
> Anyone have any idea how to do this? My solutions keep making the
> bullets go wierd ways heheh
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
--
#0421 113 305 - dayta@xxxxxxxxxxxxxxxxx
"Linux is legacy, but it will be a start." Ken Kutaragi - SCE
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: How to lead off a bullet?
- From: Sami Näätänen
- References:
- [gameprogrammer] How to lead off a bullet?
- From: Alan Wolfe
- [gameprogrammer] Re: How to lead off a bullet?
- From: Leighton Haynes
Other related posts:
- » [gameprogrammer] How to lead off a bullet?
- » [gameprogrammer] Re: How to lead off a bullet?
- » [gameprogrammer] Re: How to lead off a bullet?
- » [gameprogrammer] Re: How to lead off a bullet?
- » [gameprogrammer] Re: How to lead off a bullet?
You're looking for the time 't' when the distance between the enemy
position and the player's predicted position equals the bullet distance
at time 't'.
Assume enemy position at time zero is ex,ey
Assume player position at time zero is px,py
Bullet speed equals bs
Player speed equals ps
Normalised player direction is dx,dy
Player position at time t, pos(t) = px + dx*t, py + dy*t
Vector from enemy position vec(t) = px + dx*t - ex, py+dy*t - ey
Distance from enemy position dis(t) = sqrt((px + dx * t -ex)^2 +
(py+dy*t-ey)^2)
Bullet distance bdis(t) = bs * t
equating dis(t) and bdis(t):
bs * t = sqrt((px + dx * t -ex)^2 +(py+dy*t-ey)^2)
You should simplify this equation by squaring both sides and multiplying
out the squared terms, then grouping them. This will simplify to a
quadratic equation of the form:
0 = a*t^2 + b*t + c
the solution to which is t = (-b +- sqrt(b^2 - 4ac))/2a
This gives two times, pick the most appropriate one.
Leighton...
If it's in 3d, do the
On Tue, Jan 09, 2007 at 09:48:17PM -0800, Alan Wolfe wrote:
> Hey guys,
>
> I'm trying to program something in a game of mine but am not having much
> luck.
>
> Basically what i'm trying to do is make an enemy be able to shoot a
> bullet that leads off on the player so that if the player doesnt
> change course it will hit them.
>
> I started working it out on paper but haven't gotten very far ):
>
> As far as i see it (i might be wrong) i know these things:
>
> *Player's current position
> *Player's direction
> *Player's speed
>
> *Bullet's current position (where the enemy is)
> *Bullet's speed (all bullets go the same speed)
>
> and i'm trying to find...
>
> *Bullet's direction
>
> Anyone have any idea how to do this? My solutions keep making the
> bullets go wierd ways heheh
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
--
#0421 113 305 - dayta@xxxxxxxxxxxxxxxxx
"Linux is legacy, but it will be a start." Ken Kutaragi - SCE
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- [gameprogrammer] Re: How to lead off a bullet?
- From: Sami Näätänen
- [gameprogrammer] How to lead off a bullet?
- From: Alan Wolfe
- [gameprogrammer] Re: How to lead off a bullet?
- From: Leighton Haynes