-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.h
More file actions
115 lines (93 loc) · 3.99 KB
/
Entity.h
File metadata and controls
115 lines (93 loc) · 3.99 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include "Transform.h"
#include "VulkanAcceleration.h"
#include <string>
#include <cstdint>
#include <glm/gtc/matrix_inverse.hpp>
#include <algorithm>
enum class EntityType {
STATIC, // Objects that never move (buildings, ground, barriers)
DYNAMIC // Objects that can move (cars, debris)
};
class Entity {
public:
Entity(const std::string& name, uint32_t geometryIndex, EntityType type = EntityType::STATIC)
: name(name), geometryIndex(geometryIndex), type(type) {}
// Transform
Transform transform;
// Identity
std::string name;
uint32_t geometryIndex; // Index into VulkanAcceleration::objects array
EntityType type;
// State
bool isActive = true;
bool isDeformed = false;
// Physics
glm::vec3 velocity = glm::vec3(0.0f);
glm::vec3 angularVelocity = glm::vec3(0.0f);
float mass = 1500.0f; // kg (typical car)
float elasticity = 0.3f; // 0.0 (plastic) to 1.0 (perfectly elastic)
float structuralIntegrity = 1.0f; // 1.0 (new) to 0.0 (destroyed)
// Getters
bool isDynamic() const { return type == EntityType::DYNAMIC; }
bool isStatic() const { return type == EntityType::STATIC; }
// Apply impact and deform mesh
void applyImpact(glm::vec3 worldImpactPoint, glm::vec3 impulse, GeometryObject& geom) {
if (!isDynamic() || !isActive) return;
// Transform world impact point to local space
glm::mat4 modelMatrix = transform.getMatrix();
glm::mat4 invModelMatrix = glm::inverse(modelMatrix);
glm::vec3 localImpactPoint = glm::vec3(invModelMatrix * glm::vec4(worldImpactPoint, 1.0f));
glm::vec3 localImpulse = glm::vec3(invModelMatrix * glm::vec4(impulse, 0.0f));
float impulseMagnitude = glm::length(localImpulse);
if (impulseMagnitude < 0.1f) return;
// Damage calculation
float damage = impulseMagnitude / (mass * 10.0f);
structuralIntegrity = std::max(0.0f, structuralIntegrity - damage);
// If structural integrity is zero, deactivate the entity (total wreck)
if (structuralIntegrity <= 0.001f) {
isActive = false;
return;
}
// Deformation radius depends on mass and impulse
float radius = 0.5f + (impulseMagnitude / mass) * 2.0f;
// Deform vertices
bool changed = false;
for (auto& vertex : geom.cpuVertices) {
float dist = glm::distance(vertex.pos, localImpactPoint);
if (dist < radius) {
// Falloff based on distance
float falloff = 1.0f - (dist / radius);
// Displacement
glm::vec3 displacement = localImpulse * falloff * 0.001f; // Scale down for mesh units
// Breaking/Piercing: if impulse is very high, push vertex "away" (simulating a hole)
if (impulseMagnitude > mass * 50.0f && falloff > 0.7f) {
vertex.pos += glm::normalize(localImpulse) * 10.0f; // Push far away
} else {
vertex.pos += displacement;
// Skew normal slightly to reflect deformation
vertex.normal = glm::normalize(vertex.normal + displacement * 0.5f);
}
changed = true;
}
}
if (changed) {
isDeformed = true;
}
}
// Update (called each frame for dynamic objects)
void update(float deltaTime) {
if (!isDynamic() || !isActive) return;
// Apply velocity (basic integration)
transform.position += velocity * deltaTime;
// Apply angular velocity (simplified)
if (glm::length(angularVelocity) > 0.0001f) {
float angle = glm::length(angularVelocity) * deltaTime;
glm::vec3 axis = glm::normalize(angularVelocity);
transform.rotate(angle, axis);
}
// Basic air resistance/friction
velocity *= (1.0f - 0.1f * deltaTime);
angularVelocity *= (1.0f - 0.2f * deltaTime);
}
};