Panda3D
arrival.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Filename : arrival.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 "arrival.h"
17 
18 Arrival::Arrival(AICharacter *ai_ch, double distance) {
19  _ai_char = ai_ch;
20 
21  _arrival_distance = distance;
22  _arrival_done = false;
23 }
24 
25 Arrival::~Arrival() {
26 }
27 
28 /////////////////////////////////////////////////////////////////////////////////
29 //
30 // Function : do_arrival
31 // Description : This function performs the arrival and returns an arrival force which is used
32 // in the calculate_prioritized function.
33 // In case the steering force = 0, it resets to arrival_activate.
34 // The arrival behavior works only when seek or pursue is active.
35 // This function is not to be used by the user.
36 
37 /////////////////////////////////////////////////////////////////////////////////
38 
40  LVecBase3 direction_to_target;
41  double distance;
42 
43  if(_arrival_type) {
44  direction_to_target = _ai_char->get_ai_behaviors()->_pursue_obj->_pursue_target.get_pos(_ai_char->_window_render) - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
45  }
46  else {
47  direction_to_target = _ai_char->get_ai_behaviors()->_seek_obj->_seek_position - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
48  }
49  distance = direction_to_target.length();
50 
51  _arrival_direction = direction_to_target;
52  _arrival_direction.normalize();
53 
54  if(int(distance) == 0) {
55  _ai_char->_steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
56  _ai_char->_steering->_arrival_force = LVecBase3(0.0, 0.0, 0.0);
57 
58  if(_ai_char->_steering->_seek_obj != NULL) {
59  _ai_char->_steering->turn_off("arrival");
60  _ai_char->_steering->turn_on("arrival_activate");
61  }
62  _arrival_done = true;
63  return(LVecBase3(0.0, 0.0, 0.0));
64  }
65  else {
66  _arrival_done = false;
67  }
68 
69  double u = _ai_char->get_velocity().length();
70  LVecBase3 desired_force = ((u * u) / (2 * distance)) * _ai_char->get_mass();
71 
72  if(_ai_char->_steering->_seek_obj != NULL) {
73  return(desired_force);
74  }
75 
76  if(_ai_char->_steering->_pursue_obj != NULL) {
77 
78  if(distance > _arrival_distance) {
79  _ai_char->_steering->turn_off("arrival");
80  _ai_char->_steering->turn_on("arrival_activate");
81  _ai_char->_steering->resume_ai("pursue");
82  }
83 
84  return(desired_force);
85  }
86 
87  cout<<"Arrival works only with seek and pursue"<<endl;
88  return(LVecBase3(0.0, 0.0, 0.0));
89 }
90 
91 /////////////////////////////////////////////////////////////////////////////////
92 //
93 // Function : arrival_activate
94 // Description : This function checks for whether the target is within the arrival distance.
95 // When this is true, it calls the do_arrival function and sets the arrival direction.
96 // This function is not to be used by the user.
97 
98 /////////////////////////////////////////////////////////////////////////////////
99 
101  LVecBase3 dirn;
102  if(_arrival_type) {
103  dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _ai_char->get_ai_behaviors()->_pursue_obj->_pursue_target.get_pos(_ai_char->_window_render));
104  }
105  else {
106  dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _ai_char->get_ai_behaviors()->_seek_obj->_seek_position);
107  }
108  double distance = dirn.length();
109 
110  if(distance < _arrival_distance && _ai_char->_steering->_steering_force.length() > 0) {
111  _ai_char->_steering->turn_off("arrival_activate");
112  _ai_char->_steering->turn_on("arrival");
113 
114  if(_ai_char->_steering->is_on(_ai_char->_steering->_seek)) {
115  _ai_char->_steering->turn_off("seek");
116  }
117 
118  if(_ai_char->_steering->is_on(_ai_char->_steering->_pursue)) {
119  _ai_char->_steering->pause_ai("pursue");
120  }
121  }
122 }
void pause_ai(string ai_type)
This function pauses individual or all the AIs.
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
LVecBase3 do_arrival()
This function performs the arrival and returns an arrival force which is used in the calculate_priori...
Definition: arrival.cxx:39
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
bool is_on(_behavior_type bt)
This function returns true if an aiBehavior is on.
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:783
void resume_ai(string ai_type)
This function resumes individual or all the AIs.
void arrival_activate()
This function checks for whether the target is within the arrival distance.
Definition: arrival.cxx:100