physics revision
[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 rigidbody funnel[4] = {
13 {
14 .type = k_rb_shape_box,
15 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
16 .co = {-10.0f,5.0f,0.0f},
17 .is_world = 1
18 },
19 {
20 .type = k_rb_shape_box,
21 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
22 .co = { 10.0f,5.0f,0.0f},
23 .is_world = 1
24 },
25 {
26 .type = k_rb_shape_box,
27 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
28 .co = { 0.0f,5.0f,10.0f},
29 .is_world = 1
30 },
31 {
32 .type = k_rb_shape_box,
33 .bbx = {{-20.0f,-1.0f,-20.0f},{20.0f,1.0f,20.0f}},
34 .co = {0.0f,5.0f,-10.0f},
35 .is_world = 1
36 }
37 };
38
39
40 rigidbody ball = { .type = k_rb_shape_sphere,
41 .inf.sphere = { .radius = 2.0f },
42 .co = {0.0f,6.0f,0.0f},
43 .q = {0.0f,0.0f,0.0f,1.0f}},
44
45 ball1= { .type = k_rb_shape_sphere,
46 .inf.sphere = { .radius = 1.0f },
47 .co = {0.1f,9.0f,0.2f},
48 .q = {0.0f,0.0f,0.0f,1.0f}};
49
50 static void physics_test_start(void)
51 {
52 q_axis_angle( funnel[0].q, (v3f){1.0f,0.0f,0.0f}, 0.3f );
53 q_axis_angle( funnel[1].q, (v3f){1.0f,0.0f,0.0f}, -0.3f );
54 q_axis_angle( funnel[2].q, (v3f){0.0f,0.0f,1.0f}, 0.3f );
55 q_axis_angle( funnel[3].q, (v3f){0.0f,0.0f,1.0f}, -0.3f );
56
57 for( int i=0; i<4; i++ )
58 rb_init( &funnel[i] );
59
60 rb_init( &ground );
61 rb_init( &ball );
62 rb_init( &ball1 );
63 }
64
65 static void physics_test_update(void)
66 {
67 player_freecam();
68 player_camera_update();
69
70 {
71
72 rb_iter( &ball );
73 rb_iter( &ball1 );
74
75 rb_solver_reset();
76
77 for( int i=0; i<4; i++ )
78 {
79 rb_contact_count += rb_sphere_vs_box( &ball, &funnel[i], rb_global_ct());
80 rb_contact_count += rb_sphere_vs_box( &ball1, &funnel[i], rb_global_ct());
81 }
82
83 rb_contact_count += rb_sphere_vs_box( &ball, &ground, rb_global_ct() );
84 rb_contact_count += rb_sphere_vs_box( &ball1, &ground, rb_global_ct() );
85 rb_contact_count += rb_sphere_vs_sphere( &ball, &ball1, rb_global_ct() );
86
87 rb_presolve_contacts();
88
89 for( int i=0; i<5; i++ )
90 rb_solve_contacts();
91
92 rb_update_transform( &ball );
93 rb_update_transform( &ball1 );
94
95 }
96
97 if(glfwGetKey( vg_window, GLFW_KEY_L ))
98 {
99 v3_copy( player.camera_pos, ball.co );
100 v3_zero( ball.v );
101 v3_zero( ball.w );
102 }
103
104 for( int i=0; i<4; i++ )
105 rb_debug( &funnel[i], 0xff0060e0 );
106 rb_debug( &ground, 0xff00ff00 );
107 rb_debug( &ball, 0xffe00040 );
108 rb_debug( &ball1, 0xff00e050 );
109 }
110
111 static void physics_test_render(void)
112 {
113 m4x4f world_4x4;
114 m4x3_expand( player.camera_inverse, world_4x4 );
115
116 gpipeline.fov = freecam? 60.0f: 135.0f; /* 120 */
117 m4x4_projection( vg_pv, gpipeline.fov,
118 (float)vg_window_x / (float)vg_window_y,
119 0.1f, 2100.0f );
120
121 m4x4_mul( vg_pv, world_4x4, vg_pv );
122 glEnable( GL_DEPTH_TEST );
123
124 glDisable( GL_DEPTH_TEST );
125 vg_lines_drawall( (float *)vg_pv );
126 }
127
128 #endif /* PHYSICS_TEST_H */