Some fine-tuning to bowling balls. Optimized code by removing repetitive calls to sqrt().

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2200 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2008-08-06 00:53:40 +00:00
parent 5791464b84
commit ec26ddb1ed
6 changed files with 44 additions and 24 deletions

View File

@ -11,9 +11,9 @@
; started to be pulled back to ground ; started to be pulled back to ground
(force-updown 1.0) ; force pushing the spark down (force-updown 1.0) ; force pushing the spark down
; when it's too high above ground ; when it's too high above ground
(force-to-target 20) ; force with which a spark flies towards (force-to-target 15) ; force with which a spark flies towards
; the nearest kart ; the nearest kart
(max-distance 10) ; maximum distance the spark can be away (max-distance 25) ; maximum distance the spark can be away
; from a kart when accelerating towards it ; from a kart when accelerating towards it
) )

View File

@ -22,6 +22,7 @@
#include "camera.hpp" #include "camera.hpp"
float Bowling::m_st_max_distance; // maximum distance for a bowling ball to be attracted float Bowling::m_st_max_distance; // maximum distance for a bowling ball to be attracted
float Bowling::m_st_max_distance_squared;
float Bowling::m_st_force_to_target; float Bowling::m_st_force_to_target;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -59,10 +60,13 @@ Bowling::Bowling(Kart *kart) : Flyable(kart, COLLECT_BOWLING)
void Bowling::init(const lisp::Lisp* lisp, ssgEntity *bowling) void Bowling::init(const lisp::Lisp* lisp, ssgEntity *bowling)
{ {
Flyable::init(lisp, bowling, COLLECT_BOWLING); Flyable::init(lisp, bowling, COLLECT_BOWLING);
m_st_max_distance = 35.0f; m_st_max_distance = 20.0f;
m_st_max_distance_squared = 20.0f * 20.0f;
m_st_force_to_target = 10.0f; m_st_force_to_target = 10.0f;
lisp->get("max-distance", m_st_max_distance ); lisp->get("max-distance", m_st_max_distance );
m_st_max_distance_squared = m_st_max_distance*m_st_max_distance;
lisp->get("force-to-target", m_st_force_to_target); lisp->get("force-to-target", m_st_force_to_target);
} // init } // init
@ -74,10 +78,15 @@ void Bowling::update(float dt)
btVector3 direction; btVector3 direction;
float minDistance; float minDistance;
getClosestKart(&kart, &minDistance, &direction); getClosestKart(&kart, &minDistance, &direction);
if(minDistance<m_st_max_distance) // move bowling towards kart if(minDistance<m_st_max_distance_squared) // move bowling towards kart
{ {
direction*=1/direction.length()*m_st_force_to_target; // limit angle, so that the bowling ball does not turn
m_body->applyCentralForce(direction); // around to hit a kart behind
if(abs(m_body->getLinearVelocity().angle(direction)) < 1.3)
{
direction*=1/direction.length()*m_st_force_to_target;
m_body->applyCentralForce(direction);
}
} }
else else
{ // Bowling balls lose energy (e.g. when hitting the track), so increase { // Bowling balls lose energy (e.g. when hitting the track), so increase

View File

@ -26,6 +26,7 @@ class Bowling : public Flyable
{ {
private: private:
static float m_st_max_distance; // maximum distance for a bowling ball to be attracted static float m_st_max_distance; // maximum distance for a bowling ball to be attracted
static float m_st_max_distance_squared;
static float m_st_force_to_target; static float m_st_force_to_target;
public: public:

View File

@ -123,10 +123,12 @@ Flyable::~Flyable()
} // ~Flyable } // ~Flyable
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Flyable::getClosestKart(const Kart **minKart, float *minDist, btVector3 *minDelta) const void Flyable::getClosestKart(const Kart **minKart, float *minDistSquared, btVector3 *minDelta) const
{ {
btTransform tProjectile=getTrans(); btTransform tProjectile=getTrans();
*minDist = 99999.9f;
*minDistSquared = -1.0f;
for(unsigned int i=0 ; i<race_manager->getNumKarts(); i++ ) for(unsigned int i=0 ; i<race_manager->getNumKarts(); i++ )
{ {
Kart *kart = world -> getKart(i); Kart *kart = world -> getKart(i);
@ -136,13 +138,14 @@ void Flyable::getClosestKart(const Kart **minKart, float *minDist, btVector3 *mi
btVector3 delta = t.getOrigin()-tProjectile.getOrigin(); btVector3 delta = t.getOrigin()-tProjectile.getOrigin();
float distance2 = delta.length2(); float distance2 = delta.length2();
if(distance2 < *minDist) if(distance2 < *minDistSquared || *minDistSquared < 0 /* not yet set */)
{ {
*minDist = sqrt(distance2); *minDistSquared = distance2;
*minKart = kart; *minKart = kart;
*minDelta = delta; *minDelta = delta;
} }
} // for i<getNumKarts } // for i<getNumKarts
} // getClosestKart } // getClosestKart
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -21,6 +21,7 @@
#include "constants.hpp" #include "constants.hpp"
float Homing::m_st_max_distance; float Homing::m_st_max_distance;
float Homing::m_st_max_distance_squared;
float Homing::m_st_max_turn_angle; float Homing::m_st_max_turn_angle;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -54,7 +55,11 @@ void Homing::init(const lisp::Lisp* lisp, ssgEntity *homing)
Flyable::init(lisp, homing, COLLECT_HOMING); Flyable::init(lisp, homing, COLLECT_HOMING);
m_st_max_turn_angle = 15.0f; m_st_max_turn_angle = 15.0f;
m_st_max_distance = 20.0f; m_st_max_distance = 20.0f;
m_st_max_distance_squared = 20.0f * 20.0f;
lisp->get("max-distance", m_st_max_distance ); lisp->get("max-distance", m_st_max_distance );
m_st_max_distance_squared = m_st_max_distance*m_st_max_distance;
lisp->get("max-turn-angle", m_st_max_turn_angle); lisp->get("max-turn-angle", m_st_max_turn_angle);
} // init } // init
@ -69,7 +74,7 @@ void Homing::update(float dt)
getClosestKart(&kart, &minDistance, &direction); getClosestKart(&kart, &minDistance, &direction);
btTransform my_trans=getTrans(); btTransform my_trans=getTrans();
if(minDistance<m_st_max_distance) // move homing towards kart if(minDistance<m_st_max_distance_squared) // move homing towards kart
{ {
btTransform target=kart->getTrans(); btTransform target=kart->getTrans();

View File

@ -26,7 +26,9 @@ class Homing : public Flyable
{ {
private: private:
static float m_st_max_distance; // maximum distance for a missile to be attracted static float m_st_max_distance; // maximum distance for a missile to be attracted
static float m_st_max_distance_squared;
static float m_st_max_turn_angle; static float m_st_max_turn_angle;
btVector3 m_initial_velocity; btVector3 m_initial_velocity;
float steerTowards(btTransform& trans, btVector3& target); float steerTowards(btTransform& trans, btVector3& target);