Hey guys,
So I’ve set myself a little project to build a bowman game. Everythings working smashing until the math at the point where the computer needs to figure out the power and angle to shoot the arrow back at you.
What I’ve been working off is my knowledge from applied-math classes in school.
I’ve been using the following formula where g is gravity, t is time (this would be in frames for flash), sx is the distance between the players, u is the initial force of the arrow and a is the angle we need to find.
With a bit of moving around to seperate the angle, this becomes
For my AS3 this translates to:
//In my project, width basically refers to the distance between the players
var sx:Number = width;
//forceAverage is a variable that sets the average force for the arrow to be fired, this is enough force because I have measured what is needed for me to hit the computer player
var distTimeConst:Number = (2 * sx + g * t * t) / (2 * t * forceAverage);
//arrow going from right to left so take it from Pi/2
aimAngle = Math.PI / 2 - Math.acos(distTimeConst);
//trying to vary the force so the comp wont hit the target 100% of the time
aimForce = forceAverage + rand(forceVariance);
projectile.g = g;
projectile.x = w - w * (1 / 4);
projectile.y = h - groundHeight;
//the calcForce function basically limits the force
projectile.fire(calcForce(aimForce), aimAngle);
What happens is that it always fires the projectile at a very low angle that doesnt change to match the correct circumstances and I can’t figure out how to get around this. Changing the force doesnt make any difference and the program will need to get around changing distances between the player so this math is pretty necessary (I think). Any math heads out there know what I should change?
I realise this problem is very specific and you probably need a bit more info on stuff in the project, just ask if you do.
Thanks in advance