Back to index

supertuxkart  0.5+dfsg1
spark.cpp
Go to the documentation of this file.
00001 //  $Id: spark.cpp 1284 2007-11-08 12:31:54Z hikerstk $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2007 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "spark.hpp"
00021 float Spark::m_st_max_distance;   // maximum distance for a spark to be attracted
00022 float Spark::m_st_force_to_target;
00023 
00024 // -----------------------------------------------------------------------------
00025 Spark::Spark(Kart *kart) : Flyable(kart, COLLECT_SPARK)
00026 {
00027     float y_offset = -0.5f*kart->getKartLength()-2.0f*m_extend.getY();
00028     float speed    = -m_speed;
00029     // if the kart is driving backwards, release from the front
00030     if(m_owner->getSpeed()<0) 
00031     {
00032         y_offset = -y_offset;
00033         speed    = -speed;
00034     }
00035 
00036     createPhysics(y_offset, btVector3(0.0f, speed, 0.0f),
00037                   new btSphereShape(0.5f*m_extend.getY()));
00038 
00039     // unset no_contact_response flags, so that the spark 
00040     // will bounce off the track
00041     int flag = getBody()->getCollisionFlags();
00042     flag = flag & (~ btCollisionObject::CF_NO_CONTACT_RESPONSE);
00043     getBody()->setCollisionFlags(flag);
00044 }   // Spark
00045 
00046 // -----------------------------------------------------------------------------
00047 void Spark::init(const lisp::Lisp* lisp, ssgEntity *spark)
00048 {
00049     Flyable::init(lisp, spark, COLLECT_SPARK);
00050     m_st_max_distance    = 20.0f;
00051     m_st_force_to_target = 10.0f;
00052  
00053     lisp->get("max-distance",    m_st_max_distance   );
00054     lisp->get("force-to-target", m_st_force_to_target);
00055 }   // init
00056 
00057 // -----------------------------------------------------------------------------
00058 void Spark::update(float dt)
00059 {
00060     Flyable::update(dt);
00061     const Kart *kart=0;
00062     btVector3 direction;
00063     float minDistance;
00064     getClosestKart(&kart, &minDistance, &direction);
00065     if(minDistance<m_st_max_distance)   // move spark towards kart
00066     {
00067         direction*=1/direction.length()*m_st_force_to_target;
00068         m_body->applyCentralForce(direction);
00069     }
00070     else
00071     {   // Sparks lose energy (e.g. when hitting the track), so increase the
00072         // speed if the spark is too slow, but only if it's not too high (if
00073         // the spark is too high, it is 'pushed down', which can reduce the
00074         // speed, which causes the speed to increase, which in turn causes
00075         // the spark to fly higher and higher.
00076         btVector3 v=m_body->getLinearVelocity();
00077         btTransform trans=getTrans();
00078         float hat = trans.getOrigin().getZ();
00079         if (hat<= m_max_height)
00080         {
00081             float vlen = v.length2();
00082             if(vlen<0.8*m_speed*m_speed)
00083             {   // spark lost energy (less than 80%), i.e. it's too slow - speed it up:
00084                 if(vlen==0.0f) {
00085                     v    = btVector3(.5f, .5f, .5f);  // avoid 0 div.
00086                 }
00087                 else
00088                 {
00089                     m_body->setLinearVelocity(v*m_speed/sqrt(vlen));
00090                 }
00091             }   // vlen < 0.8*m_speed*m_speed
00092         }   // hat< m_max_height  
00093     }   // spar lose energy
00094 }   // update
00095 // -----------------------------------------------------------------------------