minecraft: collision demo
This commit is contained in:
parent
4805fcbfac
commit
e08c74e0c1
3
Makefile
3
Makefile
@ -2,7 +2,7 @@
|
|||||||
CC=$(PREFIX)gcc
|
CC=$(PREFIX)gcc
|
||||||
CXX=$(PREFIX)g++
|
CXX=$(PREFIX)g++
|
||||||
|
|
||||||
OPT = -O0 -march=x86-64-v3
|
OPT = -O2 -march=x86-64-v3
|
||||||
|
|
||||||
CSTD = -std=gnu23
|
CSTD = -std=gnu23
|
||||||
CXXSTD = -std=gnu++23
|
CXXSTD = -std=gnu++23
|
||||||
@ -12,6 +12,7 @@ CFLAGS += -I./include
|
|||||||
CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -Wno-error=unused-but-set-variable
|
CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -Wno-error=unused-but-set-variable
|
||||||
CFLAGS += -Wno-error=unknown-pragmas -Wno-unknown-pragmas
|
CFLAGS += -Wno-error=unknown-pragmas -Wno-unknown-pragmas
|
||||||
CFLAGS += $(shell pkg-config --cflags glfw3)
|
CFLAGS += $(shell pkg-config --cflags glfw3)
|
||||||
|
CFLAGS += -fno-strict-aliasing
|
||||||
|
|
||||||
LDFLAGS += -lm
|
LDFLAGS += -lm
|
||||||
LDFLAGS += $(shell pkg-config --libs glfw3)
|
LDFLAGS += $(shell pkg-config --libs glfw3)
|
||||||
|
|||||||
@ -174,7 +174,7 @@ namespace collision {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const float interval_epsilon = 0.01f;
|
static const float interval_epsilon = 0.001f;
|
||||||
|
|
||||||
static inline bool intersect_moving_sphere_aabb(Sphere const & sphere, XMVECTOR const & direction,
|
static inline bool intersect_moving_sphere_aabb(Sphere const & sphere, XMVECTOR const & direction,
|
||||||
float t0, float t1, AABB const & aabb,
|
float t0, float t1, AABB const & aabb,
|
||||||
@ -208,9 +208,36 @@ namespace collision {
|
|||||||
return AABB(min, max);
|
return AABB(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct state {
|
||||||
|
float t;
|
||||||
|
bool intersected;
|
||||||
|
XMVECTOR intersection_point;
|
||||||
|
XMVECTOR intersection_position;
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline void check_collision(collision::Sphere const & sphere, XMVECTOR const & direction,
|
||||||
|
XMVECTOR const & cube_center, float cube_half,
|
||||||
|
state & state)
|
||||||
|
{
|
||||||
|
collision::AABB aabb = collision::cube_aabb(cube_center, cube_half);
|
||||||
|
float t;
|
||||||
|
XMVECTOR intersection_point;
|
||||||
|
bool intersected = collision::intersect_moving_sphere_aabb(sphere, direction,
|
||||||
|
0, 1, aabb,
|
||||||
|
t, intersection_point);
|
||||||
|
XMVECTOR intersection_position = sphere.center + direction * t;
|
||||||
|
if (intersected && t < state.t) {
|
||||||
|
state.t = t;
|
||||||
|
state.intersected = true;
|
||||||
|
state.intersection_point = intersection_point;
|
||||||
|
state.intersection_position = intersection_position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static inline XMVECTOR sphere_collision_response(Sphere const & sphere, XMVECTOR const & direction,
|
static inline XMVECTOR sphere_collision_response(Sphere const & sphere, XMVECTOR const & direction,
|
||||||
XMVECTOR const & intersection_point,
|
XMVECTOR const & intersection_point,
|
||||||
XMVECTOR const & intersection_position)
|
XMVECTOR const & intersection_position,
|
||||||
|
XMVECTOR & intersection_normal)
|
||||||
{
|
{
|
||||||
XMVECTOR origin = intersection_point;
|
XMVECTOR origin = intersection_point;
|
||||||
XMVECTOR normal = XMVector3Normalize(intersection_position - intersection_point);
|
XMVECTOR normal = XMVector3Normalize(intersection_position - intersection_point);
|
||||||
@ -219,6 +246,8 @@ namespace collision {
|
|||||||
XMVECTOR new_destination = (destination - normal * distance) + normal * sphere.radius;
|
XMVECTOR new_destination = (destination - normal * distance) + normal * sphere.radius;
|
||||||
XMVECTOR new_direction = new_destination - intersection_position;
|
XMVECTOR new_direction = new_destination - intersection_position;
|
||||||
|
|
||||||
|
intersection_normal = normal;
|
||||||
|
|
||||||
return new_direction;
|
return new_direction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,11 +33,13 @@ namespace view {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace third_person {
|
namespace third_person {
|
||||||
void apply_transform(float forward, float strafe, float elevation,
|
XMVECTOR apply_transform(float forward, float strafe, float elevation,
|
||||||
float delta_yaw, float delta_pitch);
|
float delta_yaw, float delta_pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_fov(float delta);
|
void apply_fov(float delta);
|
||||||
void update_transforms();
|
void update_transforms();
|
||||||
void load();
|
void load();
|
||||||
|
|
||||||
|
const float at_distance = 10;
|
||||||
}
|
}
|
||||||
|
|||||||
269
minecraft/gen/icosphere.obj
Normal file
269
minecraft/gen/icosphere.obj
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
# Blender 5.0.0
|
||||||
|
# www.blender.org
|
||||||
|
o Icosphere
|
||||||
|
v 0.000000 -1.000000 0.000000
|
||||||
|
v 0.723607 -0.447220 0.525725
|
||||||
|
v -0.276388 -0.447220 0.850649
|
||||||
|
v -0.894426 -0.447216 0.000000
|
||||||
|
v -0.276388 -0.447220 -0.850649
|
||||||
|
v 0.723607 -0.447220 -0.525725
|
||||||
|
v 0.276388 0.447220 0.850649
|
||||||
|
v -0.723607 0.447220 0.525725
|
||||||
|
v -0.723607 0.447220 -0.525725
|
||||||
|
v 0.276388 0.447220 -0.850649
|
||||||
|
v 0.894426 0.447216 0.000000
|
||||||
|
v 0.000000 1.000000 0.000000
|
||||||
|
v -0.162456 -0.850654 0.499995
|
||||||
|
v 0.425323 -0.850654 0.309011
|
||||||
|
v 0.262869 -0.525738 0.809012
|
||||||
|
v 0.850648 -0.525736 0.000000
|
||||||
|
v 0.425323 -0.850654 -0.309011
|
||||||
|
v -0.525730 -0.850652 0.000000
|
||||||
|
v -0.688189 -0.525736 0.499997
|
||||||
|
v -0.162456 -0.850654 -0.499995
|
||||||
|
v -0.688189 -0.525736 -0.499997
|
||||||
|
v 0.262869 -0.525738 -0.809012
|
||||||
|
v 0.951058 0.000000 0.309013
|
||||||
|
v 0.951058 0.000000 -0.309013
|
||||||
|
v 0.000000 0.000000 1.000000
|
||||||
|
v 0.587786 0.000000 0.809017
|
||||||
|
v -0.951058 0.000000 0.309013
|
||||||
|
v -0.587786 0.000000 0.809017
|
||||||
|
v -0.587786 0.000000 -0.809017
|
||||||
|
v -0.951058 0.000000 -0.309013
|
||||||
|
v 0.587786 0.000000 -0.809017
|
||||||
|
v 0.000000 0.000000 -1.000000
|
||||||
|
v 0.688189 0.525736 0.499997
|
||||||
|
v -0.262869 0.525738 0.809012
|
||||||
|
v -0.850648 0.525736 0.000000
|
||||||
|
v -0.262869 0.525738 -0.809012
|
||||||
|
v 0.688189 0.525736 -0.499997
|
||||||
|
v 0.162456 0.850654 0.499995
|
||||||
|
v 0.525730 0.850652 0.000000
|
||||||
|
v -0.425323 0.850654 0.309011
|
||||||
|
v -0.425323 0.850654 -0.309011
|
||||||
|
v 0.162456 0.850654 -0.499995
|
||||||
|
vn 0.1024 -0.9435 0.3151
|
||||||
|
vn 0.7002 -0.6617 0.2680
|
||||||
|
vn -0.2680 -0.9435 0.1947
|
||||||
|
vn -0.2680 -0.9435 -0.1947
|
||||||
|
vn 0.1024 -0.9435 -0.3151
|
||||||
|
vn 0.9050 -0.3304 0.2680
|
||||||
|
vn 0.0247 -0.3304 0.9435
|
||||||
|
vn -0.8897 -0.3304 0.3151
|
||||||
|
vn -0.5746 -0.3304 -0.7488
|
||||||
|
vn 0.5346 -0.3304 -0.7779
|
||||||
|
vn 0.8026 -0.1256 0.5831
|
||||||
|
vn -0.3066 -0.1256 0.9435
|
||||||
|
vn -0.9921 -0.1256 -0.0000
|
||||||
|
vn -0.3066 -0.1256 -0.9435
|
||||||
|
vn 0.8026 -0.1256 -0.5831
|
||||||
|
vn 0.4089 0.6617 0.6284
|
||||||
|
vn -0.4713 0.6617 0.5831
|
||||||
|
vn -0.7002 0.6617 -0.2680
|
||||||
|
vn 0.0385 0.6617 -0.7488
|
||||||
|
vn 0.7240 0.6617 -0.1947
|
||||||
|
vn 0.2680 0.9435 -0.1947
|
||||||
|
vn 0.4911 0.7947 -0.3568
|
||||||
|
vn 0.4089 0.6617 -0.6284
|
||||||
|
vn -0.1024 0.9435 -0.3151
|
||||||
|
vn -0.1876 0.7947 -0.5773
|
||||||
|
vn -0.4713 0.6617 -0.5831
|
||||||
|
vn -0.3313 0.9435 -0.0000
|
||||||
|
vn -0.6071 0.7947 -0.0000
|
||||||
|
vn -0.7002 0.6617 0.2680
|
||||||
|
vn -0.1024 0.9435 0.3151
|
||||||
|
vn -0.1876 0.7947 0.5773
|
||||||
|
vn 0.0385 0.6617 0.7488
|
||||||
|
vn 0.2680 0.9435 0.1947
|
||||||
|
vn 0.4911 0.7947 0.3568
|
||||||
|
vn 0.7240 0.6617 0.1947
|
||||||
|
vn 0.8897 0.3304 -0.3151
|
||||||
|
vn 0.7947 0.1876 -0.5773
|
||||||
|
vn 0.5746 0.3304 -0.7488
|
||||||
|
vn -0.0247 0.3304 -0.9435
|
||||||
|
vn -0.3035 0.1876 -0.9342
|
||||||
|
vn -0.5346 0.3304 -0.7779
|
||||||
|
vn -0.9050 0.3304 -0.2680
|
||||||
|
vn -0.9822 0.1876 -0.0000
|
||||||
|
vn -0.9050 0.3304 0.2680
|
||||||
|
vn -0.5346 0.3304 0.7779
|
||||||
|
vn -0.3035 0.1876 0.9342
|
||||||
|
vn -0.0247 0.3304 0.9435
|
||||||
|
vn 0.5746 0.3304 0.7488
|
||||||
|
vn 0.7947 0.1876 0.5773
|
||||||
|
vn 0.8897 0.3304 0.3151
|
||||||
|
vn 0.3066 0.1256 -0.9435
|
||||||
|
vn 0.3035 -0.1876 -0.9342
|
||||||
|
vn 0.0247 -0.3304 -0.9435
|
||||||
|
vn -0.8026 0.1256 -0.5831
|
||||||
|
vn -0.7947 -0.1876 -0.5773
|
||||||
|
vn -0.8897 -0.3304 -0.3151
|
||||||
|
vn -0.8026 0.1256 0.5831
|
||||||
|
vn -0.7947 -0.1876 0.5773
|
||||||
|
vn -0.5746 -0.3304 0.7488
|
||||||
|
vn 0.3066 0.1256 0.9435
|
||||||
|
vn 0.3035 -0.1876 0.9342
|
||||||
|
vn 0.5346 -0.3304 0.7779
|
||||||
|
vn 0.9921 0.1256 -0.0000
|
||||||
|
vn 0.9822 -0.1876 -0.0000
|
||||||
|
vn 0.9050 -0.3304 -0.2680
|
||||||
|
vn 0.4713 -0.6617 -0.5831
|
||||||
|
vn 0.1876 -0.7947 -0.5773
|
||||||
|
vn -0.0385 -0.6617 -0.7488
|
||||||
|
vn -0.4089 -0.6617 -0.6284
|
||||||
|
vn -0.4911 -0.7947 -0.3568
|
||||||
|
vn -0.7240 -0.6617 -0.1947
|
||||||
|
vn -0.7240 -0.6617 0.1947
|
||||||
|
vn -0.4911 -0.7947 0.3568
|
||||||
|
vn -0.4089 -0.6617 0.6284
|
||||||
|
vn 0.7002 -0.6617 -0.2680
|
||||||
|
vn 0.6071 -0.7947 -0.0000
|
||||||
|
vn 0.3313 -0.9435 -0.0000
|
||||||
|
vn -0.0385 -0.6617 0.7488
|
||||||
|
vn 0.1876 -0.7947 0.5773
|
||||||
|
vn 0.4713 -0.6617 0.5831
|
||||||
|
vt 0.181819 0.000000
|
||||||
|
vt 0.227273 0.078731
|
||||||
|
vt 0.136365 0.078731
|
||||||
|
vt 0.272728 0.157461
|
||||||
|
vt 0.318182 0.078731
|
||||||
|
vt 0.363637 0.157461
|
||||||
|
vt 0.909091 0.000000
|
||||||
|
vt 0.954545 0.078731
|
||||||
|
vt 0.863636 0.078731
|
||||||
|
vt 0.727273 0.000000
|
||||||
|
vt 0.772727 0.078731
|
||||||
|
vt 0.681818 0.078731
|
||||||
|
vt 0.545455 0.000000
|
||||||
|
vt 0.590909 0.078731
|
||||||
|
vt 0.500000 0.078731
|
||||||
|
vt 0.318182 0.236191
|
||||||
|
vt 0.090910 0.157461
|
||||||
|
vt 0.181819 0.157461
|
||||||
|
vt 0.136365 0.236191
|
||||||
|
vt 0.818182 0.157461
|
||||||
|
vt 0.909091 0.157461
|
||||||
|
vt 0.863636 0.236191
|
||||||
|
vt 0.636364 0.157461
|
||||||
|
vt 0.727273 0.157461
|
||||||
|
vt 0.681818 0.236191
|
||||||
|
vt 0.454546 0.157461
|
||||||
|
vt 0.545455 0.157461
|
||||||
|
vt 0.500000 0.236191
|
||||||
|
vt 0.227273 0.236191
|
||||||
|
vt 0.045455 0.236191
|
||||||
|
vt 0.772727 0.236191
|
||||||
|
vt 0.590909 0.236191
|
||||||
|
vt 0.409092 0.236191
|
||||||
|
vt 0.181819 0.314921
|
||||||
|
vt 0.272728 0.314921
|
||||||
|
vt 0.227273 0.393651
|
||||||
|
vt 0.000000 0.314921
|
||||||
|
vt 0.090910 0.314921
|
||||||
|
vt 0.045455 0.393651
|
||||||
|
vt 0.727273 0.314921
|
||||||
|
vt 0.818182 0.314921
|
||||||
|
vt 0.772727 0.393651
|
||||||
|
vt 0.545455 0.314921
|
||||||
|
vt 0.636364 0.314921
|
||||||
|
vt 0.590909 0.393651
|
||||||
|
vt 0.363637 0.314921
|
||||||
|
vt 0.454546 0.314921
|
||||||
|
vt 0.409092 0.393651
|
||||||
|
vt 0.500000 0.393651
|
||||||
|
vt 0.454546 0.472382
|
||||||
|
vt 0.681818 0.393651
|
||||||
|
vt 0.636364 0.472382
|
||||||
|
vt 0.863636 0.393651
|
||||||
|
vt 0.818182 0.472382
|
||||||
|
vt 0.909091 0.314921
|
||||||
|
vt 0.136365 0.393651
|
||||||
|
vt 0.090910 0.472382
|
||||||
|
vt 0.318182 0.393651
|
||||||
|
vt 0.272728 0.472382
|
||||||
|
vt 0.954545 0.236191
|
||||||
|
vt 1.000000 0.157461
|
||||||
|
vt 0.409092 0.078731
|
||||||
|
vt 0.363637 0.000000
|
||||||
|
s 0
|
||||||
|
f 1/1/1 14/2/1 13/3/1
|
||||||
|
f 2/4/2 14/5/2 16/6/2
|
||||||
|
f 1/7/3 13/8/3 18/9/3
|
||||||
|
f 1/10/4 18/11/4 20/12/4
|
||||||
|
f 1/13/5 20/14/5 17/15/5
|
||||||
|
f 2/4/6 16/6/6 23/16/6
|
||||||
|
f 3/17/7 15/18/7 25/19/7
|
||||||
|
f 4/20/8 19/21/8 27/22/8
|
||||||
|
f 5/23/9 21/24/9 29/25/9
|
||||||
|
f 6/26/10 22/27/10 31/28/10
|
||||||
|
f 2/4/11 23/16/11 26/29/11
|
||||||
|
f 3/17/12 25/19/12 28/30/12
|
||||||
|
f 4/20/13 27/22/13 30/31/13
|
||||||
|
f 5/23/14 29/25/14 32/32/14
|
||||||
|
f 6/26/15 31/28/15 24/33/15
|
||||||
|
f 7/34/16 33/35/16 38/36/16
|
||||||
|
f 8/37/17 34/38/17 40/39/17
|
||||||
|
f 9/40/18 35/41/18 41/42/18
|
||||||
|
f 10/43/19 36/44/19 42/45/19
|
||||||
|
f 11/46/20 37/47/20 39/48/20
|
||||||
|
f 39/48/21 42/49/21 12/50/21
|
||||||
|
f 39/48/22 37/47/22 42/49/22
|
||||||
|
f 37/47/23 10/43/23 42/49/23
|
||||||
|
f 42/45/24 41/51/24 12/52/24
|
||||||
|
f 42/45/25 36/44/25 41/51/25
|
||||||
|
f 36/44/26 9/40/26 41/51/26
|
||||||
|
f 41/42/27 40/53/27 12/54/27
|
||||||
|
f 41/42/28 35/41/28 40/53/28
|
||||||
|
f 35/41/29 8/55/29 40/53/29
|
||||||
|
f 40/39/30 38/56/30 12/57/30
|
||||||
|
f 40/39/31 34/38/31 38/56/31
|
||||||
|
f 34/38/32 7/34/32 38/56/32
|
||||||
|
f 38/36/33 39/58/33 12/59/33
|
||||||
|
f 38/36/34 33/35/34 39/58/34
|
||||||
|
f 33/35/35 11/46/35 39/58/35
|
||||||
|
f 24/33/36 37/47/36 11/46/36
|
||||||
|
f 24/33/37 31/28/37 37/47/37
|
||||||
|
f 31/28/38 10/43/38 37/47/38
|
||||||
|
f 32/32/39 36/44/39 10/43/39
|
||||||
|
f 32/32/40 29/25/40 36/44/40
|
||||||
|
f 29/25/41 9/40/41 36/44/41
|
||||||
|
f 30/31/42 35/41/42 9/40/42
|
||||||
|
f 30/31/43 27/22/43 35/41/43
|
||||||
|
f 27/22/44 8/55/44 35/41/44
|
||||||
|
f 28/30/45 34/38/45 8/37/45
|
||||||
|
f 28/30/46 25/19/46 34/38/46
|
||||||
|
f 25/19/47 7/34/47 34/38/47
|
||||||
|
f 26/29/48 33/35/48 7/34/48
|
||||||
|
f 26/29/49 23/16/49 33/35/49
|
||||||
|
f 23/16/50 11/46/50 33/35/50
|
||||||
|
f 31/28/51 32/32/51 10/43/51
|
||||||
|
f 31/28/52 22/27/52 32/32/52
|
||||||
|
f 22/27/53 5/23/53 32/32/53
|
||||||
|
f 29/25/54 30/31/54 9/40/54
|
||||||
|
f 29/25/55 21/24/55 30/31/55
|
||||||
|
f 21/24/56 4/20/56 30/31/56
|
||||||
|
f 27/22/57 28/60/57 8/55/57
|
||||||
|
f 27/22/58 19/21/58 28/60/58
|
||||||
|
f 19/21/59 3/61/59 28/60/59
|
||||||
|
f 25/19/60 26/29/60 7/34/60
|
||||||
|
f 25/19/61 15/18/61 26/29/61
|
||||||
|
f 15/18/62 2/4/62 26/29/62
|
||||||
|
f 23/16/63 24/33/63 11/46/63
|
||||||
|
f 23/16/64 16/6/64 24/33/64
|
||||||
|
f 16/6/65 6/26/65 24/33/65
|
||||||
|
f 17/15/66 22/27/66 6/26/66
|
||||||
|
f 17/15/67 20/14/67 22/27/67
|
||||||
|
f 20/14/68 5/23/68 22/27/68
|
||||||
|
f 20/12/69 21/24/69 5/23/69
|
||||||
|
f 20/12/70 18/11/70 21/24/70
|
||||||
|
f 18/11/71 4/20/71 21/24/71
|
||||||
|
f 18/9/72 19/21/72 4/20/72
|
||||||
|
f 18/9/73 13/8/73 19/21/73
|
||||||
|
f 13/8/74 3/61/74 19/21/74
|
||||||
|
f 16/6/75 17/62/75 6/26/75
|
||||||
|
f 16/6/76 14/5/76 17/62/76
|
||||||
|
f 14/5/77 1/63/77 17/62/77
|
||||||
|
f 13/3/78 15/18/78 3/17/78
|
||||||
|
f 13/3/79 14/2/79 15/18/79
|
||||||
|
f 14/2/80 2/4/80 15/18/80
|
||||||
@ -5,7 +5,7 @@ def main():
|
|||||||
index_buffer = []
|
index_buffer = []
|
||||||
index_lookup = {}
|
index_lookup = {}
|
||||||
|
|
||||||
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "rounded-rectangle.obj")
|
obj_write.write_obj(vertex_buffer, index_buffer, index_lookup, "icosphere.obj")
|
||||||
|
|
||||||
with open("../non_block.idx", "wb") as f:
|
with open("../non_block.idx", "wb") as f:
|
||||||
obj_write.write_indices(f, "<H", index_buffer)
|
obj_write.write_indices(f, "<H", index_buffer)
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 600 B After Width: | Height: | Size: 480 B |
Binary file not shown.
@ -274,34 +274,8 @@ namespace collision_scene {
|
|||||||
};
|
};
|
||||||
static const int cubes_length = (sizeof (cubes)) / (sizeof (cubes[0]));
|
static const int cubes_length = (sizeof (cubes)) / (sizeof (cubes[0]));
|
||||||
|
|
||||||
struct collision_state {
|
|
||||||
float t;
|
|
||||||
bool intersected;
|
|
||||||
XMVECTOR intersection_point;
|
|
||||||
XMVECTOR intersection_position;
|
|
||||||
};
|
|
||||||
|
|
||||||
void check_collision(collision::Sphere const & sphere, XMVECTOR const & direction,
|
|
||||||
XMVECTOR const & cube_center,
|
|
||||||
collision_state & state)
|
|
||||||
{
|
|
||||||
collision::AABB aabb = collision::cube_aabb(cube_center, 0.5);
|
|
||||||
float t;
|
|
||||||
XMVECTOR intersection_point;
|
|
||||||
bool intersected = collision::intersect_moving_sphere_aabb(sphere, direction,
|
|
||||||
0, 1, aabb,
|
|
||||||
t, intersection_point);
|
|
||||||
XMVECTOR intersection_position = sphere.center + direction * t;
|
|
||||||
if (intersected && t < state.t) {
|
|
||||||
state.t = t;
|
|
||||||
state.intersected = true;
|
|
||||||
state.intersection_point = intersection_point;
|
|
||||||
state.intersection_position = intersection_position;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void check_collisions(collision::Sphere const & sphere, XMVECTOR const & direction,
|
void check_collisions(collision::Sphere const & sphere, XMVECTOR const & direction,
|
||||||
collision_state & state)
|
collision::state & state)
|
||||||
{
|
{
|
||||||
state.t = FLT_MAX;
|
state.t = FLT_MAX;
|
||||||
state.intersected = false;
|
state.intersected = false;
|
||||||
@ -320,7 +294,8 @@ namespace collision_scene {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
check_collision(sphere, direction, cube_center, state);
|
float cube_half = 0.5;
|
||||||
|
collision::check_collision(sphere, direction, cube_center, cube_half, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,14 +349,16 @@ namespace collision_scene {
|
|||||||
int intersections = 0;
|
int intersections = 0;
|
||||||
while (intersections < 10) {
|
while (intersections < 10) {
|
||||||
|
|
||||||
collision_state state;
|
collision::state state;
|
||||||
check_collisions(sphere, direction, state);
|
check_collisions(sphere, direction, state);
|
||||||
if (!state.intersected)
|
if (!state.intersected)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
XMVECTOR intersection_normal;
|
||||||
XMVECTOR new_direction = collision::sphere_collision_response(sphere, direction,
|
XMVECTOR new_direction = collision::sphere_collision_response(sphere, direction,
|
||||||
state.intersection_point,
|
state.intersection_point,
|
||||||
state.intersection_position);
|
state.intersection_position,
|
||||||
|
intersection_normal);
|
||||||
|
|
||||||
glUniform3f(location.uniform.base_color, 1.0, 0.5, 1.0);
|
glUniform3f(location.uniform.base_color, 1.0, 0.5, 1.0);
|
||||||
draw_line(transform, state.intersection_position, state.intersection_position + new_direction);
|
draw_line(transform, state.intersection_position, state.intersection_position + new_direction);
|
||||||
|
|||||||
@ -127,7 +127,8 @@ namespace non_block {
|
|||||||
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
||||||
|
|
||||||
void const * indices = (void *)(0);
|
void const * indices = (void *)(0);
|
||||||
int element_count = 300;
|
// element_count from vertex_buffer_non_block.py stdout
|
||||||
|
int element_count = 240;
|
||||||
glDrawElements(GL_TRIANGLES, element_count, GL_UNSIGNED_SHORT, indices);
|
glDrawElements(GL_TRIANGLES, element_count, GL_UNSIGNED_SHORT, indices);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
src/test.cpp
80
src/test.cpp
@ -18,6 +18,7 @@
|
|||||||
#include "hud.h"
|
#include "hud.h"
|
||||||
#include "lighting.h"
|
#include "lighting.h"
|
||||||
#include "collision_scene.h"
|
#include "collision_scene.h"
|
||||||
|
#include "collision.h"
|
||||||
|
|
||||||
struct line_location {
|
struct line_location {
|
||||||
struct {
|
struct {
|
||||||
@ -301,6 +302,42 @@ void update_keyboard(int up, int down, int left, int right,
|
|||||||
i, k, j, l);
|
i, k, j, l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void check_collisions(collision::Sphere const & sphere, XMVECTOR const & direction,
|
||||||
|
collision::state & state)
|
||||||
|
{
|
||||||
|
state.t = FLT_MAX;
|
||||||
|
state.intersected = false;
|
||||||
|
|
||||||
|
collision::AABB sphere_aabb = collision::moving_sphere_aabb(sphere, direction);
|
||||||
|
XMVECTOR min_floor = XMVectorFloor(sphere_aabb.min);
|
||||||
|
XMVECTOR max_ceiling = XMVectorCeiling(sphere_aabb.max);
|
||||||
|
|
||||||
|
int min_x = XMVectorGetX(min_floor);
|
||||||
|
int min_y = XMVectorGetZ(min_floor); // swizzle
|
||||||
|
int min_z = XMVectorGetY(min_floor); // swizzle
|
||||||
|
|
||||||
|
int max_x = XMVectorGetX(max_ceiling);
|
||||||
|
int max_y = XMVectorGetZ(max_ceiling); // swizzle
|
||||||
|
int max_z = XMVectorGetY(max_ceiling); // swizzle
|
||||||
|
|
||||||
|
for (int x = min_x; x <= max_x; x++) {
|
||||||
|
for (int y = min_y; y <= max_y; y++) {
|
||||||
|
for (int z = min_z; z <= max_z; z++) {
|
||||||
|
global_entry * const entry = world_lookup(x, y, z);
|
||||||
|
if (entry == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// there is a block at x, y, z
|
||||||
|
XMVECTOR cube_center = XMVectorSet(x, z, y, 1); // swizzle
|
||||||
|
float cube_half = 0.5f;
|
||||||
|
collision::check_collision(sphere, direction,
|
||||||
|
cube_center, cube_half,
|
||||||
|
state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const int max_joysticks = 8;
|
const int max_joysticks = 8;
|
||||||
|
|
||||||
void update_joystick(int joystick_index,
|
void update_joystick(int joystick_index,
|
||||||
@ -315,10 +352,45 @@ void update_joystick(int joystick_index,
|
|||||||
float elevation = (tl - tr) * 0.5;
|
float elevation = (tl - tr) * 0.5;
|
||||||
float delta_yaw = rx * -0.035;
|
float delta_yaw = rx * -0.035;
|
||||||
float delta_pitch = ry * -0.035;
|
float delta_pitch = ry * -0.035;
|
||||||
view::third_person::apply_transform(forward, strafe, elevation,
|
|
||||||
delta_yaw, delta_pitch);
|
XMVECTOR direction = view::third_person::apply_transform(forward, strafe, elevation,
|
||||||
|
delta_yaw, delta_pitch);
|
||||||
view::apply_fov(0.01 * up + -0.01 * down);
|
view::apply_fov(0.01 * up + -0.01 * down);
|
||||||
|
|
||||||
|
XMVECTOR sphere_position = view::state.at;
|
||||||
|
|
||||||
|
float sphere_radius = 0.48;
|
||||||
|
collision::Sphere sphere(sphere_position, sphere_radius);
|
||||||
|
int intersections = 0;
|
||||||
|
collision::state state;
|
||||||
|
while (intersections < 500) {
|
||||||
|
check_collisions(sphere, direction, state);
|
||||||
|
if (!state.intersected)
|
||||||
|
break;
|
||||||
|
|
||||||
|
XMVECTOR intersection_normal;
|
||||||
|
XMVECTOR new_direction = collision::sphere_collision_response(sphere, direction,
|
||||||
|
state.intersection_point,
|
||||||
|
state.intersection_position,
|
||||||
|
intersection_normal);
|
||||||
|
// collide again
|
||||||
|
sphere.center = state.intersection_position + intersection_normal * 0.01f;
|
||||||
|
direction = new_direction * 0.9f;
|
||||||
|
if (XMVectorGetX(XMVector3Length(direction)) < 0.001f) {
|
||||||
|
direction = XMVectorZero();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
intersections += 1;
|
||||||
|
}
|
||||||
|
if (intersections == 500) { // too many recursive collisions
|
||||||
|
printf("too many recursive collisions\n");
|
||||||
|
} else {
|
||||||
|
// apply the last direction impulse
|
||||||
|
view::state.at = sphere.center + direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
view::state.eye = view::state.at - view::state.direction * view::at_distance;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
lighting.quadratic += 0.01 * a + -0.01 * b;
|
lighting.quadratic += 0.01 * a + -0.01 * b;
|
||||||
if (lighting.quadratic < 0.0f)
|
if (lighting.quadratic < 0.0f)
|
||||||
@ -466,7 +538,6 @@ void draw()
|
|||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
glClearDepth(-1.0f);
|
glClearDepth(-1.0f);
|
||||||
|
|
||||||
/*
|
|
||||||
// possibly re-initialize geometry buffer if window width/height changes
|
// possibly re-initialize geometry buffer if window width/height changes
|
||||||
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
|
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
|
||||||
|
|
||||||
@ -483,11 +554,12 @@ void draw()
|
|||||||
lighting::draw();
|
lighting::draw();
|
||||||
//draw_quad();
|
//draw_quad();
|
||||||
hud::draw();
|
hud::draw();
|
||||||
*/
|
|
||||||
|
|
||||||
|
/*
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
collision_scene::draw();
|
collision_scene::draw();
|
||||||
|
*/
|
||||||
|
|
||||||
last_frame_time = current_time;
|
last_frame_time = current_time;
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/view.cpp
15
src/view.cpp
@ -8,8 +8,6 @@ constexpr bool third_person = false;
|
|||||||
namespace view {
|
namespace view {
|
||||||
view_state state = {};
|
view_state state = {};
|
||||||
|
|
||||||
const float at_distance = 10;
|
|
||||||
|
|
||||||
static inline XMMATRIX current_projection()
|
static inline XMMATRIX current_projection()
|
||||||
{
|
{
|
||||||
float fov_angle_y = XMConvertToRadians(45 * state.fov);
|
float fov_angle_y = XMConvertToRadians(45 * state.fov);
|
||||||
@ -61,8 +59,8 @@ namespace view {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace third_person {
|
namespace third_person {
|
||||||
void apply_transform(float forward, float strafe, float elevation,
|
XMVECTOR apply_transform(float forward, float strafe, float elevation,
|
||||||
float delta_yaw, float delta_pitch)
|
float delta_yaw, float delta_pitch)
|
||||||
{
|
{
|
||||||
state.pitch = clamp_pitch(delta_pitch);
|
state.pitch = clamp_pitch(delta_pitch);
|
||||||
|
|
||||||
@ -70,8 +68,11 @@ namespace view {
|
|||||||
state.normal = get_normal(); // on forward change
|
state.normal = get_normal(); // on forward change
|
||||||
state.direction = get_direction(); // on forward/normal/pitch change
|
state.direction = get_direction(); // on forward/normal/pitch change
|
||||||
|
|
||||||
|
/*
|
||||||
state.at += state.forward * forward + state.normal * strafe + state.up * elevation;
|
state.at += state.forward * forward + state.normal * strafe + state.up * elevation;
|
||||||
state.eye = state.at - state.direction * at_distance;
|
state.eye = state.at - state.direction * at_distance;
|
||||||
|
*/
|
||||||
|
return state.forward * forward + state.normal * strafe + state.up * elevation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,14 +97,14 @@ namespace view {
|
|||||||
state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
|
state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
state.fov = 1.5;
|
state.fov = 1.5;
|
||||||
state.pitch = 0;
|
state.pitch = -0.7;
|
||||||
|
|
||||||
state.forward = XMVector3Normalize(XMVectorSet(-0.63, 0.78, 0, 0));
|
state.forward = XMVector3Normalize(XMVectorSet(-0.64, 0.77, 0, 0));
|
||||||
state.normal = get_normal(); // on forward change
|
state.normal = get_normal(); // on forward change
|
||||||
state.direction = get_direction(); // on forward/normal/pitch change
|
state.direction = get_direction(); // on forward/normal/pitch change
|
||||||
|
|
||||||
// position
|
// position
|
||||||
state.eye = XMVectorSet(-55.5f, 48.25f, 50.0f, 1);
|
state.eye = XMVectorSet(-45.5f, 43.25f, 63.0f, 1);
|
||||||
state.at = state.eye + state.direction * at_distance;
|
state.at = state.eye + state.direction * at_distance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user