Panda3D
 All Classes Functions Variables Enumerations
aiCharacter.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Filename : aiCharacter.cxx
3 // Created by : Deepak, John, Navin
4 // Date : 8 Sep 09
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #include "aiCharacter.h"
17 
18 AICharacter::AICharacter(string model_name, NodePath model_np, double mass, double movt_force, double max_force) {
19  _name = model_name;
20  _ai_char_np = model_np;
21 
22  _mass = mass;
23  _max_force = max_force;
24  _movt_force = movt_force;
25 
26  _velocity = LVecBase3(0.0, 0.0, 0.0);
27  _steering_force = LVecBase3(0.0, 0.0, 0.0);
28 
29  _steering = new AIBehaviors();
30  _steering->_ai_char = this;
31 
32  _pf_guide = false;
33 }
34 
35 AICharacter::~AICharacter() {
36 }
37 
38 /////////////////////////////////////////////////////////////////////////////////////////
39 //
40 // Function : update
41 // Description : Each character's update will update its ai and physics
42 // based on his resultant steering force.
43 // This also makes the character look at the direction of the force.
44 
45 /////////////////////////////////////////////////////////////////////////////////////////
46 void AICharacter::
47 update() {
48  if (!_steering->is_off(_steering->_none)) {
49  LVecBase3 old_pos = _ai_char_np.get_pos();
50  LVecBase3 steering_force = _steering->calculate_prioritized();
51  LVecBase3 acceleration = steering_force / _mass;
52 
53  _velocity = acceleration;
54 
55  LVecBase3 direction = _steering->_steering_force;
56  direction.normalize();
57 
58  _ai_char_np.set_pos(old_pos + _velocity) ;
59 
60  if (steering_force.length() > 0) {
61  _ai_char_np.look_at(old_pos + (direction * 5));
62  _ai_char_np.set_h(_ai_char_np.get_h() + 180);
63  _ai_char_np.set_p(-_ai_char_np.get_p());
64  _ai_char_np.set_r(-_ai_char_np.get_r());
65  }
66  } else {
67  _steering->_steering_force = LVecBase3(0.0, 0.0, 0.0);
68  _steering->_seek_force = LVecBase3(0.0, 0.0, 0.0);
69  _steering->_flee_force = LVecBase3(0.0, 0.0, 0.0);
70  _steering->_pursue_force = LVecBase3(0.0, 0.0, 0.0);
71  _steering->_evade_force = LVecBase3(0.0, 0.0, 0.0);
72  _steering->_arrival_force = LVecBase3(0.0, 0.0, 0.0);
73  _steering->_flock_force = LVecBase3(0.0, 0.0, 0.0);
74  _steering->_wander_force = LVecBase3(0.0, 0.0, 0.0);
75  }
76 }
77 
78 LVecBase3 AICharacter::get_velocity() {
79  return _velocity;
80 }
81 
82 void AICharacter::set_velocity(LVecBase3 velocity) {
83  _velocity = velocity;
84 }
85 
86 double AICharacter::get_mass() {
87  return _mass;
88 }
89 
90 void AICharacter::set_mass(double m) {
91  _mass = m;
92 }
93 
94 double AICharacter::get_max_force() {
95  return _max_force;
96 }
97 
98 void AICharacter::set_max_force(double max_force) {
99  _max_force = max_force;
100 }
101 
102 NodePath AICharacter::get_node_path() {
103  return _ai_char_np;
104 }
105 
106 void AICharacter::set_node_path(NodePath np) {
107  _ai_char_np = np;
108 }
109 
110 AIBehaviors * AICharacter::get_ai_behaviors() {
111  return _steering;
112 }
113 
114 void AICharacter::set_char_render(NodePath render) {
115  _window_render = render;
116 }
117 
118 NodePath AICharacter::get_char_render() {
119  return _window_render;
120 }
121 
122 void AICharacter::set_pf_guide(bool pf_guide) {
123  _pf_guide = pf_guide;
124 }
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
LVecBase3 calculate_prioritized()
This function updates the main steering force for the ai character using the accumulate function and ...
bool is_off(_behavior_type bt)
This function returns true if an aiBehavior is off.
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
float length() const
Returns the length of the vector, by the Pythagorean theorem.
Definition: lvecBase3.h:765
LPoint3 get_pos() const
Retrieves the translation component of the transform.
Definition: nodePath.cxx:1178
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:782
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
void update()
Each character's update will update its ai and physics based on his resultant steering force...
Definition: aiCharacter.cxx:47
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:981