aboutsummaryrefslogtreecommitdiff
path: root/src/bullets.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/bullets.c
parent43e31b6e124da754ef928d22fbb9a1d7640aab4b (diff)
Working collision system
Diffstat (limited to 'src/bullets.c')
-rw-r--r--src/bullets.c75
1 files changed, 10 insertions, 65 deletions
diff --git a/src/bullets.c b/src/bullets.c
index ea913c7..1619111 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -30,71 +30,16 @@ BulletHitInfo handleBulletHit(Entity * entity, Bullet bullet) {
}
BulletHitInfo shootBullet(World * world, Bullet bullet) {
- int i, j;
- RayCollision collision;
- Entity * currentEntity;
- Ray ray;
+ EntityId hitId = traceRayToEntityInWorld(world, bullet.ray, bullet.fromFingerprint, true);
+ Entity * hitEntity = getEntityFromWorld(*world, hitId);
- // Set direction.
- ray.direction = bullet.ray.direction;
+ // We hit a fucker.
+ if (hitEntity != NULL)
+ return handleBulletHit(hitEntity, bullet);
- // Stores all the hits so we can find closest one.
- int hits[world->entitiesCount];
- size_t hitsSize = 0;
-
- // Loop through entities.
- for (i = 0; i < world->entitiesCount; ++i) {
- currentEntity = &world->entities[i];
-
- // This was the entity that shot it.
- if (currentEntity->fingerprint == bullet.fromFingerprint)
- continue;
- else if (currentEntity->model == NULL) // Null model indeed.
- continue;
-
- // Set position relative to entity.
- ray.position = Vector3Subtract(bullet.ray.position, currentEntity->position);
-
- // Loop through meshes.
- for (j = 0; j < currentEntity->model->meshCount; ++j) {
- collision = GetRayCollisionMesh(
- ray,
- currentEntity->model->meshes[j],
- currentEntity->model->transform
- );
-
- // Did hit.
- if (collision.hit) {
- hits[hitsSize] = i;
- ++hitsSize;
- break;
- }
- }
- }
-
- // No hits.
- if (hitsSize == 0)
- return (BulletHitInfo){
- .hit = false,
- .killed = false,
- .hitId = ENTITY_NONE,
- };
-
- float dis = Vector3Distance(world->entities[hits[0]].position, bullet.ray.position);
- float closest = dis;
- int closestNum = 0;
-
- // Find closest.
- for (i = 0; i < hitsSize; ++i) {
- dis = Vector3Distance(world->entities[hits[i]].position, bullet.ray.position);
-
- // This fucker is closer.
- if (dis < closest) {
- closest = dis;
- closestNum = i;
- }
- }
-
- // Handle closest bullet.
- return handleBulletHit(&world->entities[hits[closestNum]], bullet);
+ return (BulletHitInfo){
+ .hit = false,
+ .killed = false,
+ .hitId = ENTITY_NONE,
+ };
}