Line data Source code
1 : #pragma once
2 : #include <math3d/math3d.hpp>
3 : #include "rbc/AABB.hpp"
4 :
5 : namespace rbc
6 : {
7 : struct Sphere
8 : {
9 : m3d::scalar radius;
10 : // Constructors
11 : Sphere() : radius(0.5) {}
12 80 : Sphere(const m3d::scalar &radius) : radius(radius) {}
13 :
14 : inline bool operator==(const Sphere &other) const
15 : {
16 : return radius == other.radius;
17 : }
18 :
19 : inline bool operator!=(const Sphere &other) const
20 : {
21 : return !(*this == other);
22 : }
23 : };
24 :
25 520 : inline m3d::vec3 support(const Sphere &s, const m3d::vec3 &direction)
26 : {
27 : // The furthest point of a sphere in any direction is just the radius along that direction
28 520 : return m3d::normalize(direction) * s.radius;
29 : }
30 :
31 : inline m3d::scalar compute_volume(const Sphere &s)
32 : {
33 : return (4.0 / 3.0) * m3d::PI * s.radius * s.radius * s.radius; // Volume of the Sphere
34 : }
35 :
36 : inline m3d::smat3 compute_inertia_tensor(const Sphere &s)
37 : {
38 : m3d::scalar mass = compute_volume(s); // Assuming unit density for simplicity (How we should ahndle this? Should we add a density parameter to the Spehere struct?)
39 : m3d::scalar inertia = (2.0 / 5.0) * mass * s.radius * s.radius;
40 : return m3d::smat3(
41 : inertia, // Ixx
42 : inertia, // Iyy
43 : inertia, // Izz
44 : 0, // Ixy
45 : 0, // Ixz
46 : 0 // Iyz
47 : );
48 : }
49 :
50 : // Sphere: trivially radius in all directions from center.
51 736 : inline AABB compute_aabb(const Sphere &s, const m3d::tf &tf)
52 : {
53 736 : const m3d::vec3 r(s.radius, s.radius, s.radius);
54 736 : return {tf.pos - r, tf.pos + r};
55 : }
56 :
57 : }
|