Updated default AI, added getPitch() to moveable so that camera can get the

pitch in the range it needed it (-90 to 90, not -180 to 180).


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/switch_coordinate_system@4946 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2010-03-06 00:44:17 +00:00
parent ac85c71e31
commit f09a083597
4 changed files with 30 additions and 25 deletions

View File

@@ -254,12 +254,6 @@ void Camera::smoothMoveCamera(float dt, const Vec3 &wanted_position,
void Camera::computeNormalCameraPosition(Vec3 *wanted_position,
Vec3 *wanted_target)
{
// The pitch in hpr is in between -pi and pi. But for the camera it
// must be restricted to -pi/2 and pi/2 - so recompute it by restricting
// y to positive values, i.e. no pitch of more than pi/2.
Vec3 up = m_kart->getTrans().getBasis().getColumn(1);
float pitch = atan2(up.getZ(), fabsf(up.getY()));
*wanted_target = m_kart->getXYZ();
wanted_target->setY(wanted_target->getY()+ 0.75f);
// This first line moves the camera around behind the kart, pointing it
@@ -271,7 +265,7 @@ void Camera::computeNormalCameraPosition(Vec3 *wanted_position,
float dampened_steer = fabsf(steering) * steering;
float angle_around = m_kart->getHeading()
+ m_rotation_range * dampened_steer * 0.5f;
float angle_up = pitch + 30.0f*DEGREE_TO_RAD;
float angle_up = m_kart->getPitch() + 30.0f*DEGREE_TO_RAD;
wanted_position->setX(-sin(angle_around));
wanted_position->setY( sin(angle_up) );
@@ -305,8 +299,7 @@ void Camera::update(float dt)
float angle_around = m_kart->getHeading()
- m_rotation_range * m_kart->getSteerPercent()
* m_kart->getSkidding();
float angle_up = m_kart->getHPR().getPitch()
+ 30.0f*DEGREE_TO_RAD;
float angle_up = m_kart->getPitch() + 30.0f*DEGREE_TO_RAD;
wanted_position.setX( sin(angle_around));
wanted_position.setY( sin(angle_up) );
wanted_position.setZ( cos(angle_around));
@@ -323,7 +316,7 @@ void Camera::update(float dt)
float angle_around = m_kart->getHeading()
+ m_rotation_range * m_kart->getSteerPercent()
* m_kart->getSkidding();
float angle_up = m_kart->getHPR().getPitch()
float angle_up = m_kart->getPitch()
- 20.0f*DEGREE_TO_RAD;
wanted_position.setX( sin(angle_around));
wanted_position.setY(-sin(angle_up) );
@@ -341,7 +334,7 @@ void Camera::update(float dt)
// Follows the leader kart, higher off of the ground, further from the kart,
// and turns in the opposite direction from the kart for a nice effect. :)
float angle_around = kart->getHeading();
float angle_up = kart->getHPR().getPitch() + 40.0f*DEGREE_TO_RAD;
float angle_up = kart->getPitch() + 40.0f*DEGREE_TO_RAD;
wanted_position.setX(sin(angle_around));
wanted_position.setY(sin(angle_up) );
wanted_position.setZ(cos(angle_around));

View File

@@ -299,7 +299,7 @@ void DefaultAIController::handleBraking()
//We may brake if we are about to get out of the road, but only if the
//kart is on top of the road, and if we won't slow down below a certain
//limit.
if (m_crashes.m_road && m_kart->getVelocityLC().getY() > MIN_SPEED &&
if (m_crashes.m_road && m_kart->getVelocityLC().getZ() > MIN_SPEED &&
m_world->isOnRoad(m_kart->getWorldKartId()) )
{
float kart_ang_diff =
@@ -341,7 +341,7 @@ void DefaultAIController::handleBraking()
//Brake if the kart's speed is bigger than the speed we need
//to go through the curve at the widest angle, or if the kart
//is not going straight in relation to the road.
if(m_kart->getVelocityLC().getY() > m_curve_target_speed ||
if(m_kart->getVelocityLC().getZ() > m_curve_target_speed ||
kart_ang_diff > MIN_TRACK_ANGLE )
{
#ifdef AI_DEBUG
@@ -802,9 +802,9 @@ float DefaultAIController::steerToPoint(const Vec3 &point, float dt)
// No sense steering if we are not driving.
if(m_kart->getSpeed()==0) return 0.0f;
const float dx = point.getX() - m_kart->getXYZ().getX();
const float dy = point.getY() - m_kart->getXYZ().getY();
const float dz = point.getZ() - m_kart->getXYZ().getZ();
/** Angle from the kart position to the point in world coordinates. */
float theta = -atan2(dx, dy);
float theta = -atan2(dx, dz);
// Angle is the point is relative to the heading - but take the current
// angular velocity into account, too. The value is multiplied by two
@@ -857,7 +857,7 @@ void DefaultAIController::checkCrashes( const int STEPS, const Vec3& pos )
//Protection against having vel_normal with nan values
const Vec3 &VEL = m_kart->getVelocity();
Vec3 vel_normal(VEL.getX(), VEL.getY(), 0.0);
Vec3 vel_normal(VEL.getX(), VEL.getZ(), 0.0);
float speed=vel_normal.length();
// If the velocity is zero, no sense in checking for crashes in time
if(speed==0) return;
@@ -882,7 +882,7 @@ void DefaultAIController::checkCrashes( const int STEPS, const Vec3& pos )
if(kart==m_kart||kart->isEliminated()) continue; // ignore eliminated karts
const Kart *other_kart = m_world->getKart(j);
// Ignore karts ahead that are faster than this kart.
if(m_kart->getVelocityLC().getY() < other_kart->getVelocityLC().getY())
if(m_kart->getVelocityLC().getZ() < other_kart->getVelocityLC().getZ())
continue;
Vec3 other_kart_xyz = other_kart->getXYZ() + other_kart->getVelocity()*(i*dt);
float kart_distance = (step_coord - other_kart_xyz).length_2d();
@@ -1012,7 +1012,7 @@ inline float DefaultAIController::normalizeAngle(float angle)
*/
int DefaultAIController::calcSteps()
{
int steps = int( m_kart->getVelocityLC().getY() / m_kart_length );
int steps = int( m_kart->getVelocityLC().getZ() / m_kart_length );
if( steps < m_min_steps ) steps = m_min_steps;
//Increase the steps depending on the width, if we steering hard,
@@ -1086,7 +1086,7 @@ void DefaultAIController::findCurve()
{
float total_dist = 0.0f;
int i;
for(i = m_track_node; total_dist < m_kart->getVelocityLC().getY();
for(i = m_track_node; total_dist < m_kart->getVelocityLC().getZ();
i = m_next_node_index[i])
{
total_dist += m_quad_graph->getDistanceToNext(i, m_successor_index[i]);

View File

@@ -96,16 +96,21 @@ void Moveable::reset()
void Moveable::update(float dt)
{
m_motion_state->getWorldTransform(m_transform);
m_velocityLC = getVelocity()*m_transform.getBasis();
m_velocityLC = getVelocity()*m_transform.getBasis();
m_hpr.setHPR(m_transform.getRotation());
Vec3 forw(1, 0, 0);
Vec3 forw_vec = m_transform.getBasis().getColumn(0);
m_heading = -atan2f(forw_vec.getZ(), forw_vec.getX());
m_heading = -atan2f(forw_vec.getZ(), forw_vec.getX());
// The pitch in hpr is in between -pi and pi. But for the camera it
// must be restricted to -pi/2 and pi/2 - so recompute it by restricting
// y to positive values, i.e. no pitch of more than pi/2.
Vec3 up = getTrans().getBasis().getColumn(1);
m_pitch = atan2(up.getZ(), fabsf(up.getY()));
updateGraphics(Vec3(0,0,0), btQuaternion(0, 0, 0, 1));
m_first_time = false ;
} // update
//-----------------------------------------------------------------------------
void Moveable::createBody(float mass, btTransform& trans,
btCollisionShape *shape) {

View File

@@ -46,6 +46,8 @@ private:
/** The heading in m_hpr is between -90 and 90 degrees only. The 'real'
* heading between -180 to 180 degrees is stored in this variable. */
float m_heading;
/** The pitch between -90 and 90 degrees. */
float m_pitch;
protected:
UserPointer m_user_pointer;
@@ -70,10 +72,15 @@ public:
const Vec3& getXYZ() const {return (Vec3&)m_transform.getOrigin();}
/** Return the rotation, but heading is restricted to -90 and 90 degrees. */
const Vec3& getHPR() const {return m_hpr; }
/** Returns the heading between -180 and 180 degrees. */
/** Returns the heading between -180 and 180 degrees. Note that using
* getHPR().getHeading() can result a different heading (e.g. a heading
* of 180 degrees is the same as a roll and pitch around 180).*/
float getHeading() const {return m_heading; }
/** Returns the pitch of the kart, restricted to between -90 and 90 degrees.
* Note that using getHPR().getPitch can result in a different value! */
float getPitch() const {return m_pitch; }
const btQuaternion
getRotation() const {return m_transform.getRotation(); }
getRotation() const {return m_transform.getRotation(); }
/** Sets the XYZ coordinates of the moveable. */
void setXYZ(const Vec3& a)