Panda3D
 All Classes Functions Variables Enumerations
evade.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Filename : evade.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 "evade.h"
17 
18 Evade::Evade(AICharacter *ai_ch, NodePath target_object, double panic_distance,
19  double relax_distance, float evade_wt) {
20  _ai_char = ai_ch;
21 
22  _evade_target = target_object;
23  _evade_distance = panic_distance;
24  _evade_relax_distance = relax_distance;
25  _evade_weight = evade_wt;
26 
27  _evade_done = true;
28  _evade_activate_done = false;
29 }
30 
31 Evade::~Evade() {
32 }
33 
34 /////////////////////////////////////////////////////////////////////////////////
35 //
36 // Function : do_evade
37 // Description : This function performs the evade and returns an evade force which is used
38 // in the calculate_prioritized function.
39 // In case the AICharacter is past the (panic + relax) distance,
40 // it resets to evade_activate.
41 // This function is not to be used by the user.
42 
43 /////////////////////////////////////////////////////////////////////////////////
44 
46  assert(_evade_target && "evade target not assigned");
47 
48  _evade_direction = _ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render);
49  double distance = _evade_direction.length();
50 
51  _evade_direction.normalize();
52  LVecBase3 desired_force = _evade_direction * _ai_char->_movt_force;
53 
54  if(distance > (_evade_distance + _evade_relax_distance)) {
55  if((_ai_char->_steering->_behaviors_flags | _ai_char->_steering->_evade) == _ai_char->_steering->_evade) {
56  _ai_char->_steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
57  }
58  _ai_char->_steering->turn_off("evade");
59  _ai_char->_steering->turn_on("evade_activate");
60  _evade_done = true;
61  return(LVecBase3(0.0, 0.0, 0.0));
62  }
63  else {
64  _evade_done = false;
65  return(desired_force);
66  }
67 }
68 
69 /////////////////////////////////////////////////////////////////////////////////
70 //
71 // Function : evade_activate
72 // Description : This function checks for whether the target is within the panic distance.
73 // When this is true, it calls the do_evade function and sets the evade direction.
74 // This function is not to be used by the user.
75 
76 /////////////////////////////////////////////////////////////////////////////////
77 
79  _evade_direction = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _evade_target.get_pos(_ai_char->_window_render));
80  double distance = _evade_direction.length();
81  _evade_activate_done = false;
82 
83  if(distance < _evade_distance) {
84  _ai_char->_steering->turn_off("evade_activate");
85  _ai_char->_steering->turn_on("evade");
86  _evade_activate_done = true;
87  }
88 }
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
LVecBase3 do_evade()
This function performs the evade and returns an evade force which is used in the calculate_prioritize...
Definition: evade.cxx:45
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:765
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:1178
void turn_off(string ai_type)
This function turns off any aiBehavior which is passed as a string.
void turn_on(string ai_type)
This function turns on any aiBehavior which is passed as a string.
void evade_activate()
This function checks for whether the target is within the panic distance.
Definition: evade.cxx:78
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:782
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165