Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes
Homing Class Reference

#include <homing.hpp>

Inheritance diagram for Homing:
Inheritance graph
[legend]
Collaboration diagram for Homing:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Homing (Kart *kart)
 A homing missile is handled as a kinematic object, since this simplifies computation of turning (otherwise rotational forces would have to be applied).
virtual void update (float dt)
virtual void hitTrack ()
virtual const btVector3getVelocity () const
virtual void setVelocity (const btVector3 &v)
virtual void update (const btVector3 &pos)
void placeModel ()
void explode (Kart *kart, MovingPhysics *moving_physics=NULL)
bool hasHit ()
void reset ()
ssgTransform * getModelTransform ()
const btVector3getVelocityLC () const
sgCoord * getCoord ()
const sgCoord * getCoord () const
const btVector3 getPos () const
const sgVec4 * getNormalHOT () const
void setCoord (sgCoord *pos)
virtual void handleZipper ()
void WriteHistory (char *s, int kartNumber, int indx)
void ReadHistory (char *s, int kartNumber, int indx)
btRigidBodygetBody () const
void createBody (float mass, btTransform &trans, btCollisionShape *shape)
const btTransformgetTrans () const
void setTrans (btTransform &t)
float getHoT () const
const MaterialgetMaterial () const
const btVector3getNormal () const
float getTerrainPitch (float heading) const
 Returns the pitch of the terrain depending on the heading.

Static Public Member Functions

static void init (const lisp::Lisp *lisp, ssgEntity *homing)
static void init (const lisp::Lisp *lisp, ssgEntity *model, CollectableType type)

Protected Member Functions

void getClosestKart (const Kart **minKart, float *minDist, btVector3 *minDelta) const
void createPhysics (float y_offset, const btVector3 velocity, btCollisionShape *shape)

Protected Attributes

const Kartm_owner
btCollisionShapem_shape
float m_max_height
float m_min_height
float m_average_height
float m_force_updown
float m_speed
float m_mass
btVector3 m_extend
UserPointer m_user_pointer
sgCoord m_reset_pos
sgCoord m_curr_pos
sgVec4 * m_normal_hot
Materialm_material_hot
ssgTransform * m_model_transform
ssgTransform * m_shadow
int m_collided
int m_crashed
sgVec3 m_surface_avoidance_vector
int m_first_time
sgCoord * m_history_velocity
sgCoord * m_history_position
btRigidBodym_body
btDefaultMotionStatem_motion_state
btTransform m_transform

Static Protected Attributes

static float m_st_speed [COLLECT_MAX]
static ssgEntity * m_st_model [COLLECT_MAX]
static float m_st_min_height [COLLECT_MAX]
static float m_st_max_height [COLLECT_MAX]
static float m_st_force_updown [COLLECT_MAX]
static btVector3 m_st_extend [COLLECT_MAX]

Private Member Functions

float steerTowards (btTransform &trans, btVector3 &target)

Private Attributes

btVector3 m_initial_velocity

Static Private Attributes

static float m_st_max_distance
static float m_st_max_turn_angle

Detailed Description

Definition at line 25 of file homing.hpp.


Constructor & Destructor Documentation

Homing::Homing ( Kart kart)

A homing missile is handled as a kinematic object, since this simplifies computation of turning (otherwise rotational forces would have to be applied).

As a result, the mass must be zero, and linear velocity of the body can not be set (asserts in bullet). So this object implements its own setting/getting of velocity, to be able to use flyables functions.

Definition at line 33 of file homing.cpp.

                          : 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).
    float y_offset=kart->getKartLength()+2.0f*m_extend.getY();
    
    m_initial_velocity = btVector3(0.0f, m_speed, 0.0f);
    createPhysics(y_offset, m_initial_velocity, 
                  new btCylinderShape(0.5f*m_extend));
    m_body->setCollisionFlags(m_body->getCollisionFlags()           |
                              btCollisionObject::CF_KINEMATIC_OBJECT );
    m_body->setActivationState(DISABLE_DEACTIVATION);
}   // Homing

Here is the call graph for this function:


Member Function Documentation

void Moveable::createBody ( float  mass,
btTransform trans,
btCollisionShape shape 
) [inherited]

Definition at line 80 of file moveable.cpp.

                                                   {
    
    btVector3 inertia;
    shape->calculateLocalInertia(mass, inertia);
    m_motion_state = new btDefaultMotionState(trans);

    btRigidBody::btRigidBodyConstructionInfo info(mass, m_motion_state, shape, inertia);
    info.m_restitution=0.5f;

    // Then create a rigid body
    // ------------------------
    m_body = new btRigidBody(info);
    // The value of user_pointer must be set from the actual class, otherwise this
    // is only a pointer to moveable, not to (say) kart, and virtual 
    // functions are not called correctly. So only init the pointer to zero.
    m_user_pointer.zero();
    m_body->setUserPointer(&m_user_pointer);
}   // createBody

