📄 ivp_controller_raycast_car.cxx
字号:
void IVP_Controller_Raycast_Car::change_stabilizer_constant(IVP_POS_AXIS pos, IVP_FLOAT stabi_constant)
{
IVP_Raycast_Car_Axis *axis = get_axis(pos);
axis->stabilizer_constant = stabi_constant;
}
void IVP_Controller_Raycast_Car::change_fast_turn_factor( IVP_FLOAT fast_turn_factor_ )
{
//fast_turn_factor = fast_turn_factor_;
}
void IVP_Controller_Raycast_Car::change_body_downforce(IVP_FLOAT force)
{
down_force = force;
}
IVP_CONTROLLER_PRIORITY IVP_Controller_Raycast_Car::get_controller_priority()
{
return IVP_CP_CONSTRAINTS_MAX;
}
void IVP_Controller_Raycast_Car::set_booster_acceleration(IVP_FLOAT acceleration)
{
booster_force = acceleration;
}
void IVP_Controller_Raycast_Car::activate_booster(IVP_FLOAT thrust, IVP_FLOAT duration, IVP_FLOAT delay)
{
if(this->booster_force)
return;
// booster not ready yet
if(this->booster_seconds_until_ready>0.0f)
return;
booster_force = thrust;
this->booster_seconds_to_go = duration; // seconds
this->booster_seconds_until_ready = duration + delay; // time when next ignition is possible
}
IVP_FLOAT IVP_Controller_Raycast_Car::get_booster_delay()
{
return booster_seconds_until_ready;
}
void IVP_Controller_Raycast_Car::do_steering(IVP_FLOAT steering_angle_in)
{
// tell constraint system new steering positions of wheels
if ( steering_angle == steering_angle_in)
return;
this->steering_angle = steering_angle_in;
car_body->get_environment()->get_controller_manager()->ensure_controller_in_simulation( this );
for (int i = 0; i< wheels_per_axis; i++)
{
this->do_steering_wheel(IVP_POS_WHEEL(i), steering_angle_in);
}
}
IVP_Controller_Raycast_Car::~IVP_Controller_Raycast_Car()
{
car_body->get_environment()->get_controller_manager()->remove_controller_from_environment( this, IVP_TRUE );
}
IVP_DOUBLE IVP_Controller_Raycast_Car::get_wheel_angular_velocity(IVP_POS_WHEEL pos)
{
IVP_Raycast_Car_Wheel *wheel = get_wheel(pos);
return wheel->wheel_angular_velocity;
}
IVP_DOUBLE IVP_Controller_Raycast_Car::get_body_speed(IVP_COORDINATE_INDEX index)
{
// return (IVP_FLOAT)car_body->get_geom_center_speed();
IVP_U_Float_Point *vec_ws = &car_body->get_core()->speed;
// works well as we do not use merged cores
const IVP_U_Matrix *mat_ws = car_body->get_core()->get_m_world_f_core_PSI();
IVP_U_Point orientation;
mat_ws->get_col(index, &orientation);
return orientation.dot_product(vec_ws);
};
IVP_DOUBLE IVP_Controller_Raycast_Car::get_orig_front_wheel_distance()
{
IVP_U_Float_Point *left_wheel_cs = &this->get_wheel(IVP_FRONT_LEFT)->hp_cs;
IVP_U_Float_Point *right_wheel_cs = &this->get_wheel(IVP_FRONT_RIGHT)->hp_cs;
IVP_DOUBLE dist = left_wheel_cs->k[this->index_x] - right_wheel_cs->k[this->index_x];
return IVP_Inline_Math::fabsd(dist); // was fabs, which was a sml call
}
IVP_DOUBLE IVP_Controller_Raycast_Car::get_orig_axles_distance()
{
IVP_U_Float_Point *front_wheel_cs = &this->get_wheel(IVP_FRONT_LEFT)->hp_cs;
IVP_U_Float_Point *rear_wheel_cs = &this->get_wheel(IVP_REAR_LEFT)->hp_cs;
IVP_DOUBLE dist = front_wheel_cs->k[this->index_z] - rear_wheel_cs->k[this->index_z];
return IVP_Inline_Math::fabsd(dist); // was fabs, which was a sml call
}
void IVP_Controller_Raycast_Car::get_skid_info( IVP_Wheel_Skid_Info *array_of_skid_info_out)
{
for ( int w = 0; w < n_wheels; w++)
{
IVP_Wheel_Skid_Info &info = array_of_skid_info_out[w];
//IVP_Constraint_Car_Object *wheel = car_constraint_solver->wheel_objects.element_at(w);
info.last_contact_position_ws.set_to_zero(); // = wheel->last_contact_position_ws;
info.last_skid_value = 0.0f; // wheel->last_skid_value;
info.last_skid_time = 0.0f; //wheel->last_skid_time;
}
}
//-----------------------------------------------------------------------------
// Purpose: (Ipion) Create a raycast car controller.
// Input: pEnvironment - the physics environment the car is to reside in
// pCarSystemTemplate - ipion's car system template (filled out with car parameters)
//-----------------------------------------------------------------------------
IVP_Controller_Raycast_Car::IVP_Controller_Raycast_Car( IVP_Environment *pEnvironment/*environment*/,
const IVP_Template_Car_System *pCarSystemTemplate/*tcs*/ )
{
InitRaycastCarBody( pCarSystemTemplate );
InitRaycastCarEnvironment( pEnvironment, pCarSystemTemplate );
InitRaycastCarWheels( pCarSystemTemplate );
InitRaycastCarAxes( pCarSystemTemplate );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::InitRaycastCarEnvironment( IVP_Environment *pEnvironment,
const IVP_Template_Car_System *pCarSystemTemplate )
{
// Copies of the car system template component indices and handedness.
index_x = pCarSystemTemplate->index_x;
index_y = pCarSystemTemplate->index_y;
index_z = pCarSystemTemplate->index_z;
is_left_handed = pCarSystemTemplate->is_left_handed;
// Add this controller to the physics environment and setup the objects gravity.
pEnvironment->get_controller_manager()->announce_controller_to_environment( this );
extra_gravity = pCarSystemTemplate->extra_gravity_force_value;
if ( pEnvironment->get_gravity()->k[index_y] > 0 )
{
gravity_y_direction = 1.0f;
}
else
{
gravity_y_direction = -1.0f;
}
normized_gravity_ws.set( pEnvironment->get_gravity() );
normized_gravity_ws.normize();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::InitRaycastCarBody( const IVP_Template_Car_System *pCarSystemTemplate )
{
// Car body attributes.
n_wheels = pCarSystemTemplate->n_wheels;
n_axis = pCarSystemTemplate->n_axis;
wheels_per_axis = n_wheels / n_axis;
// Add the car body "core" to the list of raycast car controller "cores."
car_body = pCarSystemTemplate->car_body;
this->vector_of_cores.add( car_body->get_core() );
// Initialize the car's booster system.
booster_force = 0.0f;
booster_seconds_until_ready = 0.0f;
booster_seconds_to_go = 0.0f;
// Init extra downward force applied to car.
down_force_vertical_offset = pCarSystemTemplate->body_down_force_vertical_offset;
down_force = 0.0f;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::InitRaycastCarWheels( const IVP_Template_Car_System *pCarSystemTemplate )
{
IVP_U_Matrix m_core_f_object;
car_body->calc_m_core_f_object( &m_core_f_object );
// Initialize the car wheel system.
for ( int iWheel = 0; iWheel < n_wheels; iWheel++ )
{
// Get and clear out memory for the current raycast wheel.
IVP_Raycast_Car_Wheel *pRaycastWheel = get_wheel( IVP_POS_WHEEL( iWheel ) );
P_MEM_CLEAR( pRaycastWheel );
// Put the wheel in car space.
m_core_f_object.vmult4( &pCarSystemTemplate->wheel_pos_Bos[iWheel], &pRaycastWheel->hp_cs );
// Spring (Shocks) data.
// pRaycastWheel->distance_orig_hp_to_hp = pRaycastWheel->hp_cs.k[index_y] + ( gravity_y_direction * pCarSystemTemplate->raycast_startpoint_height_offset );
// pRaycastWheel->spring_len = gravity_y_direction * ( pRaycastWheel->distance_orig_hp_to_hp - pCarSystemTemplate->spring_pre_tension[iWheel] );
pRaycastWheel->spring_len = -pCarSystemTemplate->spring_pre_tension[iWheel];
// pRaycastWheel->hp_cs.k[index_y] = -gravity_y_direction * pCarSystemTemplate->raycast_startpoint_height_offset;
pRaycastWheel->spring_direction_cs.set_to_zero();
pRaycastWheel->spring_direction_cs.k[index_y] = gravity_y_direction;
pRaycastWheel->spring_constant = pCarSystemTemplate->spring_constant[iWheel];
pRaycastWheel->spring_damp_relax = pCarSystemTemplate->spring_dampening[iWheel];
pRaycastWheel->spring_damp_compress = pCarSystemTemplate->spring_dampening_compression[iWheel];
// Wheel data.
pRaycastWheel->friction_of_wheel = 1.0f;//pCarSystemTemplate->friction_of_wheel[iWheel];
pRaycastWheel->wheel_radius = pCarSystemTemplate->wheel_radius[iWheel];
pRaycastWheel->inv_wheel_radius = 1.0f / pCarSystemTemplate->wheel_radius[iWheel];
do_steering_wheel( IVP_POS_WHEEL( iWheel ), 0.0f );
pRaycastWheel->wheel_is_fixed = IVP_FALSE;
pRaycastWheel->max_rotation_speed = pCarSystemTemplate->wheel_max_rotation_speed[iWheel>>1];
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::InitRaycastCarAxes( const IVP_Template_Car_System *pCarSystemTemplate )
{
this ->steering_angle = -1.0f; // make sure next call is not optimized
this->do_steering( 0.0f ); // make sure next call gets through
for ( int iAxis = 0; iAxis < n_axis; iAxis++ )
{
IVP_Raycast_Car_Axis *pAxis = get_axis( IVP_POS_AXIS( iAxis ) );
pAxis->stabilizer_constant = pCarSystemTemplate->stabilizer_constant[iAxis];
}
}
//-----------------------------------------------------------------------------
// Purpose: Debug data for use in vphysics and the engine to visualize car data.
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::SetCarSystemDebugData( const IVP_CarSystemDebugData_t &carSystemDebugData )
{
// Wheels (raycast data only!)
for ( int iWheel = 0; iWheel < IVP_CAR_SYSTEM_MAX_WHEELS; ++iWheel )
{
m_CarSystemDebugData.wheelRaycasts[iWheel][0] = carSystemDebugData.wheelRaycasts[iWheel][0];
m_CarSystemDebugData.wheelRaycasts[iWheel][1] = carSystemDebugData.wheelRaycasts[iWheel][1];
m_CarSystemDebugData.wheelRaycastImpacts[iWheel] = carSystemDebugData.wheelRaycastImpacts[iWheel];
}
}
//-----------------------------------------------------------------------------
// Purpose: Debug data for use in vphysics and the engine to visualize car data.
//-----------------------------------------------------------------------------
void IVP_Controller_Raycast_Car::GetCarSystemDebugData( IVP_CarSystemDebugData_t &carSystemDebugData )
{
// Wheels (raycast data only!)
for ( int iWheel = 0; iWheel < IVP_CAR_SYSTEM_MAX_WHEELS; ++iWheel )
{
carSystemDebugData.wheelRaycasts[iWheel][0] = m_CarSystemDebugData.wheelRaycasts[iWheel][0];
carSystemDebugData.wheelRaycasts[iWheel][1] = m_CarSystemDebugData.wheelRaycasts[iWheel][1];
carSystemDebugData.wheelRaycastImpacts[iWheel] = m_CarSystemDebugData.wheelRaycastImpacts[iWheel];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -