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
(force-updown 1.0) ; force pushing the spark down
; 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
(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
)

View File

@ -22,6 +22,7 @@
#include "camera.hpp"
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;
// -----------------------------------------------------------------------------
@ -59,10 +60,13 @@ Bowling::Bowling(Kart *kart) : Flyable(kart, COLLECT_BOWLING)
void Bowling::init(const lisp::Lisp* lisp, ssgEntity *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;
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);
} // init
@ -74,10 +78,15 @@ void Bowling::update(float dt)
btVector3 direction;
float minDistance;
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;
m_body->applyCentralForce(direction);
// limit angle, so that the bowling ball does not turn
// 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
{ // Bowling balls lose energy (e.g. when hitting the track), so increase

View File

@ -26,8 +26,9 @@ class Bowling : public Flyable
{
private:
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;
public:
Bowling(Kart* kart);
static void init(const lisp::Lisp* lisp, ssgEntity* bowling);

View File

@ -123,10 +123,12 @@ 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();
*minDist = 99999.9f;
*minDistSquared = -1.0f;
for(unsigned int i=0 ; i<race_manager->getNumKarts(); 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();
float distance2 = delta.length2();
if(distance2 < *minDist)
if(distance2 < *minDistSquared || *minDistSquared < 0 /* not yet set */)
{
*minDist = sqrt(distance2);
*minDistSquared = distance2;
*minKart = kart;
*minDelta = delta;
}
} // for i<getNumKarts
} // getClosestKart
//-----------------------------------------------------------------------------

View File

@ -21,6 +21,7 @@
#include "constants.hpp"
float Homing::m_st_max_distance;
float Homing::m_st_max_distance_squared;
float Homing::m_st_max_turn_angle;
// -----------------------------------------------------------------------------
@ -31,13 +32,13 @@ float Homing::m_st_max_turn_angle;
* own setting/getting of velocity, to be able to use flyables functions.
*/
Homing::Homing (Kart *kart) : Flyable(kart, COLLECT_HOMING)
{
// A bit of a hack: the mass of this kinematic object is still 1.0
// (see flyable), which enables collisions. I tried setting
// collisionFilterGroup/mask, but still couldn't get this object to
// collide with the track. By setting the mass to 1, collisions happen.
// (if bullet is compiled with _DEBUG, a warning will be printed the first
// time a homing-track collision happens).
{
// A bit of a hack: the mass of this kinematic object is still 1.0
// (see flyable), which enables collisions. I tried setting
// collisionFilterGroup/mask, but still couldn't get this object to
// collide with the track. By setting the mass to 1, collisions happen.
// (if bullet is compiled with _DEBUG, a warning will be printed the first
// time a homing-track collision happens).
float y_offset=kart->getKartLength()+2.0f*m_extend.getY();
m_initial_velocity = btVector3(0.0f, m_speed, 0.0f);
@ -54,7 +55,11 @@ void Homing::init(const lisp::Lisp* lisp, ssgEntity *homing)
Flyable::init(lisp, homing, COLLECT_HOMING);
m_st_max_turn_angle = 15.0f;
m_st_max_distance = 20.0f;
m_st_max_distance_squared = 20.0f * 20.0f;
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);
} // init
@ -69,7 +74,7 @@ void Homing::update(float dt)
getClosestKart(&kart, &minDistance, &direction);
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();
@ -77,11 +82,11 @@ void Homing::update(float dt)
if(fabsf(steer)>90.0f) steer=0.0f;
if(steer<-m_st_max_turn_angle) steer = -m_st_max_turn_angle;
if(steer> m_st_max_turn_angle) steer = m_st_max_turn_angle;
// The steering must be interpreted as grad/s (otherwise this would
// depend on the frame rate). But this would usually miss the karts,
// since the angle isn't adjusted quickly enough when coming closer
// So we allow for (much) larger steering angles the closer the
// kart is by multiplying the rotation/sec with max_distance/minDistance
// The steering must be interpreted as grad/s (otherwise this would
// depend on the frame rate). But this would usually miss the karts,
// since the angle isn't adjusted quickly enough when coming closer
// So we allow for (much) larger steering angles the closer the
// kart is by multiplying the rotation/sec with max_distance/minDistance
steer *=(dt*m_st_max_distance/minDistance);
btMatrix3x3 steerMatrix(btQuaternion(0.0f,0.0f,DEGREE_TO_RAD(steer)));
my_trans.setBasis(my_trans.getBasis()*steerMatrix);

View File

@ -26,7 +26,9 @@ class Homing : public Flyable
{
private:
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;
btVector3 m_initial_velocity;
float steerTowards(btTransform& trans, btVector3& target);