Skip to content

Commit

Permalink
Added sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 28, 2024
1 parent 7c9baca commit ec75d16
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions engine/core/Supernova.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
#include "math/Quaternion.h"
#include "math/Ray.h"
#include "math/Rect.h"
#include "math/Sphere.h"
#include "math/Vector2.h"
#include "math/Vector3.h"
#include "math/Vector4.h"
Expand Down
30 changes: 30 additions & 0 deletions engine/core/math/Ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,36 @@ RayReturn Ray::intersects(AABB box){
return NO_HIT;
}

RayReturn Ray::intersects(Sphere sphere) {
Vector3 rayorig = origin - sphere.center;
float radius = sphere.radius;

// discard inside sphere
//if (rayorig.squaredLength() <= radius*radius && discardInside){
// return {true, 0, Vector3::ZERO, Vector3::ZERO, NULL_ENTITY, 0};
//}

float a = direction.dotProduct(direction);
float b = 2 * rayorig.dotProduct(direction);
float c = rayorig.dotProduct(rayorig) - radius * radius;

// determinant
float d = (b * b) - (4 * a * c);
if (d >= 0){
float t = ( -b - sqrt(d) ) / (2 * a);
if (t < 0){
t = ( -b + sqrt(d) ) / (2 * a);
}

Vector3 point = getPoint(t);
Vector3 normal = (point - sphere.center).normalize();

return {true, t, point, normal, NULL_ENTITY, 0};
}

return NO_HIT;
}

RayReturn Ray::intersects(Body2D body){
Body2DComponent& bodycomp = body.getComponent<Body2DComponent>();

Expand Down
2 changes: 2 additions & 0 deletions engine/core/math/Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "math/Vector3.h"
#include "math/Plane.h"
#include "math/AABB.h"
#include "math/Sphere.h"
#include "object/physics/Body2D.h"
#include "object/physics/Body3D.h"

Expand Down Expand Up @@ -55,6 +56,7 @@ namespace Supernova {

RayReturn intersects(Plane plane);
RayReturn intersects(AABB box);
RayReturn intersects(Sphere sphere);
RayReturn intersects(Body2D body);
RayReturn intersects(Body2D body, size_t shape);
RayReturn intersects(Body3D body);
Expand Down
55 changes: 55 additions & 0 deletions engine/core/math/Sphere.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "Sphere.h"


using namespace Supernova;


Sphere::Sphere() : center(Vector3(0, 0, 0)), radius(1.0f){

}

Sphere::Sphere(Vector3 c, float r) : center(c), radius(r){

}

Sphere::Sphere(const Sphere& s){
this->center = s.center;
this->radius = s.radius;
}

Sphere& Sphere::operator = (const Sphere& s){
this->center = s.center;
this->radius = s.radius;

return *this;
}

bool Sphere::operator == (const Sphere& s){
return ((this->center == s.center) && (this->radius == s.radius));
}

bool Sphere::operator != (const Sphere& s){
return ((this->center != s.center) || (this->radius != s.radius));
}

std::string Sphere::toString() const {
return std::string("Sphere: center (") + center.toString() + "), radius " + std::to_string(radius);
}

bool Sphere::contains(const Vector3& point) const {
return (point - center).squaredLength() <= (radius * radius);
}

bool Sphere::intersects(const Sphere& other) const {
float distanceSquared = (center - other.center).squaredLength();
float radiusSum = radius + other.radius;
return distanceSquared <= (radiusSum * radiusSum);
}

float Sphere::surfaceArea() const {
return 4 * M_PI * radius * radius;
}

float Sphere::volume() const {
return (4.0f / 3.0f) * M_PI * radius * radius * radius;
}
31 changes: 31 additions & 0 deletions engine/core/math/Sphere.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef sphere_h
#define sphere_h

#include <cmath>
#include "Vector3.h" // Ensure you have this header for the Vector3 class

namespace Supernova{
class Sphere {
public:
Vector3 center;
float radius;

Sphere();
Sphere(Vector3 c, float r);
Sphere(const Sphere& s);

Sphere& operator = (const Sphere& s);
bool operator == (const Sphere& s);
bool operator != (const Sphere& s);

std::string toString() const;

bool contains(const Vector3& point) const;
bool intersects(const Sphere& other) const;

float surfaceArea() const;
float volume() const;
};
}

#endif

0 comments on commit ec75d16

Please sign in to comment.