-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrazyArcadeVehicle.cpp
More file actions
143 lines (102 loc) · 3.57 KB
/
Copy pathCrazyArcadeVehicle.cpp
File metadata and controls
143 lines (102 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Fill out your copyright notice in the Description page of Project Settings.
#include "Vehicles/CrazyArcadeVehicle.h"
//#include "SuspensionHelpers.h"
#include "Vehicles/CrazyWheelComponent.h"
#include "Components/BoxComponent.h"
#include "PBDRigidsSolver.h"
#include "Physics/NetworkPhysicsComponent.h"
#include "Physics/Experimental/PhysScene_Chaos.h"
// Sets default values
ACrazyArcadeVehicle::ACrazyArcadeVehicle()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Simulation = MakeUnique<FCrazyVehicleSimulation>();
NetworkPhysicsComponent =
CreateDefaultSubobject<UNetworkPhysicsComponent>(
TEXT("NetworkPhysics"));
CachedWorld = nullptr;
RootCollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("RootCollisionBox"));
RootComponent = RootCollisionBox;
RootCollisionBox->SetCollisionProfileName(TEXT("Pawn"));
RootCollisionBox->SetSimulatePhysics(true);
RootCollisionBox->SetEnableGravity(false);
// Initialize the visual mesh component
VehicleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VehicleMesh"));
VehicleMesh->SetupAttachment(RootComponent);
VehicleMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
VehicleMesh->SetSimulatePhysics(false);
GetComponents<UCrazyWheelComponent>(Wheels);
DebugVisibility = true;
}
void ACrazyArcadeVehicle::BeginPlay()
{
Super::BeginPlay();
Simulation->Initialize(this);
CachedWorld = GetWorld();
if (!CachedWorld.IsValid()) return;
// 1. Dynamically gather all USuspension components added via the BP Editor
GetComponents<UCrazyWheelComponent>(Wheels);
// Safety check: Don't initialize if there are no wheels to process
if (Wheels.Num() == 0)
{
UE_LOG(LogTemp, Warning, TEXT("AVehicle: No USuspension components found on this actor!"));
return;
}
// 3. Move physics properties to the Root Collision Box
if (RootCollisionBox)
{
RootCollisionBox->SetMassOverrideInKg(NAME_None, 1500.0f);
RootCollisionBox->SetSimulatePhysics(true);
RootCollisionBox->SetEnableGravity(true);
}
// Turn off physics and collision on the visual mesh so it doesn't fight the box root
if (VehicleMesh)
{
VehicleMesh->SetSimulatePhysics(false);
VehicleMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
}
void ACrazyArcadeVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (!CachedWorld.IsValid()) return;
Super::EndPlay(EndPlayReason);
}
void ACrazyArcadeVehicle::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!CachedWorld.IsValid()) return;
// 1. Dynamic validation check: Ensure we actually have wheels to simulate
if (Wheels.Num() == 0) return;
// 2. Wake the rigid body on our new Root Box Component instead of the visual mesh
if (RootCollisionBox)
{
RootCollisionBox->WakeRigidBody();
}
}
void ACrazyArcadeVehicle::SetThrottle(float Value)
{
PendingInputs.Throttle = Value;
}
void ACrazyArcadeVehicle::SetSteering(float Value)
{
PendingInputs.Steering = Value;
}
void ACrazyArcadeVehicle::SetBrake(float Value)
{
PendingInputs.Brake = Value;
}
//void ACrazyArcadeVehicle::DrawSuspensionDebug(const UWorld* World, const FAsyncSuspensionOut& Suspension)
//{
// for (const FDebugDrawLineCommand& Command : Suspension.DebugDrawCommands)
// {
// DrawDebugLine(World,
// Command.StartPoint,
// Command.EndPoint,
// Command.Color,
// false,
// Command.Duration,
// 0,
// Command.Thickness);
// }
//}