Panda3D
arrival.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 arrival.cxx
10  * @author Deepak, John, Navin
11  * @date 2009-10-24
12  */
13 
14 #include "arrival.h"
15 
16 Arrival::Arrival(AICharacter *ai_ch, double distance) {
17  _ai_char = ai_ch;
18 
19  _arrival_distance = distance;
20  _arrival_done = false;
21 }
22 
23 Arrival::~Arrival() {
24 }
25 
26 /**
27  * This function performs the arrival and returns an arrival force which is
28  * used in the calculate_prioritized function. In case the steering force =
29  * 0, it resets to arrival_activate. The arrival behavior works only when
30  * seek or pursue is active. This function is not to be used by the user.
31  */
32 LVecBase3 Arrival::do_arrival() {
33  LVecBase3 direction_to_target;
34  double distance;
35 
36  if(_arrival_type) {
37  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);
38  }
39  else {
40  direction_to_target = _ai_char->get_ai_behaviors()->_seek_obj->_seek_position - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render);
41  }
42  distance = direction_to_target.length();
43 
44  _arrival_direction = direction_to_target;
45  _arrival_direction.normalize();
46 
47  if(int(distance) == 0) {
48  _ai_char->_steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
49  _ai_char->_steering->_arrival_force = LVecBase3(0.0, 0.0, 0.0);
50 
51  if(_ai_char->_steering->_seek_obj != nullptr) {
52  _ai_char->_steering->turn_off("arrival");
53  _ai_char->_steering->turn_on("arrival_activate");
54  }
55  _arrival_done = true;
56  return(LVecBase3(0.0, 0.0, 0.0));
57  }
58  else {
59  _arrival_done = false;
60  }
61 
62  double u = _ai_char->get_velocity().length();
63  LVecBase3 desired_force = ((u * u) / (2 * distance)) * _ai_char->get_mass();
64 
65  if(_ai_char->_steering->_seek_obj != nullptr) {
66  return(desired_force);
67  }
68 
69  if(_ai_char->_steering->_pursue_obj != nullptr) {
70 
71  if(distance > _arrival_distance) {
72  _ai_char->_steering->turn_off("arrival");
73  _ai_char->_steering->turn_on("arrival_activate");
74  _ai_char->_steering->resume_ai("pursue");
75  }
76 
77  return(desired_force);
78  }
79 
80  std::cout << "Arrival works only with seek and pursue" << std::endl;
81  return(LVecBase3(0.0, 0.0, 0.0));
82 }
83 
84 /**
85  * This function checks for whether the target is within the arrival distance.
86  * When this is true, it calls the do_arrival function and sets the arrival
87  * direction. This function is not to be used by the user.
88  */
90  LVecBase3 dirn;
91  if(_arrival_type) {
92  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));
93  }
94  else {
95  dirn = (_ai_char->_ai_char_np.get_pos(_ai_char->_window_render) - _ai_char->get_ai_behaviors()->_seek_obj->_seek_position);
96  }
97  double distance = dirn.length();
98 
99  if(distance < _arrival_distance && _ai_char->_steering->_steering_force.length() > 0) {
100  _ai_char->_steering->turn_off("arrival_activate");
101  _ai_char->_steering->turn_on("arrival");
102 
103  if(_ai_char->_steering->is_on(_ai_char->_steering->_seek)) {
104  _ai_char->_steering->turn_off("seek");
105  }
106 
107  if(_ai_char->_steering->is_on(_ai_char->_steering->_pursue)) {
108  _ai_char->_steering->pause_ai("pursue");
109  }
110  }
111 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void resume_ai(std::string ai_type)
This function resumes individual or all the AIs.
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_arrival()
This function performs the arrival and returns an arrival force which is used in the calculate_priori...
Definition: arrival.cxx:32
void pause_ai(std::string ai_type)
This function pauses individual or all the AIs.
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:992
bool is_on(_behavior_type bt)
This function returns true if an aiBehavior is on.
void arrival_activate()
This function checks for whether the target is within the arrival distance.
Definition: arrival.cxx:89