Panda3D

flee.cxx

00001 ////////////////////////////////////////////////////////////////////////
00002 // Filename    : flee.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 "flee.h"
00017 
00018 Flee::Flee(AICharacter *ai_ch, NodePath target_object, double panic_distance,
00019                                       double relax_distance, float flee_wt){
00020 
00021   _ai_char = ai_ch;
00022 
00023   _flee_position = target_object.get_pos(_ai_char->_window_render);
00024   _flee_distance = panic_distance;
00025   _flee_weight = flee_wt;
00026   _flee_relax_distance = relax_distance;
00027 
00028   _flee_done = false;
00029   _flee_activate_done = false;
00030 }
00031 
00032 Flee::Flee(AICharacter *ai_ch, LVecBase3f pos, double panic_distance,
00033                                 double relax_distance, float flee_wt){
00034 
00035     _ai_char = ai_ch;
00036 
00037   _flee_position = pos;
00038   _flee_distance = panic_distance;
00039   _flee_weight = flee_wt;
00040   _flee_relax_distance = relax_distance;
00041 
00042   _flee_done = false;
00043   _flee_activate_done = false;
00044 }
00045 
00046 Flee::~Flee() {
00047 }
00048 
00049 /////////////////////////////////////////////////////////////////////////////////
00050 //
00051 // Function : do_flee
00052 // Description : This function performs the flee and returns a flee force which is used
00053 //                in the calculate_prioritized function.
00054 //                In case the AICharacter is past the (panic + relax) distance,
00055 //                it resets to flee_activate.
00056 //                This function is not to be used by the user.
00057 
00058 /////////////////////////////////////////////////////////////////////////////////
00059 
00060 LVecBase3f Flee::do_flee() {
00061   LVecBase3f dirn;
00062   double distance;
00063   LVecBase3f desired_force;
00064 
00065   dirn = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_present_pos;
00066   distance = dirn.length();
00067   desired_force = _flee_direction * _ai_char->_movt_force;
00068 
00069   if(distance > (_flee_distance + _flee_relax_distance)) {
00070     if((_ai_char->_steering->_behaviors_flags | _ai_char->_steering->_flee) == _ai_char->_steering->_flee) {
00071         _ai_char->_steering->_steering_force = LVecBase3f(0.0, 0.0, 0.0);
00072     }
00073     _flee_done = true;
00074     _ai_char->_steering->turn_off("flee");
00075     _ai_char->_steering->turn_on("flee_activate");
00076     return(LVecBase3f(0.0, 0.0, 0.0));
00077   }
00078   else {
00079       return(desired_force);
00080   }
00081 }
00082 
00083 /////////////////////////////////////////////////////////////////////////////////
00084 //
00085 // Function : flee_activate
00086 // Description : This function checks for whether the target is within the panic distance.
00087 //                When this is true, it calls the do_flee function and sets the flee direction.
00088 //                This function is not to be used by the user.
00089 
00090 /////////////////////////////////////////////////////////////////////////////////
00091 
00092 void Flee::flee_activate() {
00093   LVecBase3f dirn;
00094   double distance;
00095 
00096   _flee_activate_done = false;
00097 
00098   dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_position);
00099   distance = dirn.length();
00100 
00101   if(distance < _flee_distance) {
00102       _flee_direction = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_position;
00103       _flee_direction.normalize();
00104       _flee_present_pos = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
00105       _ai_char->_steering->turn_off("flee_activate");
00106       _ai_char->_steering->turn_on("flee");
00107       _flee_activate_done = true;
00108   }
00109 }
 All Classes Functions Variables Enumerations