item intersection uses y_offset, hat initialised properly and some redundant code removed.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3850 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
davemk 2009-08-13 12:12:22 +00:00
parent 1033bfdf59
commit c27a807e64
4 changed files with 83 additions and 105 deletions

@ -48,7 +48,7 @@ Cake::Cake (Kart *kart) : Flyable(kart, POWERUP_CAKE)
// give a speed proportional to kart speed
m_speed = kart->getSpeed() * m_speed / 23.0f;
if (kart->getSpeed() < 0)
m_speed /= 3.5f; //when going backwards, decrease speed of cake by less
m_speed /= 3.6f; //when going backwards, decrease speed of cake by less
m_speed += 16.0f;
@ -80,14 +80,10 @@ Cake::Cake (Kart *kart) : Flyable(kart, POWERUP_CAKE)
float fire_angle = 0.0f;
float time_estimated = 0.0f;
getLinearKartItemIntersection (kart->getTrans().getOrigin(), closest_kart,
m_speed, m_gravity,
&fire_angle, &up_velocity, &time_estimated);
btMatrix3x3 thisKartDirMatrix = kart->getKartHeading().getBasis();
btVector3 thisKartDirVector(thisKartDirMatrix[0][1],
thisKartDirMatrix[1][1],
thisKartDirMatrix[2][1]);
getLinearKartItemIntersection (kart->getTrans().getOrigin(), closest_kart,
m_speed, m_gravity, y_offset,
&fire_angle, &up_velocity, &time_estimated);
// apply transformation to the bullet object (without pitch)
btMatrix3x3 m;
@ -108,6 +104,9 @@ Cake::Cake (Kart *kart) : Flyable(kart, POWERUP_CAKE)
new btCylinderShape(0.5f*m_extend), -m_gravity,
true /* rotation */, false /* backwards */, &trans);
//do not adjust height according to terrain
setAdjustZVelocity(false);
m_body->setActivationState(DISABLE_DEACTIVATION);
m_body->applyTorque( btVector3(5,-3,7) );
@ -133,43 +132,31 @@ void Cake::init(const lisp::Lisp* lisp, scene::IMesh *cake_model)
// -----------------------------------------------------------------------------
void Cake::update(float dt)
{
//The following commented out code adds a lock on to the cake. It is kept
//because it shows how to lock on to a moving target precisely with the
//intersection algorithm and may be one day useful for something else.
/*
if(m_target != NULL)
{
/*
// correct direction to go towards aimed kart
btTransform my_trans = getTrans();
btTransform target = m_target->getTrans();
float fire_angle = 0.0f;
float time_estimated = 0.0f;
float up_velocity = 0.0f;
getLinearKartItemIntersection (my_trans.getOrigin(), m_target,
m_speed, m_gravity,
btVector3 origin = my_trans.getOrigin() - m_target->getNormal() * 0.5 * m_target->getKartHeight();
getLinearKartItemIntersection (origin, m_target,
m_speed, m_gravity, 0,
&fire_angle, &up_velocity, &time_estimated);
m_body->setLinearVelocity( btVector3(-m_speed * sinf (fire_angle),
m_speed * cosf (fire_angle),
up_velocity) );
*/
/*
// pull towards aimed kart
btVector3 pullForce = target.getOrigin() - my_trans.getOrigin();
pullForce.setZ(0);
pullForce.normalize();
pullForce *= 10;
m_body->applyCentralImpulse( pullForce );
*/
/*
// if over aimed kart, pull down
if(fabsf(my_trans.getOrigin().getX() - target.getOrigin().getX()) < 5.0 &&
fabsf(my_trans.getOrigin().getY() - target.getOrigin().getY()) < 5.0)
{
m_body->applyCentralForce( btVector3(0, 0, -20.0f) );
}
*/
}
*/
Flyable::update(dt);
} // update

