[gameprogrammer] Re: How to lead off a bullet?

On Wednesday 10 January 2007 06:48, 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

I don't have any code to show, but I do have a few ideas. :-)

I see a bunch of equations here, or rather, a few functions of time;

        * The extrapolated position of the player:
                x = px(t)
                y = py(t)

        * The radius of the expanding circle describing
          the possible positions of the bullet:
                r = br(t)

Unless I still haven't had enough caffeine, you should be able to 
solve it by first finding the t where
        br(t) == sqrt(px(t)**2 + py(t)**2)

That is, the time at which the "expanding circle" hits the estimated 
player position.

Don't know if it makes any difference, but obviously, the expression
        sqrt(px(t)**2 + py(t)**2)
describes the estimated distance from the enemy to the player, as a 
function of time. You can probably simplify things a bit by rotating 
based on the initial enemy-player angle, so that px(t) or py(t) 
evaluates to a constant value. I don't think it gets much simpler 
than that, except in the special cases where the player move directly 
towards or away from the enemy, as it gets non-linear and potentially 
non monotonous otherwise. (This becomes obvious if you consider that 
the player may come from a distance for a close fly-by and then fly 
off into the distance again, by simply going on a straight line.)

Finally, you extrapolate the players position at that time (using the 
first two functions), and calculate the angle from the enemy's 
current position to that calculated position.


BTW, a simplified approximation might actually make it more realistic, 
if that's a desirable feature. ;-)


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: