diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-12-13 04:46:45 -0500 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-12-13 04:53:35 -0500 |
commit | f887bd83942f13efe98d0c2cd997d037ed2ef068 (patch) | |
tree | c9bbf009b7181ca40ca0fa8b7e0b0ba45ffe1761 /src/physics | |
parent | c6c65df3122329a8748899d48ddeeb04ed23518d (diff) |
fixing changes
Diffstat (limited to 'src/physics')
-rw-r--r-- | src/physics/physics.cpp | 78 | ||||
-rw-r--r-- | src/physics/physics.h | 26 |
2 files changed, 104 insertions, 0 deletions
diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp new file mode 100644 index 0000000..3cba57c --- /dev/null +++ b/src/physics/physics.cpp @@ -0,0 +1,78 @@ +// +// Created by Michael Foiani on 12/13/23. +// +#include "physics.h" + +bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +{ + glm::vec4 currentCenter = currentShape.translation4d; + glm::vec4 shapeCenter = shape.translation4d; + // define a radius vector + glm::vec4 radiusVector = {.5f, 0, 0, 0}; + glm::vec4 r1 = currentShape.ctm * radiusVector; + glm::vec4 r2 = shape.ctm * radiusVector; + float distance = glm::distance(currentCenter, shapeCenter); + + // update velocity + if (distance <= r1.x + r2.x) + { + currentShape.velocity = -currentShape.velocity; + shape.velocity = -shape.velocity; + } + + return distance <= r1.x + r2.x; +} + +bool Physics::checkForConeCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +{ + return false; +} + +bool Physics::checkForCylinderCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +{ + return false; +} + +bool Physics::checkForCubeCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +{ + return false; +} + +void Physics::handleCollisions(std::vector<RenderShapeData> &shapes) { + for (auto &shape : shapes) + { + for (auto &otherShape : shapes) + { + if (shape.ctm == otherShape.ctm && shape.translation4d == otherShape.translation4d) + { + continue; + } + switch (shape.primitive.type) + { + case PrimitiveType::PRIMITIVE_CONE: + checkForConeCollision(shape, otherShape); + break; + case PrimitiveType::PRIMITIVE_CYLINDER: + checkForCylinderCollision(shape, otherShape); + break; + case PrimitiveType::PRIMITIVE_CUBE: + checkForCubeCollision(shape, otherShape); + break; + case PrimitiveType::PRIMITIVE_SPHERE: + checkForSphereCollision(shape, otherShape); + break; + default: + break; + } + } + } +} + +void Physics::updateShapePositions(std::vector<RenderShapeData> &shapes) +{ + for (auto &shape : shapes) + { + shape.translation4d += shape.velocity; + shape.inverseTranslation4d -= shape.velocity; + } +}
\ No newline at end of file diff --git a/src/physics/physics.h b/src/physics/physics.h new file mode 100644 index 0000000..6410d74 --- /dev/null +++ b/src/physics/physics.h @@ -0,0 +1,26 @@ +// +// Created by Michael Foiani on 12/13/23. +// + +#ifndef PROJECTS_RAY_PHYSICS_H +#define PROJECTS_RAY_PHYSICS_H + + +#include "utils/sceneparser.h" + +class Physics { +public: + static bool checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + + static bool checkForConeCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + + static bool checkForCylinderCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + + static bool checkForCubeCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + + static void updateShapePositions(std::vector<RenderShapeData> &shapes); + + static void handleCollisions(std::vector<RenderShapeData> &shapes); +}; + +#endif //PROJECTS_RAY_PHYSICS_H |