acf4ed3cd96c6e46183b3995a81093075a51601d
[carveJwlIkooP6JGAAIwe30JlM.git] / physics_test.h
1 #ifndef PHYSICS_TEST_H
2 #define PHYSICS_TEST_H
3
4 #include "rigidbody.h"
5 #include "player.h"
6
7 rigidbody ground = { .type = k_rb_shape_box,
8 .bbx = {{-100.0f,-1.0f,-100.0f},{100.0f,0.0f,100.0f}},
9 .co = {0.0f, 0.0f, 0.0f},
10 .q = {0.0f,0.0f,0.0f,1.0f},
11 .is_world = 1 };
12
13 rigidbody blocky =
14 {
15 .type = k_rb_shape_box,
16 .bbx = {{-2.0f,-1.0f,-3.0f},{2.0f,1.0f,2.0f}},
17 .co = {30.0f,2.0f,30.0f},
18 .q = {0.0f,0.0f,0.0f,1.0f},
19 .is_world = 1
20 };
21
22 rigidbody marko =
23 {
24 .type = k_rb_shape_box,
25 .bbx = {{-2.0f,-2.0f,-2.0f},{2.0f,2.0f,2.0f}},
26 .co = {-36.0f,8.0f,-36.0f},
27 .q = {0.0f,0.0f,0.0f,1.0f},
28 .is_world = 0
29 };
30
31 scene epic_scene;
32
33 rigidbody epic_scene_rb =
34 {
35 .type = k_rb_shape_scene,
36 .co = {0.0f,0.0f,0.0f},
37 .q = {0.0f,0.0f,0.0f,1.0f},
38 .is_world = 1,
39 .inf.scene = { .pscene = &epic_scene }
40 };
41
42 rigidbody funnel[4] = {
43 {
44 .type = k_rb_shape_box,
45 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
46 .co = {-10.0f,5.0f,0.0f},
47 .is_world = 1
48 },
49 {
50 .type = k_rb_shape_box,
51 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
52 .co = { 10.0f,5.0f,0.0f},
53 .is_world = 1
54 },
55 {
56 .type = k_rb_shape_box,
57 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
58 .co = { 0.0f,5.0f,10.0f},
59 .is_world = 1
60 },
61 {
62 .type = k_rb_shape_box,
63 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
64 .co = {0.0f,5.0f,-10.0f},
65 .is_world = 1
66 }
67 };
68
69 rigidbody jeff1 = { .type = k_rb_shape_capsule,
70 .inf.capsule = { .radius = 0.75f, .height = 3.0f },
71 .co = {30.0f, 4.0f, 30.0f },
72 .q = {1.0f,0.0f,0.0f,0.0f}
73 };
74
75 rigidbody ball = { .type = k_rb_shape_sphere,
76 .inf.sphere = { .radius = 4.0f },
77 .co = {0.0f,20.0f,2.0f},
78 .q = {0.0f,0.0f,0.0f,1.0f}},
79
80 ball1= { .type = k_rb_shape_sphere,
81 .inf.sphere = { .radius = 2.0f },
82 .co = {0.1f,25.0f,0.2f},
83 .q = {0.0f,0.0f,0.0f,1.0f}};
84
85 rigidbody jeffs[16];
86
87 static void reorg_jeffs(void)
88 {
89 for( int i=0; i<vg_list_size(jeffs); i++ )
90 {
91 v3_zero( jeffs[i].v );
92 v3_zero( jeffs[i].w );
93 v3_copy( (v3f){ (vg_randf()-0.5f) * 10.0f,
94 (vg_randf()-0.5f) * 10.0f + 17.0f,
95 (vg_randf()-0.5f) * 10.0f }, jeffs[i].co );
96 v4_copy( (v4f){ vg_randf(), vg_randf(), vg_randf(), vg_randf() },
97 jeffs[i].q );
98 q_normalize( jeffs[i].q );
99
100 jeffs[i].type = k_rb_shape_capsule;
101 jeffs[i].inf.capsule.radius = 0.75f;
102 jeffs[i].inf.capsule.height = 8.0f;
103
104 rb_init( &jeffs[i] );
105 }
106 }
107
108 static void physics_test_start(void)
109 {
110 q_axis_angle( funnel[0].q, (v3f){1.0f,0.0f,0.0f}, 0.6f );
111 q_axis_angle( funnel[1].q, (v3f){1.0f,0.0f,0.0f}, -0.6f );
112 q_axis_angle( funnel[2].q, (v3f){0.0f,0.0f,1.0f}, 0.6f );
113 q_axis_angle( funnel[3].q, (v3f){0.0f,0.0f,1.0f}, -0.6f );
114
115 for( int i=0; i<4; i++ )
116 rb_init( &funnel[i] );
117
118 reorg_jeffs();
119
120 rb_init( &ground );
121 rb_init( &ball );
122 rb_init( &ball1 );
123 rb_init( &jeff1 );
124 rb_init( &blocky );
125
126 scene_init( &epic_scene );
127
128 mdl_header *mdl = mdl_load( "models/epic_scene.mdl" );
129
130 m4x3f transform;
131 m4x3_identity( transform );
132
133 for( int i=0; i<mdl->node_count; i++ )
134 {
135 mdl_node *pnode = mdl_node_from_id( mdl, i );
136
137 for( int j=0; j<pnode->submesh_count; j++ )
138 {
139 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
140 scene_add_submesh( &epic_scene, mdl, sm, transform );
141 }
142 }
143
144 free( mdl );
145 scene_bh_create( &epic_scene );
146
147 rb_init( &epic_scene_rb );
148 rb_init( &marko );
149 }
150
151 static void physics_test_update(void)
152 {
153 player_freecam();
154 player_camera_update();
155
156 for( int i=0; i<4; i++ )
157 rb_debug( &funnel[i], 0xff0060e0 );
158 rb_debug( &ground, 0xff00ff00 );
159 rb_debug( &ball, 0xffe00040 );
160 rb_debug( &ball1, 0xff00e050 );
161
162 rb_debug( &blocky, 0xffcccccc );
163 rb_debug( &jeff1, 0xff00ffff );
164
165 rb_debug( &epic_scene_rb, 0xffcccccc );
166 rb_debug( &marko, 0xffffcc00 );
167
168 {
169
170 rb_solver_reset();
171
172 for( int i=0; i<4; i++ )
173 {
174 rigidbody *fn = &funnel[i];
175 rb_collide( &ball, fn );
176 rb_collide( &ball1, fn );
177 rb_collide( &jeff1, fn );
178
179 for( int i=0; i<vg_list_size(jeffs); i++ )
180 rb_collide( jeffs+i, fn );
181 }
182
183 for( int i=0; i<vg_list_size(jeffs)-1; i++ )
184 {
185 for( int j=i+1; j<vg_list_size(jeffs); j++ )
186 {
187 rb_collide( jeffs+i, jeffs+j );
188 }
189 }
190
191 for( int i=0; i<vg_list_size(jeffs); i++ )
192 {
193 rb_collide( jeffs+i, &ground );
194 rb_collide( jeffs+i, &ball );
195 rb_collide( jeffs+i, &ball1 );
196 rb_collide( jeffs+i, &jeff1 );
197 }
198
199 rb_collide( &jeff1, &ground );
200 rb_collide( &jeff1, &blocky );
201 rb_collide( &jeff1, &ball );
202 rb_collide( &jeff1, &ball1 );
203
204 rb_collide( &ball, &ground );
205 rb_collide( &ball1, &ground );
206 rb_collide( &ball1, &ball );
207 rb_collide( &marko, &epic_scene_rb );
208
209 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
210 for( int i=0; i<8; i++ )
211 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
212
213
214 /* ITERATE */
215 {
216 for( int i=0; i<vg_list_size(jeffs); i++ )
217 {
218 rb_debug( &jeffs[i], (u32[]){ 0xff0000ff, 0xff00ff00, 0xff00ffff,
219 0xffff0000, 0xffff00ff, 0xffffff00,
220 }[i%6] );
221 rb_iter( jeffs+i );
222 }
223
224 rb_iter( &ball );
225 rb_iter( &ball1 );
226 rb_iter( &jeff1 );
227 rb_iter( &marko );
228 }
229
230 /* POSITION OVERRIDE */
231 {
232 if(glfwGetKey( vg_window, GLFW_KEY_L ))
233 {
234 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, marko.co );
235 v3_zero( marko.v );
236 v3_zero( marko.w );
237 }
238 if(glfwGetKey( vg_window, GLFW_KEY_K ))
239 {
240 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, ball.co );
241 v3_zero( ball.v );
242 v3_zero( ball.w );
243 }
244 if(glfwGetKey( vg_window, GLFW_KEY_J ))
245 {
246 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, ball1.co );
247 v3_zero( ball1.v );
248 v3_zero( ball1.w );
249 }
250
251 if(glfwGetKey( vg_window, GLFW_KEY_H ))
252 {
253 reorg_jeffs();
254 }
255 }
256
257 /* UPDATE TRANSFORMS */
258 for( int i=0; i<vg_list_size(jeffs); i++ )
259 {
260 rb_update_transform(jeffs+i);
261 }
262
263 rb_update_transform( &ball );
264 rb_update_transform( &ball1 );
265 rb_update_transform( &jeff1 );
266 rb_update_transform( &marko );
267
268 }
269 }
270
271 static void physics_test_render(void)
272 {
273 m4x4f world_4x4;
274 m4x3_expand( player.camera_inverse, world_4x4 );
275
276 gpipeline.fov = 60.0f;
277 m4x4_projection( vg_pv, gpipeline.fov,
278 (float)vg_window_x / (float)vg_window_y,
279 0.1f, 2100.0f );
280
281 m4x4_mul( vg_pv, world_4x4, vg_pv );
282 glEnable( GL_DEPTH_TEST );
283
284 glDisable( GL_DEPTH_TEST );
285 vg_lines_drawall( (float *)vg_pv );
286 }
287
288 #endif /* PHYSICS_TEST_H */