list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(GameObject a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) //note that this handy little statement checks if you're on target and will hit ;). I used this to change the colour of the marker on the HUD to help you 'lock on' as it were. { } //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(GameObject a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(GameObject a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) //note that this handy little statement checks if you're on target and will hit ;). I used this to change the colour of the marker on the HUD to help you 'lock on' as it were. { } //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(AsteroidGameObject a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(Asteroid a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(GameObject a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int targettingRange = 500, maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(Asteroid a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int targettingRange = 500, maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(Asteroid a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } list<Point> targetPoints = new list<Point>(); list<Double> targetRotations = new list<Double>(); //the function you call in your update step: public void doTargetting(list<Asteroid> targets, int targettingRange) { targetPoints.clear(); //reset the list of targets targetRotations.clear(); if(targets.size() == 0) return; int maxPoints = 50, stepsPerPoint = 3; list<Point> mypoints = simulateCurve(maxPoints, stepsPerPoint, myProjectile); for(Asteroid a in targets) { if(distance(a, this) < targettingRange) { list<Point> apoints = simulateCurve(maxPoints, stepsPerPoint, a); doTargettingFor(a, this, mypoints, apoints); } } } //physics stuff: public list<Point> simulateCurve(int maxPoints, int stepsPerPoint, GameObject obj) { list<Point> points = new list<Point>(); double velx = obj.velx, vely = obj.vely; double accellx = obj.accellx, accelly = obj.accelly; double maxSpeed = obj.maxSpeed; //if you want a max speed double burnout = obj.burnout; //a point at which the projectile runs out of thruster juice double loss = 1 - obj.friction; //velocity loss in each step, because space dust or something? double x = 0, y = 0; points.add({x, y}); int spl = 0; //counter for steps per line for(int i=0; i<maxPoints*stepsPerPoint; i++) { if(burnout != 0 && i < burnout) { velx += accellx; vely += accelly; } velx *= loss; vely *= loss; double mag = Math.hypot(velx, vely); if(maxSpeed != 0 && mag > maxSpeed) { double mul = maxSpeed/mag; velx *= mul; vely *= mul; } x += velx; y += vely; spl++; if(spl > stepsPerLine) { spl = 0; points.add({x, y}); } } return points; } //targetting stuff: public void doTargettingFor(Asteroid a, Ship me, list<Point> mypoints, list<Point> apoints) { int closestIndex = 0; double closest = MAX_VALUE; //a really really big number for(int i=0; i<points.size(); i++) { double distance = Math.hypot((apoints[i].x+a.x) - (mypoints[i].x+me.x), (apoints[i].y+a.y) - (mypoints[i].y+me.y)); if(distance < closest) { closest = distance; closestIndex = i; } } if(closest < a.radius) { //this bit looks complicated but it really isn't. I'm taking the simulated points of the asteroid and adding it to the asteroid's current coordinates, then doing the same for the projectile targetPoints.add({apoints[closestIndex].x + a.x, apoints[closestIndex].y + a.y}); targetRotations.add(Math.atan2((apoints[closestIndex].y + a.y) - (mypoints[closestIndex].y + me.y), (apoints[closestIndex].x + a.x) - (mypoints[closestIndex].x + me.x))); } } default