aboutsummaryrefslogtreecommitdiff
path: root/src/world.c
diff options
context:
space:
mode:
authornathansmithsmith <thenathansmithsmith@gmail.com>2023-07-21 00:12:00 -0600
committernathansmithsmith <thenathansmithsmith@gmail.com>2023-07-21 00:12:00 -0600
commitd4b40dcf7589bef2bbd0b6b940ee992da9db2343 (patch)
tree42756cfe9a23382bc406732fdfeccce64fddb6d4 /src/world.c
parent43e31b6e124da754ef928d22fbb9a1d7640aab4b (diff)
Working collision system
Diffstat (limited to 'src/world.c')
-rw-r--r--src/world.c103
1 files changed, 65 insertions, 38 deletions
diff --git a/src/world.c b/src/world.c
index 553f2ed..e6f24bb 100644
--- a/src/world.c
+++ b/src/world.c
@@ -225,47 +225,19 @@ KfError removeEntityFromWorld(World * world, EntityId id) {
return KFSUCCESS;
}
-void checkCollisionWithWorld(World * world, Entity * entity) {
- int i, j;
- Entity * entity2;
- BoundingBox box1, box2;
-
- Mesh mesh = entity->model->meshes[0];
-
- Vector3 triangle[3];
-
- for (i = 0; i < mesh.triangleCount; ++i) {
-
- for (j = 0; j < 3; ++j)
- triangle[j] = (Vector3){
- mesh.vertices[(i * 9) + (j * 3)],
- mesh.vertices[(i * 9) + (j * 3) + 1],
- mesh.vertices[(i * 9) + (j * 3) + 2],
- };
-
- DrawLine3D(triangle[0], triangle[1], BLUE);
- DrawLine3D(triangle[1], triangle[2], BLUE);
- DrawLine3D(triangle[2], triangle[0], BLUE);
- }
-
- // Check for collision.
- //for (i = 0; i < world->entitiesCount; ++i) {
- // entity2 = &world->entities[i];
+void handleCollisionInWorld(Entity * entity1, Entity * entity2) {
+ if (entity1->id != 0)
+ entity1->health = 0.0;
+ if (entity2->id != 0)
+ entity2->health = 0.0;
- // if (entity->fingerprint == entity2->fingerprint)
- // continue;
-
- // // These fuckers collided.
- // if (CheckCollisionBoxes(box1, box2)) {
- // printf("hi %ld\n", clock());
- // break;
- // }
- //}
+ puts("cc");
}
void updateWorld(World * world, Game * game) {
- int i;
+ int i, j;
Entity * entity;
+ Entity * entity2;
// People are fucking dying.
EntityId kills[world->entitiesCount];
@@ -278,6 +250,15 @@ void updateWorld(World * world, Game * game) {
if (entity->updateCb != NULL)
entity->updateCb(game, entity);
+ // Check for collision.
+ for (j = 0; j < i; ++j) {
+ entity2 = &world->entities[j];
+
+ // Collided.
+ if (checkEntityCollision(*entity, *entity2))
+ handleCollisionInWorld(entity, entity2);
+ }
+
// It fucking died.
if (entity->health <= 0.0) {
kills[killCount] = entity->id;
@@ -297,14 +278,60 @@ void drawWorld(World * world, Game * game) {
for (i = 0; i < world->entitiesCount; ++i) {
entity = &world->entities[i];
- //checkCollisionWithWorld(world, entity);
-
// Call draw callback.
if (entity->drawCb != NULL)
entity->drawCb(game, entity);
}
}
+
+EntityId traceRayToEntityInWorld(World * world, Ray ray, EntityFingerprint from, bool useFrom) {
+ int i, j;
+ RayCollision collision;
+ Entity * currentEntity;
+ Ray transformedRay;
+
+ // Set direction.
+ transformedRay.direction = ray.direction;
+
+ // Used for finding closest.
+ float closest = -1.0; // -1.0 means ray haven't hit anything.
+ EntityId closestId = ENTITY_NONE;
+
+ // Loop through entities.
+ for (i = 0; i < world->entitiesCount; ++i) {
+ currentEntity = &world->entities[i];
+
+ if (currentEntity->fingerprint == from && useFrom)
+ continue;
+ else if (currentEntity->model == NULL) // Null model indeed.
+ continue;
+
+ // Set position relative to entity.
+ transformedRay.position = Vector3Subtract(ray.position, currentEntity->position);
+
+ // Loop through meshes.
+ for (j = 0; j < currentEntity->model->meshCount; ++j) {
+ collision = GetRayCollisionMesh(
+ transformedRay,
+ currentEntity->model->meshes[j],
+ currentEntity->model->transform
+ );
+
+ // Did hit.
+ if (collision.hit) {
+ // Find closest.
+ if (collision.distance < closest || closest == -1.0) {
+ closest = collision.distance;
+ closestId = currentEntity->id;
+ }
+ }
+ }
+ }
+
+ return closestId;
+}
+
KfError addEntryToWorld(World * world, Game * game, WorldEntry entry) {
// Create entity.
Entity entity = createEntity(entry.type, game);