-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeClass.cpp
44 lines (36 loc) · 1.47 KB
/
CubeClass.cpp
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
#include "CubeClass.h"
const GLfloat CubeClass::gravity = -9.8f;
CubeClass::CubeClass(glm::vec3 position, glm::vec3 size) : position(position), size(size), velocity(0.0f), deltaTime(0.0f), currentFrame(0.0f), lastFrame(0.0f) {}
glm::vec3 CubeClass::getPosition() const
{
return position;
}
void CubeClass::update()
{
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Euler integration
velocity += gravity * deltaTime;
position.y += velocity * deltaTime;
// Collision detection with the ground
if (position.y < -0.5f) {
position.y = -0.5f;
velocity = -velocity * 0.8f; // Bounce back with some energy loss
}
}
void CubeClass::resolveCollision(const CubeClass& other) {
// Very simple collision response: just reverse velocities
velocity = -velocity;
// You could add more complex collision response here, such as
// calculating new velocities based on mass and momentum conservation.
}
bool CubeClass::checkCollision(const CubeClass& other) const {
// AABB collision detection
return position.x - size.x < other.position.x + other.size.x &&
position.x + size.x > other.position.x - other.size.x &&
position.y - size.y < other.position.y + other.size.y &&
position.y + size.y > other.position.y - other.size.y &&
position.z - size.z < other.position.z + other.size.z &&
position.z + size.z > other.position.z - other.size.z;
}