Panda3D
|
00001 //////////////////////////////////////////////////////////////////////// 00002 // Filename : evade.cxx 00003 // Created by : Deepak, John, Navin 00004 // Date : 24 Oct 09 00005 //////////////////////////////////////////////////////////////////// 00006 // 00007 // PANDA 3D SOFTWARE 00008 // Copyright (c) Carnegie Mellon University. All rights reserved. 00009 // 00010 // All use of this software is subject to the terms of the revised BSD 00011 // license. You should have received a copy of this license along 00012 // with this source code in a file named "LICENSE." 00013 // 00014 //////////////////////////////////////////////////////////////////// 00015 00016 #include "evade.h" 00017 00018 Evade::Evade(AICharacter *ai_ch, NodePath target_object, double panic_distance, 00019 double relax_distance, float evade_wt) { 00020 _ai_char = ai_ch; 00021 00022 _evade_target = target_object; 00023 _evade_distance = panic_distance; 00024 _evade_relax_distance = relax_distance; 00025 _evade_weight = evade_wt; 00026 00027 _evade_done = true; 00028 _evade_activate_done = false; 00029 } 00030 00031 Evade::~Evade() { 00032 } 00033 00034 ///////////////////////////////////////////////////////////////////////////////// 00035 // 00036 // Function : do_evade 00037 // Description : This function performs the evade and returns an evade force which is used 00038 // in the calculate_prioritized function. 00039 // In case the AICharacter is past the (panic + relax) distance, 00040 // it resets to evade_activate. 00041 // This function is not to be used by the user. 00042 00043 ///////////////////////////////////////////////////////////////////////////////// 00044 00045 LVecBase3f Evade::do_evade() { 00046 assert(_evade_target && "evade target not assigned"); 00047 00048 _evade_direction = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render); 00049 double distance = _evade_direction.length(); 00050 00051 _evade_direction.normalize(); 00052 LVecBase3f desired_force = _evade_direction * _ai_char->_movt_force; 00053 00054 if(distance > (_evade_distance + _evade_relax_distance)) { 00055 if((_ai_char->_steering->_behaviors_flags | _ai_char->_steering->_evade) == _ai_char->_steering->_evade) { 00056 _ai_char->_steering->_steering_force = LVecBase3f(0.0, 0.0, 0.0); 00057 } 00058 _ai_char->_steering->turn_off("evade"); 00059 _ai_char->_steering->turn_on("evade_activate"); 00060 _evade_done = true; 00061 return(LVecBase3f(0.0, 0.0, 0.0)); 00062 } 00063 else { 00064 _evade_done = false; 00065 return(desired_force); 00066 } 00067 } 00068 00069 ///////////////////////////////////////////////////////////////////////////////// 00070 // 00071 // Function : evade_activate 00072 // Description : This function checks for whether the target is within the panic distance. 00073 // When this is true, it calls the do_evade function and sets the evade direction. 00074 // This function is not to be used by the user. 00075 00076 ///////////////////////////////////////////////////////////////////////////////// 00077 00078 void Evade::evade_activate() { 00079 _evade_direction = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render)); 00080 double distance = _evade_direction.length(); 00081 _evade_activate_done = false; 00082 00083 if(distance < _evade_distance) { 00084 _ai_char->_steering->turn_off("evade_activate"); 00085 _ai_char->_steering->turn_on("evade"); 00086 _evade_activate_done = true; 00087 } 00088 }