Panda3D

aiCharacter.cxx

00001 ////////////////////////////////////////////////////////////////////////
00002 // Filename    :  aiCharacter.cxx
00003 // Created by  :  Deepak, John, Navin
00004 // Date        :  8 Sep 09
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #include "aiCharacter.h"
00017 
00018 AICharacter::AICharacter(string model_name, NodePath model_np, double mass, double movt_force, double max_force) {
00019   _name = model_name;
00020   _ai_char_np = model_np;
00021 
00022   _mass = mass;
00023   _max_force = max_force;
00024   _movt_force = movt_force;
00025 
00026   _velocity = LVecBase3f(0.0, 0.0, 0.0);
00027   _steering_force = LVecBase3f(0.0, 0.0, 0.0);
00028 
00029   _steering = new AIBehaviors();
00030   _steering->_ai_char = this;
00031 
00032   _pf_guide = false;
00033 }
00034 
00035 AICharacter::~AICharacter() {
00036 }
00037 
00038 /////////////////////////////////////////////////////////////////////////////////////////
00039 //
00040 // Function : update
00041 // Description : Each character's update will update its ai and physics
00042 //                based on his resultant steering force.
00043 //                This also makes the character  look at the direction of the force.
00044 
00045 /////////////////////////////////////////////////////////////////////////////////////////
00046 
00047 void AICharacter::update() {
00048 
00049   if(!_steering->is_off(_steering->_none)) {
00050 
00051     LVecBase3f old_pos = _ai_char_np.get_pos();
00052 
00053     LVecBase3f steering_force = _steering->calculate_prioritized();
00054 
00055     LVecBase3f acceleration = steering_force / _mass;
00056 
00057     _velocity = acceleration;
00058 
00059     LVecBase3f direction = _steering->_steering_force;
00060     direction.normalize();
00061 
00062     _ai_char_np.set_pos(old_pos + _velocity) ;
00063 
00064     if(steering_force.length() > 0) {
00065       _ai_char_np.look_at(old_pos + (direction * 5));
00066       _ai_char_np.set_h(_ai_char_np.get_h() + 180);
00067       _ai_char_np.set_p(-_ai_char_np.get_p());
00068       _ai_char_np.set_r(-_ai_char_np.get_r());
00069     }
00070   }
00071   else {
00072     _steering->_steering_force = LVecBase3f(0.0, 0.0, 0.0);
00073     _steering->_seek_force = LVecBase3f(0.0, 0.0, 0.0);
00074     _steering->_flee_force = LVecBase3f(0.0, 0.0, 0.0);
00075     _steering->_pursue_force = LVecBase3f(0.0, 0.0, 0.0);
00076     _steering->_evade_force = LVecBase3f(0.0, 0.0, 0.0);
00077     _steering->_arrival_force = LVecBase3f(0.0, 0.0, 0.0);
00078     _steering->_flock_force = LVecBase3f(0.0, 0.0, 0.0);
00079     _steering->_wander_force = LVecBase3f(0.0, 0.0, 0.0);
00080   }
00081 }
00082 
00083 LVecBase3f AICharacter::get_velocity() {
00084   return _velocity;
00085 }
00086 
00087 void AICharacter::set_velocity(LVecBase3f velocity) {
00088   _velocity = velocity;
00089 }
00090 
00091 double AICharacter::get_mass() {
00092   return _mass;
00093 }
00094 
00095 void AICharacter::set_mass(double m) {
00096   _mass = m;
00097 }
00098 
00099 double AICharacter::get_max_force() {
00100   return _max_force;
00101 }
00102 
00103 void AICharacter::set_max_force(double max_force) {
00104   _max_force = max_force;
00105 }
00106 
00107 NodePath AICharacter::get_node_path() {
00108   return _ai_char_np;
00109 }
00110 
00111 void AICharacter::set_node_path(NodePath np) {
00112   _ai_char_np = np;
00113 }
00114 
00115 AIBehaviors * AICharacter::get_ai_behaviors() {
00116   return _steering;
00117 }
00118 
00119 void AICharacter::set_char_render(NodePath render) {
00120   _window_render = render;
00121 }
00122 
00123 NodePath AICharacter::get_char_render() {
00124   return _window_render;
00125 }
00126 
00127 void AICharacter::set_pf_guide(bool pf_guide) {
00128   _pf_guide = pf_guide;
00129 }
 All Classes Functions Variables Enumerations