Here is the call graph for this function:

Here is the caller graph for this function:

void Flyable::createPhysics ( float  y_offset,
const btVector3  velocity,
btCollisionShape shape 
) [protected, inherited]

Definition at line 63 of file flyable.cpp.

{
    // The actual transform is determined as follows:
    // 1) Compute the heading of the kart
    // 2) Compute the pitch of the terrain. This avoids the problem of the
    //    rocket hitting the floor (e.g. if the kart is braking and therefore
    //    pointing downwards).
    btTransform trans = m_owner->getTrans();

    // get heading=trans.getBasis*(0,1,0) ... so save the multiplication:
    btVector3 direction(trans.getBasis()[0][1],
                        trans.getBasis()[1][1],
                        trans.getBasis()[2][1]);
    float heading=atan2(-direction.getX(), direction.getY());

    TerrainInfo::update(trans.getOrigin());
    float pitch = getTerrainPitch(heading);

    btMatrix3x3 m;
    m.setEulerZYX(pitch, 0.0f, heading);
    trans.setBasis(m);

    // Apply rotation and offset
    btTransform offset_transform;
    offset_transform.setIdentity();
    btVector3 offset=btVector3(0,y_offset,m_average_height);
    offset_transform.setOrigin(offset);
        
    trans  *= offset_transform;

    m_shape = shape;
    createBody(m_mass, trans, m_shape);
    m_user_pointer.set(this);
    world->getPhysics()->addBody(getBody());

    // Simplified rockets: no gravity
    m_body->setGravity(btVector3(0.0f, 0.0f, 0.0f));

    // Rotate velocity to point in the right direction
    btVector3 v=trans.getBasis()*velocity;

    if(m_mass!=0.0f)  // Don't set velocity for kinematic or static objects
    {
        m_body->setLinearVelocity(v);
        m_body->setAngularFactor(0.0f);   // prevent rotations
    }
    m_body->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);

}   // createPhysics

Here is the call graph for this function:

Here is the caller graph for this function:

void Flyable::explode ( Kart kart,
MovingPhysics moving_physics = NULL 
) [inherited]

Definition at line 201 of file flyable.cpp.

{
       if(m_exploded) return;

    m_has_hit_something=true;
    // Notify the projectile manager that this rocket has hit something.
    // The manager will create the appropriate explosion object.
    projectile_manager->explode();

    // Now remove this projectile from the graph:
    ssgTransform *m = getModelTransform();
    m->removeAllKids();
    scene->remove(m);

    // The explosion is a bit higher in the air
    btVector3 pos_explosion=getPos();
    pos_explosion.setZ(pos_explosion.getZ()+1.2f);
    world->getPhysics()->removeBody(getBody());
       m_exploded=true;

    for ( unsigned int i = 0 ; i < race_manager->getNumKarts() ; i++ )
    {
        Kart *kart = world->getKart(i);
        // Handle the actual explosion. The kart that fired a flyable will 
        // only be affected if it's a direct hit. This allows karts to use
        // rockets on short distance.
        if(m_owner!=kart || m_owner==kart_hit) 
        {
            // Set a flag it if was a direct hit.
            kart->handleExplosion(getPos(), kart==kart_hit);
        }
    }
    callback_manager->handleExplosion(pos_explosion, moving_physics);
}   // explode

Here is the call graph for this function:

Here is the caller graph for this function:

btRigidBody* Moveable::getBody ( ) const [inline, inherited]

Definition at line 77 of file moveable.hpp.

{return m_body; }

Here is the caller graph for this function:

void Flyable::getClosestKart ( const Kart **  minKart,
float *  minDist,
btVector3 minDelta 
) const [protected, inherited]

Definition at line 142 of file flyable.cpp.

{
    btTransform tProjectile=getTrans();
    *minDist = 99999.9f;
    for(unsigned int i=0 ; i<race_manager->getNumKarts(); i++ )
    {
        Kart *kart = world -> getKart(i);
        if(kart->isEliminated() || kart == m_owner) continue;
        btTransform t=kart->getTrans();
       
        btVector3 delta = t.getOrigin()-tProjectile.getOrigin();
        float distance2 = delta.length2();

        if(distance2 < *minDist)
        {
            *minDist  = sqrt(distance2);
            *minKart  = kart;
            *minDelta = delta;
        }
    }  // for i<getNumKarts
}   // getClosestKart

