Panda3D
evade.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 evade.cxx
10  * @author Deepak, John, Navin
11  * @date 2009-10-24
12  */
13 
14 #include "evade.h"
15 
16 Evade::Evade(AICharacter *ai_ch, NodePath target_object, double panic_distance,
17  double relax_distance, float evade_wt) {
18  _ai_char = ai_ch;
19 
20  _evade_target = target_object;
21  _evade_distance = panic_distance;
22  _evade_relax_distance = relax_distance;
23  _evade_weight = evade_wt;
24 
25  _evade_done = true;
26  _evade_activate_done = false;
27 }
28 
29 Evade::~Evade() {
30 }
31 
32 /**
33  * This function performs the evade and returns an evade force which is used
34  * in the calculate_prioritized function. In case the AICharacter is past the
35  * (panic + relax) distance, it resets to evade_activate. This function is
36  * not to be used by the user.
37  */
38 LVecBase3 Evade::do_evade() {
39  assert(_evade_target && "evade target not assigned");
40 
41  _evade_direction = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render);
42  double distance = _evade_direction.length();
43 
44  _evade_direction.normalize();
45  LVecBase3 desired_force = _evade_direction * _ai_char->_movt_force;
46 
47  if(distance > (_evade_distance + _evade_relax_distance)) {
48  if((_ai_char->_steering->_behaviors_flags | _ai_char->_steering->_evade) == _ai_char->_steering->_evade) {
49  _ai_char->_steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
50  }
51  _ai_char->_steering->turn_off("evade");
52  _ai_char->_steering->turn_on("evade_activate");
53  _evade_done = true;
54  return(LVecBase3(0.0, 0.0, 0.0));
55  }
56  else {
57  _evade_done = false;
58  return(desired_force);
59  }
60 }
61 
62 /**
63  * This function checks for whether the target is within the panic distance.
64  * When this is true, it calls the do_evade function and sets the evade
65  * direction. This function is not to be used by the user.
66  */
68  _evade_direction = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render));
69  double distance = _evade_direction.length();
70  _evade_activate_done = false;
71 
72  if(distance < _evade_distance) {
73  _ai_char->_steering->turn_off("evade_activate");
74  _ai_char->_steering->turn_on("evade");
75  _evade_activate_done = true;
76  }
77 }
void turn_on(std::string ai_type)
This function turns on any aiBehavior which is passed as a string.
LVecBase3 do_evade()
This function performs the evade and returns an evade force which is used in the calculate_prioritize...
Definition: evade.cxx:38
void turn_off(std::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:992
void evade_activate()
This function checks for whether the target is within the panic distance.
Definition: evade.cxx:67
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161