Panda3D
 All Classes Functions Variables Enumerations
pathFollow.cxx
1 
2 #include "pathFollow.h"
3 
4 PathFollow::PathFollow(AICharacter *ai_ch, float follow_wt) {
5  _follow_weight = follow_wt;
6  _curr_path_waypoint = -1;
7  _start = false;
8  _ai_char = ai_ch;
10 }
11 
12 PathFollow::~PathFollow() {
13 }
14 
15 /////////////////////////////////////////////////////////////////////////////////////////
16 //
17 // Function : add_to_path
18 // Description : This function adds the positions generated from a pathfind or a simple
19 // path follow behavior to the _path list.
20 
21 /////////////////////////////////////////////////////////////////////////////////////////
22 
24  _path.push_back(pos);
25 }
26 
27 /////////////////////////////////////////////////////////////////////////////////////////
28 //
29 // Function : start
30 // Description : This function initiates the path follow behavior.
31 
32 /////////////////////////////////////////////////////////////////////////////////////////
33 
34 void PathFollow::start(string type) {
35  _type = type;
36  _start = true;
37  if(_path.size() > 0) {
38  _curr_path_waypoint = _path.size() - 1;
39  _dummy = _ai_char->_window_render.attach_new_node("dummy");
40  _dummy.set_pos(_path[_curr_path_waypoint]);
41  _ai_char->_steering->pursue(_dummy, _follow_weight);
42  _time = _myClock->get_real_time();
43  }
44 }
45 
46 /////////////////////////////////////////////////////////////////////////////////////////
47 //
48 // Function : do_follow
49 // Description : This function allows continuous path finding by ai chars. There are 2
50 // ways in which this is implemented.
51 // 1. The character re-calculates the optimal path everytime the target
52 // changes its position. Less computationally expensive.
53 // 2. The character continuosly re-calculates its optimal path to the
54 // target. This is used in a scenario where the ai chars have to avoid
55 // other ai chars. More computationally expensive.
56 
57 /////////////////////////////////////////////////////////////////////////////////////////
58 
60  if((_myClock->get_real_time() - _time) > 0.5) {
61  if(_type=="pathfind") {
62  // This 'if' statement when 'true' causes the path to be re-calculated irrespective of target position.
63  // This is done when _dynamice_avoid is active. More computationally expensive.
64  if(_ai_char->_steering->_path_find_obj->_dynamic_avoid) {
65  _ai_char->_steering->_path_find_obj->do_dynamic_avoid();
66  if(check_if_possible()) {
67  _path.clear();
68  _ai_char->_steering->_path_find_obj->path_find(_ai_char->_steering->_path_find_obj->_path_find_target);
69  // Ensure that the path size is not 0.
70  if(_path.size() > 0) {
71  _curr_path_waypoint = _path.size() - 1;
72  _dummy.set_pos(_path[_curr_path_waypoint]);
73  }
74  else {
75  // Refresh the _curr_path_waypoint value if path size is <= 0.
76  _curr_path_waypoint = -1;
77  }
78  }
79  }
80  // This 'if' statement causes the path to be re-calculated only when there is a change in target position.
81  // Less computationally expensive.
82  else if(_ai_char->_steering->_path_find_obj->_path_find_target.get_pos(_ai_char->_window_render)
83  != _ai_char->_steering->_path_find_obj->_prev_position) {
84  if(check_if_possible()) {
85  _path.clear();
86  _ai_char->_steering->_path_find_obj->path_find(_ai_char->_steering->_path_find_obj->_path_find_target);
87  // Ensure that the path size is not 0.
88  if(_path.size() > 0) {
89  _curr_path_waypoint = _path.size() - 1;
90  _dummy.set_pos(_path[_curr_path_waypoint]);
91  }
92  else {
93  // Refresh the _curr_path_waypoint value if path size is 0.
94  _curr_path_waypoint = -1;
95  }
96  }
97  }
98  _time = _myClock->get_real_time();
99  }
100  }
101 
102  if(_curr_path_waypoint > 0) {
103  double distance = (_path[_curr_path_waypoint] - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render)).length();
104 
105  if(distance < 5) {
106  _curr_path_waypoint--;
107  _dummy.set_pos(_path[_curr_path_waypoint]);
108  }
109  }
110 }
111 
112 /////////////////////////////////////////////////////////////////////////////////////////
113 //
114 // Function : check_if_possible
115 // Description : This function checks if the current positions of the ai char and the
116 // target char can be used to generate an optimal path.
117 
118 /////////////////////////////////////////////////////////////////////////////////////////
119 
121  Node* src = find_in_mesh(_ai_char->_steering->_path_find_obj->_nav_mesh, _ai_char->_ai_char_np.get_pos(_ai_char->_window_render), _ai_char->_steering->_path_find_obj->_grid_size);
122  LVecBase3 _prev_position = _ai_char->_steering->_path_find_obj->_path_find_target.get_pos(_ai_char->_window_render);
123  Node* dst = find_in_mesh(_ai_char->_steering->_path_find_obj->_nav_mesh, _prev_position, _ai_char->_steering->_path_find_obj->_grid_size);
124 
125  if(src && dst) {
126  return true;
127  }
128  else {
129  return false;
130  }
131 }
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:271
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This class is used to assign the nodes on the mesh.
Definition: meshNode.h:18
void start(string type)
This function initiates the path follow behavior.
Definition: pathFollow.cxx:34
void do_follow()
This function allows continuous path finding by ai chars.
Definition: pathFollow.cxx:59
void set_pos(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Sets the translation component of the transform, leaving rotation and scale untouched.
Definition: nodePath.I:783
void add_to_path(LVecBase3 pos)
This function adds the positions generated from a pathfind or a simple path follow behavior to the _p...
Definition: pathFollow.cxx:23
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:1178
void path_find(LVecBase3 pos, string type="normal")
This function checks for the source and target in the navigation mesh for its availability and then f...
Definition: pathFind.cxx:200
bool check_if_possible()
This function checks if the current positions of the ai char and the target char can be used to gener...
Definition: pathFollow.cxx:120
void do_dynamic_avoid()
This function does the updation of the collisions to the mesh based on the new positions of the obsta...
Definition: pathFind.cxx:384
double get_real_time() const
Returns the actual number of seconds elapsed since the ClockObject was created, or since it was last ...
Definition: clockObject.I:68
NodePath attach_new_node(PandaNode *node, int sort=0, Thread *current_thread=Thread::get_current_thread()) const
Attaches a new node, with or without existing parents, to the scene graph below the referenced node o...
Definition: nodePath.cxx:723
void pursue(NodePath target_object, float pursue_wt=1.0)
This function activates pursue.