Panda3D
flee.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Filename : flee.cxx
3 // Created by : Deepak, John, Navin
4 // Date : 24 Oct 09
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #include "flee.h"
17 
18 Flee::Flee(AICharacter *ai_ch, NodePath target_object, double panic_distance,
19  double relax_distance, float flee_wt){
20 
21  _ai_char = ai_ch;
22 
23  _flee_position = target_object.get_pos(_ai_char->_window_render);
24  _flee_distance = panic_distance;
25  _flee_weight = flee_wt;
26  _flee_relax_distance = relax_distance;
27 
28  _flee_done = false;
29  _flee_activate_done = false;
30 }
31 
32 Flee::Flee(AICharacter *ai_ch, LVecBase3 pos, double panic_distance,
33  double relax_distance, float flee_wt){
34 
35  _ai_char = ai_ch;
36 
37  _flee_position = pos;
38  _flee_distance = panic_distance;
39  _flee_weight = flee_wt;
40  _flee_relax_distance = relax_distance;
41 
42  _flee_done = false;
43  _flee_activate_done = false;
44 }
45 
46 Flee::~Flee() {
47 }
48 
49 /////////////////////////////////////////////////////////////////////////////////
50 //
51 // Function : do_flee
52 // Description : This function performs the flee and returns a flee force which is used
53 // in the calculate_prioritized function.
54 // In case the AICharacter is past the (panic + relax) distance,
55 // it resets to flee_activate.
56 // This function is not to be used by the user.
57 
58 /////////////////////////////////////////////////////////////////////////////////
59 
61  LVecBase3 dirn;
62  double distance;
63  LVecBase3 desired_force;
64 
65  dirn = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_present_pos;
66  distance = dirn.length();
67  desired_force = _flee_direction * _ai_char->_movt_force;
68 
69  if(distance > (_flee_distance + _flee_relax_distance)) {
70  if((_ai_char->_steering->_behaviors_flags | _ai_char->_steering->_flee) == _ai_char->_steering->_flee) {
71  _ai_char->_steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
72  }
73  _flee_done = true;
74  _ai_char->_steering->turn_off("flee");
75  _ai_char->_steering->turn_on("flee_activate");
76  return(LVecBase3(0.0, 0.0, 0.0));
77  }
78  else {
79  return(desired_force);
80  }
81 }
82 
83 /////////////////////////////////////////////////////////////////////////////////
84 //
85 // Function : flee_activate
86 // Description : This function checks for whether the target is within the panic distance.
87 // When this is true, it calls the do_flee function and sets the flee direction.
88 // This function is not to be used by the user.
89 
90 /////////////////////////////////////////////////////////////////////////////////
91 
93  LVecBase3 dirn;
94  double distance;
95 
96  _flee_activate_done = false;
97 
98  dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_position);
99  distance = dirn.length();
100 
101  if(distance < _flee_distance) {
102  _flee_direction = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _flee_position;
103  _flee_direction.normalize();
104  _flee_present_pos = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
105  _ai_char->_steering->turn_off("flee_activate");
106  _ai_char->_steering->turn_on("flee");
107  _flee_activate_done = true;
108  }
109 }
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
LVecBase3 do_flee()
This function performs the flee and returns a flee force which is used in the calculate_prioritized f...
Definition: flee.cxx:60
void turn_off(string ai_type)
This function turns off any aiBehavior which is passed as a string.
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:1178
void turn_on(string ai_type)
This function turns on any aiBehavior which is passed as a string.
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:766
void flee_activate()
This function checks for whether the target is within the panic distance.
Definition: flee.cxx:92
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:783
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165