bad char
[vg.git] / vg_rigidbody.h
1 #pragma once
2 #include "vg/vg_platform.h"
3
4 /*
5 * Copyright (C) 2021-2024 Mt.ZERO Software - All Rights Reserved
6 *
7 * Rigidbody main file, related:
8 * vg_rigidbody_collision.h
9 * vg_rigidbody_constraints.h
10 */
11
12 #define k_friction 0.4f
13 #define k_damp_linear 0.1f /* scale velocity 1/(1+x) */
14 #define k_damp_angular 0.1f /* scale angular 1/(1+x) */
15 #define k_penetration_slop 0.01f
16 #define k_rb_inertia_scale 4.0f
17 #define k_phys_baumgarte 0.2f
18 #define k_gravity 9.6f
19 #define k_rb_density 8.0f
20
21 enum rb_shape {
22 k_rb_shape_none = 0,
23 k_rb_shape_box = 1,
24 k_rb_shape_sphere = 2,
25 k_rb_shape_capsule = 3,
26 };
27
28 typedef struct rigidbody rigidbody;
29 typedef struct rb_capsule rb_capsule;
30
31 struct rb_capsule
32 {
33 f32 h, r;
34 };
35
36 struct rigidbody
37 {
38 v3f co, v, w;
39 v4f q;
40
41 f32 inv_mass;
42
43 m3x3f iI, iIw; /* inertia model and inverse world tensor */
44 m4x3f to_world, to_local;
45 };
46
47 /*
48 * Initialize rigidbody inverse mass and inertia tensor with some common shapes
49 */
50 void rb_setbody_capsule( rigidbody *rb, f32 r, f32 h,
51 f32 density, f32 inertia_scale );
52 void rb_setbody_box( rigidbody *rb, boxf box, f32 density, f32 inertia_scale );
53 void rb_setbody_sphere( rigidbody *rb, f32 r, f32 density, f32 inertia_scale );
54
55 /*
56 * Update ALL matrices and tensors on rigidbody
57 */
58 void rb_update_matrices( rigidbody *rb );
59
60 /*
61 * Extrapolate rigidbody into a transform based on vg accumulator.
62 * Useful for rendering
63 */
64 void rb_extrapolate( rigidbody *rb, v3f co, v4f q );
65 void rb_iter( rigidbody *rb );
66 void rb_solve_gyroscopic( rigidbody *rb, m3x3f I, f32 h );
67
68 /*
69 * Creates relative contact velocity vector
70 */
71 void rb_rcv( rigidbody *rba, rigidbody *rbb, v3f ra, v3f rb, v3f rv );
72
73 /*
74 * Apply impulse to object
75 */
76 void rb_linear_impulse( rigidbody *rb, v3f delta, v3f impulse );
77
78 /*
79 * Effectors
80 */
81 void rb_effect_simple_bouyency( rigidbody *ra, v4f plane, f32 amt, f32 drag );
82 void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt,
83 f32 spring, f32 dampening, f32 timestep );