Anyway for who need it more clear here there is the whole code.
This code need to be converted on Leadwers.
#include <iostream>
#include <cmath>
typedef double Real;
struct Vector3D {
Real x, y, z;
Vector3D(Real x, Real y, Real z) : x(x), y(y), z(z) {
}
};
std::ostream &operator<<(std::ostream &os, Vector3D const &v) {
return os << '(' << v.x << ',' << v.y << ',' << v.z << ')';
}
Vector3D operator+(Vector3D const &v1, Vector3D const &v2) {
return Vector3D(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
}
Vector3D operator-(Vector3D const &v1, Vector3D const &v2) {
return Vector3D(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
}
Vector3D operator*(Real s, Vector3D const &v) {
return Vector3D(s*v.x, s*v.y, s*v.z);
}
Real dot_product(Vector3D const &v1, Vector3D const &v2) {
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
struct Point3D {
Real x, y, z;
Point3D(Real x, Real y, Real z) : x(x), y(y), z(z) {
}
};
Vector3D operator-(Point3D const &p1, Point3D const &p2) {
return Vector3D(p1.x-p2.x, p1.y-p2.y, p1.z-p2.z);
}
Real largest_root_of_second_degree_equation(double a, double b, double c) {
return (-b+std::sqrt(b*b-4*a*c))/(2*a);
}
Vector3D shooting_direction(Point3D const &target_position,
Vector3D const &target_velocity,
Point3D const &shooter_position,
Vector3D const &shooter_velocity,
Real bullet_speed) {
Vector3D P = target_position - shooter_position;
Vector3D V = target_velocity - shooter_velocity;
Real a = bullet_speed*bullet_speed - dot_product(V,V);
Real b = -2*dot_product(P,V);
Real c = -dot_product(P,P);
Real t = largest_root_of_second_degree_equation(a,b,c);
return target_position + t*V;
}
int main() {
Point3D target(0,0,4);
Vector3D target_velocity(3,0,0);
Point3D O(0,0,0);
Vector3D my_velocity(0,0,0);
Real bullet_speed = 5;
std::cout << shooting_direction(target,
target_velocity,
O,
my_velocity,
bullet_speed) << '\n';
}