blob: 6b95bbea66c16d7606934cdba16c9ed65ed1117f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
//
// Created by Michael Foiani on 12/13/23.
//
#include <iostream>
#include "physics.h"
bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &shape)
{
glm::vec4 currentCenter = currentShape.translation4d;
glm::vec4 shapeCenter = shape.translation4d;
// define a radius vector
float radius = .5;
float distance = glm::distance(currentCenter, shapeCenter);
// std::cout << "distance: " << distance << std::endl;
// update velocity
if (distance <= radius + radius)
{
currentShape.velocity *= -1.f;
// move a little in other direction so it doesn't flip again
currentShape.translation4d += currentShape.velocity;
}
return distance <= radius + radius;
}
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;
}
}
|