Panda3D
aiCharacter.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 aiCharacter.cxx
10  * @author Deepak, John, Navin
11  * @date 2009-09-08
12  */
13 
14 #include "aiCharacter.h"
15 
16 AICharacter::AICharacter(std::string model_name, NodePath model_np, double mass, double movt_force, double max_force) {
17  _name = model_name;
18  _ai_char_np = model_np;
19 
20  _mass = mass;
21  _max_force = max_force;
22  _movt_force = movt_force;
23 
24  _velocity = LVecBase3(0.0, 0.0, 0.0);
25  _steering_force = LVecBase3(0.0, 0.0, 0.0);
26 
27  _world = nullptr;
28 
29  _steering = new AIBehaviors();
30  _steering->_ai_char = this;
31 
32  _pf_guide = false;
33 }
34 
35 AICharacter::~AICharacter() {
36  nassertv(_world == nullptr);
37 }
38 
39 /**
40  * Each character's update will update its AI and physics based on his
41  * resultant steering force. This also makes the character look in the
42  * direction of the force.
43  */
45 update() {
46  if (!_steering->is_off(_steering->_none)) {
47  LVecBase3 old_pos = _ai_char_np.get_pos();
48  LVecBase3 steering_force = _steering->calculate_prioritized();
49  LVecBase3 acceleration = steering_force / _mass;
50 
51  _velocity = acceleration;
52 
53  LVecBase3 direction = _steering->_steering_force;
54  direction.normalize();
55 
56  _ai_char_np.set_pos(old_pos + _velocity) ;
57 
58  if (steering_force.length() > 0) {
59  _ai_char_np.look_at(old_pos + (direction * 5));
60  _ai_char_np.set_h(_ai_char_np.get_h() + 180);
61  _ai_char_np.set_p(-_ai_char_np.get_p());
62  _ai_char_np.set_r(-_ai_char_np.get_r());
63  }
64  } else {
65  _steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
66  _steering->_seek_force = LVecBase3(0.0, 0.0, 0.0);
67  _steering->_flee_force = LVecBase3(0.0, 0.0, 0.0);
68  _steering->_pursue_force = LVecBase3(0.0, 0.0, 0.0);
69  _steering->_evade_force = LVecBase3(0.0, 0.0, 0.0);
70  _steering->_arrival_force = LVecBase3(0.0, 0.0, 0.0);
71  _steering->_flock_force = LVecBase3(0.0, 0.0, 0.0);
72  _steering->_wander_force = LVecBase3(0.0, 0.0, 0.0);
73  }
74 }
75 
76 LVecBase3 AICharacter::get_velocity() {
77  return _velocity;
78 }
79 
80 void AICharacter::set_velocity(LVecBase3 velocity) {
81  _velocity = velocity;
82 }
83 
84 double AICharacter::get_mass() {
85  return _mass;
86 }
87 
88 void AICharacter::set_mass(double m) {
89  _mass = m;
90 }
91 
92 double AICharacter::get_max_force() {
93  return _max_force;
94 }
95 
96 void AICharacter::set_max_force(double max_force) {
97  _max_force = max_force;
98 }
99 
100 NodePath AICharacter::get_node_path() {
101  return _ai_char_np;
102 }
103 
104 void AICharacter::set_node_path(NodePath np) {
105  _ai_char_np = np;
106 }
107 
108 AIBehaviors * AICharacter::get_ai_behaviors() {
109  return _steering;
110 }
111 
112 void AICharacter::set_char_render(NodePath render) {
113  _window_render = render;
114 }
115 
116 NodePath AICharacter::get_char_render() {
117  return _window_render;
118 }
119 
120 void AICharacter::set_pf_guide(bool pf_guide) {
121  _pf_guide = pf_guide;
122 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class implements all the steering behaviors of the AI framework, such as seek,...
Definition: aiBehaviors.h:44
bool is_off(_behavior_type bt)
This function returns true if an aiBehavior is off.
LVecBase3 calculate_prioritized()
This function updates the main steering force for the ai character using the accumulate function and ...
void update()
Each character's update will update its AI and physics based on his resultant steering force.
Definition: aiCharacter.cxx:45
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:159
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:1044
void look_at(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Sets the transform on this NodePath so that it rotates to face the indicated point in space.
Definition: nodePath.I:789