Hey I have the following problem:
I have two 3 dimensional vectors (locations).
Vector3 Person = {100, 0, 50};
Vector3 Ball = {200, 50, 100};
Now let's say I would shoot the ball with a gun. In this example, the pistol bullet moves at a constant velocity, with no bullet drop.
I have the velocity and acceleration of the ball, and the speed of the bullet:
Vector3 VelocityBall = {-10, -10, 0};
Vector3 AccelerationBall = {0, -10, 0};
float velocityBullet = 140; //M/s
Now, using the above information, I want to calculate how many seconds it will take for the pistol to hit the ball.
For this I found the following formula using magnitudes instead of vectors since time is not a vector but scalar.
Positon(ball) + Velocty(ball)^time + 1/2(Acceleration(ball) * time^2) = Position(bullet) + Velocity(bullet) * time //resolve t
Using code it would look like that:
Vector3 Personpos = Vector3(100, 0, 50);
Vector3 Ballpos = Vector3(200,50, 100);
Vector3 Bulletveloc = Vector3(140,0,0);
Vector3 Ballveloc = Vector3(-10,-10,0);
Vector3 Ballacceleration = Vector3(0,-10,0);
float pos1 = Personpos.Length();
float pos2 = Ballpos.Length();
float vel1 = Bulletveloc.Length();
float vel2 = Ballveloc.Length();
float acc1 = Ballacceleration.Length();
float calc1 = (2 * acc1 * pos1); //acc1 will be 10 instead of -10 which is wrong
float calc2 = (2 * acc1 * pos2); //acc1 will be 10 instead of -10 which is wrong
float calc3 = pow(vel1, 2);
float calc4 = (2 * vel1 * vel2);
float calc5 = pow(vel2, 2);
float calc6 = vel1;
float calc7 = vel2;
float calc8 = (acc1); //here acc1 can be positive which is correct
float time = (sqrt(calc1 - calc2 + calc3 - calc4 + calc5) + calc6 - calc7) / (acceleration2).Length();
this also works... but only as long as there are no negative numbers in the acceleration, otherwise the result is wrong.
The problem is that when I calculate the magnitude of the acceleration vector {-10, -10, 0}, a positive length comes out instead of a negative length which then falsifies the result. because except below the fraction line, the vector length must also be negative if the vector is negative.
Can I solve this somehow? Are there possibly simpler methods for this?