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 . . . ------------------ |
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 X=position Solve for Δ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=Time it takes for projectile to hit target So, the final equation: Xtf=Vt*(ΔXp/Vp)+Xt Xtf=Target's predicted future position 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! ------------------ [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† ------------------ |
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. |