I currently have the math right to hit a moving target with 99% accuracy as long as I don't move.
When I start moving the aim is nearly 80%, it should also be 99% accuracy because the target is moving with you at a constant speed.
You can't have 100% accuracy even if you are not a moving player because the other target can dodge your bullet when they see it flying near them, the bullet is shot into the future knowing where the target will end up if they keep moving at the same constant X,Y speeds.
I don't seem to understand how to calculate the new shot angle when I am moving with a moving target together.
Each player has 5 variables
- X Coordinate in Pixels
- Y Coordinate in Pixels
- X Speed (can be negative) this is measured in pixels in 10 milliseconds
- Y Speed (can be negative) this is measured in pixels in 10 milliseconds
- Bullet Speed (-32768 to 32767) (negative bullet means how fast a shot is backwards) also measured using pixels, number of pixels traveled in 10 seconds.
The calculates are done using 10 milliseconds so all the X,Y's have to be scaled from 10 seconds to 10 milliseconds as well as Bullet Speed which uses the same 10 seconds pixels traveled system.
Weapon Speed of 1000 [Just a base speed for testing]
-----------------------------------------------------------
1000 pixels = 10 seconds 1000/10 = 100
900 pixels = 9 seconds 900/9 = 100
...
500 pixels = 5 seconds 500/5 = 100
100 pixels = 1 second. 100/1 = 100
50 pixels = 500 milliseconds. 50/500 = 0.1
25 pixels = 250 milliseconds. 25/250 = 0.1
12.5 pixels = 125 milliseconds. 12.5/125 = 0.1
6.25 pixels = 62.5 milliseconds. 6.25/62.5 = 0.1
3.125 pixels = 31.25 milliseconds. 3.125/31.25 = 0.1
1.5625 pixels = 15.625 milliseconds. 1.5625/15.625 = 0.1
0.78125 pixels = 7.8125 milliseconds. 0.78125/7.8125 = 0.1
1 pixel = 10 milliseconds. (seems correct). 1/10 = 0.1
-----------------------------------------------------------
Conclusion How much pixels to travel in time equation is
double PixelsTraveled = (WeaponSpeed / 10,000) * timeSpentInMilliSeconds;
The game does a update every 10 milliseconds to everything.
When the Bullet Speed is 1000
that means it travels 100
pixels per second. (1000/10)=100
Doubt I need to take into account that 10 milliseconds per game update, since when I don't move I never use 10 milliseconds and it works perfect.
EDIT: This thread (outside game loop) actually has a sleep time of 10 milliseconds which cancels out the effect even though it doesn't synchronize the time dot to dot.
Here is the nearly perfect code, 99% accuracy
For myPlayer attacking a moving target when you have X Speed=0, Y Speed=0
double xDelta = targetPlayer.XCoordinate - myPlayer.XCoordinate;
double yDelta = targetPlayer.YCoordinate - myPlayer.YCoordinate;
double targetDistance = sqrt((xDelta*xDelta)+(yDelta*yDelta));
// Prediction of shot.
double impactTime = targetDistance/targetPlayer.BulletSpeed;
xDelta += impactTime*targetPlayer.XSpeed;
yDelta += impactTime*targetPlayer.YSpeed;
// Gets new firing angle.
double radian = atan(yDelta/xDelta);
printf("new angle = %g\n", radian);
Here is the code I attempted to make it get a new firing angle when both targets are moving which I have to take into account both player's speeds and bullet speed.
Seems I have over complicated it with sqrt
and it doesn't work right.
double xDelta = targetPlayer.XCoordinate - myPlayer.XCoordinate;
double yDelta = targetPlayer.YCoordinate - myPlayer.YCoordinate;
double targetDistance = sqrt((xDelta*xDelta)+(yDelta*yDelta));
// Prediction of shot.
double predictBulletSpeedX = myPlayer.BulletSpeed + myPlayer.XSpeed;
double predictBulletSpeedY = myPlayer.BulletSpeed + myPlayer.YSpeed;
double finalBulletSpeed = sqrt((predictBulletSpeedX*predictBulletSpeedX)+(predictBulletSpeedY*predictBulletSpeedY));
double impactTime = (targetDistance/finalBulletSpeed);
xDelta += impactTime*targetPlayer.XSpeed;
yDelta += impactTime*targetPlayer.YSpeed;
// Gets new firing angle.
double radian = atan(yDelta/xDelta);
printf("new angle = %g\n", radian);