Here is the call graph for this function:

Here is the caller graph for this function:

sgCoord* Moveable::getCoord ( ) [inline, inherited]

Definition at line 66 of file moveable.hpp.

{return &m_curr_pos;                }

Here is the caller graph for this function:

const sgCoord* Moveable::getCoord ( ) const [inline, inherited]

Definition at line 68 of file moveable.hpp.

{return &m_curr_pos;                }
float TerrainInfo::getHoT ( ) const [inline, inherited]

Definition at line 44 of file terrain_info.hpp.

{ return m_HoT;      }

Here is the caller graph for this function:

const Material* TerrainInfo::getMaterial ( ) const [inline, inherited]

Definition at line 45 of file terrain_info.hpp.

{ return m_material; }

Here is the caller graph for this function:

ssgTransform* Moveable::getModelTransform ( ) [inline, inherited]

Definition at line 62 of file moveable.hpp.

{return m_model_transform;          }

Here is the caller graph for this function:

const btVector3& TerrainInfo::getNormal ( ) const [inline, inherited]

Definition at line 46 of file terrain_info.hpp.

{ return m_normal;   }
const sgVec4* Moveable::getNormalHOT ( ) const [inline, inherited]

Definition at line 69 of file moveable.hpp.

{return m_normal_hot;               }
const btVector3 Moveable::getPos ( ) const [inline, inherited]

Definition at line 67 of file moveable.hpp.

{return m_transform.getOrigin();    }

Here is the call graph for this function:

Here is the caller graph for this function:

float TerrainInfo::getTerrainPitch ( float  heading) const [inherited]

Returns the pitch of the terrain depending on the heading.

Definition at line 49 of file terrain_info.cpp.

                                                      {
    if(m_HoT==Track::NOHIT) return 0.0f;

    const float X =-sin(heading);
    const float Y = cos(heading);
    // Compute the angle between the normal of the plane and the line to
    // (x,y,0).  (x,y,0) is normalised, so are the coordinates of the plane,
    // simplifying the computation of the scalar product.
    float pitch = ( m_normal.getX()*X + m_normal.getY()*Y );  // use ( x,y,0)
        
    // The actual angle computed above is between the normal and the (x,y,0)
    // line, so to compute the actual angles 90 degrees must be subtracted.
    pitch = acosf(pitch) - NINETY_DEGREE_RAD;
    return pitch;
}   // getTerrainPitch

Here is the call graph for this function:

Here is the caller graph for this function:

const btTransform& Moveable::getTrans ( ) const [inline, inherited]

Definition at line 80 of file moveable.hpp.

{return m_transform;}

Here is the caller graph for this function:

virtual const btVector3& Homing::getVelocity ( ) const [inline, virtual]

Reimplemented from Moveable.

Definition at line 40 of file homing.hpp.

const btVector3& Moveable::getVelocityLC ( ) const [inline, inherited]

Definition at line 64 of file moveable.hpp.

{return m_velocityLC;               }

Here is the caller graph for this function:

virtual void Moveable::handleZipper ( ) [inline, virtual, inherited]

Reimplemented in Kart, and PlayerKart.

Definition at line 72 of file moveable.hpp.

{};
bool Flyable::hasHit ( ) [inline, inherited]

Definition at line 72 of file flyable.hpp.

{ return m_has_hit_something; }
virtual void Homing::hitTrack ( ) [inline, virtual]

Reimplemented from Flyable.

Definition at line 37 of file homing.hpp.

{ explode(NULL);           }

Here is the call graph for this function:

void Homing::init ( const lisp::Lisp lisp,
ssgEntity *  homing 
) [static]

Definition at line 52 of file homing.cpp.

{
    Flyable::init(lisp, homing, COLLECT_HOMING);
    m_st_max_turn_angle = 15.0f;
    m_st_max_distance   = 20.0f;
    lisp->get("max-distance",    m_st_max_distance  );
    lisp->get("max-turn-angle",  m_st_max_turn_angle);
}   // init

Here is the call graph for this function:

Here is the caller graph for this function:

void Flyable::init ( const lisp::Lisp lisp,
ssgEntity *  model,
CollectableType  type 
) [static, inherited]

Definition at line 115 of file flyable.cpp.

