routes
[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 = {{-0.5f,-0.5f,-0.5f},{0.5f,0.5f,0.5f}},
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 = 2.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_copy( (v3f){ (vg_randf()-0.5f) * 10.0f,
92 (vg_randf()-0.5f) * 10.0f + 17.0f,
93 (vg_randf()-0.5f) * 10.0f }, jeffs[i].co );
94 v4_copy( (v4f){ vg_randf(), vg_randf(), vg_randf(), vg_randf() },
95 jeffs[i].q );
96 q_normalize( jeffs[i].q );
97
98 jeffs[i].type = k_rb_shape_capsule;
99 jeffs[i].inf.capsule.radius = 0.75f;
100 jeffs[i].inf.capsule.height = 3.0f;
101
102 rb_init( &jeffs[i] );
103 }
104 }
105
106 static void physics_test_start(void)
107 {
108 q_axis_angle( funnel[0].q, (v3f){1.0f,0.0f,0.0f}, 0.6f );
109 q_axis_angle( funnel[1].q, (v3f){1.0f,0.0f,0.0f}, -0.6f );
110 q_axis_angle( funnel[2].q, (v3f){0.0f,0.0f,1.0f}, 0.6f );
111 q_axis_angle( funnel[3].q, (v3f){0.0f,0.0f,1.0f}, -0.6f );
112
113 for( int i=0; i<4; i++ )
114 rb_init( &funnel[i] );
115
116 reorg_jeffs();
117
118 rb_init( &ground );
119 rb_init( &ball );
120 rb_init( &ball1 );
121 rb_init( &jeff1 );
122 rb_init( &blocky );
123
124 scene_init( &epic_scene );
125
126 mdl_header *mdl = mdl_load( "models/epic_scene.mdl" );
127
128 m4x3f transform;
129 m4x3_identity( transform );
130
131 for( int i=0; i<mdl->node_count; i++ )
132 {
133 mdl_node *pnode = mdl_node_from_id( mdl, i );
134
135 for( int j=0; j<pnode->submesh_count; j++ )
136 {
137 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
138 scene_add_submesh( &epic_scene, mdl, sm, transform );
139 }
140 }
141
142 free( mdl );
143 scene_bh_create( &epic_scene );
144
145 rb_init( &epic_scene_rb );
146 rb_init( &marko );
147 }
148
149 static void physics_test_update(void)
150 {
151 player_freecam();
152 player_camera_update();
153
154 for( int i=0; i<4; i++ )
155 rb_debug( &funnel[i], 0xff0060e0 );
156 rb_debug( &ground, 0xff00ff00 );
157 rb_debug( &ball, 0xffe00040 );
158 rb_debug( &ball1, 0xff00e050 );
159
160 rb_debug( &blocky, 0xffcccccc );
161 rb_debug( &jeff1, 0xff00ffff );
162
163 rb_debug( &epic_scene_rb, 0xffcccccc );
164 rb_debug( &marko, 0xffffcc00 );
165
166 {
167
168 rb_solver_reset();
169
170 for( int i=0; i<4; i++ )
171 {
172 rigidbody *fn = &funnel[i];
173 rb_collide( &ball, fn );
174 rb_collide( &ball1, fn );
175 rb_collide( &jeff1, fn );
176
177 for( int i=0; i<vg_list_size(jeffs); i++ )
178 rb_collide( jeffs+i, fn );
179 }
180
181 for( int i=0; i<vg_list_size(jeffs)-1; i++ )
182 {
183 for( int j=i+1; j<vg_list_size(jeffs); j++ )
184 {
185 rb_collide( jeffs+i, jeffs+j );
186 }
187 }
188
189 for( int i=0; i<vg_list_size(jeffs); i++ )
190 {
191 rb_collide( jeffs+i, &ground );
192 rb_collide( jeffs+i, &ball );
193 rb_collide( jeffs+i, &ball1 );
194 rb_collide( jeffs+i, &jeff1 );
195 }
196
197 rb_collide( &jeff1, &ground );
198 rb_collide( &jeff1, &blocky );
199 rb_collide( &jeff1, &ball );
200 rb_collide( &jeff1, &ball1 );
201
202 rb_collide( &ball, &ground );
203 rb_collide( &ball1, &ground );
204 rb_collide( &ball1, &ball );
205 rb_collide( &marko, &epic_scene_rb );
206
207 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
208 for( int i=0; i<8; i++ )
209 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
210
211
212 /* ITERATE */
213 {
214 for( int i=0; i<vg_list_size(jeffs); i++ )
215 {
216 rb_debug( &jeffs[i], (u32[]){ 0xff0000ff, 0xff00ff00, 0xff00ffff,
217 0xffff0000, 0xffff00ff, 0xffffff00,
218 }[i%6] );
219 rb_iter( jeffs+i );
220 }
221
222 rb_iter( &ball );
223 rb_iter( &ball1 );
224 rb_iter( &jeff1 );
225 rb_iter( &marko );
226 }
227
228 /* POSITION OVERRIDE */
229 {
230 if(glfwGetKey( vg_window, GLFW_KEY_L ))
231 {
232 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, marko.co );
233 v3_zero( marko.v );
234 v3_zero( marko.w );
235 }
236 if(glfwGetKey( vg_window, GLFW_KEY_K ))
237 {
238 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, ball.co );
239 v3_zero( ball.v );
240 v3_zero( ball.w );
241 }
242 if(glfwGetKey( vg_window, GLFW_KEY_J ))
243 {
244 m4x3_mulv( player.camera, (v3f){0.0f,0.0f,-5.0f}, ball1.co );
245 v3_zero( ball1.v );
246 v3_zero( ball1.w );
247 }
248
249 if(glfwGetKey( vg_window, GLFW_KEY_H ))
250 {
251 reorg_jeffs();
252 }
253 }
254
255 /* UPDATE TRANSFORMS */
256 for( int i=0; i<vg_list_size(jeffs); i++ )
257 {
258 rb_update_transform(jeffs+i);
259 }
260
261 rb_update_transform( &ball );
262 rb_update_transform( &ball1 );
263 rb_update_transform( &jeff1 );
264 rb_update_transform( &marko );
265
266 }
267 }
268
269 static void physics_test_render(void)
270 {
271 m4x4f world_4x4;
272 m4x3_expand( player.camera_inverse, world_4x4 );
273
274 gpipeline.fov = 60.0f;
275 m4x4_projection( vg_pv, gpipeline.fov,
276 (float)vg_window_x / (float)vg_window_y,
277 0.1f, 2100.0f );
278
279 m4x4_mul( vg_pv, world_4x4, vg_pv );
280 glEnable( GL_DEPTH_TEST );
281
282 glDisable( GL_DEPTH_TEST );
283 vg_lines_drawall( (float *)vg_pv );
284 }
285
286 #endif /* PHYSICS_TEST_H */