GameDev.net Posting Guidelines (please read before posting)
Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 30 June 2006 - 06:26 AM
Posted 30 June 2006 - 07:22 AM
Posted 30 June 2006 - 07:25 AM
Posted 30 June 2006 - 07:35 AM
Posted 30 June 2006 - 07:53 AM
#include <iostream> #include <cmath> struct Vector { double x,y; Vector(double x, double y):x(x),y(y){ } double dot(Vector const &v) const{ return x*v.x+y*v.y; } }; Vector operator*(double s, Vector const &v){ return Vector(s*v.x,s*v.y); } struct Point { double x,y; Point(double x, double y):x(x),y(y){ } Vector operator-(Point const &p) const{ return Vector(x-p.x,y-p.y); } Point operator+(Vector const &v) const{ return Point(x+v.x,y+v.y); } }; double largest_root_of_quadratic_equation(double A, double B, double C){ return (B+std::sqrt(B*B-4*A*C))/(2*A); } Point intercept(Point const &shooter, double bullet_speed, Point const &target, Vector const &target_velocity){ double a = bullet_speed*bullet_speed - target_velocity.dot(target_velocity); double b = -2*target_velocity.dot(target-shooter); double c = -(target-shooter).dot(target-shooter); return target+largest_root_of_quadratic_equation(a,b,c)*target_velocity; } Vector shoot_at(Point const &shooter, Point const &interception, double bullet_speed){ Vector v = interception-shooter; return bullet_speed/std::sqrt(v.dot(v))*v; } int main(){ Point shooter(0,0); Point target(0,10); Vector target_velocity(3,0); double bullet_speed = 5.0; Point P = intercept(shooter,bullet_speed,target,target_velocity); Vector V = shoot_at(shooter,P,bullet_speed); std::cout << "Shoot in direction (" << V.x << ',' << V.y << ") and you'll hit the target at (" << P.x << ',' << P.y << ").\n"; }
Posted 30 June 2006 - 11:03 AM
Posted 01 July 2006 - 01:29 AM
Posted 01 July 2006 - 06:56 AM
Posted 01 July 2006 - 07:42 AM
Quote:
Original post by Anonymous Poster
alvaro,
what is your reason to have both, point and vector, instead of a single class?
Posted 01 July 2006 - 09:53 AM
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;
class Vector {
double x, y, z;
public:
Vector();
Vector(double x, double y, double z);
friend ostream &operator<<(ostream &stream, const Vector &v);
friend Vector operator+(const Vector &v1, const Vector &v2);
friend Vector operator-(const Vector &v1, const Vector &v2);
friend Vector operator*(const double &m, const Vector &v);
friend Vector operator*(const Vector &v, const double &m);
friend double operator%(const Vector &v1, const Vector &v2);
};
Vector::Vector()
{
}
Vector::Vector(double x, double y, double z): x(x), y(y), z(z)
{
}
ostream &operator<<(ostream &stream, const Vector &v)
{
stream << '(' << v.x << ',' << v.y << ',' << v.z << ')';
return stream;
}
Vector operator+(const Vector &v1, const Vector &v2)
{
return Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
Vector operator-(const Vector &v1, const Vector &v2)
{
return Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
Vector operator*(const double &m, const Vector &v)
{
return Vector(m * v.x, m * v.y, m * v.z);
}
Vector operator*(const Vector &v, const double &m)
{
return Vector(m * v.x, m * v.y, m * v.z);
}
double operator%(const Vector &v1, const Vector &v2)
{
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}
Vector intercept(const Vector &p_shooter, double bullet_speed, const Vector &p_target, const Vector &v_target)
{
Vector BA = p_target - p_shooter;
double vxv = v_target % v_target;
double b = BA % v_target;
double a = bullet_speed * bullet_speed - vxv;
return p_target + (b + sqrt(b*b + a * (BA%BA))) / a * v_target;
}
Vector shoot_at(const Vector &p_shooter, const Vector &p_interception, double bullet_speed)
{
Vector v = p_interception-p_shooter;
return bullet_speed/sqrt(v%v) * v;
}
int main()
{
unsigned int c, n;
unsigned int EVENT_COUNT;
bool q;
// ask the user
cout << "Enter number of shots to calculate: " << endl;
cin >> EVENT_COUNT;
// allocate the memory
cout << "allocating memory" << endl;
Vector *p_shooter = new(nothrow) Vector[EVENT_COUNT];
Vector *p_target = new(nothrow) Vector[EVENT_COUNT];
double *bullet_speed = new(nothrow) double[EVENT_COUNT];
Vector *v_target = new(nothrow) Vector[EVENT_COUNT];
Vector *p_hit = new(nothrow) Vector[EVENT_COUNT];
Vector *v_shoot = new(nothrow) Vector[EVENT_COUNT];
if(!p_shooter || !p_target || !bullet_speed || !v_target || !p_hit || !v_shoot)
{
cout << "failed!" << endl;
return 1;
}
else cout << "completed" << endl;
// randomize timer
cout << "randomize_timer: 1 second" << endl;
c = clock() + 1000;
while(clock() < c) rand();
cout << "randomize_timer completed\n" << endl;
// simulate EVENT_COUNT shots
cout << "calculating " << EVENT_COUNT << " events" << endl;
c = clock();
for(n = 0; n < EVENT_COUNT; n++)
{
*p_shooter = Vector((rand() % 101) - 50, (rand() % 101) - 50, (rand() % 101) - 50);
*p_target = Vector((rand() % 101) - 50, (rand() % 101) - 50, (rand() % 101) - 50);
*bullet_speed = (rand() % 5) + 10;
*v_target = Vector((rand() % 7) - 3, (rand() % 7) - 3, (rand() % 7) - 3);
*p_hit = intercept(*p_shooter, *bullet_speed, *p_target, *v_target);
*v_shoot = shoot_at(*p_shooter, *p_hit, *bullet_speed);
p_shooter++; p_target++; bullet_speed++; v_target++; p_hit++; v_shoot++;
}
cout << "calculation finished in " << clock() - c << " milliseconds" << endl;
// ask the user
cout << "do u want to see the results? (1/0)" << endl;
cin >> q;
if(!q) return 0;
// display 4xEVENT_COUNT lines of bulk data
cout << "displaying calculations:" << endl;
p_shooter -= EVENT_COUNT; p_target -= EVENT_COUNT; bullet_speed -= EVENT_COUNT; v_target -= EVENT_COUNT; p_hit -= EVENT_COUNT; v_shoot -= EVENT_COUNT;
for(n = 0; n < EVENT_COUNT; n++)
{
cout << clock();
cout << "shooter loc: " << *p_shooter << ", target loc: " << *p_target << '\n';
cout << "bullet's speed: " << *bullet_speed << ", target dir " << *v_target << '\n';
cout << "shoot dir: " << *v_shoot << ", intercept loc: " << *p_hit << '\n' << endl;
p_shooter++; p_target++; bullet_speed++; v_target++; p_hit++; v_shoot++;
}
return 0;
}
Posted 01 July 2006 - 04:40 PM
Quote:
Original post by Anonymous Poster
alvaro,
what is your reason to have both, point and vector, instead of a single class?
Posted 01 July 2006 - 05:05 PM
Quote:
Original post by deffer(*) the only exception was when I was tweaking projection-matrix, and un-projecting screen-positions - affine w wasn't equal to 0 or 1.
Posted 11 June 2011 - 03:57 AM
Posted 12 June 2011 - 08:00 AM
At some point you are solving a quadratic equation and using sqrt(). If you would be taking the square root of a negative number, no interception is possible. If the solutions of the quadratic equation are both negative, there is no interception.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.