Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
GameDevWeek
Sommersemester 2015
Cpp
Deth Buff Arr
Commits
a937181f
Commit
a937181f
authored
Sep 22, 2015
by
Elias Broschin
Browse files
sphere collision
parents
12b96ca0
fa98676e
Changes
9
Hide whitespace changes
Inline
Side-by-side
include/physics/collision/collision_component.h
View file @
a937181f
...
...
@@ -5,6 +5,7 @@
#include
<physics/collision/collision_shape.h>
#include
<util/id.hpp>
#include
<functional>
#include
<memory>
namespace
gdw
{
...
...
@@ -13,10 +14,10 @@ namespace gdw{
class
collision_component
:
public
component
{
public:
collision_component
(
engine
&
engine
,
entity
&
owner
,
const
gdw
::
collision_shape
&
col_shape
);
collision_component
(
engine
&
engine
,
entity
&
owner
,
gdw
::
collision_shape
*
col_shape
);
~
collision_component
();
gdw
::
collision_shape
&
collision_shape
()
{
return
collision_shape_
;}
gdw
::
collision_shape
*
collision_shape
()
{
return
collision_shape_
;}
void
set_trigger
(
std
::
function
<
void
()
>
trigger
)
{
trigger_
=
trigger
;}
void
call_trigger
()
{
trigger_
();}
...
...
@@ -24,7 +25,7 @@ namespace gdw{
return
gdw
::
type_id
<
gdw
::
collision_component
>
();
}
private:
gdw
::
collision_shape
collision_shape_
;
gdw
::
collision_shape
*
collision_shape_
;
std
::
function
<
void
()
>
trigger_
;
};
}
...
...
include/physics/collision/collision_shape.h
View file @
a937181f
...
...
@@ -15,7 +15,7 @@ namespace gdw{
virtual
~
collision_shape
();
shape_type
type
()
{
return
type_
;}
const
entity
*
owner
()
{
return
owner_
;}
const
entity
*
owner
()
const
{
return
owner_
;}
private:
friend
class
collision_component
;
void
set_owner
(
const
entity
*
owner
)
{
owner_
=
owner
;}
...
...
include/physics/collision/sphere.h
View file @
a937181f
...
...
@@ -2,12 +2,13 @@
#define SPHERE_
#include
<physics/collision/collision_shape.h>
#include
<iostream>
namespace
gdw
{
class
sphere
:
public
collision_shape
{
public:
sphere
(
glm
::
vec3
pos
ition
,
float
rad
ius
)
:
collision_shape
(
shape_type
::
sphere
),
position
(
pos
ition
),
radius
(
rad
ius
){}
sphere
(
glm
::
vec3
pos
,
float
rad
)
:
collision_shape
(
shape_type
::
sphere
),
position
(
pos
),
radius
(
rad
){}
~
sphere
()
=
default
;
public:
glm
::
vec3
position
;
...
...
include/physics/physics_system.h
View file @
a937181f
...
...
@@ -19,7 +19,7 @@ public:
void
narrow_phase
();
template
<
shape_type
A
,
shape_type
B
>
bool
checkCollisions
(
collision_shape
s1
,
collision_shape
s2
);
bool
checkCollisions
(
const
collision_shape
*
s1
,
const
collision_shape
*
s2
);
private:
void
register_movement_component
(
movement_component
*
comp
);
...
...
include/util/make_unique.hpp
View file @
a937181f
...
...
@@ -5,7 +5,7 @@
template
<
typename
t
,
typename
...
arguments
>
std
::
unique_ptr
<
t
>
make_unique
(
arguments
&&
...
args
)
{
return
std
::
unique_ptr
<
t
>
(
new
t
(
std
::
forward
<
arguments
>
(
args
)...));
return
std
::
unique_ptr
<
t
>
(
new
t
(
std
::
forward
<
arguments
>
(
args
)...));
}
#endif
src/core/engine.cpp
View file @
a937181f
...
...
@@ -25,6 +25,7 @@ namespace gdw {
SDL_ClearError
();
throw
std
::
runtime_error
(
"could not initialize SDL"
+
sdl_error
);
}
physics_system_
=
make_unique
<
gdw
::
physics_system
>
(
*
this
);
audio_
=
make_unique
<
gdw
::
audio
>
(
*
this
);
graphics_system_
=
make_unique
<
gdw
::
graphics_system
>
(
*
this
);
...
...
src/game_state_machine/play_state.cpp
View file @
a937181f
...
...
@@ -12,6 +12,8 @@
#include
<physics/collision/collision_component.h>
#include
<physics/collision/sphere.h>
#include
<iostream>
#include
<util/make_unique.hpp>
#include
<util/logger.hpp>
namespace
gdw
{
...
...
@@ -22,17 +24,19 @@ play_state::play_state(engine &engine):game_state(engine) {
play_state
::~
play_state
()
{}
void
play_state
::
on_enter
()
{
wcube_
=
&
engine_
.
entity_manager
().
emplace_back
(
glm
::
vec3
(
0.
f
),
glm
::
angleAxis
(
0.
f
,
glm
::
vec3
(
0.
f
)));
wcube_
->
emplace_back
<
staticmesh_component
>
(
"mesh/cube.msh"
);
auto
&
wcube_col_
=
wcube_
->
emplace_back
<
collision_component
>
(
sphere
(
glm
::
vec3
(
0.
f
),
1.2
f
));
auto
&
wcube_col_
=
wcube_
->
emplace_back
<
collision_component
>
(
new
sphere
(
glm
::
vec3
(
0.
f
),
1.2
f
));
wcube_col_
.
set_trigger
([](){
std
::
cout
<<
"trigger 1"
<<
std
::
endl
;});
wcube_move_
=
&
wcube_
->
emplace_back
<
movement_component
>
();
wcube2_
=
&
engine_
.
entity_manager
().
emplace_back
(
glm
::
vec3
(
0.
f
),
glm
::
angleAxis
(
0.
f
,
glm
::
vec3
(
0.
f
)));
wcube2_
->
emplace_back
<
staticmesh_component
>
(
"mesh/cube.msh"
);
auto
&
wcube_col2_
=
wcube2_
->
emplace_back
<
collision_component
>
(
sphere
(
glm
::
vec3
(
0.
f
),
1.2
f
));
auto
&
wcube_col2_
=
wcube2_
->
emplace_back
<
collision_component
>
(
new
sphere
(
glm
::
vec3
(
0.
f
),
1.2
f
));
wcube_col2_
.
set_trigger
([](){
std
::
cout
<<
"trigger 2"
<<
std
::
endl
;});
wcube2_move_
=
&
wcube2_
->
emplace_back
<
movement_component
>
();
// wcube_move_->set_damping(0.001f);
// wcube_move_->set_mass(0.01f);
}
...
...
src/physics/collision/collision_component.cpp
View file @
a937181f
...
...
@@ -7,10 +7,11 @@
namespace
gdw
{
collision_component
::
collision_component
(
engine
&
engine
,
entity
&
owner
,
const
gdw
::
collision_shape
&
col_shape
)
:
component
(
engine
,
owner
)
,
collision_shape_
(
col_shape
)
{
collision_component
::
collision_component
(
engine
&
engine
,
entity
&
owner
,
gdw
::
collision_shape
*
col_shape
)
:
component
(
engine
,
owner
)
{
engine_
.
physics_system
().
register_collision_component
(
this
);
collision_shape_
.
set_owner
(
&
owner
);
collision_shape_
=
col_shape
;
collision_shape_
->
set_owner
(
&
owner
);
}
collision_component
::~
collision_component
()
{
...
...
src/physics/physics_system.cpp
View file @
a937181f
...
...
@@ -4,8 +4,11 @@
#include
<physics/movement/movement_component.h>
#include
<physics/collision/collision_component.h>
#include
<iostream>
#include
<physics/collision/sphere.h>
#include
<ecs/entity.hpp>
#include
<physics/collision/sphere.h>
#include
<physics/collision/ray.h>
#include
<glm/gtx/norm.hpp>
#include
<glm/gtx/intersect.hpp>
namespace
gdw
{
...
...
@@ -18,18 +21,28 @@ physics_system::~physics_system() {
}
template
<
>
bool
physics_system
::
checkCollisions
<
shape_type
::
sphere
,
shape_type
::
sphere
>
(
collision_shape
s1
,
collision_shape
s2
)
{
//IMPLEMENT RAY X SPHERE CHECK HERE
return
false
;
bool
physics_system
::
checkCollisions
<
shape_type
::
sphere
,
shape_type
::
sphere
>
(
const
collision_shape
*
s1
,
const
collision_shape
*
s2
)
{
const
sphere
*
one
=
static_cast
<
const
sphere
*>
(
s1
);
const
sphere
*
two
=
static_cast
<
const
sphere
*>
(
s2
);
return
glm
::
distance2
(
one
->
owner
()
->
position
()
+
one
->
position
,
two
->
owner
()
->
position
()
+
two
->
position
)
<=
((
one
->
radius
+
two
->
radius
)
*
(
one
->
radius
+
two
->
radius
));
}
template
<
>
bool
physics_system
::
checkCollisions
<
shape_type
::
ray
,
shape_type
::
sphere
>
(
collision_shape
s1
,
collision_shape
s2
)
{
bool
physics_system
::
checkCollisions
<
shape_type
::
ray
,
shape_type
::
sphere
>
(
const
collision_shape
*
s1
,
const
collision_shape
*
s2
)
{
//IMPLEMENT RAY X SPHERE CHECK HERE
return
false
;
const
ray
*
one
=
static_cast
<
const
ray
*>
(
s1
);
const
sphere
*
two
=
static_cast
<
const
sphere
*>
(
s2
);
float
dist
=
0.
f
;
return
glm
::
intersectRaySphere
(
one
->
owner
()
->
position
()
+
one
->
source
,
one
->
owner
()
->
position
()
+
one
->
destination
,
two
->
owner
()
->
position
()
+
two
->
position
,
two
->
radius
,
dist
);
}
template
<
>
bool
physics_system
::
checkCollisions
<
shape_type
::
sphere
,
shape_type
::
ray
>
(
collision_shape
s1
,
collision_shape
s2
)
{
bool
physics_system
::
checkCollisions
<
shape_type
::
sphere
,
shape_type
::
ray
>
(
const
collision_shape
*
s1
,
const
collision_shape
*
s2
)
{
return
checkCollisions
<
shape_type
::
ray
,
shape_type
::
sphere
>
(
s2
,
s1
);
}
...
...
@@ -37,16 +50,17 @@ void physics_system::update(float dt) {
for
(
auto
&
move
:
movement_components_
)
move
->
update
(
dt
);
std
::
cout
<<
"frame"
<<
std
::
endl
;
narrow_phase
();
}
void
physics_system
::
narrow_phase
()
{
for
(
int
i
=
0
;
i
<
collision_components_
.
size
();
i
++
)
{
auto
&
c1
=
collision_components_
[
i
]
->
collision_shape
();
auto
t1
=
c1
.
type
();
for
(
int
j
=
i
;
j
<
collision_components_
.
size
();
j
++
)
{
auto
&
c2
=
collision_components_
[
j
]
->
collision_shape
();
auto
t2
=
c2
.
type
();
auto
c1
=
collision_components_
[
i
]
->
collision_shape
();
auto
t1
=
c1
->
type
();
for
(
int
j
=
i
+
1
;
j
<
collision_components_
.
size
();
j
++
)
{
auto
c2
=
collision_components_
[
j
]
->
collision_shape
();
auto
t2
=
c2
->
type
();
bool
collision_occured
=
false
;
if
(
t1
==
shape_type
::
sphere
&&
t2
==
shape_type
::
sphere
)
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment