Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
kg-OpenGLPrj SuperPang
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cg
kg-OpenGLPrj SuperPang
Commits
5219277b
Commit
5219277b
authored
3 years ago
by
Мартин Трајковски
Browse files
Options
Downloads
Patches
Plain Diff
Fixed Ball-Rectangle Collision
parent
ac3d054f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/BallObject.cpp
+17
-17
17 additions, 17 deletions
src/BallObject.cpp
src/Game.cpp
+7
-26
7 additions, 26 deletions
src/Game.cpp
with
24 additions
and
43 deletions
src/BallObject.cpp
+
17
−
17
View file @
5219277b
...
...
@@ -16,24 +16,24 @@ float clamp(float value, float minn, float maxx) {
}
Collision
BallObject
::
checkCollision
(
GameObject
&
obj
)
{
glm
::
vec2
center
(
this
->
Position
+
this
->
Radius
);
// calculate AABB info (center, half-extents)
glm
::
vec2
rectangle_half_extents
(
obj
.
Size
.
x
/
2.0
f
,
obj
.
Size
.
y
/
2.0
f
);
glm
::
vec2
rectangle_center
(
obj
.
Position
.
x
+
rectangle_half_extents
.
x
,
obj
.
Position
.
y
+
rectangle_half_extents
.
y
);
glm
::
vec2
difference
=
center
-
rectangle_center
;
glm
::
vec2
clamped
=
glm
::
clamp
(
difference
,
-
rectangle_half_extents
,
rectangle_half_extents
);
// add clamped value to AABB_center and we get the value of box closest to circle
glm
::
vec2
closest
=
rectangle_center
+
clamped
;
glm
::
vec2
center
(
this
->
Position
);
// calculate AABB info (center, half-extents)
glm
::
vec2
rectangle_half_extents
(
obj
.
Size
.
x
/
2.0
f
,
obj
.
Size
.
y
/
2.0
f
);
glm
::
vec2
rectangle_center
(
obj
.
Position
.
x
,
obj
.
Position
.
y
);
glm
::
vec2
difference
=
center
-
rectangle_center
;
glm
::
vec2
clamped
=
glm
::
clamp
(
difference
,
-
rectangle_half_extents
,
rectangle_half_extents
);
// add clamped value to AABB_center and we get the value of box closest to circle
glm
::
vec2
closest
=
rectangle_center
+
clamped
;
difference
=
closest
-
center
;
if
(
glm
::
length
(
difference
)
<=
this
->
Radius
/
2.0
f
)
{
return
Collision
(
true
,
VectorDirection
(
difference
),
difference
);
}
else
{
return
Collision
();
}
difference
=
closest
-
center
;
if
(
glm
::
length
(
difference
)
<=
this
->
Radius
)
{
return
Collision
(
true
,
VectorDirection
(
difference
),
difference
);
}
else
{
return
Collision
();
}
}
glm
::
vec3
&
BallObject
::
Move
(
float
dt
,
unsigned
int
windowWidth
,
unsigned
int
windowHeight
)
{
...
...
This diff is collapsed.
Click to expand it.
src/Game.cpp
+
7
−
26
View file @
5219277b
...
...
@@ -65,9 +65,7 @@ void Game::LoadFiles() {
}
glm
::
vec3
camPosition
(
0.0
f
,
0.0
f
,
0.000001
f
);
glm
::
vec3
worldUp
(
0.0
f
,
1.0
f
,
0.0
f
);
glm
::
vec3
camFront
(
0.0
f
,
0.0
f
,
0.0
f
);
void
Game
::
Init
()
{
...
...
@@ -77,6 +75,7 @@ void Game::Init()
ResourceManager
::
GetShader
(
"sprite3D"
).
SetInteger
(
"image"
,
0
,
true
);
ResourceManager
::
GetShader
(
"sprite3D"
).
SetMatrix4
(
"projection"
,
glm
::
ortho
(
0.0
f
,
(
float
)
this
->
Width
,
(
float
)
this
->
Height
,
0.0
f
,
0.0
f
,
1.0
f
));
ResourceManager
::
GetShader
(
"sprite3D"
).
SetMatrix4
(
"view"
,
glm
::
mat4
(
1.0
f
));
//create game menu
Option
option1
(
"GAME START"
);
...
...
@@ -104,7 +103,7 @@ void Game::Init()
void
Game
::
Update
(
float
dt
)
{
//std::cout << camPosition.x << " " << camPosition.y << " " << camPosition.z << std::endl;
ResourceManager
::
GetShader
(
"sprite3D"
).
SetMatrix4
(
"view"
,
glm
::
lookAt
(
camPosition
,
camPosition
*
camFront
,
worldUp
),
true
);
//
ResourceManager::GetShader("sprite3D").SetMatrix4("view", glm::lookAt(camPosition, camPosition * camFront, worldUp), true);
if
(
this
->
State
==
GAME_ACTIVE
)
{
for
(
auto
&
object
:
this
->
Levels
[
Level
]
->
Objects
)
{
object
->
Move
(
dt
,
this
->
Width
,
this
->
Height
);
...
...
@@ -157,35 +156,17 @@ void Game::ProcessInput(float dt)
this
->
KeysProcessed
[
GLFW_KEY_ENTER
]
=
true
;
}
float
camVelocity
=
20.0
f
;
if
(
this
->
Keys
[
GLFW_KEY_W
])
{
camPosition
.
z
-=
camVelocity
*
dt
;
}
if
(
this
->
Keys
[
GLFW_KEY_S
])
{
camPosition
.
z
+=
camVelocity
*
dt
;
}
if
(
this
->
Keys
[
GLFW_KEY_A
])
{
camPosition
.
x
-=
camVelocity
*
dt
;
}
if
(
this
->
Keys
[
GLFW_KEY_D
])
{
camPosition
.
x
+=
camVelocity
*
dt
;
}
std
::
string
direction
=
""
;
if
(
this
->
Keys
[
GLFW_KEY_LEFT
])
{
if
(
Player
->
Position
.
x
>=
0
)
{
if
(
Player
->
Position
.
x
>=
Player
->
Size
.
x
/
2.0
f
)
{
Player
->
Position
.
x
-=
Player
->
Velocity
.
x
*
dt
;
}
direction
=
"-left-"
;
}
if
(
this
->
Keys
[
GLFW_KEY_RIGHT
])
{
if
(
Player
->
Position
.
x
+
Player
->
Size
.
x
<=
this
->
Width
)
{
if
(
Player
->
Position
.
x
<=
this
->
Width
-
Player
->
Size
.
x
/
2.0
f
)
{
Player
->
Position
.
x
+=
Player
->
Velocity
.
x
*
dt
;
}
direction
=
"-right-"
;
...
...
@@ -285,7 +266,7 @@ void Game::Render()
void
Game
::
DoCollisions
()
{
for
(
auto
&
object
:
this
->
Levels
[
this
->
Level
]
->
Objects
)
{
if
(
!
object
->
Destroyed
&&
dynamic_cast
<
BallObject
*>
(
object
)
!=
nullptr
)
{
Collision
&
collisionPlayer
=
Player
->
checkCollision
(
*
object
);
Collision
&
collisionPlayer
=
object
->
checkCollision
(
*
Player
);
if
(
collisionPlayer
.
collision
)
{
--
this
->
Lives
;
SoundEngine
->
stopAllSounds
();
...
...
@@ -296,7 +277,7 @@ void Game::DoCollisions() {
}
for
(
auto
&
Weapon
:
Player
->
Weapons
)
{
Collision
&
collisionWeapon
=
Weapon
->
checkCollision
(
*
object
);
Collision
&
collisionWeapon
=
object
->
checkCollision
(
*
Weapon
);
if
(
Weapon
->
Using
&&
collisionWeapon
.
collision
)
{
if
(
dynamic_cast
<
BallObject
*>
(
object
)
!=
nullptr
)
{
object
->
Destroyed
=
true
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment