A 3D game engine from scratch.
1// (c) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
2
3#include "logs.hpp"
4#include "state.hpp"
5#include "behavior_functions.hpp"
6
7
8void
9behavior_functions::test(entities::Handle entity_handle)
10{
11 spatial::Component *spatial_component = spatial::get_component(entity_handle);
12 if (!spatial_component) {
13 logs::error("Could not get spatial::Component for behavior::Component");
14 return;
15 }
16
17 spatial_component->rotation =
18 glm::angleAxis((f32)sin(1.0f - (engine::get_t())), v3(0.0f, 1.0f, 0.0f)) *
19 glm::angleAxis((f32)cos(1.0f - (engine::get_t())), v3(1.0f, 0.0f, 0.0f));
20}
21
22
23void
24behavior_functions::char_movement_test(entities::Handle entity_handle)
25{
26 spatial::Component *spatial_component = spatial::get_component(entity_handle);
27 if (!spatial_component) {
28 logs::error("Could not get spatial::Component for behavior::Component");
29 return;
30 }
31
32 physics::Component *physics_component = physics::get_component(entity_handle);
33 if (!physics_component) {
34 logs::error("Could not get physics::Component for behavior::Component");
35 return;
36 }
37 spatial::Obb *obb = &physics_component->transformed_obb;
38
39 // Update position
40 spatial_component->position.x =
41 (f32)sin((engine::get_t()) * 1.0f) * 4.0f +
42 (f32)sin((engine::get_t()) * 2.0f) * 0.1f +
43 (f32)cos((engine::get_t()) * 3.0f) * 0.3f;
44 spatial_component->position.z =
45 (f32)cos((engine::get_t()) * 1.0f) * 4.0f +
46 (f32)cos((engine::get_t()) * 2.0f) * 0.3f +
47 (f32)sin((engine::get_t()) * 3.0f) * 0.1f;
48 spatial_component->rotation =
49 glm::angleAxis(
50 (f32)sin((engine::get_t()) * 3.0f) + radians(70.0f), v3(0.0f, 1.0f, 0.0f)
51 ) *
52 glm::angleAxis(
53 (f32)cos((engine::get_t()) * 2.0f) / 3.0f, v3(0.0f, 1.0f, 0.0f)
54 ) *
55 glm::angleAxis((f32)cos((engine::get_t()) * 2.0f), v3(1.0f, 0.0f, 0.0f)) *
56 glm::angleAxis((f32)sin((engine::get_t()) * 1.5f) / 2.0f, v3(1.0f, 0.0f, 0.0f)) *
57 glm::angleAxis((f32)sin((engine::get_t()) * 2.5f) / 1.5f, v3(0.5f, 0.5f, 0.2f));
58#if 0
59 spatial_component->position.x = -5.0f;
60 spatial_component->position.z = -5.0f;
61 spatial_component->rotation =
62 glm::angleAxis((f32)sin((engine::get_t())) + radians(70.0f), v3(0.0f, 1.0f, 0.0f)) *
63 glm::angleAxis(radians(90.0f), v3(1.0f, 0.0f, 0.0f));
64#endif
65
66 // Check collision with other entities
67 {
68 physics::CollisionManifold manifold = physics::find_collision(
69 physics_component, spatial_component);
70
71 if (manifold.did_collide) {
72 v4 color;
73 if (manifold.axis <= 5) {
74 color = v4(1.0f, 0.0f, 0.0f, 1.0f);
75 } else {
76 color = v4(1.0f, 1.0f, 0.0f, 1.0f);
77 }
78 debugdraw::draw_obb(obb, color);
79 debugdraw::draw_obb(&manifold.collidee->transformed_obb, color);
80 debugdraw::draw_line(obb->center,
81 obb->center + manifold.normal * 100.0f, color);
82 gui::log("manifold.axis = %d", manifold.axis);
83 gui::log("manifold.sep_max = %f", manifold.sep_max);
84 gui::log("manifold.normal = (%f, %f, %f)",
85 manifold.normal.x, manifold.normal.y, manifold.normal.z);
86 gui::log("length(manifold.normal) = %f", length(manifold.normal));
87 gui::log("---");
88 } else {
89 debugdraw::draw_obb(obb, v4(1.0f, 1.0f, 1.0f, 1.0f));
90 }
91 }
92
93 // Check ray collision
94#if 0
95 {
96 spatial::Ray ray = {
97 .origin = obb->center + obb->y_axis * obb->extents[1],
98 .direction = obb->y_axis,
99 };
100 RayCollisionResult ray_collision_result = physics::find_ray_collision(
101 &ray, physics_component);
102
103 if (ray_collision_result.did_intersect) {
104 debugdraw::draw_ray(&ray, ray_collision_result.distance,
105 v4(1.0f, 0.0f, 0.0f, 0.0f));
106 debugdraw::draw_obb(&ray_collision_result.collidee->transformed_obb,
107 v4(1.0f, 0.0f, 0.0f, 1.0f));
108 } else {
109 debugdraw::draw_ray(&ray, 500.0f,
110 v4(1.0f, 1.0f, 1.0f, 0.0f));
111 }
112 }
113#endif
114}