diff --git a/Makefile b/Makefile index 726d0bc..5a7b1e7 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=$(PREFIX)gcc CXX=$(PREFIX)g++ -OPT = -O0 -march=x86-64-v3 +OPT = -O2 -march=x86-64-v3 CSTD = -std=gnu23 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 += -Wno-error=unknown-pragmas -Wno-unknown-pragmas CFLAGS += $(shell pkg-config --cflags glfw3) +CFLAGS += -fno-strict-aliasing LDFLAGS += -lm LDFLAGS += $(shell pkg-config --libs glfw3) diff --git a/include/collision.h b/include/collision.h index f9fa5d4..430afd6 100644 --- a/include/collision.h +++ b/include/collision.h @@ -174,7 +174,7 @@ namespace collision { 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, float t0, float t1, AABB const & aabb, @@ -208,9 +208,36 @@ namespace collision { 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, XMVECTOR const & intersection_point, - XMVECTOR const & intersection_position) + XMVECTOR const & intersection_position, + XMVECTOR & intersection_normal) { XMVECTOR origin = 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_direction = new_destination - intersection_position; + intersection_normal = normal; + return new_direction; } } diff --git a/include/view.h b/include/view.h index 15a9cd2..47c04ed 100644 --- a/include/view.h +++ b/include/view.h @@ -33,11 +33,13 @@ namespace view { } namespace third_person { - void apply_transform(float forward, float strafe, float elevation, - float delta_yaw, float delta_pitch); + XMVECTOR apply_transform(float forward, float strafe, float elevation, + float delta_yaw, float delta_pitch); } void apply_fov(float delta); void update_transforms(); void load(); + + const float at_distance = 10; } diff --git a/minecraft/gen/icosphere.obj b/minecraft/gen/icosphere.obj new file mode 100644 index 0000000..c947d3f --- /dev/null +++ b/minecraft/gen/icosphere.obj @@ -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 diff --git a/minecraft/gen/vertex_buffer_non_block.py b/minecraft/gen/vertex_buffer_non_block.py index cb3e046..773c827 100644 --- a/minecraft/gen/vertex_buffer_non_block.py +++ b/minecraft/gen/vertex_buffer_non_block.py @@ -5,7 +5,7 @@ def main(): index_buffer = [] 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: obj_write.write_indices(f, "