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