Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified TempoMovement/Content/Controllers/TempoVehicleController_BP.uasset
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ void UTempoMovementControlServiceSubsystem::CommandVehicle(const VehicleCommandR
ResponseContinuation.ExecuteIfBound(TempoEmpty(), grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "More than one commandable vehicle found, vehicle name required."));
return;
}

ITempoVehicleControlInterface* Controller = Cast<ITempoVehicleControlInterface>(VehicleControllers[0]);
Controller->HandleDrivingInput(FNormalizedDrivingInput(Request.acceleration(), Request.steering()));
Controller->HandleDrivingInput(FNormalizedDrivingInput(Request.longitudinal(), Request.lateral()));
ResponseContinuation.ExecuteIfBound(TempoEmpty(), grpc::Status_OK);
return;
}
Expand All @@ -95,8 +95,8 @@ void UTempoMovementControlServiceSubsystem::CommandVehicle(const VehicleCommandR
ITempoVehicleControlInterface* Controller = Cast<ITempoVehicleControlInterface>(VehicleController);
if (Controller->GetVehicleName().Equals(RequestedVehicleName, ESearchCase::IgnoreCase))
{
Controller->HandleDrivingInput(FNormalizedDrivingInput(Request.acceleration(), Request.steering()));
ResponseContinuation.ExecuteIfBound(TempoEmpty(), grpc::Status_OK);
Controller->HandleDrivingInput(FNormalizedDrivingInput(Request.longitudinal(), Request.lateral()));
ResponseContinuation.ExecuteIfBound(TempoEmpty(), grpc::Status(grpc::OK, ""));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ message CommandableVehiclesResponse{

message VehicleCommandRequest {
string vehicle_name = 1;
float acceleration = 2;
float steering = 3;
float longitudinal = 2;
float lateral = 3;
}

message CommandablePawnsResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ struct FNormalizedDrivingInput {
GENERATED_BODY();

FNormalizedDrivingInput() = default;
FNormalizedDrivingInput(float AccelerationIn, float SteeringIn)
: Acceleration(FMath::Clamp(AccelerationIn, -1.0, 1.0)), Steering(FMath::Clamp(SteeringIn, -1.0, 1.0))
FNormalizedDrivingInput(float LongitudinalIn, float LateralIn)
: Longitudinal(FMath::Clamp(LongitudinalIn, -1.0, 1.0)), Lateral(FMath::Clamp(LateralIn, -1.0, 1.0))
{}

float GetAcceleration() const { return Acceleration; }
float GetLongitudinal() const { return Longitudinal; }

float GetSteering() const { return Steering; }
float GetLateral() const { return Lateral; }

protected:
// Normalized acceleration in [-1.0 <-> 1.0]
// Normalized longitudinal input [-1.0 <-> 1.0]
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(UIMin=-1.0, UIMax=1.0, ClampMin=-1.0, ClampMax=1.0))
float Acceleration = 0.0;
float Longitudinal = 0.0;

// Normalized steering in [-1.0 <-> 1.0]
// Normalized lateral input [-1.0 <-> 1.0]
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(UIMin=-1.0, UIMax=1.0, ClampMin=-1.0, ClampMax=1.0))
float Steering = 0.0;
float Lateral = 0.0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ void ATempoVehicleController::Tick(float DeltaTime)
if (Input->Frame < GFrameCounter)
{
// Apply some very small acceleration, alternating + and -, to avoid no-input deceleration
const FVector ControlInputLocal((GFrameCounter % 2 ? 1 : -1) * UE_KINDA_SMALL_NUMBER, Input->Input.GetSteering(), 0.0);
const float LongitudinalInput = bPersistLongitudinalInput ? Input->Input.GetLongitudinal() : 0.0;
const float LateralInput = bPersistLateralInput ? Input->Input.GetLateral() : 0.0;
const FVector ControlInputLocal(LongitudinalInput + (GFrameCounter % 2 ? 1 : -1) * UE_KINDA_SMALL_NUMBER, LateralInput, 0.0);
ControlledPawn->AddMovementInput(ControlledPawn->GetActorTransform().TransformVector(ControlInputLocal));
}
}
Expand All @@ -46,9 +48,9 @@ void ATempoVehicleController::HandleDrivingInput(const FNormalizedDrivingInput&
return;
}

const FVector ControlInputLocal(Input.GetAcceleration() + (GFrameCounter % 2 ? 1 : -1) * UE_KINDA_SMALL_NUMBER, Input.GetSteering(), 0.0);
const FVector ControlInputLocal(Input.GetLongitudinal() + (GFrameCounter % 2 ? 1 : -1) * UE_KINDA_SMALL_NUMBER, Input.GetLateral(), 0.0);
ControlledPawn->AddMovementInput(ControlledPawn->GetActorTransform().TransformVector(ControlInputLocal));
if (bPersistSteering)
if (bPersistLateralInput || bPersistLongitudinalInput)
{
LastInput = FLastInput(Input, GFrameCounter);
}
Expand Down Expand Up @@ -96,6 +98,6 @@ void ATempoVehicleController::ApplyDrivingInputToChaosVehicle(UChaosVehicleMovem
}
};

