2 * Resources: Box2D - Erin Catto
9 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
10 static bh_system bh_system_rigidbodies
;
16 #define k_rb_rate 60.0f
17 #define k_rb_delta (1.0f/k_rb_rate)
19 typedef struct rigidbody rigidbody
;
49 v3f right
, up
, forward
;
54 v3f delta
; /* where is the origin of this in relation to a parent body */
55 m4x3f to_world
, to_local
;
58 static void rb_debug( rigidbody
*rb
, u32 colour
);
65 float bias
, norm_impulse
, tangent_impulse
[2];
67 rb_contact_buffer
[256];
68 static int rb_contact_count
= 0;
70 static void rb_update_transform( rigidbody
*rb
)
73 q_m3x3( rb
->q
, rb
->to_world
);
74 v3_copy( rb
->co
, rb
->to_world
[3] );
76 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
78 box_copy( rb
->bbx
, rb
->bbx_world
);
79 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
81 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
82 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
83 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
86 static float sphere_volume( float radius
)
88 float r3
= radius
*radius
*radius
;
89 return (4.0f
/3.0f
) * VG_PIf
* r3
;
92 static void rb_init( rigidbody
*rb
)
96 if( rb
->type
== k_rb_shape_box
)
99 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
100 volume
= dims
[0]*dims
[1]*dims
[2];
102 else if( rb
->type
== k_rb_shape_sphere
)
104 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
105 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
106 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
108 else if( rb
->type
== k_rb_shape_capsule
)
110 float r
= rb
->inf
.capsule
.radius
,
111 h
= rb
->inf
.capsule
.height
;
112 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
114 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
115 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
120 rb
->inv_mass
= 1.0f
/(8.0f
*volume
);
125 rb_update_transform( rb
);
128 static void rb_iter( rigidbody
*rb
)
130 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
131 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
133 /* intergrate velocity */
134 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
135 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
137 /* inegrate inertia */
138 if( v3_length2( rb
->w
) > 0.0f
)
142 v3_copy( rb
->w
, axis
);
144 float mag
= v3_length( axis
);
145 v3_divs( axis
, mag
, axis
);
146 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
147 q_mul( rotation
, rb
->q
, rb
->q
);
151 static void rb_torque( rigidbody
*rb
, v3f axis
, float mag
)
153 v3_muladds( rb
->w
, axis
, mag
*k_rb_delta
, rb
->w
);
156 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
158 /* Compute tangent basis (box2d) */
159 if( fabsf( n
[0] ) >= 0.57735027f
)
173 v3_cross( n
, tx
, ty
);
176 static void rb_solver_reset(void);
177 static void rb_build_manifold_terrain( rigidbody
*rb
);
178 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
);
179 static void rb_solve_contacts(void);
181 static float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
182 float *s
, float *t
, v3f c1
, v3f c2
)
185 v3_sub( q1
, p1
, d1
);
186 v3_sub( q2
, p2
, d2
);
189 float a
= v3_length2( d1
),
190 e
= v3_length2( d2
),
193 const float kEpsilon
= 0.0001f
;
195 if( a
<= kEpsilon
&& e
<= kEpsilon
)
203 v3_sub( c1
, c2
, v0
);
205 return v3_length2( v0
);
211 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
215 float c
= v3_dot( d1
, r
);
219 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
223 float b
= v3_dot(d1
,d2
),
228 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
240 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
245 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
250 v3_muladds( p1
, d1
, *s
, c1
);
251 v3_muladds( p2
, d2
, *t
, c2
);
254 v3_sub( c1
, c2
, v0
);
255 return v3_length2( v0
);
258 static void closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
262 v3_sub( point
, a
, v1
);
264 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
265 v3_muladds( a
, v0
, vg_clampf(t
,0.0f
,1.0f
), dest
);
268 /* Real-Time Collision Detection */
269 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
274 /* Region outside A */
275 v3_sub( tri
[1], tri
[0], ab
);
276 v3_sub( tri
[2], tri
[0], ac
);
277 v3_sub( p
, tri
[0], ap
);
281 if( d1
<= 0.0f
&& d2
<= 0.0f
)
283 v3_copy( tri
[0], dest
);
284 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
288 /* Region outside B */
292 v3_sub( p
, tri
[1], bp
);
293 d3
= v3_dot( ab
, bp
);
294 d4
= v3_dot( ac
, bp
);
296 if( d3
>= 0.0f
&& d4
<= d3
)
298 v3_copy( tri
[1], dest
);
299 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
303 /* Edge region of AB */
304 float vc
= d1
*d4
- d3
*d2
;
305 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
307 float v
= d1
/ (d1
-d3
);
308 v3_muladds( tri
[0], ab
, v
, dest
);
309 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
313 /* Region outside C */
316 v3_sub( p
, tri
[2], cp
);
320 if( d6
>= 0.0f
&& d5
<= d6
)
322 v3_copy( tri
[2], dest
);
323 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
328 float vb
= d5
*d2
- d1
*d6
;
329 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
331 float w
= d2
/ (d2
-d6
);
332 v3_muladds( tri
[0], ac
, w
, dest
);
333 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
338 float va
= d3
*d6
- d5
*d4
;
339 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
341 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
343 v3_sub( tri
[2], tri
[1], bc
);
344 v3_muladds( tri
[1], bc
, w
, dest
);
345 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
349 /* P inside region, Q via barycentric coordinates uvw */
350 float d
= 1.0f
/(va
+vb
+vc
),
354 v3_muladds( tri
[0], ab
, v
, dest
);
355 v3_muladds( dest
, ac
, w
, dest
);
358 static int sphere_vs_triangle( v3f c
, float r
, v3f tri
[3],
359 v3f co
, v3f norm
, float *p
)
362 closest_on_triangle( c
, tri
, co
);
364 v3_sub( c
, co
, delta
);
367 float d
= v3_length2( delta
);
371 v3_sub( tri
[1], tri
[0], ab
);
372 v3_sub( tri
[2], tri
[0], ac
);
373 v3_cross( ac
, ab
, tn
);
375 if( v3_dot( delta
, tn
) > 0.0f
)
376 v3_muls( delta
, -1.0f
, delta
);
378 vg_line_pt3( co
, 0.05f
, 0xff00ff00 );
381 v3_muls( delta
, 1.0f
/d
, norm
);
392 static void rb_solver_reset(void)
394 rb_contact_count
= 0;
397 static struct contact
*rb_start_contact(void)
399 if( rb_contact_count
== vg_list_size(rb_contact_buffer
) )
401 vg_error( "rigidbody: too many contacts generated (%u)\n",
406 return &rb_contact_buffer
[ rb_contact_count
];
409 static void rb_commit_contact( struct contact
*ct
, float p
)
411 ct
->bias
= -0.2f
*k_rb_rate
*vg_minf(0.0f
,-p
+0.04f
);
412 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
414 ct
->norm_impulse
= 0.0f
;
415 ct
->tangent_impulse
[0] = 0.0f
;
416 ct
->tangent_impulse
[1] = 0.0f
;
421 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
)
425 int len
= bh_select( &world
.geo
.bhtris
, rb
->bbx_world
, geo
, 256 );
427 for( int i
=0; i
<len
; i
++ )
429 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
431 for( int j
=0; j
<3; j
++ )
432 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
434 vg_line(tri
[0],tri
[1],0xff00ff00 );
435 vg_line(tri
[1],tri
[2],0xff00ff00 );
436 vg_line(tri
[2],tri
[0],0xff00ff00 );
441 for( int j
=0; j
<2; j
++ )
443 if( sphere_vs_triangle( rb
->co
, rb
->inf
.sphere
.radius
, tri
,co
,norm
,&p
))
445 struct contact
*ct
= rb_start_contact();
451 v3_muladds( rb
->co
, norm
, p
, p1
);
452 vg_line( rb
->co
, p1
, 0xffffffff );
455 v3_copy( co
, ct
->co
);
456 v3_copy( norm
, ct
->n
);
458 v3_sub( co
, rb
->co
, ct
->delta
);
459 rb_commit_contact( ct
, p
);
467 static void rb_build_manifold_terrain( rigidbody
*rb
)
471 float *p000
= pts
[0], *p001
= pts
[1], *p010
= pts
[2], *p011
= pts
[3],
472 *p100
= pts
[4], *p101
= pts
[5], *p110
= pts
[6], *p111
= pts
[7];
474 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
475 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
476 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
477 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
479 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
480 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
481 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
482 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
484 m4x3_mulv( rb
->to_world
, p000
, p000
);
485 m4x3_mulv( rb
->to_world
, p001
, p001
);
486 m4x3_mulv( rb
->to_world
, p010
, p010
);
487 m4x3_mulv( rb
->to_world
, p011
, p011
);
488 m4x3_mulv( rb
->to_world
, p100
, p100
);
489 m4x3_mulv( rb
->to_world
, p101
, p101
);
490 m4x3_mulv( rb
->to_world
, p110
, p110
);
491 m4x3_mulv( rb
->to_world
, p111
, p111
);
495 for( int i
=0; i
<8; i
++ )
497 float *point
= pts
[i
];
498 struct contact
*ct
= rb_start_contact();
506 v3_copy( point
, surface
);
511 if( !ray_world( surface
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
514 v3_copy( hit
.pos
, surface
);
516 float p
= vg_minf( surface
[1] - point
[1], 1.0f
);
520 v3_copy( hit
.normal
, ct
->n
);
521 v3_add( point
, surface
, ct
->co
);
522 v3_muls( ct
->co
, 0.5f
, ct
->co
);
523 v3_sub( ct
->co
, rb
->co
, ct
->delta
);
525 rb_commit_contact( ct
, p
);
533 static void rb_solve_contacts(void)
535 float k_friction
= 0.1f
;
537 /* Friction Impulse */
538 for( int i
=0; i
<rb_contact_count
; i
++ )
540 struct contact
*ct
= &rb_contact_buffer
[i
];
541 rigidbody
*rb
= ct
->rba
;
544 v3_cross( rb
->w
, ct
->delta
, dv
);
545 v3_add( rb
->v
, dv
, dv
);
547 for( int j
=0; j
<2; j
++ )
549 float vt
= vg_clampf( -v3_dot( dv
, ct
->t
[j
] ),
550 -k_friction
, k_friction
);
552 vt
= -v3_dot( dv
, ct
->t
[j
] );
554 float temp
= ct
->tangent_impulse
[j
];
555 ct
->tangent_impulse
[j
] = vg_clampf( temp
+vt
, -k_friction
, k_friction
);
556 vt
= ct
->tangent_impulse
[j
] - temp
;
560 v3_muls( ct
->t
[j
], vt
, impulse
);
561 v3_add( impulse
, rb
->v
, rb
->v
);
562 v3_cross( ct
->delta
, impulse
, impulse
);
563 v3_add( impulse
, rb
->w
, rb
->w
);
568 for( int i
=0; i
<rb_contact_count
; i
++ )
570 struct contact
*ct
= &rb_contact_buffer
[i
];
571 rigidbody
*rb
= ct
->rba
;
574 v3_cross( rb
->w
, ct
->delta
, dv
);
575 v3_add( rb
->v
, dv
, dv
);
577 float vn
= -v3_dot( dv
, ct
->n
);
580 float temp
= ct
->norm_impulse
;
581 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
582 vn
= ct
->norm_impulse
- temp
;
586 v3_muls( ct
->n
, vn
, impulse
);
587 v3_add( impulse
, rb
->v
, rb
->v
);
588 v3_cross( ct
->delta
, impulse
, impulse
);
589 v3_add( impulse
, rb
->w
, rb
->w
);
593 struct rb_angle_limit
595 rigidbody
*rba
, *rbb
;
600 static int rb_angle_limit_force( rigidbody
*rba
, v3f va
,
601 rigidbody
*rbb
, v3f vb
,
605 m3x3_mulv( rba
->to_world
, va
, wva
);
606 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
608 float dt
= v3_dot(wva
,wvb
)*0.999f
,
613 float correction
= max
-ang
;
616 v3_cross( wva
, wvb
, axis
);
619 q_axis_angle( rotation
, axis
, -correction
*0.25f
);
620 q_mul( rotation
, rba
->q
, rba
->q
);
622 q_axis_angle( rotation
, axis
, correction
*0.25f
);
623 q_mul( rotation
, rbb
->q
, rbb
->q
);
631 static void rb_constraint_angle_limit( struct rb_angle_limit
*limit
)
637 static void rb_constraint_angle( rigidbody
*rba
, v3f va
,
638 rigidbody
*rbb
, v3f vb
,
639 float max
, float spring
)
642 m3x3_mulv( rba
->to_world
, va
, wva
);
643 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
645 float dt
= v3_dot(wva
,wvb
)*0.999f
,
649 v3_cross( wva
, wvb
, axis
);
650 v3_muladds( rba
->w
, axis
, ang
*spring
*0.5f
, rba
->w
);
651 v3_muladds( rbb
->w
, axis
, -ang
*spring
*0.5f
, rbb
->w
);
655 /* TODO: convert max into the dot product value so we dont have to always
656 * evaluate acosf, only if its greater than the angle specified */
660 float correction
= max
-ang
;
663 q_axis_angle( rotation
, axis
, -correction
*0.125f
);
664 q_mul( rotation
, rba
->q
, rba
->q
);
666 q_axis_angle( rotation
, axis
, correction
*0.125f
);
667 q_mul( rotation
, rbb
->q
, rbb
->q
);
671 static void rb_relative_velocity( rigidbody
*ra
, v3f lca
,
672 rigidbody
*rb
, v3f lcb
, v3f rcv
)
675 m3x3_mulv( ra
->to_world
, lca
, wca
);
676 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
678 v3_sub( ra
->v
, rb
->v
, rcv
);
681 v3_cross( ra
->w
, wca
, rcv_Ra
);
682 v3_cross( rb
->w
, wcb
, rcv_Rb
);
683 v3_add( rcv_Ra
, rcv
, rcv
);
684 v3_sub( rcv
, rcv_Rb
, rcv
);
687 static void rb_constraint_position( rigidbody
*ra
, v3f lca
,
688 rigidbody
*rb
, v3f lcb
)
690 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
692 m3x3_mulv( ra
->to_world
, lca
, wca
);
693 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
696 v3_add( wcb
, rb
->co
, delta
);
697 v3_sub( delta
, wca
, delta
);
698 v3_sub( delta
, ra
->co
, delta
);
700 v3_muladds( ra
->co
, delta
, 0.5f
, ra
->co
);
701 v3_muladds( rb
->co
, delta
, -0.5f
, rb
->co
);
704 v3_sub( ra
->v
, rb
->v
, rcv
);
707 v3_cross( ra
->w
, wca
, rcv_Ra
);
708 v3_cross( rb
->w
, wcb
, rcv_Rb
);
709 v3_add( rcv_Ra
, rcv
, rcv
);
710 v3_sub( rcv
, rcv_Rb
, rcv
);
712 float nm
= 0.5f
/(rb
->inv_mass
+ ra
->inv_mass
);
714 float mass_a
= 1.0f
/ra
->inv_mass
,
715 mass_b
= 1.0f
/rb
->inv_mass
,
716 total_mass
= mass_a
+mass_b
;
719 v3_muls( rcv
, 1.0f
, impulse
);
720 v3_muladds( rb
->v
, impulse
, mass_b
/total_mass
, rb
->v
);
721 v3_cross( wcb
, impulse
, impulse
);
722 v3_add( impulse
, rb
->w
, rb
->w
);
724 v3_muls( rcv
, -1.0f
, impulse
);
725 v3_muladds( ra
->v
, impulse
, mass_a
/total_mass
, ra
->v
);
726 v3_cross( wca
, impulse
, impulse
);
727 v3_add( impulse
, ra
->w
, ra
->w
);
731 * this could be used for spring joints
732 * its not good for position constraint
735 v3_muls( delta
, 0.5f
*spring
, impulse
);
737 v3_add( impulse
, ra
->v
, ra
->v
);
738 v3_cross( wca
, impulse
, impulse
);
739 v3_add( impulse
, ra
->w
, ra
->w
);
741 v3_muls( delta
, -0.5f
*spring
, impulse
);
743 v3_add( impulse
, rb
->v
, rb
->v
);
744 v3_cross( wcb
, impulse
, impulse
);
745 v3_add( impulse
, rb
->w
, rb
->w
);
749 static void debug_sphere( m4x3f m
, float radius
, u32 colour
)
751 v3f ly
= { 0.0f
, 0.0f
, radius
},
752 lx
= { 0.0f
, radius
, 0.0f
},
753 lz
= { 0.0f
, 0.0f
, radius
};
755 for( int i
=0; i
<16; i
++ )
757 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
761 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
762 px
= { s
*radius
, c
*radius
, 0.0f
},
763 pz
= { 0.0f
, s
*radius
, c
*radius
};
765 v3f p0
, p1
, p2
, p3
, p4
, p5
;
766 m4x3_mulv( m
, py
, p0
);
767 m4x3_mulv( m
, ly
, p1
);
768 m4x3_mulv( m
, px
, p2
);
769 m4x3_mulv( m
, lx
, p3
);
770 m4x3_mulv( m
, pz
, p4
);
771 m4x3_mulv( m
, lz
, p5
);
773 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
774 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
775 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
783 static void rb_debug( rigidbody
*rb
, u32 colour
)
785 if( rb
->type
== k_rb_shape_box
)
788 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
790 else if( rb
->type
== k_rb_shape_sphere
)
792 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
797 * out penetration distance, normal
799 static int rb_point_in_body( rigidbody
*rb
, v3f pos
, float *pen
, v3f normal
)
802 m4x3_mulv( rb
->to_local
, pos
, local
);
804 if( local
[0] > rb
->bbx
[0][0] && local
[0] < rb
->bbx
[1][0] &&
805 local
[1] > rb
->bbx
[0][1] && local
[1] < rb
->bbx
[1][1] &&
806 local
[2] > rb
->bbx
[0][2] && local
[2] < rb
->bbx
[1][2] )
808 v3f area
, com
, comrel
;
809 v3_add( rb
->bbx
[0], rb
->bbx
[1], com
);
810 v3_muls( com
, 0.5f
, com
);
812 v3_sub( rb
->bbx
[1], rb
->bbx
[0], area
);
813 v3_sub( local
, com
, comrel
);
814 v3_div( comrel
, area
, comrel
);
817 float max_mag
= fabsf(comrel
[0]);
819 if( fabsf(comrel
[1]) > max_mag
)
822 max_mag
= fabsf(comrel
[1]);
824 if( fabsf(comrel
[2]) > max_mag
)
827 max_mag
= fabsf(comrel
[2]);
831 normal
[axis
] = vg_signf(comrel
[axis
]);
833 if( normal
[axis
] < 0.0f
)
834 *pen
= local
[axis
] - rb
->bbx
[0][axis
];
836 *pen
= rb
->bbx
[1][axis
] - local
[axis
];
838 m3x3_mulv( rb
->to_world
, normal
, normal
);
846 static void rb_build_manifold_rb_static( rigidbody
*ra
, rigidbody
*rb_static
)
851 v3_copy( ra
->bbx
[0], a
);
852 v3_copy( ra
->bbx
[1], b
);
854 m4x3_mulv( ra
->to_world
, (v3f
){ a
[0], a
[1], a
[2] }, verts
[0] );
855 m4x3_mulv( ra
->to_world
, (v3f
){ a
[0], b
[1], a
[2] }, verts
[1] );
856 m4x3_mulv( ra
->to_world
, (v3f
){ b
[0], b
[1], a
[2] }, verts
[2] );
857 m4x3_mulv( ra
->to_world
, (v3f
){ b
[0], a
[1], a
[2] }, verts
[3] );
858 m4x3_mulv( ra
->to_world
, (v3f
){ a
[0], a
[1], b
[2] }, verts
[4] );
859 m4x3_mulv( ra
->to_world
, (v3f
){ a
[0], b
[1], b
[2] }, verts
[5] );
860 m4x3_mulv( ra
->to_world
, (v3f
){ b
[0], b
[1], b
[2] }, verts
[6] );
861 m4x3_mulv( ra
->to_world
, (v3f
){ b
[0], a
[1], b
[2] }, verts
[7] );
863 vg_line_boxf_transformed( rb_static
->to_world
, rb_static
->bbx
, 0xff0000ff );
867 for( int i
=0; i
<8; i
++ )
869 if( ra
->manifold_count
== vg_list_size(ra
->manifold
) )
872 struct contact
*ct
= &ra
->manifold
[ ra
->manifold_count
];
877 if( rb_point_in_body( rb_static
, verts
[i
], &p
, normal
))
879 v3_copy( normal
, ct
->n
);
880 v3_muladds( verts
[i
], ct
->n
, p
*0.5f
, ct
->co
);
881 v3_sub( ct
->co
, ra
->co
, ct
->delta
);
883 vg_line_pt3( ct
->co
, 0.0125f
, 0xffff00ff );
885 ct
->bias
= -0.2f
* (1.0f
/k_rb_delta
) * vg_minf( 0.0f
, -p
+0.04f
);
886 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
888 ct
->norm_impulse
= 0.0f
;
889 ct
->tangent_impulse
[0] = 0.0f
;
890 ct
->tangent_impulse
[1] = 0.0f
;
892 ra
->manifold_count
++;
905 static void debug_capsule( m4x3f m
, float height
, float radius
, u32 colour
)
907 v3f last
= { 0.0f
, 0.0f
, radius
};
909 m3x3_copy( m
, lower
);
910 m3x3_copy( m
, upper
);
911 m4x3_mulv( m
, (v3f
){0.0f
,-height
*0.5f
+radius
,0.0f
}, lower
[3] );
912 m4x3_mulv( m
, (v3f
){0.0f
, height
*0.5f
-radius
,0.0f
}, upper
[3] );
914 for( int i
=0; i
<16; i
++ )
916 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
920 v3f p
= { s
*radius
, 0.0f
, c
*radius
};
923 m4x3_mulv( lower
, p
, p0
);
924 m4x3_mulv( lower
, last
, p1
);
925 vg_line( p0
, p1
, colour
);
927 m4x3_mulv( upper
, p
, p0
);
928 m4x3_mulv( upper
, last
, p1
);
929 vg_line( p0
, p1
, colour
);
934 for( int i
=0; i
<4; i
++ )
936 float t
= ((float)(i
) * (1.0f
/4.0f
)) * VG_PIf
* 2.0f
,
940 v3f p
= { s
*radius
, 0.0f
, c
*radius
};
943 m4x3_mulv( lower
, p
, p0
);
944 m4x3_mulv( upper
, p
, p1
);
945 vg_line( p0
, p1
, colour
);
947 m4x3_mulv( lower
, (v3f
){0.0f
,-radius
,0.0f
}, p0
);
948 m4x3_mulv( upper
, (v3f
){0.0f
, radius
,0.0f
}, p1
);
949 vg_line( p0
, p1
, colour
);
954 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
958 static void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
960 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
961 box_concat( bound
, rb
->bbx_world
);
964 static float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
966 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
967 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
970 static void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
972 rigidbody temp
, *rba
, *rbb
;
973 rba
= &((rigidbody
*)user
)[ ia
];
974 rbb
= &((rigidbody
*)user
)[ ib
];
981 static void rb_bh_debug( void *user
, u32 item_index
)
983 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
984 rb_debug( rb
, 0xff00ffff );
987 static bh_system bh_system_rigidbodies
=
989 .expand_bound
= rb_bh_expand_bound
,
990 .item_centroid
= rb_bh_centroid
,
991 .item_swap
= rb_bh_swap
,
992 .item_debug
= rb_bh_debug
,
996 #endif /* RIGIDBODY_H */