@@ -178,8 +178,10 @@ void ACarPawn::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other,
178178 HitNormal, NormalImpulse, Hit);
179179}
180180
181- void ACarPawn::initializeForBeginPlay (bool enable_rpc, const std::string& api_server_address, bool engine_sound)
181+ void ACarPawn::initializeForBeginPlay (bool enable_rpc, const std::string& api_server_address, bool engine_sound, int remote_control_id )
182182{
183+ remote_control_id_ = remote_control_id;
184+
183185 if (engine_sound)
184186 EngineSoundComponent->Activate ();
185187 else
@@ -209,17 +211,25 @@ void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_se
209211
210212 startApiServer (enable_rpc, api_server_address);
211213
214+ // TODO: should do reset() here?
215+ keyboard_controls_ = joystick_controls_ = CarPawnApi::CarControls ();
216+
212217 // joystick
213- joystick_.getJoyStickState (0 , joystick_state_);
214- if (joystick_state_.is_initialized )
215- UAirBlueprintLib::LogMessageString (" RC Controller on USB: " , joystick_state_.pid_vid , LogDebugLevel::Informational);
216- else
217- UAirBlueprintLib::LogMessageString (" RC Controller on USB not detected: " ,
218- std::to_string (joystick_state_.connection_error_code ), LogDebugLevel::Informational);
218+ if (remote_control_id_ >= 0 ) {
219+ joystick_.getJoyStickState (remote_control_id_, joystick_state_);
220+ if (joystick_state_.is_initialized )
221+ UAirBlueprintLib::LogMessageString (" RC Controller on USB: " , joystick_state_.pid_vid == " " ?
222+ " (Detected)" : joystick_state_.pid_vid , LogDebugLevel::Informational);
223+ else
224+ UAirBlueprintLib::LogMessageString (" RC Controller on USB not detected: " ,
225+ std::to_string (joystick_state_.connection_error_code ), LogDebugLevel::Informational);
226+ }
227+
219228}
220229
221230void ACarPawn::reset (bool disable_api_control)
222231{
232+ keyboard_controls_ = joystick_controls_ = CarPawnApi::CarControls ();
223233 api_->reset ();
224234
225235 if (disable_api_control)
@@ -385,22 +395,7 @@ void ACarPawn::Tick(float Delta)
385395{
386396 Super::Tick (Delta);
387397
388- const msr::airlib::CarApiBase::CarControls* current_controls = &keyboard_controls_;
389- if (!api_->isApiControlEnabled ()) {
390- UAirBlueprintLib::LogMessageString (" Control Mode: " , " Keyboard" , LogDebugLevel::Informational);
391- api_->setCarControls (keyboard_controls_);
392- }
393- else {
394- UAirBlueprintLib::LogMessageString (" Control Mode: " , " API" , LogDebugLevel::Informational);
395- current_controls = & api_->getCarControls ();
396- }
397- UAirBlueprintLib::LogMessageString (" Accel: " , std::to_string (current_controls->throttle ), LogDebugLevel::Informational);
398- UAirBlueprintLib::LogMessageString (" Break: " , std::to_string (current_controls->brake ), LogDebugLevel::Informational);
399- UAirBlueprintLib::LogMessageString (" Steering: " , std::to_string (current_controls->steering ), LogDebugLevel::Informational);
400- UAirBlueprintLib::LogMessageString (" Handbreak: " , std::to_string (current_controls->handbrake ), LogDebugLevel::Informational);
401- UAirBlueprintLib::LogMessageString (" Target Gear: " , std::to_string (current_controls->manual_gear ), LogDebugLevel::Informational);
402-
403-
398+ updateCarControls ();
404399
405400 updateKinematics (Delta);
406401
@@ -420,6 +415,57 @@ void ACarPawn::Tick(float Delta)
420415 getVehiclePawnWrapper ()->setLogLine (getLogString ());
421416}
422417
418+ void ACarPawn::updateCarControls ()
419+ {
420+ const msr::airlib::CarApiBase::CarControls* current_controls = nullptr ;
421+ if (remote_control_id_ >= 0 && joystick_state_.is_initialized ) {
422+ joystick_.getJoyStickState (0 , joystick_state_);
423+
424+ if ((joystick_state_.buttons & 4 ) | (joystick_state_.buttons & 1024 )) { // X button or Start button
425+ reset ();
426+ return ;
427+ }
428+
429+ joystick_controls_.steering = joystick_state_.left_y * 1.25 ;
430+ joystick_controls_.throttle = (-joystick_state_.right_x + 1 ) / 2 ;
431+ joystick_controls_.brake = -joystick_state_.right_z + 1 ;
432+
433+ // Two steel levers behind wheel
434+ joystick_controls_.handbrake = (joystick_state_.buttons & 32 ) | (joystick_state_.buttons & 64 ) ? 1 : 0 ;
435+
436+ if ((joystick_state_.buttons & 256 ) | (joystick_state_.buttons & 2 )) { // RSB button or B button
437+ joystick_controls_.manual_gear = -1 ;
438+ joystick_controls_.is_manual_gear = true ;
439+ joystick_controls_.gear_immediate = true ;
440+ }
441+ else if ((joystick_state_.buttons & 512 ) | (joystick_state_.buttons & 1 )) { // LSB button or A button
442+ joystick_controls_.manual_gear = 0 ;
443+ joystick_controls_.is_manual_gear = false ;
444+ joystick_controls_.gear_immediate = true ;
445+ }
446+
447+ UAirBlueprintLib::LogMessageString (" Control Mode: " , " Wheel/Joystick" , LogDebugLevel::Informational);
448+ current_controls = &joystick_controls_;
449+ }
450+ else {
451+ UAirBlueprintLib::LogMessageString (" Control Mode: " , " Keyboard" , LogDebugLevel::Informational);
452+ current_controls = &keyboard_controls_;
453+ }
454+
455+ if (!api_->isApiControlEnabled ()) {
456+ api_->setCarControls (* current_controls);
457+ }
458+ else {
459+ UAirBlueprintLib::LogMessageString (" Control Mode: " , " API" , LogDebugLevel::Informational);
460+ current_controls = & api_->getCarControls ();
461+ }
462+ UAirBlueprintLib::LogMessageString (" Accel: " , std::to_string (current_controls->throttle ), LogDebugLevel::Informational);
463+ UAirBlueprintLib::LogMessageString (" Break: " , std::to_string (current_controls->brake ), LogDebugLevel::Informational);
464+ UAirBlueprintLib::LogMessageString (" Steering: " , std::to_string (current_controls->steering ), LogDebugLevel::Informational);
465+ UAirBlueprintLib::LogMessageString (" Handbreak: " , std::to_string (current_controls->handbrake ), LogDebugLevel::Informational);
466+ UAirBlueprintLib::LogMessageString (" Target Gear: " , std::to_string (current_controls->manual_gear ), LogDebugLevel::Informational);
467+ }
468+
423469void ACarPawn::BeginPlay ()
424470{
425471 Super::BeginPlay ();
@@ -450,6 +496,8 @@ void ACarPawn::UpdateHUDStrings()
450496
451497 UAirBlueprintLib::LogMessage (TEXT (" Speed: " ), SpeedDisplayString.ToString (), LogDebugLevel::Informational);
452498 UAirBlueprintLib::LogMessage (TEXT (" Gear: " ), GearDisplayString.ToString (), LogDebugLevel::Informational);
499+ UAirBlueprintLib::LogMessage (TEXT (" RPM: " ), FText::AsNumber (GetVehicleMovement ()->GetEngineRotationSpeed ()).ToString (), LogDebugLevel::Informational);
500+
453501
454502}
455503
0 commit comments