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