From d4b40dcf7589bef2bbd0b6b940ee992da9db2343 Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Fri, 21 Jul 2023 00:12:00 -0600 Subject: Working collision system --- src/bullets.c | 75 ++++++++--------------------------------------------------- 1 file changed, 10 insertions(+), 65 deletions(-) (limited to 'src/bullets.c') 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, + }; } -- cgit v1.2.3