GameDev.net Posting Guidelines (please read before posting)
For Beginners Forum FAQs (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 01 November 2011 - 03:15 PM
Posted 01 November 2011 - 04:26 PM
Posted 01 November 2011 - 04:49 PM
(Bullet.X - Gun.X) * (Bullet.X - Gun.X) + (Bullet.Y - Gun.Y) * (Bullet.Y - Gun.Y) = (t * BulletSpeed) * (t * BulletSpeed)
Target.X = TargetStart.X + TargetVel.X * t; Target.Y = TargetStart.Y + TargetVel.Y * t;
(Target.X - Gun.X) * (Target.X - Gun.X) + (Target.Y - Gun.Y) * (Target.Y - Gun.Y) = (Bullet.X - Gun.X) * (Bullet.X - Gun.X) + (Bullet.Y - Gun.Y) * (Bullet.Y - Gun.Y)
((TargetStart.X + TargetVel.X*t) - Gun.X) * ((TargetStart.X + TargetVel.X*t) - Gun.X) + ((TargetStart.Y +TargetVel.Y * t) - Gun.Y) * ((TargetStart.Y + TargetVel.Y * t) - Gun.Y) = (Bullet.X - Gun.X) * (Bullet.X - Gun.X) + (Bullet.Y - Gun.Y) * (Bullet.Y - Gun.Y)
((TargetStart.X + TargetVel.X*t) - Gun.X) * ((TargetStart.X + TargetVel.X*t) - Gun.X) + ((TargetStart.Y +TargetVel.Y * t) - Gun.Y) * ((TargetStart.Y + TargetVel.Y * t) - Gun.Y) = (t * BulletSpeed) * (t * BulletSpeed)
((TargetVel.X * TargetVel.X) + (TargetVel.Y * TargetVel.Y) - (BulletSpeed * BulletSpeed)) * t*t + 2*(TargetVel.X * (TargetStart.X - Gun.X) + TargetVel.Y * (TargetStart.Y - Gun.Y)) * t + ((TargetStart.X - Gun.X) * (TargetStart.X - Gun.X)) + ((TargetStart.Y - Gun.Y) * (TargetStart.Y - Gun.Y) = 0
a = ((TargetVel.X * TargetVel.X) + (TargetVel.Y * TargetVel.Y) - (BulletSpeed * BulletSpeed)) b = 2*(TargetVel.X * (TargetStart.X - Gun.X) + TargetVel.Y * (TargetStart.Y - Gun.Y)) c = ((TargetStart.X - Gun.X) * (TargetStart.X - Gun.X)) + ((TargetStart.Y - Gun.Y) * (TargetStart.Y - Gun.Y)
t = (-b (+ or -) sqrt(b*b - 4 * a * c)) / (2 * a)
Posted 01 November 2011 - 05:39 PM
Posted 01 November 2011 - 10:41 PM
Posted 01 November 2011 - 11:30 PM
Posted 02 November 2011 - 09:31 AM
//Compute the distance between the player and the enemy var v1:Vector2D = new Vector2D(newBullet.position.x - player.position.x, newBullet.position.y - player.position.y); var dist:Number = Math.sqrt(v1.a * v1.a + v1.b * v1.b); //Divide the distance by the speed of the projectile to determine how long it will take for the shot to reach the player var time:Number = dist / Monsters.trooperRangedSpeed; // RangedSpeed = 6 //Take the player's current velocity vector var v2:Vector2D = new Vector2D(player.movement.a, player.movement.b); //Scale the velocity vector by the time taken for the shot to arrive v2.a = v2.a / v2.magnitude; v2.b = v2.b / v2.magnitude; v2.a *= time * 4; // <--- scaling time by player velocity seems to give closer results v2.b *= time * 4; //Sum this vector with the player's current position v2.a += player.position.x; v2.b += player.position.y; //This gives you the target location you should aim at // Shoot
Posted 02 November 2011 - 10:36 AM
bool calcInterceptTrajectory(vec2 &gun, vec2 &targetstart, vec2 &targetvel, float bulletspeed, vec2 &impactpoint, vec2 &bullettrajectory) { // The coefficients of the quadratic equation: // a = ((TargetVel.X * TargetVel.X) + (TargetVel.Y * TargetVel.Y) - (BulletSpeed * BulletSpeed)) // b = 2*(TargetVel.X * (TargetStart.X - Gun.X) + TargetVel.Y * (TargetStart.Y - Gun.Y)) // c = ((TargetStart.X - Gun.X) * (TargetStart.X - Gun.X)) + ((TargetStart.Y - Gun.Y) * (TargetStart.Y - Gun.Y) float a = ((targetvel.x * targetvel.x) + (targetvel.y * targetvel.y) - (bulletspeed * bulletspeed)); float b = 2.0f * (targetvel.x * (targetstart.x - gun.x) + targetvel.y * (targetstart.y - gun.y)); float c = ((targetstart.x - gun.x) * (targetstart.x - gun.x)) + ((targetstart.y - gun.y) * (targetstart.y - gun.y)); // First, calculate the discriminant to see if we can even make the shot float disc = b*b - 4.0f*a*c; // If the discriminant is <0, there is no chance of hitting the target. Just not gonna happen, not in this universe of real numbers if (disc < 0.0f) return false; if (a==0.0f) return false; // Avoid the case of a divide-by-zero // Calculate the possible solutions float t1 = (-b + sqrt(disc)) / (2.0f * a); float t2 = (-b - sqrt(disc)) / (2.0f * a); float t = std::max(t1, t2); if (t < 0.0f) return false; // Time-traveling bullets are, sadly, not allowed // We get here, we have a valid time. Use it to extrapolate the target's position at time t, or the impact point impactpoint=vec2(targetstart.x + t * targetvel.x, targetstart.y + t*targetvel.y); // And subtract to obtain the vector along which to shoot bullettrajectory = vec2(impactpoint.x - gun.x, impactpoint.y - gun.y); // Normalize it to unit length float len=sqrt(bullettrajectory.x * bullettrajectory.x + bullettrajectory.y * bullettrajectory.y); bullettrajectory.x /= len; bullettrajectory.y /= len; return true; }
Posted 02 November 2011 - 12:06 PM
Posted 02 November 2011 - 12:19 PM
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.