00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00014
00015 #include "arrival.h"
00016
00017 Arrival::Arrival(AICharacter *ai_ch, NodePath target_object, double distance) {
00018 _ai_char = ai_ch;
00019
00020 _arrival_target = target_object;
00021 _arrival_distance = distance;
00022
00023 _arrival_done = false;
00024 }
00025
00026 Arrival::~Arrival() {
00027 }
00028
00037
00039
00040 LVecBase3f Arrival::do_arrival() {
00041 LVecBase3f direction_to_target;
00042 double distance;
00043
00044 direction_to_target = _arrival_target.get_pos(_ai_char->_window_render) - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
00045 distance = direction_to_target.length();
00046
00047 _arrival_direction = direction_to_target;
00048 _arrival_direction.normalize();
00049
00050 if(int(distance) == 0) {
00051 _ai_char->_steering->_steering_force = LVecBase3f(0.0, 0.0, 0.0);
00052 _ai_char->_steering->_arrival_force = LVecBase3f(0.0, 0.0, 0.0);
00053
00054 if(_ai_char->_steering->_seek_obj != NULL) {
00055 _ai_char->_steering->arrival_off();
00056 _ai_char->_steering->arrival_activate_on();
00057 }
00058 _arrival_done = true;
00059 return(LVecBase3f(0.0, 0.0, 0.0));
00060 }
00061 else {
00062 _arrival_done = false;
00063 }
00064
00065 double u = _ai_char->get_velocity().length();
00066 LVecBase3f desired_force = ((u * u) / (2 * distance)) * _ai_char->get_mass();
00067
00068 if(_ai_char->_steering->_seek_obj != NULL) {
00069 return(desired_force);
00070 }
00071
00072 if(_ai_char->_steering->_pursue_obj != NULL) {
00073
00074 if(distance > _arrival_distance) {
00075 _ai_char->_steering->arrival_off();
00076 _ai_char->_steering->arrival_activate_on();
00077 _ai_char->_steering->resume_ai("pursue");
00078 }
00079
00080 return(desired_force);
00081 }
00082
00083 cout<<"Arrival works only with seek and pursue for now"<<endl;
00084 return(LVecBase3f(0.0, 0.0, 0.0));
00085 }
00086
00093
00095
00096 void Arrival::arrival_activate() {
00097 LVecBase3f dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _arrival_target.get_pos(_ai_char->_window_render));
00098 double distance = dirn.length();
00099
00100 if(distance < _arrival_distance && _ai_char->_steering->_steering_force.length() > 0) {
00101 _ai_char->_steering->arrival_activate_off();
00102 _ai_char->_steering->arrival_on();
00103
00104 if(_ai_char->_steering->is_seek_on()) {
00105 _ai_char->_steering->seek_off();
00106 }
00107
00108 if(_ai_char->_steering->is_pursue_on()) {
00109 _ai_char->_steering->pause_ai("pursue");
00110 }
00111 }
00112 }