Jump to content

Prediction Algorithm?


Joh
 Share

Recommended Posts

Hi,

i did a prediction algorithm but it's not good enough for me, do you know some good prediction algorithm?

 

In my project the projectile is slower then a gunshoot, so i need to predict where it will be the target and where i should aim to hit it.

What i did is predicting good but is not so fast as i expected so did you have any good one?

 

Thx.

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Never mind, found it.

 

distance_to_origin(P+t*V) = s*t

dot_product(P+t*V,P+t*V) = s^2*t^2

dot_product(P,P)+2*dot_product(P,V)*t+dot_product( V,V)*t^2 = s^2*t^2

dot_product(P,P)+2*dot_product(P,V)*t+(dot_product (V,V)-s^2)*t^2 = 0

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Why would you not just do a raycast for the distance of the projectile and then calculate a projection curve to hit that point... if you don't hit a target just us a pivot entity to move to the range distance from your weapon... check the raycast to that point and plot a path to there?

 

Alternatively, You could simply create a projectile object, apply mass then apply a force in the relevant direction... so long as the mass and force are appropriate your projectile should travel accordingly using physics and gravity..

If it's not Tactical realism then you are just playing..

Link to comment
Share on other sites

You misunderstood i should predict WHERE to hit.. It's for Ai agent, as projectile is not fast as a gun (you fire, and it will hit few millisecs later) i should predict where the target will be to be able to hit it.

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Well you know a target moves. You know it's speed and direction. You know the distance to the target as well. Based on that distance you calculate the time needed for a bullet to get there (in millisecs). Knowing that time you calculate where the target will be by that time. Then just fire there.

I create the game you play. I create the rulles you will obey.

Link to comment
Share on other sites

mmm did i wrote incorrectly? In second post i said i found.

Anyway in the way you suggest the projectile won't be precise.. Just immagine the target is moving far to you on right.. You'll never hit in that way.

Anyway i found the solution (second post) should i write solved on title? How?

Sorry i am pretty new on this forum.

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

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';
}

Intel Corei7-6700, NVIDIA GeForce GTX 980, 32GB DDR4, W-10.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...