void StupidBehaviour::ReactToObstacle()
{
  // Turn to face new direction.
  // TODO smoothly turn to face new direction
  m_pCharacter->RotateY(GetTurnDir());

  // If the new direction is blocked, try the opposite direction.
  // Otherwise Stupids will be excessively stupid, and backtrack rather than
  // going in the available new direction.
  // (If both directions are blocked, the call in the next frame will cause
  // backtracking.)
  if (IsBlocked())
  {
    m_pCharacter->RotateY(180.0f); // go in the opposite direction.
  }
}
Vector4 RPhysics::CalculateInputForce() {

    const float accel = 1.25*GetAccelConst();
    Vector4 ret(0,0,0);
    if(m_pedalState==PRESSED||m_boostLeft>0) {
        Vector4 inputDir = GetTurnDir();
        ret = inputDir*accel;
        float angle = fabs(Angle3(m_player->GetAhead(),m_velocity));
        if(angle*0==0)
            ret*=(1+angle);
        //ret*=2;
        //cerr<<"A:"<<angle<<endl;
    } else if(m_brakeState==PRESSED) {
        Vector4 ahead = m_player->GetAhead();
        ret = -ahead*BRAKE_CONSTANT;
    }
    return ret;
}
void RPhysics::DoPhysics(int deltaTime) {
    
 
    Vector4 ahead = m_player->GetAhead();
    if(Length3(m_velocity)==0) {
        m_velocity = MIN_VEL*ahead;
    }
    Vector4 inputForce = CalculateInputForce();
    
    Vector4 resistance = CalculateResistanceForce();
    //cerr<<"input force";
    //PrintVector(inputForce);
    //cerr<<"resistance force:";
    //PrintVector(resistance);
    Vector4 turnDir = GetTurnDir();
    if(m_boostLeft>0) {
        inputForce+=BOOST_AMT*Normalize3(turnDir);
        m_boostLeft-=deltaTime;
    }
    Vector4 totalForce = inputForce+resistance;
    for(int i=0;i<m_impulseForces.size();i++) {
        totalForce+=m_impulseForces[i];
    }
    m_impulseForces.clear();
    float currSpeed = Length3(m_velocity);
    
    Vector4 newVelocity = m_velocity+deltaTime/1000.0*totalForce;
    //cerr<<"new velocity:";
    //PrintVector(newVelocity);
    float newSpeed = Length3(newVelocity);
    Vector4 deltaPos = newVelocity*deltaTime/1000.0;
    float distance = Length3(deltaPos);
    if(distance>0) {
        Vector4 oldUp = m_player->GetUp();
        Vector4 oldAhead = m_player->GetAhead();
        Vector4 oldDir = newVelocity;
        if(currSpeed==0) oldDir = oldAhead;
        collision = false;
        Vector4 newDir;
        if(newSpeed>MIN_VEL)
            m_player->Move(distance,deltaPos,newDir);
        else
            newDir = deltaPos;
        if(!collision) {
            m_velocity = Normalize3(newDir)*newSpeed;
        }
            Matrix4 transform = GetOrientTransform(oldDir,oldUp,newDir,m_player->GetUp());
            oldAhead(3) = 1;
            //cerr<<"orient transform:";
            //PrintMatrix(transform);
            Vector4 newAhead = transform*oldAhead;
            Vector4 desiredAhead = transform*turnDir;
          
            //cerr<<"new ahead:";
            //PrintVector(newAhead);
            //cerr<<deltaTime<<endl;
            Vector4 toSetAhead = Normalize3(8*33.3/deltaTime*newAhead+desiredAhead);
            //            if(currSpeed==0&&(m_brakeState==PRESSED||m_pedalState==RELEASED)) toSetAhead*=-1;
            m_player->SetAhead(toSetAhead);
            //}
    }

    
}