MovementComponent->SetSteeringInput(Input.GetSteering());
SetAccelInput(Input.GetAcceleration());
MovementComponent->SetSteeringInput(Input.GetLongitudinal());
SetAccelInput(Input.GetLongitudinal());
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class TEMPOVEHICLECONTROL_API ATempoVehicleController : public AAIController, pu
virtual void ApplyDrivingInputToChaosVehicle(class UChaosVehicleMovementComponent* ChaosVehicleMovementComponent, const FNormalizedDrivingInput& Input);

UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bPersistSteering = true;
bool bPersistLateralInput = true;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bPersistLongitudinalInput = false;

struct FLastInput
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,48 @@ void UKinematicVehicleMovementComponent::TickComponent(float DeltaTime, ELevelTi
ControlInput = GetOwner()->GetActorRotation().GetInverse().RotateVector(Input);
}

const float NormalizedAcceleration = FMath::IsNearlyZero(ControlInput.X) ?
FMath::Sign(LinearVelocity) * -1.0 * NoInputNormalizedDeceleration * MaxDeceleration :
ControlInput.X;
const float SteeringInput = ControlInput.Y;
float NewLinearVelocity = 0.0;
switch (LongitudinalInputMode)
{
case EVehicleInputMode::Acceleration:
{
const float NormalizedAcceleration = FMath::IsNearlyZero(ControlInput.X) ?
FMath::Sign(LinearVelocity) * -1.0 * NoInputNormalizedDeceleration * MaxDeceleration :
ControlInput.X;

const float Acceleration = NormalizedAcceleration > 0.0 ?
FMath::Min(MaxAcceleration, NormalizedAcceleration * MaxAcceleration) : LinearVelocity > 0.0 ?
// Moving forward, slowing down
FMath::Max(-MaxDeceleration, NormalizedAcceleration * MaxDeceleration) :
// Moving backwards, speeding up (in reverse)
FMath::Max(-MaxAcceleration, NormalizedAcceleration * MaxAcceleration);
const float Acceleration = NormalizedAcceleration > 0.0 ?
FMath::Min(MaxAcceleration, NormalizedAcceleration * MaxAcceleration) : LinearVelocity > 0.0 ?
// Moving forward, slowing down
FMath::Max(-MaxDeceleration, NormalizedAcceleration * MaxDeceleration) :
// Moving backwards, speeding up (in reverse)
FMath::Max(-MaxAcceleration, NormalizedAcceleration * MaxAcceleration);

const float SteeringAngle = FMath::Clamp(SteeringInput * MaxSteering, -MaxSteering, MaxSteering);
float DeltaVelocity = DeltaTime * Acceleration;
if (LinearVelocity > 0.0 && DeltaVelocity < 0.0)
{
// If slowing down, don't start reversing.
DeltaVelocity = FMath::Max(-LinearVelocity, DeltaVelocity);
}
NewLinearVelocity = LinearVelocity + DeltaVelocity;

float DeltaVelocity = DeltaTime * Acceleration;
if (LinearVelocity > 0.0 && DeltaVelocity < 0.0)
{
// If slowing down, don't start reversing.
DeltaVelocity = FMath::Max(-LinearVelocity, DeltaVelocity);
NewLinearVelocity = FMath::Clamp(NewLinearVelocity, -MaxSpeed, MaxSpeed);
break;
}
case EVehicleInputMode::Velocity:
{
const float NormalizedLinearVelocity = ControlInput.X;
NewLinearVelocity = NormalizedLinearVelocity * MaxSpeed;
break;
}
}
float NewLinearVelocity = LinearVelocity + DeltaVelocity;

if (!bReverseEnabled)
{
NewLinearVelocity = FMath::Max(NewLinearVelocity, 0.0);
}

NewLinearVelocity = FMath::Clamp(NewLinearVelocity, -MaxSpeed, MaxSpeed);
const float LateralInput = ControlInput.Y;
const float SteeringAngle = FMath::Clamp(LateralInput * MaxSteering, -MaxSteering, MaxSteering);

FVector NewVelocity = Velocity;
float NewAngularVelocity = AngularVelocity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "GameFramework/PawnMovementComponent.h"
#include "KinematicVehicleMovementComponent.generated.h"

UENUM(BlueprintType)
enum class EVehicleInputMode : uint8
{
Velocity = 0,
Acceleration = 1,
};

UCLASS(Abstract, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class TEMPOVEHICLEMOVEMENT_API UKinematicVehicleMovementComponent : public UPawnMovementComponent, public ITempoMovementInterface
{
Expand Down Expand Up @@ -48,4 +55,7 @@ class TEMPOVEHICLEMOVEMENT_API UKinematicVehicleMovementComponent : public UPawn

UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
float AngularVelocity = 0.0; // Deg/S

UPROPERTY(EditAnywhere, BlueprintReadWrite)
EVehicleInputMode LongitudinalInputMode = EVehicleInputMode::Acceleration;
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct TFromROSConverter<tempo_movement_ros_bridge::srv::CommandVehicle::Request
{
TempoMovement::VehicleCommandRequest TempoValue;
TempoValue.set_vehicle_name(ROSValue.vehicle_name);
TempoValue.set_acceleration(ROSValue.acceleration);
TempoValue.set_steering(ROSValue.steering);
TempoValue.set_longitudinal(ROSValue.longitudinal);
TempoValue.set_lateral(ROSValue.lateral);
return TempoValue;
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
string vehicle_name
float32 acceleration
float32 steering
float32 longitudinal
float32 lateral
---