-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoccersim.html
More file actions
281 lines (232 loc) · 10 KB
/
Copy pathsoccersim.html
File metadata and controls
281 lines (232 loc) · 10 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE HTML>
<!--
Stellar by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>Soccer Simulation</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<link rel="stylesheet" type="text/css" href="prism.css">
<script src="prism.js"></script>
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<nav class="new-nav">
<ul>
<li><a href="index.html#intro" class="active">About Me</a></li>
<li><a href="index.html#first">Work Experience</a></li>
<li><a href="index.html#second">Projects</a></li>
<li><a href="#footer">Contact Information</a></li>
</ul>
</nav>
<!-- Main -->
<div class="project-page-background">
<!-- Content -->
<section class="main">
<h1 class="project-page-title">Soccer Simulation - Unreal Engine 4/C++</h1>
<h2 class="project-page-subtitle">Gameplay</h2>
<iframe class="image vid"
src="https://www.youtube.com/embed/x_5ZJ8B69fM">
</iframe>
<h2 class="project-page-subtitle">Overview</h2>
<ul class="project-page-subtitle">
<li>Designed and implemented an AI class for the players on the pitch</li>
<li>Implemented behaviors such as dribbling, chasing the ball, and returning to home positions in C++</li>
<li>Used behavior trees to execute certain tasks based on conditions on the soccer pitch</li>
</ul>
<h2 class="project-page-subtitle">Code Snippets</h2>
<button class="collapsible">AI Character Class</button>
<!-- Code Snippet -->
<div class="content" style="display: none;"><pre class="line-numbers"><code class="language-clike">#include "AICharacter.h"
// Sets default values
AAICharacter::AAICharacter() {
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Sets up trigger component
TriggerComponent = CreateDefaultSubobject<USphereComponent>(TEXT("TriggerComponent"));
TriggerComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
// Sets up material components
RedTeamMaterial = CreateDefaultSubobject<UMaterial>(TEXT("RedTeamMaterial"));
BlueTeamMaterial = CreateDefaultSubobject<UMaterial>(TEXT("BlueTeamMaterial"));
}
// Called every frame
void AAICharacter::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
heading = GetActorForwardVector();
}
// Called to bind functionality to input
void AAICharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
// Called when the game starts or when spawned
void AAICharacter::BeginPlay() {
Super::BeginPlay();
TriggerComponent->OnComponentBeginOverlap.AddDynamic(this, &AAICharacter::OnOverlapBegin);
}
// Returns if this character is the closest teammate to the ball
bool AAICharacter::isClosestTeamMemberToBall()
{
if (team)
{
return team->playerClosestToBall == this;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("TEAM IS NULL IN AICHARACTER"));
return false;
}
}
// Returns if this character is controlling the ball
bool AAICharacter::isControllingPlayer()
{
if (team && IsDribble)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("Controlling Player has been set")));
return team->controllingPlayer == this;
}
else
{
//GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("TEAM IS NULL IN AICHARACTER or IsDribble IS NOT TRUE"));
return false;
}
}
// Called when defender overlaps with another actor
void AAICharacter::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
// Casts the other actor to soccer ball
ASoccerBall* CollisionSoccerBall = Cast<ASoccerBall>(OtherActor);
// Checks if collided with soccer ball and not currently dribbling
if (CollisionSoccerBall && !IsDribble) {
// Sets the soccer ball pointer for player
SoccerBall = CollisionSoccerBall;
// Handles soccer ball dribbling
Dribble();
//TEST
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("IMMA DRIBBLE"));
}
}
// Called for player kick
void AAICharacter::Kick() {
// Only kicks the ball if in front of player
if (SoccerBall && IsDribble) {
// Lets go of the soccer ball
(SoccerBall->GetStaticMeshComponent())->SetSimulatePhysics(true);
(SoccerBall->GetStaticMeshComponent())->DetachFromComponent(FDetachmentTransformRules(EDetachmentRule::KeepRelative, false));
// Random vertical vector for lift
float LiftFactor = FMath::FRandRange(1.f, 2.f);
FVector LiftVector = FVector(0, 0, 7500 * LiftFactor);
// Creates vector for kick impulse
FVector KickVector = LiftVector + (GetActorForwardVector() * 20000);
// Applies impulse to soccer ball mesh
(SoccerBall->GetStaticMeshComponent())->AddImpulseAtLocation(KickVector, SoccerBall->GetActorLocation());
// Enables collision for soccer ball
(SoccerBall->GetStaticMeshComponent())->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
// No longer dribbling
IsDribble = false;
}
}
// Called for player pass
void AAICharacter::Pass() {
// Only passes the ball if in front of player
if (SoccerBall && IsDribble) {
// Lets go of the soccer ball
(SoccerBall->GetStaticMeshComponent())->SetSimulatePhysics(true);
(SoccerBall->GetStaticMeshComponent())->DetachFromComponent(FDetachmentTransformRules(EDetachmentRule::KeepRelative, false));
// Creates vector for pass impulse
FVector PassVector = GetActorForwardVector() * 17500;
// Applies impulse to soccer ball mesh
(SoccerBall->GetStaticMeshComponent())->AddImpulseAtLocation(PassVector, SoccerBall->GetActorLocation());
// Enables collision for soccer ball
(SoccerBall->GetStaticMeshComponent())->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
// No longer dribbling
IsDribble = false;
}
}
// Called when the play needs to dribble
void AAICharacter::Dribble() {
// Player is currently dribbling
IsDribble = true;
team->controllingPlayer = this;
// Snaps the soccer ball to the player's socket
(SoccerBall->GetStaticMeshComponent())->SetSimulatePhysics(false);
(SoccerBall->GetStaticMeshComponent())->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);
(SoccerBall->GetStaticMeshComponent())->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("soccerBallSocket"));
}
FVector AAICharacter::Seek(FVector targetPos)
{
FVector desiredVelocity = (targetPos - GetActorLocation() * 10.0f);
desiredVelocity.Normalize();
return desiredVelocity - GetVelocity();
}
// Setter for the home region
void AAICharacter::SetHomeRegion(ARegions* NewHomeRegion) {
HomeRegion = NewHomeRegion;
}</code></pre></div>
<button class="collapsible">AI Chase Behavior</button>
<!-- Code Snippet -->
<div class="content" style="display: none;"><pre class="line-numbers"><code class="language-clike">#include "SoccerPitch.h"
#include "DefenderAIController.h"
#include "AICharacter.h"
#include "ChaseBallTaskNode.h"
EBTNodeResult::Type UChaseBallTaskNode::ExecuteTask(UBehaviorTreeComponent& OwnerComponent, uint8* NodeMemory) {
// Getting the AI controller
ADefenderAIController* AIController = Cast<ADefenderAIController>(OwnerComponent.GetAIOwner());
if (AIController) {
// Getting the soccer pitch and the AI character
ASoccerPitch* SoccerPitch = AIController->GetSoccerPitch();
AAICharacter* DefenderAI = AIController->GetAICharacter();
if (SoccerPitch && DefenderAI) {
// Check if this AI is the closest Team Member to the Ball, if it is, pursue the ball
if (DefenderAI->isClosestTeamMemberToBall())
{
AIController->MoveToLocation(DefenderAI->team->pitch->ball->GetActorLocation());
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("Closest Player is: %d"), DefenderAI->playerID));
}
}
// The task succeeded
return EBTNodeResult::Failed;
}
// The task failed
return EBTNodeResult::Succeeded;
}</code></pre></div>
<button class="collapsible">AI Behavior Tree</button>
<!-- Code Snippet -->
<div class="content" style="display: none;">
<img src="ai.PNG" class="image fit">
</div>
</section>
</div>
<!-- Footer -->
<footer id="footer">
<section>
<h2>Contact information</h2>
<dl class="alt">
<dt>Linkedin</dt>
<a href="https://www.linkedin.com/in/dani-amir/"><dd>https://www.linkedin.com/in/dani-amir/</dd></a>
<dt>Email</dt>
<dd><a href="#">daniamir2001@yahoo.com</a></dd>
</dl>
<ul class="icons">
<li><a href="https://github.com/Htmlpro19/" class="icon brands fa-github alt"><span class="label">GitHub</span></a></li>
<li><a href="https://www.linkedin.com/in/dani-amir/" class="icon brands fa-linkedin alt"><span class="label">Linkedin</span></a></li>
</ul>
</section>
<p class="copyright">© Dani Amir. Design: <a href="https://html5up.net">HTML5 UP</a>.</p>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>