Game Programming and Development Tools

Targeting formula – Curry

Curry
Member

Posts: 134
From: USA
Registered: 11-21-2002
Does anyone know where to find a good basic algorithm or snippet of code for targeting in a 2D game? I've got an alien firing at the player, with a simple bullet that will go in a straight line (not a missile that will home in and alter course) and I want the shot to anticipate where the player will be rather than firing at where the player already is.

Thanks,

Curry

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Calculate the direction/speed of movement and distance, then extrapolate . . .

------------------
There are only 10 types of people - those who understand binary, and those who don't.

Curry
Member

Posts: 134
From: USA
Registered: 11-21-2002
Yeah, it is the extrapolating that is the bad part!

A relative just gave me some help on it, doing some scary things with trig, and showed me a solution for my current game, which has only horizontal movement for the player.

He also knows the approach for doing it in a way that will work for any situation (any direction of movement), but I'll have to wait until he can help me again. However, I know this thing has been done a million times, and it would be nice to find a good efficient formula. I searched and browsed at some game sites but still didn't find anything.

I know this has been done over and over and I know there must be some game coding resource that have it. But I must have not looked in the right place or used the right search keywords, because I didn't come across it.

Curry

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
From my physics book:

V=ΔX/ΔT

ΔX=change in X
ΔT=change in T

X=position
T=time
V=velocity

Solve for ΔX
V*ΔT=ΔX

So, calculating the future position is simply multiplying the velocity by the time it takes for the projectile to reach the target, which can be calculated using the same equation, this time for the projectile:
T=ΔX/V

T=Time it takes for projectile to hit target
ΔX=Distance to target
V=Velocity of projectile

So, the final equation:

Xtf=Vt*(ΔXp/Vp)+Xt
NOTE: Added "Xt" = target's current position - Vt*(ΔXp/Vp) calculates change in position, not the actual predicted position!

Xtf=Target's predicted future position
Vt=Current velocity of target
ΔXp=Distance between gun (projectile) and target
ΔVp=Velocity the gun will shoot the projectile at
Xt=Current position of projectile

Do this for all three dimensions, and you have a 3D point to aim at .

NOTE: ΔX(whatever)=difference in X-coordinates, not "as-the-crow-flies" distance!

------------------
There are only 10 types of people - those who understand binary, and those who don't.

[This message has been edited by CobraA1 (edited June 30, 2003).]

Imsold4christ

Member

Posts: 305
From: Gresham, OR, US
Registered: 01-20-2001
W007. Yay for physics. Nice one Cobra.

†Caleb†

------------------
"True friendship is not characterized by the absence of conflict, but by the ability to resolve conflict."

Curry
Member

Posts: 134
From: USA
Registered: 11-21-2002
Wow, thanks Cobra1! That looks a lot cleaner than some of the approaches I've seen.

That really helps.

Curry

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
yes, I was impressed.
that means a lot coming from me, especially in the physics and math area.
It looks good.

------------------