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