{
    m_st_speed[type]        = 25.0f;
    m_st_max_height[type]   = 1.0f;
    m_st_min_height[type]   = 3.0f;
    m_st_force_updown[type] = 15.0f;
    lisp->get("speed",           m_st_speed[type]       );
    lisp->get("min-height",      m_st_min_height[type]  );
    lisp->get("max-height",      m_st_max_height[type]  );
    lisp->get("force-updown",    m_st_force_updown[type]);

    // Store the size of the model
    float x_min, x_max, y_min, y_max, z_min, z_max;
    MinMax(model, &x_min, &x_max, &y_min, &y_max, &z_min, &z_max);
    m_st_extend[type] = btVector3(x_max-x_min,y_max-y_min, z_max-z_min);
    m_st_model[type]  = model;
}   // init

Here is the call graph for this function:

void Flyable::placeModel ( ) [virtual, inherited]

Reimplemented from Moveable.

Definition at line 191 of file flyable.cpp.

{
       btTransform t=getTrans();
    float m[4][4];
    t.getOpenGLMatrix((float*)&m);
    sgSetCoord(&m_curr_pos, m);
    Moveable::placeModel();
}  // placeModel

Here is the call graph for this function:

void Moveable::ReadHistory ( char *  s,
int  kartNumber,
int  indx 
) [inherited]

Definition at line 193 of file moveable.cpp.

{
    int k;
    sscanf(s, "Kart %d: v=%f,%f,%f,%f,%f,%f, p=%f,%f,%f,%f,%f,%f", &k,
           m_history_velocity[indx].xyz+0,
           m_history_velocity[indx].xyz+1,
           m_history_velocity[indx].xyz+2,
           m_history_velocity[indx].hpr+0,
           m_history_velocity[indx].hpr+1,
           m_history_velocity[indx].hpr+2,
           m_history_position[indx].xyz+0,
           m_history_position[indx].xyz+1,
           m_history_position[indx].xyz+2,
           m_history_position[indx].hpr+0,
           m_history_position[indx].hpr+1,
           m_history_position[indx].hpr+2);
    if(k!=kartNumber)
    {
        fprintf(stderr,"WARNING: tried reading data for kart %d, found:\n",
                kartNumber);
        fprintf(stderr,"%s\n",s);
        exit(-2);
    }
}   // ReadHistory

Here is the caller graph for this function:

void Flyable::reset ( ) [inline, virtual, inherited]

Reimplemented from Moveable.

Definition at line 73 of file flyable.hpp.

                                { Moveable::reset();
                                  sgCopyCoord(&m_last_pos,&m_reset_pos );   }
void Moveable::setCoord ( sgCoord *  pos) [inline, inherited]

Definition at line 70 of file moveable.hpp.

{sgCopyCoord ( &m_curr_pos,pos);    }
void Moveable::setTrans ( btTransform t) [inline, inherited]

Definition at line 81 of file moveable.hpp.

Here is the call graph for this function:

Here is the caller graph for this function:

virtual void Homing::setVelocity ( const btVector3 v) [inline, virtual]

Reimplemented from Moveable.

Definition at line 41 of file homing.hpp.

float Homing::steerTowards ( btTransform trans,
btVector3 target 
) [private]

Definition at line 94 of file homing.cpp.

{
    btMatrix3x3 m(trans.getBasis());
    btVector3 forwards(0.f,1.f,0.0f);
    btVector3 direction=m*forwards;
    float heading = RAD_TO_DEGREE(atan2(direction.getY(),direction.getX()));

    btVector3 pos=trans.getOrigin();
    float angle = RAD_TO_DEGREE(atan2(target.getY()-pos.getY(), target.getX()-pos.getX()));
    angle -=heading;
    if(angle> 180.0f) angle=angle-360.0f;
    if(angle<-180.0f) angle=angle+360.0f;
    return angle;
}   // steerTowards

Here is the call graph for this function:

Here is the caller graph for this function:

void Homing::update ( float  dt) [virtual]

Reimplemented from Flyable.

Definition at line 62 of file homing.cpp.

{
    Flyable::update(dt);

    const Kart *kart=0;
    btVector3 direction;
    float minDistance;

    getClosestKart(&kart, &minDistance, &direction);
    btTransform my_trans=getTrans();
    if(minDistance<m_st_max_distance)   // move homing towards kart
    {
        btTransform target=kart->getTrans();
        
        float steer=steerTowards(my_trans, target.getOrigin());
        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
        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);
    }   // minDistance<m_st_max_distance
    btVector3 v =my_trans.getBasis()*m_initial_velocity;
    my_trans.setOrigin(my_trans.getOrigin()+dt*v);
    setTrans(my_trans);
}   // update

Here is the call graph for this function:

void TerrainInfo::update ( const btVector3 pos) [virtual, inherited]

