Projectile Leading Problem

Started by
14 comments, last by dawidjoubert 18 years, 4 months ago
B is only true if the targets velocity is lower than the projectile speed, otherwise they will never intersect. But in more cases than not, if the targets velocity is larger than the projectile speed, the projectile will never intercept the target.
Advertisement
typedef struct Vector{	float x;	float y;	float z;}Vector;void main(){	Vector Tpos;		//Target's starting Pos	Tpos.x = 5.0f;	Tpos.y = 0.0f;	Tpos.z = 5.0f;		Vector Spos;		//Shooter's Pos	Spos.x = 0.0f;	Spos.y = 0.0f;	Spos.z = 0.0f;	float Sspeed = 1.0f;	//shot speed	Vector Tvel;	Tvel.x = -1;	Tvel.y = 0;	Tvel.z = 0;	int t = 0;	float dist  = -999.0f ;	while(dist != Sspeed * t)	{		//update the targets Postion at time (t)		Tpos.x += Tvel.x;		Tpos.y += Tvel.y;		Tpos.z += Tvel.z;		dist = sqrt(((Tpos.x - Spos.x) * (Tpos.x - Spos.x)) + 			((Tpos.y - Spos.y) * (Tpos.y - Spos.y)) + 			((Tpos.z - Spos.z) * (Tpos.z - Spos.z)));				cout << "At Time "<< t << " the Distance is "<< dist << endl;		t++;	}	cout << "At Time " << t << " the bullet would be able to hit the target"<< endl;}


This is what I have come up with to solve for t. I think im on the right track, the next step would be to figure out the angle Spos needs to be facing to hit Tpos at its new location?

This is nothing but random musings and twisted ideas, but here it goes.

They showed an interesting experiment on TV involving a crossbow, a falling melon and the question where you would have to aim at to hit it in flight. Answer: at the melon itself. Both are subject to gravity and it happens to just fit very nicely. The faster the bolt the higher it flies and the sooner it hits.

Now, could that be transfered to the problem? Or would it fall apart where we replace acceleration (gravity) with a constant velocity?
f@dzhttp://festini.device-zero.de
Trienco the example you gave is perfectly correct, how ever consider someone throwing the melon.

You wouldnt have to worry about gravity because ass you say it is applied equally to both objects, how ever you would have to consider the velocity of the melon as soon as it leaves the persons hand, there after it has 0 Acceleration (assuming that you exempt gravity)

-Dawid Joubert

--BlackDragon--
Your iterative method works except it has limits.

First Limit it will only work for problems with whole numbers, instead try the following code.

Secondly your program will go into an infinite loop if the target is moving away from the projectile at a greater speed , thus the projectile will never be able to catch the target!
-
Try this instead it is way faster and will always return the answer if it exists
[Source]#include <windows.h>					// Include the much need windows.h#include <iostream>						// Include our c++ standard header#include <fstream>						// Include this to use ifstream#include <stdio.h>#include <stdarg.h>#include <math.h>#include <iostream>						// Include our c++ standard header for Coutusing namespace std;typedef struct Vector{float x;float y;float z;}Vector;float Distance(Vector v,Vector r){	Vector f;	f.x = v.x - r.x;	f.y = v.y - r.y;	f.z = v.z - r.z;	return sqrt(f.x * f.x + f.y * f.y + f.z * f.z);}float Magnitude(Vector v){	return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);}Vector Normalize(Vector v){	float m =  Magnitude(v);	v.x /=  m;	v.y /=  m;	v.z /=  m;	return v;}void main(){	srand(2);	/* Generate Random Target Position */	Vector Tpos;	Tpos.x = rand()- RAND_MAX/2;	Tpos.y = rand()- RAND_MAX/2;	Tpos.z = rand()- RAND_MAX/2;	// Spos Is always 0,0,0	// Set Projectile Speed to a Constant //	float Sspeed = 2.0f;//shot speed	Vector Tvel;	/* Generate Random Target Velocity with a Mangnitude/Speed of 1.0 */	Tvel.x = -float(rand())/float(RAND_MAX);	Tvel.y = -float(rand())/float(RAND_MAX);	Tvel.z = -float(rand())/float(RAND_MAX);	Tvel = Normalize(Tvel);	float t;	// Minimum Time is if both objects fly directly towards each other!	float MinTime = Magnitude(Tpos) / (Magnitude(Tvel) + Sspeed);	// Maximum Time is if the Target is fly exactly away from the projectile and	// then the projectile must catch it	float MaxTime = Magnitude(Tpos) / (Sspeed - Magnitude(Tvel));	t = (MinTime + MaxTime)/2.0f;	float dist;	float Accuracy = 1.0f;	float inc = t / 2.0f;	int its = 0;	do 	{		its++;		//update the targets Postion at time (t)		Vector newT;		newT.x = Tpos.x + Tvel.x * t;		newT.y = Tpos.y + Tvel.y * t;		newT.z = Tpos.z + Tvel.z * t;		dist = Magnitude(newT);		if (dist > Sspeed * t)		{			// Increase T			t = t + inc;			inc = inc / 2.0f;		}		else if (dist < Sspeed * t)		{			// Decrease T			t = t - inc;			inc = inc / 2.0f;		}		cout << "At Time "<< t << " the Distance is "<< dist << endl;	} while(abs(dist - Sspeed * t) > Accuracy);	cout << "At Time " << t << " the bullet would be able to hit the target"<< endl;	cout << "Iterations : " << its << endl;}[/Source]
[/source][/source][/source][/source][/source][/source]

For highter accuracy try changing all floats to doubles

Regards
----- EDIT --------
You might want to set a Max Iterations limit, at which it will exit, to find the needed time to get a solution simple uses this.

2^I = INT_MAX / Accuracy
Where I is the iterations, note that this is the longest time



[Edited by - dawidjoubert on November 28, 2005 9:59:03 AM]
----------------------------

http://djoubert.co.uk
The asin() method will give you the correct solution in all possible cases, and an easy way to detect impossible cases (if the value you're feeding to asin is greater than 1, it's impossible to hit the target).
So your saying this : asin(sin(90)*G/C) = F is correct?

Anyways, the estimate program is quite a good example of Binary Iterative searching :-)

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

Now Fingers the next thing for him to do is convert his 3d problem to a 2d problem which can be done because if take 3 coordinates Spos,Tpos,Tpos+Tvel*1 and use that to construct your plane then your all done :-)
----------------------------

http://djoubert.co.uk

This topic is closed to new replies.

Advertisement