@ -189,49 +189,52 @@ void Flyable::getClosestKart(const Kart **minKart, float *minDistSquared,
//-----------------------------------------------------------------------------
void Flyable::getLinearKartItemIntersection (const btVector3 origin, const Kart *target_kart,
float item_XY_speed, float gravity,
float item_XY_speed, float gravity, float y_offset,
float *fire_angle, float *up_velocity, float *time_estimated)
{
btVector3 targetKartLoc = target_kart->getTrans().getOrigin();
float dx = targetKartLoc.getX() - origin.getX();
float dy = targetKartLoc.getY() - origin.getY();
float dz = targetKartLoc.getZ() - origin.getZ();
btVector3 relative_target_kart_loc = target_kart->getTrans().getOrigin() - origin;
btTransform trans = target_kart->getTrans();
Vec3 target_direction(trans.getBasis()[0][1],
trans.getBasis()[1][1],
trans.getBasis()[2][1]);
btVector3 target_direction(trans.getBasis()[0][1],
trans.getBasis()[1][1],
trans.getBasis()[2][1]);
float dx = relative_target_kart_loc.getX();
float dy = relative_target_kart_loc.getY();
float dz = relative_target_kart_loc.getZ();
float gx = target_direction.getX();
float gy = target_direction.getY();
float gz = target_direction.getZ();
float target_kart_speed = target_direction.length_2d() * target_kart->getSpeed(); //Projected onto X-Y plane
float target_kart_speed = hypotf(gx, gy) * target_kart->getSpeed(); //Projected onto X-Y plane
float target_kart_heading = atan2f(-gx, gy); //anti-clockwise
target_kart_heading += M_PI;
float dist = (target_kart_speed / item_XY_speed) * (dx * cosf(target_kart_heading) + dy * sinf(target_kart_heading));
float dist = -(target_kart_speed / item_XY_speed) * (dx * cosf(target_kart_heading) + dy * sinf(target_kart_heading));
float fire_th = (dx*dist - dy * sqrtf(dx*dx + dy*dy - dist*dist)) / (dx*dx + dy*dy);
fire_th = (((dist - dx*fire_th) / dy < 0) ? -acosf(fire_th): acosf(fire_th));
float time = 0.0f;
float a = item_XY_speed * sinf (fire_th) - target_kart_speed * sinf (target_kart_heading);
float b = item_XY_speed * cosf (fire_th) - target_kart_speed * cosf (target_kart_heading);
float a = item_XY_speed * sinf (fire_th) + target_kart_speed * sinf (target_kart_heading);
float b = item_XY_speed * cosf (fire_th) + target_kart_speed * cosf (target_kart_heading);
if (fabsf(a) > fabsf(b))
time = fabsf (dx / a);
else if (b != 0.0f)
time = fabsf(dy / b);
fire_th += M_PI;
if (fire_th > M_PI)
fire_th -= M_PI;
else
fire_th += M_PI;
//createPhysics offset
time -= y_offset / hypotf(a, b);
*fire_angle = fire_th;
*up_velocity = (0.5f * time * gravity) + (dz / time) + (gz * target_kart->getSpeed());
*up_velocity = (0.5 * time * gravity) + (dz / time) + (gz * target_kart->getSpeed());
*time_estimated = time;
}

@ -96,7 +96,7 @@ protected:
XY-plane.
*/
void getLinearKartItemIntersection(const btVector3 origin, const Kart *target_kart,
float item_XY_velocity, float gravity,
float item_XY_velocity, float gravity, float y_offset,
float *fire_angle, float *up_velocity, float *time);

@ -65,7 +65,7 @@ Plunger::Plunger(Kart *kart) : Flyable(kart, POWERUP_PLUNGER)
float fire_angle = 0.0f;
float time_estimated = 0.0f;
getLinearKartItemIntersection (kart->getTrans().getOrigin(), closest_kart,
plunger_speed, gravity,
plunger_speed, gravity, y_offset,
&fire_angle, &up_velocity, &time_estimated);
// apply transformation to the bullet object (without pitch)
@ -86,6 +86,9 @@ Plunger::Plunger(Kart *kart) : Flyable(kart, POWERUP_PLUNGER)
new btCylinderShape(0.5f*m_extend), gravity, false /* rotates */, m_reverse_mode, &trans );
}
//adjust height according to terrain
setAdjustZVelocity(true);
// pulling back makes no sense in battle mode, since this mode is not a race.
// so in battle mode, always hide view
if( m_reverse_mode || race_manager->isBattleMode(race_manager->getMinorMode()) )
@ -131,21 +134,6 @@ void Plunger::update(float dt)
if(m_rubber_band != NULL) m_rubber_band->update(dt);
if(getHoT()==Track::NOHIT) return;
float hat = getTrans().getOrigin().getZ()-getHoT();
// Use the Height Above Terrain to set the Z velocity.
// HAT is clamped by min/max height. This might be somewhat
// unphysical, but feels right in the game.
float delta = m_average_height - std::max(std::min(hat, m_max_height), m_min_height);
Vec3 v = getVelocity();
float heading = atan2f(-v.getX(), v.getY());
float pitch = getTerrainPitch (heading);
float vel_z = m_force_updown*(delta);
if (hat < m_max_height) // take into account pitch of surface
vel_z += v.length_2d()*tanf(pitch);
v.setZ(vel_z);
setVelocity(v);
} // update
// -----------------------------------------------------------------------------