Definition at line 34 of file terrain_info.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void Moveable::WriteHistory ( char *  s,
int  kartNumber,
int  indx 
) [inherited]

Definition at line 175 of file moveable.cpp.

{
    sprintf(s, "Kart %d: v=%f,%f,%f,%f,%f,%f, p=%f,%f,%f,%f,%f,%f", kartNumber,
            m_history_velocity[indx].xyz[0],
            m_history_velocity[indx].xyz[1],
            m_history_velocity[indx].xyz[2],
            m_history_velocity[indx].hpr[0],
            m_history_velocity[indx].hpr[1],
            m_history_velocity[indx].hpr[2],
            m_history_position[indx].xyz[0],
            m_history_position[indx].xyz[1],
            m_history_position[indx].xyz[2],
            m_history_position[indx].hpr[0],
            m_history_position[indx].hpr[1],
            m_history_position[indx].hpr[2]);
}   // WriteHistory

Here is the caller graph for this function:


Member Data Documentation

float Flyable::m_average_height [protected, inherited]

Definition at line 40 of file flyable.hpp.

btRigidBody* Moveable::m_body [protected, inherited]

Definition at line 53 of file moveable.hpp.

int Moveable::m_collided [protected, inherited]

Definition at line 47 of file moveable.hpp.

int Moveable::m_crashed [protected, inherited]

Definition at line 48 of file moveable.hpp.

sgCoord Moveable::m_curr_pos [protected, inherited]

Definition at line 42 of file moveable.hpp.

btVector3 Flyable::m_extend [protected, inherited]

Definition at line 44 of file flyable.hpp.

int Moveable::m_first_time [protected, inherited]

Definition at line 50 of file moveable.hpp.

float Flyable::m_force_updown [protected, inherited]

Definition at line 41 of file flyable.hpp.

sgCoord* Moveable::m_history_position [protected, inherited]

Definition at line 52 of file moveable.hpp.

sgCoord* Moveable::m_history_velocity [protected, inherited]

Definition at line 51 of file moveable.hpp.

Definition at line 30 of file homing.hpp.

float Flyable::m_mass [protected, inherited]

Definition at line 43 of file flyable.hpp.

Material* Moveable::m_material_hot [protected, inherited]

Definition at line 44 of file moveable.hpp.

float Flyable::m_max_height [protected, inherited]

Definition at line 38 of file flyable.hpp.

float Flyable::m_min_height [protected, inherited]

Definition at line 39 of file flyable.hpp.

ssgTransform* Moveable::m_model_transform [protected, inherited]

Definition at line 45 of file moveable.hpp.

Definition at line 54 of file moveable.hpp.

sgVec4* Moveable::m_normal_hot [protected, inherited]

Definition at line 43 of file moveable.hpp.

const Kart* Flyable::m_owner [protected, inherited]

Definition at line 36 of file flyable.hpp.

sgCoord Moveable::m_reset_pos [protected, inherited]

Definition at line 41 of file moveable.hpp.

ssgTransform* Moveable::m_shadow [protected, inherited]

Definition at line 46 of file moveable.hpp.

btCollisionShape* Flyable::m_shape [protected, inherited]

Definition at line 37 of file flyable.hpp.

float Flyable::m_speed [protected, inherited]

Definition at line 42 of file flyable.hpp.

btVector3 Flyable::m_st_extend [static, protected, inherited]

Definition at line 54 of file flyable.hpp.

float Flyable::m_st_force_updown [static, protected, inherited]

Definition at line 53 of file flyable.hpp.

float Homing::m_st_max_distance [static, private]

Definition at line 28 of file homing.hpp.

float Flyable::m_st_max_height [static, protected, inherited]

Definition at line 52 of file flyable.hpp.

float Homing::m_st_max_turn_angle [static, private]

Definition at line 29 of file homing.hpp.

float Flyable::m_st_min_height [static, protected, inherited]

Definition at line 51 of file flyable.hpp.

ssgEntity * Flyable::m_st_model [static, protected, inherited]

Definition at line 50 of file flyable.hpp.

float Flyable::m_st_speed [static, protected, inherited]

Definition at line 49 of file flyable.hpp.

sgVec3 Moveable::m_surface_avoidance_vector [protected, inherited]

Definition at line 49 of file moveable.hpp.

btTransform Moveable::m_transform [protected, inherited]

Definition at line 55 of file moveable.hpp.

UserPointer Moveable::m_user_pointer [protected, inherited]

Definition at line 40 of file moveable.hpp.


The documentation for this class was generated from the following files: