Panda3D
Loading...
Searching...
No Matches
pathFollow.cxx
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 pathFind.cxx
10 * @author Deepak, John, Navin
11 * @date 2009-10-24
12 */
13
14#include "pathFollow.h"
15
16#include "pathFind.h"
17
18PathFollow::PathFollow(AICharacter *ai_ch, float follow_wt) {
19 _follow_weight = follow_wt;
20 _curr_path_waypoint = -1;
21 _start = false;
22 _ai_char = ai_ch;
24}
25
26PathFollow::~PathFollow() {
27}
28
29/**
30 * This function adds the positions generated from a pathfind or a simple path
31 * follow behavior to the _path list.
32 */
33void PathFollow::add_to_path(LVecBase3 pos) {
34 _path.push_back(pos);
35}
36
37/**
38 * This function initiates the path follow behavior.
39 */
40void PathFollow::start(std::string type) {
41 _type = type;
42 _start = true;
43 if(_path.size() > 0) {
44 _curr_path_waypoint = _path.size() - 1;
45 _dummy = _ai_char->_window_render.attach_new_node("dummy");
46 _dummy.set_pos(_path[_curr_path_waypoint]);
47 _ai_char->_steering->pursue(_dummy, _follow_weight);
48 _time = _myClock->get_real_time();
49 }
50}
51
52/**
53 * This function allows continuous path finding by ai chars. There are 2 ways
54 * in which this is implemented. 1. The character re-calculates the optimal
55 * path everytime the target changes its position. Less computationally
56 * expensive. 2. The character continuosly re-calculates its optimal path to
57 * the target. This is used in a scenario where the ai chars have to avoid
58 * other ai chars. More computationally expensive.
59 */
61 if((_myClock->get_real_time() - _time) > 0.5) {
62 if(_type=="pathfind") {
63 // This 'if' statement when 'true' causes the path to be re-calculated
64 // irrespective of target position. This is done when _dynamice_avoid
65 // is active. More computationally expensive.
66 if(_ai_char->_steering->_path_find_obj->_dynamic_avoid) {
67 _ai_char->_steering->_path_find_obj->do_dynamic_avoid();
68 if(check_if_possible()) {
69 _path.clear();
70 _ai_char->_steering->_path_find_obj->path_find(_ai_char->_steering->_path_find_obj->_path_find_target);
71 // Ensure that the path size is not 0.
72 if(_path.size() > 0) {
73 _curr_path_waypoint = _path.size() - 1;
74 _dummy.set_pos(_path[_curr_path_waypoint]);
75 }
76 else {
77 // Refresh the _curr_path_waypoint value if path size is <= 0.
78 _curr_path_waypoint = -1;
79 }
80 }
81 }
82 // This 'if' statement causes the path to be re-calculated only when
83 // there is a change in target position. Less computationally
84 // expensive.
85 else if(_ai_char->_steering->_path_find_obj->_path_find_target.get_pos(_ai_char->_window_render)
86 != _ai_char->_steering->_path_find_obj->_prev_position) {
87 if(check_if_possible()) {
88 _path.clear();
89 _ai_char->_steering->_path_find_obj->path_find(_ai_char->_steering->_path_find_obj->_path_find_target);
90 // Ensure that the path size is not 0.
91 if(_path.size() > 0) {
92 _curr_path_waypoint = _path.size() - 1;
93 _dummy.set_pos(_path[_curr_path_waypoint]);
94 }
95 else {
96 // Refresh the _curr_path_waypoint value if path size is 0.
97 _curr_path_waypoint = -1;
98 }
99 }
100 }
101 _time = _myClock->get_real_time();
102 }
103 }
104
105 if(_curr_path_waypoint > 0) {
106 double distance = (_path[_curr_path_waypoint] - _ai_char->_ai_char_np.get_pos(_ai_char->_window_render)).length();
107
108 if(distance < 5) {
109 _curr_path_waypoint--;
110 _dummy.set_pos(_path[_curr_path_waypoint]);
111 }
112 }
113}
114
115/**
116 * This function checks if the current positions of the ai char and the target
117 * char can be used to generate an optimal path.
118 */
120 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);
121 LVecBase3 _prev_position = _ai_char->_steering->_path_find_obj->_path_find_target.get_pos(_ai_char->_window_render);
122 Node* dst = find_in_mesh(_ai_char->_steering->_path_find_obj->_nav_mesh, _prev_position, _ai_char->_steering->_path_find_obj->_grid_size);
123
124 if(src && dst) {
125 return true;
126 }
127 else {
128 return false;
129 }
130}
Node * find_in_mesh(NavMesh nav_mesh, LVecBase3 pos, int grid_size)
This function allows the user to pass a position and it returns the corresponding node on the navigat...
void pursue(NodePath target_object, float pursue_wt=1.0)
This function activates pursue.
get_real_time
Returns the actual number of seconds elapsed since the ClockObject was created, or since it was last ...
Definition clockObject.h:92
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
LPoint3 get_pos() const
Retrieves the translation component of the transform.
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:599
This class is used to assign the nodes on the mesh.
Definition meshNode.h:16
void path_find(LVecBase3 pos, std::string type="normal")
This function checks for the source and target in the navigation mesh for its availability and then f...
Definition pathFind.cxx:203
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:367
void do_follow()
This function allows continuous path finding by ai chars.
void start(std::string type)
This function initiates the path follow behavior.
bool check_if_possible()
This function checks if the current positions of the ai char and the target char can be used to gener...
void add_to_path(LVecBase3 pos)
This function adds the positions generated from a pathfind or a simple path follow behavior to the _p...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.