aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/physics/physics.cpp78
-rw-r--r--src/physics/physics.h26
-rw-r--r--src/raytracer/raytracer.cpp9
-rw-r--r--src/utils/sceneparser.cpp23
4 files changed, 114 insertions, 22 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 &currentShape, 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 &currentShape, RenderShapeData &shape)
+{
+ return false;
+}
+
+bool Physics::checkForCylinderCollision(RenderShapeData &currentShape, RenderShapeData &shape)
+{
+ return false;
+}
+
+bool Physics::checkForCubeCollision(RenderShapeData &currentShape, 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 &currentShape, RenderShapeData &shape);
+
+ static bool checkForConeCollision(RenderShapeData &currentShape, RenderShapeData &shape);
+
+ static bool checkForCylinderCollision(RenderShapeData &currentShape, RenderShapeData &shape);
+
+ static bool checkForCubeCollision(RenderShapeData &currentShape, RenderShapeData &shape);
+
+ static void updateShapePositions(std::vector<RenderShapeData> &shapes);
+
+ static void handleCollisions(std::vector<RenderShapeData> &shapes);
+};
+
+#endif //PROJECTS_RAY_PHYSICS_H
diff --git a/src/raytracer/raytracer.cpp b/src/raytracer/raytracer.cpp
index 1918493..f9bef4f 100644
--- a/src/raytracer/raytracer.cpp
+++ b/src/raytracer/raytracer.cpp
@@ -10,6 +10,7 @@
#include <QKeyEvent>
#include <QTimerEvent>
#include "vec4ops/vec4ops.h"
+#include "physics/physics.h"
// RayTracer::RayTracer(const Config &config) : m_config(config) {}
RayTracer::RayTracer(QWidget *parent) : QWidget(parent) {
@@ -51,7 +52,13 @@ void RayTracer::render(RGBA *imageData, const RayTraceScene &scene) {
if (settings.currentTime < settings.maxTime) { // still more to render
// render the next frame
settings.currentTime++;
- settings.w++;
+ // settings.w++;
+
+ // update physics
+ Physics::updateShapePositions(m_metaData.shapes);
+ Physics::handleCollisions(m_metaData.shapes);
+
+
emit settingsChanged(m_imageLabel); // emit to allow the UI to update then render the next frame
} else { // done rendering
// assemble the video
diff --git a/src/utils/sceneparser.cpp b/src/utils/sceneparser.cpp
index 7db16b0..c601f0b 100644
--- a/src/utils/sceneparser.cpp
+++ b/src/utils/sceneparser.cpp
@@ -43,14 +43,11 @@ void initTree(SceneNode* currentNode, std::vector<RenderShapeData> *shapes, std:
// currentCTM *= SceneParser::getRotationMatrix4(1.f, glm::vec3(settings.xy, settings.xz, settings.yz), glm::vec3(settings.xw, settings.yw, settings.zw));
// }
- SceneParser::translate4(currentTranslation4d, glm::vec4(0.f, 0.f, 0.f, settings.w));
-
for (auto t : currentNode->transformations) {
switch (t->type)
{
case TransformationType::TRANSFORMATION_TRANSLATE:
- SceneParser::translate4(currentTranslation4d, glm::vec4(t->translate.xyz(), 0.f));
- // currentCTM = glm::translate(glm::mat4(1.0f), glm::vec3(currentTranslation4d));
+ SceneParser::translate4(currentTranslation4d, t->translate);
break;
case TransformationType::TRANSFORMATION_ROTATE:
currentCTM *= SceneParser::getRotationMatrix4(t->angle, t->rotate3, t->rotateW);
@@ -60,7 +57,7 @@ void initTree(SceneNode* currentNode, std::vector<RenderShapeData> *shapes, std:
break;
case TransformationType::TRANSFORMATION_MATRIX:
currentCTM *= glm::mat4(t->matrix);
- currentTranslation4d *= glm::vec4(t->matrixRight4d); // TODO
+ currentTranslation4d += t->matrixRight4d;
break;
default:
std::cout << "Invalid transformation type" << std::endl;
@@ -72,22 +69,6 @@ void initTree(SceneNode* currentNode, std::vector<RenderShapeData> *shapes, std:
for(auto primitive : currentNode->primitives) {
- // primitive->material.textureData = loadTextureFromFile(QString::fromStdString(primitive->material.textureMap.filename));
- // float unitMass = 1.f;
- // switch (primitive->type)
- // {
- // case PrimitiveType::PRIMITIVE_CUBE:
- // unitMass = 0.5f * 0.5f * 0.5f;
- // break;
- // case PrimitiveType::PRIMITIVE_SPHERE:
- // unitMass = 4.f / 3.f * 3.14159f * 0.5f * 0.5f * 0.5f;
- // break;
- // case PrimitiveType::PRIMITIVE_CONE:
- // unitMass = 1.f / 3.f * 3.14159f * 0.5f * 0.5f * 0.5f;
- // break;
- // case PrimitiveType::PRIMITIVE_CYLINDER:
- // unitMass = 3.14159f * 0.5f * 0.5f * 0.5f;
- // break;
RenderShapeData rsd = {
.primitive = *primitive,
.ctm = currentCTM,