JSim 2026.06.01-p(1)
Loading...
Searching...
No Matches
sphere.hpp
Go to the documentation of this file.
1// Copyright (c) JSim contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the LGPLv3 license file in the root directory of this project.
4
5#pragma once
6
8#ifndef M_PI
9#define M_PI 3.14159265358979323846
10#endif
11#include <cmath>
12
13namespace frcsim {
14
18class Sphere : public Shape {
19public:
24 explicit Sphere(double radius);
25
26 virtual ~Sphere() = default;
27
28 ShapeType GetType() const override;
29 double CalculateVolume() const override;
30 Vector3 CalculateLocalInertia(double mass) const override;
31
36 double GetRadius() const;
37
38private:
39 double m_radius;
40};
41
42inline Sphere::Sphere(double radius) : m_radius(radius) {}
43
45
46inline double Sphere::CalculateVolume() const {
47 return (4.0 / 3.0) * M_PI * m_radius * m_radius * m_radius;
48}
49
50inline Vector3 Sphere::CalculateLocalInertia(double mass) const {
51 double i = 0.4 * mass * m_radius * m_radius;
52 return Vector3(i, i, i);
53}
54
55inline double Sphere::GetRadius() const { return m_radius; }
56
57} // namespace frcsim
Base class for all collision geometries in the physics engine.
Definition shape.hpp:28
double GetRadius() const
Gets the radius of the sphere.
Definition sphere.hpp:55
Sphere(double radius)
Constructs a new Sphere centered at the local origin.
Definition sphere.hpp:42
virtual ~Sphere()=default
Vector3 CalculateLocalInertia(double mass) const override
Computes the diagonal of the local inertia tensor for this shape given a mass.
Definition sphere.hpp:50
double CalculateVolume() const override
Calculates the volume of the specific shape.
Definition sphere.hpp:46
ShapeType GetType() const override
Gets the exact type of this shape.
Definition sphere.hpp:44
Definition vector.hpp:13
ShapeType
Enum identifying the specific types of collision shapes available.
Definition shape.hpp:14
@ SPHERE
Definition shape.hpp:16
#define M_PI
Definition sphere.hpp:9
3D vector utility used throughout JSim physics.
Definition vector.hpp:22