From 54d7f8b46e71f67a8d0c08447299aadb0920abe4 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Wed, 13 Dec 2023 05:57:27 -0500 Subject: getting clsoer to collision --- src/physics/physics.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/physics/physics.cpp') diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 9c9a513..a624bc5 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -1,6 +1,7 @@ // // Created by Michael Foiani on 12/13/23. // +#include #include "physics.h" bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &shape) @@ -8,19 +9,20 @@ bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShape 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 radius = .5; float distance = glm::distance(currentCenter, shapeCenter); + // std::cout << "distance: " << distance << std::endl; + // update velocity - if (distance <= r1.x + r2.x) + if (distance <= radius + radius) { - currentShape.velocity = -currentShape.velocity; - shape.velocity = -shape.velocity; + currentShape.velocity *= -1.f; + // move a little in other direction so it doesn't flip again + currentShape.translation4d = currentShape.velocity; } - return distance <= r1.x + r2.x; + return distance <= radius + radius; } bool Physics::checkForConeCollision(RenderShapeData ¤tShape, RenderShapeData &shape) -- cgit v1.2.3-70-g09d2