/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/Luky/Geo/LLInterceptAngle.m

00001 //
00002 //  LLInterceptorAngle.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 27/04/2006.
00006 //  Copyright 2006 __MyCompanyName__. All rights reserved.
00007 //
00008 
00009 #import "LLInterceptAngle.h"
00010 
00011 
00012 @implementation LLInterceptAngle
00013 
00014 -(int) findAngleFrom:(NSPoint) pos1 to:(NSPoint) pos2 {
00015     return 0; 
00016 }
00017 
00018 - (int) angleForTarget:(LLTarget*) target fromSource:(LLTarget*)source projectileSpeed:(int)speed {
00019 
00020         bool interceptPossible = YES;
00021         int interceptTime = 0;
00022 
00023         // relative position of target
00024         LLVector *delta = [target substract:source];
00025 
00026         // set up the quadratic equation's variables
00027         int a = speed * speed - [delta size] * [delta size];
00028         int b = -( 2 * [delta size] * 
00029                         ( [delta origin].x * sin([delta angle]) - [delta origin].y * cos([delta angle]) ) );
00030         int c = - ([delta origin].x * [delta origin].x + [delta origin].y * [delta origin].y);
00031     
00032         // ensure there's no problem with the square root, and no divide by zero
00033         int sq = (b * b) - (4 * a * c);
00034         if ((sq < 0) || (a == 0)) {
00035                 interceptPossible = NO;
00036         } else {
00037                 // we're good to go, get the two results of the quadratic equation
00038                 int t1 = (-b - sqrt(sq)) / (2 * a);
00039                 int t2 = (-b + sqrt(sq)) / (2 * a);
00040 
00041                 // is the first Time value the optimal one?
00042                 if ((t1 > 0) && (t1 < t2)) {
00043                         interceptTime = t1;     
00044                 } else if (t2 > 0) {
00045                         interceptTime = t2;
00046                 } else {
00047                         interceptPossible = NO;
00048                 }
00049         }
00050     
00051         // is there a solution?
00052         if (interceptPossible) {
00053                 // where will the target be, in interceptTime seconds?
00054                 NSPoint hit;
00055                 hit.x = [target position].x + [target speed] * sin([target course]) * interceptTime;
00056                 hit.y = [target position].y - [target speed] * cos([target course]) * interceptTime;
00057                 // return the angle to hit the target
00058                 return [self findAngleFrom:[source position] to:hit];
00059         }
00060         else {
00061                 return INTERCEPT_NOT_POSSIBLE;
00062         }
00063 }
00064 
00065 @end

Generated on Fri Jul 28 19:15:22 2006 for MacTrek by  doxygen 1.4.7