Back to index

supertuxkart  0.5+dfsg1
moveable.cpp
Go to the documentation of this file.
00001 //  $Id: moveable.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004-2005 Steve Baker <sjbaker1@airmail.net>
00005 //  Copyright (C) 2006 Joerg Henrichs, Steve Baker
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 #include "world.hpp"
00022 #include "player_kart.hpp"
00023 #include "material_manager.hpp"
00024 #include "material.hpp"
00025 #include "user_config.hpp"
00026 #include "history.hpp"
00027 
00028 Moveable::Moveable (bool bHasHistory)
00029 {
00030     m_body            = 0;
00031     m_motion_state    = 0;
00032     m_shadow          = 0;
00033     m_first_time      = true ;
00034     m_model_transform = new ssgTransform();
00035 
00036     m_model_transform->ref();
00037 
00038     sgZeroVec3 ( m_reset_pos.xyz ) ; sgZeroVec3 ( m_reset_pos.hpr ) ;
00039 
00040     reset ();
00041     if(bHasHistory)
00042     {
00043         m_history_velocity = new sgCoord[history->GetSize()];
00044         m_history_position = new sgCoord[history->GetSize()];
00045     }
00046     else
00047     {
00048         m_history_velocity = NULL;
00049         m_history_position = NULL;
00050     }
00051 }   // Moveable
00052 
00053 //-----------------------------------------------------------------------------
00054 Moveable::~Moveable()
00055 {
00056     // The body is being removed from the world in kart/projectile
00057     if(m_body)         delete m_body;
00058     if(m_motion_state) delete m_motion_state;
00059     if(m_history_velocity)
00060     {
00061         delete [] m_history_velocity;
00062         delete [] m_history_position;
00063     }
00064     // FIXME what about model?
00065 }   // ~Moveable
00066 
00067 //-----------------------------------------------------------------------------
00068 void Moveable::reset ()
00069 {
00070     m_collided         = false;
00071     m_crashed          = false;
00072     m_material_hot     = NULL;
00073     m_normal_hot       = NULL;
00074     if(m_body) m_body->setLinearVelocity(btVector3(0.0, 0.0, 0.0));
00075     sgCopyCoord ( &m_curr_pos, &m_reset_pos ) ;
00076 
00077 }   // reset
00078 
00079 //-----------------------------------------------------------------------------
00080 void Moveable::createBody(float mass, btTransform& trans, 
00081                           btCollisionShape *shape) {
00082     
00083     btVector3 inertia;
00084     shape->calculateLocalInertia(mass, inertia);
00085     m_motion_state = new btDefaultMotionState(trans);
00086 
00087     btRigidBody::btRigidBodyConstructionInfo info(mass, m_motion_state, shape, inertia);
00088     info.m_restitution=0.5f;
00089 
00090     // Then create a rigid body
00091     // ------------------------
00092     m_body = new btRigidBody(info);
00093     // The value of user_pointer must be set from the actual class, otherwise this
00094     // is only a pointer to moveable, not to (say) kart, and virtual 
00095     // functions are not called correctly. So only init the pointer to zero.
00096     m_user_pointer.zero();
00097     m_body->setUserPointer(&m_user_pointer);
00098 }   // createBody
00099 
00100 //-----------------------------------------------------------------------------
00101 void Moveable::update (float dt)
00102 {
00103     if(m_history_velocity)
00104     {
00105         if(user_config->m_replay_history)
00106         {
00107             sgCoord tmp;
00108             sgCopyCoord(&tmp, &(m_history_velocity[history->GetCurrentIndex()]));
00109             //printf("m_velocity=%f,%f,%f,%f,%f,%f\n",
00110             //     m_velocity.xyz[0],m_velocity.xyz[1],m_velocity.xyz[2],
00111             //     m_velocity.hpr[0],m_velocity.hpr[1],m_velocity.hpr[2]);
00112             //printf("tmp     =%f,%f,%f,%f,%f,%f\n",
00113             //     tmp.xyz[0],tmp.xyz[1],tmp.xyz[2],
00114             //     tmp.hpr[0],tmp.hpr[1],tmp.hpr[2]);
00115 
00116 #undef IGNORE_Z_IN_HISTORY
00117 #ifdef IGNORE_Z_IN_HISTORY
00118             const float DUMMY=m_velocity.xyz[2];
00119             sgCopyCoord(&m_velocity, &tmp);
00120             m_velocity.xyz[2]=DUMMY;
00121 #else
00122             m_velocityLC.setValue(tmp.xyz[0],tmp.xyz[1],tmp.xyz[2]);
00123 #endif
00124         }
00125         else
00126         {
00127             m_history_velocity[history->GetCurrentIndex()].xyz[0]=m_velocityLC.getX();
00128             m_history_velocity[history->GetCurrentIndex()].xyz[1]=m_velocityLC.getY();
00129             m_history_velocity[history->GetCurrentIndex()].xyz[2]=m_velocityLC.getZ();
00130         }
00131     }   // if m_history_velocity
00132 
00133     if(m_history_position)
00134     {
00135         if(user_config->m_replay_history)
00136         {
00137             sgCoord tmp;
00138             sgCopyCoord(&tmp, &(m_history_position[history->GetCurrentIndex()]));
00139             //printf("m_curr_pos=%f,%f,%f,%f,%f,%f\n",
00140             //     m_curr_pos.xyz[0],m_curr_pos.xyz[1],m_curr_pos.xyz[2],
00141             //     m_curr_pos.hpr[0],m_curr_pos.hpr[1],m_curr_pos.hpr[2]);
00142             //printf("tmp     =%f,%f,%f,%f,%f,%f --> %d\n",
00143             //     tmp.xyz[0],tmp.xyz[1],tmp.xyz[2],
00144             //     tmp.hpr[0],tmp.hpr[1],tmp.hpr[2],
00145             //     history->GetCurrentIndex());
00146 
00147 #ifdef IGNORE_Z_IN_HISTORY
00148             const float DUMMY=m_curr_pos.xyz[2];
00149             sgCopyCoord(&m_curr_pos, &tmp);
00150             m_curr_pos.xyz[2]=DUMMY;
00151 #else
00152             sgCopyCoord(&m_curr_pos, &tmp);
00153 #endif
00154 
00155         }
00156         else
00157         {
00158             sgCopyCoord(&(m_history_position[history->GetCurrentIndex()]), &m_curr_pos);
00159         }
00160     }   // if m_history_position
00161 
00162     m_velocityLC = getVelocity()*getTrans().getBasis();
00163     placeModel();
00164     m_first_time = false ;
00165 }   // update
00166 
00167 //-----------------------------------------------------------------------------
00168 void Moveable::placeModel()
00169 {
00170     m_motion_state->getWorldTransform(m_transform);
00171     m_model_transform->setTransform(&m_curr_pos);
00172 }   // placeModel
00173 
00174 //-----------------------------------------------------------------------------
00175 void Moveable::WriteHistory(char* s, int kartNumber, int indx)
00176 {
00177     sprintf(s, "Kart %d: v=%f,%f,%f,%f,%f,%f, p=%f,%f,%f,%f,%f,%f", kartNumber,
00178             m_history_velocity[indx].xyz[0],
00179             m_history_velocity[indx].xyz[1],
00180             m_history_velocity[indx].xyz[2],
00181             m_history_velocity[indx].hpr[0],
00182             m_history_velocity[indx].hpr[1],
00183             m_history_velocity[indx].hpr[2],
00184             m_history_position[indx].xyz[0],
00185             m_history_position[indx].xyz[1],
00186             m_history_position[indx].xyz[2],
00187             m_history_position[indx].hpr[0],
00188             m_history_position[indx].hpr[1],
00189             m_history_position[indx].hpr[2]);
00190 }   // WriteHistory
00191 
00192 //-----------------------------------------------------------------------------
00193 void Moveable::ReadHistory(char* s, int kartNumber, int indx)
00194 {
00195     int k;
00196     sscanf(s, "Kart %d: v=%f,%f,%f,%f,%f,%f, p=%f,%f,%f,%f,%f,%f", &k,
00197            m_history_velocity[indx].xyz+0,
00198            m_history_velocity[indx].xyz+1,
00199            m_history_velocity[indx].xyz+2,
00200            m_history_velocity[indx].hpr+0,
00201            m_history_velocity[indx].hpr+1,
00202            m_history_velocity[indx].hpr+2,
00203            m_history_position[indx].xyz+0,
00204            m_history_position[indx].xyz+1,
00205            m_history_position[indx].xyz+2,
00206            m_history_position[indx].hpr+0,
00207            m_history_position[indx].hpr+1,
00208            m_history_position[indx].hpr+2);
00209     if(k!=kartNumber)
00210     {
00211         fprintf(stderr,"WARNING: tried reading data for kart %d, found:\n",
00212                 kartNumber);
00213         fprintf(stderr,"%s\n",s);
00214         exit(-2);
00215     }
00216 }   // ReadHistory