#include "entity_collision.h" #include "entity_player.h" #include "entity_projectile.h" #include "game.h" #include "grid_entity_door.h" #include "log.h" #include "manager.h" #include "room.h" #include #include #include // What crushes rocks on contact: The Nail, Leo (worn or rolled by Zodiac), Thunder Thighs, Stompy and Mega Mush. constexpr int COLLECTIBLE_THE_NAIL = 83; constexpr int COLLECTIBLE_LEO = 302; constexpr int COLLECTIBLE_THUNDER_THIGHS = 314; constexpr int PLAYERFORM_STOMPY = 13; // A transformation counts once the player holds this many of its items. constexpr int PLAYER_FORM_ITEMS = 3; // A poop's reach for a player's collision: a giga poop's quarter from its shared center, any other from its cell's. constexpr int POOP_GIGA_FIRST = 7; constexpr int POOP_GIGA_LAST = 10; constexpr float POOP_GIGA_REACH = 38; constexpr float POOP_REACH = 18; constexpr float POOP_GIGA_QUARTER = 20; constexpr std::array POOP_GIGA_CENTER_X = {POOP_GIGA_QUARTER, -POOP_GIGA_QUARTER, POOP_GIGA_QUARTER, -POOP_GIGA_QUARTER}; constexpr std::array POOP_GIGA_CENTER_Y = {POOP_GIGA_QUARTER, POOP_GIGA_QUARTER, -POOP_GIGA_QUARTER, -POOP_GIGA_QUARTER}; // The Lost passes the mirror door in the mirror world. constexpr int MIRROR_DIMENSION = 1; // The Beast's dungeon keeps players this far above its floor's center line. constexpr float BEAST_DUNGEON_OFFSET_Y = 20; // A player's wall hit keeps this much of the speed into it, any other entity's turns it back this much, and both // push back a little more. Entities then lose up to half their speed against the wall. constexpr float WALL_BOUNCE = 0.9f; constexpr float WALL_REBOUND = 1.8f; constexpr float WALL_PUSH = 0.1f; constexpr float WALL_FRICTION = 0.5f; constexpr float NORMAL_EPSILON = 0.001f; constexpr float PUSH_OUT_MIN_SPEED = 2; constexpr int PUSH_OUT_STEPS_GROUND = 12; constexpr int PUSH_OUT_STEPS = 6; constexpr float HALF_CELL = GRID_CELL_SIZE / 2; // The enemies that bounce harder off walls, with the sounds some make. constexpr int ENTITY_GURDY_JR = 99; constexpr int ENTITY_DIP = 217; constexpr int ENTITY_SQUIRT = 220; constexpr int ENTITY_BALL_AND_CHAIN = 893; constexpr int ENTITY_SINGE = 915; constexpr int SINGE_BALL = 1; constexpr float REBOUND_GURDY_JR = 1.4f; constexpr float REBOUND_BOUNCER = 1.2f; constexpr int SOUND_DEATH_BURST_LARGE = 28; constexpr int SOUND_STONE_IMPACT = 487; constexpr int SOUND_BALL_AND_CHAIN_HIT = 581; constexpr float BOUNCE_VOLUME_SCALE = 0.05f; constexpr float BOUNCE_VOLUME_BASE = 0.2f; constexpr float BOUNCE_VOLUME_MAX = 1.2f; constexpr float BOUNCE_PITCH_BASE = 0.9f; constexpr float BOUNCE_PITCH_SPREAD = 0.2f; constexpr float RANDOM_UNIT_SCALE = 2.3283064e-10f; // Samson's Chains' ball breaks rocks it hits faster than this. constexpr int FAMILIAR_SAMSONS_CHAINS = 75; constexpr float CHAINS_CRUSH_SPEED_SQUARED = 25; // Mega Satan's room lets shots leave through its walls. constexpr int BOSS_MEGA_SATAN = 55; // The room types whose doors Notched Axe hacks open. constexpr int ROOM_TYPE_SECRET = 7; constexpr int ROOM_TYPE_SUPERSECRET = 8; // What weighs an entity down as it collides: a magnet against enemies, Mega Mush and the Cube Baby for a player, // enemies that shots barely move, and what makes a player shrug off projectiles. constexpr float MASS_HEAVY = 10; constexpr float MASS_ANCHORED = 100; constexpr float MASS_PROJECTILE_PROOF = 20; constexpr int FAMILIAR_CUBE_BABY = 239; constexpr int COLLECTIBLE_ISAACS_HEART = 276; constexpr int NULL_TOOTH_AND_NAIL = 59; // Charger, Boom Fly, Knight, Leech, Pin, Swarmer, Floating Knight, Bone Knight, Poop and Movable TNT. constexpr std::array SHOT_ANCHORED_TYPES = {23, 25, 41, 55, 62, 91, 254, 283, 245, 292}; // Two entities closer than this are not pushed apart. A player or the Punching Bag is not pushed into a wall. constexpr float PUSH_MIN_DISTANCE = 0.0001f; constexpr int FAMILIAR_PUNCHING_BAG = 67; constexpr float PLAYER_PUSH_SHARE = 0.5f; constexpr float BEAST_PUSH_DAMPING = 0.25f; // One side of a push apart: the direction it moves along, the impulse its velocity takes, the overlap and its share. struct EntityPush { Vector direction{}; Vector impulse{}; float overlap = 0; float share = 0; }; static float vector_length(Vector v) { return std::sqrt(v.x * v.x + v.y * v.y); } // A vector scaled to length one, or as it is with no length (Vector::Normalized). static Vector vector_normalized(Vector v) { float length = vector_length(v); if (length > 0) return vector_make(v.x * (1 / length), v.y * (1 / length)); return v; } static bool is_unit_size(const Entity& entity) { return entity.sizeMulti.x == 1 && entity.sizeMulti.y == 1; } static bool is_wall(uint32_t collision) { return collision == COLLISION_WALL || collision == COLLISION_WALL_EXCEPT_PLAYER; } static bool is_singe_ball(const Entity& entity) { return entity.type == ENTITY_SINGE && entity.variant == SINGE_BALL; } // Plays a sound through the fart seeds. static void sound_play(int sound, float volume, float pitch) { seed_fart_sound_override(&sound); g_manager->sfxManager.Play(sound, {.volume = volume, .pitch = pitch}); } // The knock of a heavy ball, louder the faster it goes, at a slightly random pitch. static void bounce_sound_play(int sound, float speed) { float pitch = (float)Random() * RANDOM_UNIT_SCALE * BOUNCE_PITCH_SPREAD + BOUNCE_PITCH_BASE; sound_play(sound, std::min(speed * BOUNCE_VOLUME_SCALE + BOUNCE_VOLUME_BASE, BOUNCE_VOLUME_MAX), pitch); } // Moves an entity back along a wall's normal out of what blocks it, bisecting for steps tries between its depth into // the wall and that depth less its speed (at least two). template static void grid_push_out(Entity& entity, Vector normal, int steps, IsFreeAt isFreeAt) { float depth = std::sqrt(normal.y * entity.sizeMulti.y * (normal.y * entity.sizeMulti.y) + normal.x * entity.sizeMulti.x * (normal.x * entity.sizeMulti.x)) * entity.size; float freeDepth = depth - std::max(vector_length(entity.velocity), PUSH_OUT_MIN_SPEED); float blockedDepth = depth; for (int step = 0; step < steps; step++) { float middle = (blockedDepth - freeDepth) * 0.5f + freeDepth; Vector at = vector_make(normal.x * middle + entity.pos.x, normal.y * middle + entity.pos.y); (isFreeAt(at) ? freeDepth : blockedDepth) = middle; } entity.pos.x += normal.x * (freeDepth - depth); entity.pos.y += normal.y * (freeDepth - depth); } // A crushed rock bridges the pit beyond it, on the axis its crusher came along. static void crush_bridge(Room& room, Entity& crusher, GridEntity& rock) { Vector center = room.GetGridPosition(rock.gridIndex); float dx = center.x - crusher.pos.x; float dy = center.y - crusher.pos.y; float directionX = 0; float directionY = 0; if (std::abs(dx) > std::abs(dy)) directionX = dx < 0 ? -1 : 1; else directionY = dy < 0 ? -1 : 1; int beyond = room.GetGridIndex( vector_make(center.x + directionX * GRID_CELL_SIZE, center.y + directionY * GRID_CELL_SIZE)); GridEntity* pit = room.GetGridEntity(beyond); if (pit && pit->type == GRID_PIT) reconstruction_missing("Room::TryMakeBridge 0081e6e0"); } // 007aa7c0 Whether the player has a transformation: three items of its set, out of the cursed mist and alive. bool Entity_Player::HasPlayerForm(int playerForm) { if (hasCurseMistEffect || isCoopGhost || playerForm < 0 || playerForm >= PLAYER_FORM_SLOTS) return false; return playerForms[playerForm] >= PLAYER_FORM_ITEMS; } // 006b5fd0 The grid collision a player meets at a position: a poop only within its reach, the Lost's way through a // mirror door, and walls for anyone but a main player. uint32_t Entity_Player::get_grid_collision_at(Vector position) { Room& room = *g_game->room; int gridIndex = room.GetGridIndex(position); uint32_t collision = room.GetGridCollision(gridIndex); GridEntity* grid = room.GetGridEntity(gridIndex); if (collision == COLLISION_SOLID && grid && grid->type == GRID_POOP) { bool isGiga = grid->variant >= POOP_GIGA_FIRST && grid->variant <= POOP_GIGA_LAST; Vector center = room.GetGridPosition(grid->gridIndex); if (isGiga) { center.x += POOP_GIGA_CENTER_X[grid->variant - POOP_GIGA_FIRST]; center.y += POOP_GIGA_CENTER_Y[grid->variant - POOP_GIGA_FIRST]; } float dx = position.x - center.x; float dy = position.y - center.y; float reach = isGiga ? POOP_GIGA_REACH : POOP_REACH; if (dy * dy + dx * dx > reach * reach) collision = COLLISION_NONE; } if (collision == COLLISION_WALL) { if (type != ENTITY_PLAYER || !grid || grid->type != GRID_DOOR) return COLLISION_WALL; GridEntity_Door& door = static_cast(*grid); if (!(door.doorFlags & DOOR_FLAG_MIRROR) || door.state == DOOR_STATE_OPEN) return COLLISION_WALL; bool isSpirit = GetHealthType() == HEALTH_NO_HEALTH || HasInstantDeathCurse(); if (!isSpirit && g_game->currentDimensionIdx != MIRROR_DIMENSION) return COLLISION_WALL; collision = COLLISION_WALL_EXCEPT_PLAYER; } else if (collision != COLLISION_WALL_EXCEPT_PLAYER) return collision; // The original reads a non-player's fields here. A non-player meets the wall. if (type != ENTITY_PLAYER || variant != 0 || isCoopGhost || parent || IsHologram()) return COLLISION_WALL; return collision; } // 007c3380 Whether the player crushes rocks it walks into. bool Entity_Player::CanCrushRocks() { return temporaryEffects.HasCollectibleEffect(COLLECTIBLE_THE_NAIL) || HasCollectible(COLLECTIBLE_LEO) || temporaryEffects.HasCollectibleEffect(COLLECTIBLE_LEO) || GetZodiacEffect() == COLLECTIBLE_LEO || HasCollectible(COLLECTIBLE_THUNDER_THIGHS) || HasPlayerForm(PLAYERFORM_STOMPY) || temporaryEffects.HasCollectibleEffect(COLLECTIBLE_MEGA_MUSH); } // Mega Mush's stomp on a block, pillar or lock: the cell becomes a plain rock (the original's inlined // Room::SpawnGridEntity), which the stomp breaks. static void mega_mush_crush(Room& room, Entity& player, GridEntity& grid, int gridIndex) { uint32_t seed = grid.spawnSeed; GridEntity* old = room.gridEntities[gridIndex]; if (old && old->type != GRID_DECORATION) { room.gridPaths[gridIndex] = GRID_PATH_FREED; room.gridEntities[gridIndex] = nullptr; delete old; } if (room.gridPaths[gridIndex] <= GRID_PATH_FREED) { GridEntity* rock = CreateGridEntity(GRID_ROCK, gridIndex); delete room.gridEntities[gridIndex]; room.gridEntities[gridIndex] = rock; rock->variant = 0; rock->varData = 0; rock->Init(seed); } if (room.gridEntities[gridIndex]) room.gridEntities[gridIndex]->Destroy(false, EntityRef(&player)); } // What a player does to the grid entity at one of its collision points: Mega Mush stomps blocks and kicks doors and // webs, rock crushers break what they touch, Notched Axe opens secret doors. Returns whether the point is spent on it. static bool player_grid_act(Room& room, Entity_Player& player, GridEntity* grid, int gridIndex, uint32_t collision) { if (player.temporaryEffects.HasCollectibleEffect(COLLECTIBLE_MEGA_MUSH) && grid) { bool isStompable = grid->type == GRID_ROCKB || grid->type == GRID_PILLAR || grid->type == GRID_LOCK; if (isStompable && collision != COLLISION_NONE) { mega_mush_crush(room, player, *grid, gridIndex); return true; } if (grid->type == GRID_DOOR && collision == COLLISION_WALL) { static_cast(grid)->TryBlowOpen(true, &player); return true; } if (grid->type == GRID_SPIDERWEB) grid->Destroy(false, EntityRef(&player)); } if (!grid) return false; bool isSolid = collision == COLLISION_SOLID || collision == COLLISION_OBJECT; bool isCrusher = player.CanCrushRocks() || (grid->type == GRID_POOP && player.temporaryEffects.HasCollectibleEffect(COLLECTIBLE_MARS)); bool isBreakable = grid->collisionClass == COLLISION_SOLID || grid->collisionClass == COLLISION_OBJECT; if (isSolid && isCrusher && isBreakable && grid->Destroy(false, EntityRef(&player))) { crush_bridge(room, player, *grid); return true; } if (collision != COLLISION_WALL || grid->type != GRID_DOOR || player.itemState != COLLECTIBLE_NOTCHED_AXE) return false; GridEntity_Door& door = static_cast(*grid); bool isSecret = door.currentRoomType == ROOM_TYPE_SECRET || door.targetRoomType == ROOM_TYPE_SECRET || door.currentRoomType == ROOM_TYPE_SUPERSECRET || door.targetRoomType == ROOM_TYPE_SUPERSECRET; if (!isSecret) return false; if (GridEntity* target = room.GetGridEntity(gridIndex)) target->Destroy(false, EntityRef(&player)); return true; } // Keeps a player inside the room: out of the walls, and in the Beast's dungeon above its floor's line. static void player_room_clamp(Entity& player, Room& room, GridCollisionMode mode) { bool isSimulated = mode == GRID_COLLISION_SIMULATE; Vector clamped = room.GetClampedPositionImpl(player.pos, 0, 0, 0, 0); if (room.IsBeastDungeon()) { float margin = player.size; Vector lifted = room.GetClampedPositionImpl(vector_make(player.pos.x, player.pos.y - BEAST_DUNGEON_OFFSET_Y), margin, margin, margin, margin); lifted.y += BEAST_DUNGEON_OFFSET_Y; if (!isSimulated) player.isCollidesWithGrid = player.pos.x != lifted.x || player.pos.y != lifted.y; player.pos = lifted; return; } Entity_Player& self = static_cast(player); if (room.GetGridIndex(player.pos) >= 0 && self.get_grid_collision_at(player.pos) != COLLISION_WALL) return; if (!isSimulated) player.isCollidesWithGrid = player.pos.x != clamped.x || player.pos.y != clamped.y; if (room.roomType != ROOM_DUNGEON) player.pos = clamped; } // 006ab440 A player's collision with the room's grid: clamped into the room, acting on what its collision points touch, // then pushed out along the walls' normal and slowed against them. Simulating (Interpolate's prediction) only moves the // position. void Entity::PlayerCollideWithGrid(GridCollisionMode mode) { Entity_Player* player = dynamic_cast(this); // The original carries on with a non-player and crashes on its effects. It stops here instead. if (!player) { KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"Called PlayerCollideWithGrid with " "Entity type != Entity_Player\n"); return; } bool isSimulated = mode == GRID_COLLISION_SIMULATE; if (!isSimulated) isCollidesWithGrid = false; if (entityGridCollisionClass == GRIDCOLL_NONE || collisionPoints.empty() || minecart) return; Room& room = *g_game->room; player_room_clamp(*this, room, mode); Vector normal{}; for (const Vector& point : collisionPoints) { Vector position = vector_make(pos.x + point.x, pos.y + point.y); int gridIndex = room.GetGridIndex(position); if (gridIndex < 0) continue; GridEntity* grid = room.GetGridEntity(gridIndex); uint32_t collision = player->get_grid_collision_at(position); if (!isSimulated && player_grid_act(room, *player, grid, gridIndex, collision)) continue; bool isWall = collision == COLLISION_WALL || (collision == COLLISION_SOLID && grid && grid->type == GRID_DOOR); bool isBlocked = collision != COLLISION_NONE && collision != COLLISION_WALL_EXCEPT_PLAYER; if ((entityGridCollisionClass == GRIDCOLL_WALLS && isWall) || (entityGridCollisionClass == GRIDCOLL_GROUND && isBlocked)) { normal.x += point.x; normal.y += point.y; } } if (normal.x * normal.x + normal.y * normal.y < NORMAL_EPSILON) { if (!isSimulated) gridCollisionNormal = {}; return; } normal = vector_normalized(normal); if (!isSimulated) gridCollisionNormal = normal; float approach = -normal.x * velocity.x - normal.y * velocity.y; if (approach < 0 && !isSimulated) { isCollidesWithGrid = true; gridCollisionVelocity = velocity; float push = approach * WALL_BOUNCE - WALL_PUSH; velocity.x += push * gridCollisionNormal.x; velocity.y += push * gridCollisionNormal.y; } bool isGround = entityGridCollisionClass == GRIDCOLL_GROUND; grid_push_out(*this, normal, isGround ? PUSH_OUT_STEPS_GROUND : PUSH_OUT_STEPS, [player, isGround](Vector at) { uint32_t collision = player->get_grid_collision_at(at); return isGround ? collision == COLLISION_NONE || collision == COLLISION_WALL_EXCEPT_PLAYER : collision != COLLISION_WALL; }); } // Clamps a wall-bound entity into the room along its one axis, noting the hit with a normal back into the room. static void axis_clamp(Entity& entity, Vector clamped, bool isAxisX, bool isSimulated) { float& coordinate = isAxisX ? entity.pos.x : entity.pos.y; float target = isAxisX ? clamped.x : clamped.y; if (!isSimulated && coordinate != target) { entity.isCollidesWithGrid = true; entity.gridCollisionVelocity = entity.velocity; float direction = coordinate <= target ? 1.0f : -1.0f; entity.gridCollisionNormal = isAxisX ? vector_make(direction, 0) : vector_make(0, direction); } coordinate = target; } // Clamps an entity into the room. Applied, the hit is noted, and the returned normal points out of the room along // each axis it was clamped on. static Vector room_clamp(Entity& entity, Vector clamped, bool isSimulated) { Vector normal{}; if (!isSimulated && (entity.pos.x != clamped.x || entity.pos.y != clamped.y)) { entity.isCollidesWithGrid = true; entity.gridCollisionVelocity = entity.velocity; if (entity.pos.x != clamped.x) normal.x = entity.pos.x <= clamped.x ? -1 : 1; if (entity.pos.y != clamped.y) normal.y = entity.pos.y <= clamped.y ? -1 : 1; } entity.pos = clamped; return normal; } // A shot only notes where it stands in what stops it: a wall it does not fly back from (Mega Satan's room and rooms // without walls let it leave), a rock or other solid, and for a tear an object too. Pits stop nothing. static void shot_grid_collide(Entity& shot, Room& room) { bool isOpen = room.bossId == BOSS_MEGA_SATAN || (room.descriptor && (room.descriptor->flags & ROOM_DESCRIPTOR_FLAG_NO_WALLS)); int gridIndex = room.GetGridIndex(shot.pos); uint32_t collision = room.GetGridCollision(gridIndex); if (is_wall(collision)) { Vector clamped = room.GetClampedPositionImpl(shot.pos, 0, 0, 0, 0); bool isOutside = clamped.x != shot.pos.x || clamped.y != shot.pos.y; float inward = (clamped.y - shot.pos.y) * shot.velocity.y + (clamped.x - shot.pos.x) * shot.velocity.x; if (isOutside && (isOpen || inward >= 0)) return; } else if (collision != COLLISION_SOLID && (shot.type != ENTITY_TEAR || collision != COLLISION_OBJECT)) return; GridEntity* grid = room.GetGridEntity(gridIndex); if (!grid || grid->type == GRID_PIT) return; shot.isCollidesWithGrid = true; shot.gridCollisionVelocity = shot.velocity; } // Whether a cell blocks an entity of a grid collision class: anything for ground, all but pits for pits only, what // stands for no pits, and walls outside crawlspaces for walls. static bool is_cell_blocked(int gridClass, uint32_t collision, GridEntity* grid) { switch (gridClass) { case GRIDCOLL_GROUND: return collision != COLLISION_NONE; case GRIDCOLL_PITSONLY: return collision != COLLISION_PIT; case GRIDCOLL_NOPITS: return collision != COLLISION_NONE && collision != COLLISION_PIT && (!grid || grid->type != GRID_PIT); default: return is_wall(collision) && g_game->room->roomType != ROOM_DUNGEON; } } // Whether the push out of a wall stops at a cell for a grid collision class. static bool is_cell_free(int gridClass, uint32_t collision) { switch (gridClass) { case GRIDCOLL_GROUND: return collision == COLLISION_NONE; case GRIDCOLL_PITSONLY: return collision == COLLISION_PIT; case GRIDCOLL_NOPITS: return collision == COLLISION_NONE || collision == COLLISION_PIT; default: return !is_wall(collision); } } // Samson's Chains' ball breaks a rock it hits fast enough, bridging the pit beyond. Whether it broke one. static bool chains_crush(Entity& ball, Room& room, GridEntity& grid) { float speedSquared = ball.velocity.x * ball.velocity.x + ball.velocity.y * ball.velocity.y; if (speedSquared <= CHAINS_CRUSH_SPEED_SQUARED || grid.collisionClass != COLLISION_SOLID) return false; if (!grid.Destroy(false, EntityRef(&ball))) return false; crush_bridge(room, ball, grid); return true; } // The sum of the collision points in cells that block the entity. Samson's Chains' ball that meets a rock without // breaking it starts the sum over. static Vector blocked_points_sum(Entity& entity, Room& room, int gridClass, bool isSimulated) { bool isChains = !isSimulated && entity.type == ENTITY_FAMILIAR && entity.variant == FAMILIAR_SAMSONS_CHAINS && gridClass == GRIDCOLL_NOPITS; Vector sum{}; for (const Vector& point : entity.collisionPoints) { int gridIndex = room.GetGridIndex(vector_make(entity.pos.x + point.x, entity.pos.y + point.y)); uint32_t collision = room.GetGridCollision(gridIndex); GridEntity* grid = room.GetGridEntity(gridIndex); if (is_cell_blocked(gridClass, collision, grid)) { sum.x += point.x; sum.y += point.y; } bool isHit = collision == COLLISION_SOLID || collision == COLLISION_OBJECT; if (isChains && isHit && grid && !chains_crush(entity, room, *grid)) sum = {}; } return sum; } // A tear whose collision points cancel out takes its normal from where it sits in its cell: back along the axis it is // nearer the cell's edge on, when it reaches past one. static Vector tear_cell_normal(Entity& tear, Room& room, Vector normal) { Vector center = room.GetGridPosition(room.GetGridIndex(tear.pos)); float halfWidth = tear.size * tear.sizeMulti.x; float offsetX = tear.pos.x - center.x; float offsetY = tear.pos.y - center.y; if (std::abs(offsetX) + halfWidth >= HALF_CELL && std::abs(offsetY) + tear.size * tear.sizeMulti.y >= HALF_CELL) return normal; if (std::abs(tear.sizeMulti.x * offsetX) > std::abs(tear.sizeMulti.y * offsetY)) return vector_make(offsetX > 0 ? -halfWidth : halfWidth, 0); float halfHeight = tear.sizeMulti.y * tear.size; return vector_make(0, offsetY > 0 ? -halfHeight : halfHeight); } // Turns an entity back from a wall it runs into, harder for Gurdy Jr., Dips, Squirts and the heavy balls, which // knock. static void wall_bounce(Entity& entity, float approach) { entity.gridCollisionVelocity = entity.velocity; entity.isCollidesWithGrid = true; float push = approach * WALL_REBOUND - WALL_PUSH; if (entity.type == ENTITY_GURDY_JR) { sound_play(SOUND_DEATH_BURST_LARGE, std::min(-approach * BOUNCE_VOLUME_SCALE, BOUNCE_VOLUME_MAX), 1); push *= REBOUND_GURDY_JR; } else if (entity.type == ENTITY_DIP || entity.type == ENTITY_SQUIRT) push *= REBOUND_BOUNCER; else if (is_singe_ball(entity) || entity.type == ENTITY_BALL_AND_CHAIN) { bool isChain = entity.type == ENTITY_BALL_AND_CHAIN; bounce_sound_play(isChain ? SOUND_BALL_AND_CHAIN_HIT : SOUND_STONE_IMPACT, vector_length(entity.velocity)); push *= REBOUND_BOUNCER; } entity.velocity.x += push * entity.gridCollisionNormal.x; entity.velocity.y += push * entity.gridCollisionNormal.y; } // 006a9c90 An entity's collision with the room's grid by its grid collision class: clamped into the room along one // axis or both, a shot only noting what stops it, the rest pushed out along the normal of the cells their collision // points are blocked in, bouncing off and slowed. Simulating (Interpolate's prediction) only moves the position. // A projectile in a crawlspace flies over pits rather than stopping at walls only. void Entity::CollideWithGrid(GridCollisionMode mode) { bool isSimulated = mode == GRID_COLLISION_SIMULATE; if (!isSimulated) isCollidesWithGrid = false; if (minecart || entityGridCollisionClass == GRIDCOLL_NONE) return; Room& room = *g_game->room; int gridClass = entityGridCollisionClass; if (type == ENTITY_PROJECTILE && gridClass == GRIDCOLL_BULLET && room.roomType == ROOM_DUNGEON) gridClass = GRIDCOLL_NOPITS; Vector clamped = room.GetClampedPositionImpl(pos, 0, 0, 0, 0); if (gridClass == GRIDCOLL_WALLS_X || gridClass == GRIDCOLL_WALLS_Y) return axis_clamp(*this, clamped, gridClass == GRIDCOLL_WALLS_X, isSimulated); if (gridClass == GRIDCOLL_BULLET) { if (!isSimulated) shot_grid_collide(*this, room); return; } Vector clampNormal = room_clamp(*this, clamped, isSimulated); if (collisionPoints.empty()) return; Vector normal = blocked_points_sum(*this, room, gridClass, isSimulated); if (normal.x == 0 && normal.y == 0) { if (!isSimulated) gridCollisionNormal = clampNormal; return; } bool isCancelled = normal.y * normal.y + normal.x * normal.x < NORMAL_EPSILON; if (type == ENTITY_TEAR && isCancelled) normal = tear_cell_normal(*this, room, normal); else if (IsEnemy() && gridClass == GRIDCOLL_GROUND && isCancelled) reconstruction_missing("Room::FindFreeTilePosition 0081e520"); normal = vector_normalized(normal); if (!isSimulated) gridCollisionNormal = normal; float approach = -normal.x * velocity.x - velocity.y * normal.y; if (approach >= 0) return; if (!isSimulated) wall_bounce(*this, approach); float speed = vector_length(velocity); int steps = gridClass == GRIDCOLL_GROUND ? PUSH_OUT_STEPS_GROUND : PUSH_OUT_STEPS; auto isFreeAt = [&room, gridClass](Vector at) { return is_cell_free(gridClass, room.GetGridCollision(room.GetGridIndex(at))); }; if (speed > 0) grid_push_out(*this, normal, steps, isFreeAt); if (isSimulated) return; float slowdown = 1 - (-approach / speed) * WALL_FRICTION; velocity.y *= slowdown; velocity.x *= slowdown; } // 006ab240 Whether the entity's collision points, moved by an offset, meet walls (or a solid door) for a walls class, // or anything but a players' wall for a ground class. bool Entity::is_grid_collision_at(const Vector& offset) { if (entityGridCollisionClass == GRIDCOLL_NONE) return false; Room& room = *g_game->room; Vector sum{}; for (const Vector& point : collisionPoints) { int gridIndex = room.GetGridIndex(vector_make(point.x + offset.x + pos.x, point.y + pos.y + offset.y)); if (gridIndex < 0) continue; uint32_t collision = room.GetGridCollision(gridIndex); GridEntity* grid = room.GetGridEntity(gridIndex); bool isWall = collision == COLLISION_WALL || (collision == COLLISION_SOLID && grid && grid->type == GRID_DOOR); bool isBlocked = collision != COLLISION_NONE && collision != COLLISION_WALL_EXCEPT_PLAYER; if ((entityGridCollisionClass == GRIDCOLL_WALLS && isWall) || (entityGridCollisionClass == GRIDCOLL_GROUND && isBlocked)) { sum.x += point.x; sum.y += point.y; } } return sum.x != 0 || sum.y != 0; } // 006b2210 How much the entity weighs against another as they push apart: heavier when magnetized against enemies, // as a player against the Cube Baby or under Mega Mush, as an enemy shots barely move, and as a player whom // projectiles do not move (Isaac's Heart, Tooth and Nail, the Forgotten's soul). float Entity::collision_mass_get(Entity* other) { float weight = mass; if (HasEntityFlags(FLAG_MAGNETIZED) && is_enemy_type(other->type)) weight *= MASS_HEAVY; if (type == ENTITY_PLAYER) { if (other->type == ENTITY_FAMILIAR && other->variant == FAMILIAR_CUBE_BABY) weight *= MASS_HEAVY; if (static_cast(this)->temporaryEffects.HasCollectibleEffect(COLLECTIBLE_MEGA_MUSH)) weight *= MASS_HEAVY; } bool isShot = other->type == ENTITY_TEAR || other->type == ENTITY_LASER; bool isAnchored = std::ranges::find(SHOT_ANCHORED_TYPES, type) != SHOT_ANCHORED_TYPES.end(); if (is_enemy_type(type) && isShot && isAnchored) weight *= MASS_ANCHORED; if (type != ENTITY_PLAYER || other->type != ENTITY_PROJECTILE) return weight; Entity_Player& player = static_cast(*this); bool isProof = player.HasCollectible(COLLECTIBLE_ISAACS_HEART) || player.temporaryEffects.HasNullEffect(NULL_TOOTH_AND_NAIL) || player.playerType == PLAYER_THEFORGOTTEN_B; return isProof ? weight * MASS_PROJECTILE_PROOF : weight; } // Moves an entity by its share of a push and changes its velocity by its share of the impulse. In the Beast's dungeon // a player only slows, a quarter as much. An entity without physics knockback stays. static void push_apply(Entity& entity, const EntityPush& push) { if (entity.HasEntityFlags(FLAG_NO_PHYSICS_KNOCKBACK)) return; Vector impulse = push.impulse; if (g_game->room->IsBeastDungeon() && entity.type == ENTITY_PLAYER) impulse = vector_make(impulse.x * BEAST_PUSH_DAMPING, impulse.y * BEAST_PUSH_DAMPING); else { entity.pos.x += push.direction.x * push.share * push.overlap; entity.pos.y += push.direction.y * push.share * push.overlap; } entity.velocity.x += impulse.x * push.share; entity.velocity.y += impulse.y * push.share; } // 006b2940 Pushes the entity and another apart as circles when they overlap and may meet, the lower type first. // Each moves by the other's share of their weights (a player half as far, and not into a wall) and their velocities // trade along the line between them. Whether they met. bool Entity::push_apart_circle(Entity* other) { bool isVersusTearHit = g_game->deathmatchManager.is_active() && type == ENTITY_TEAR && other->type == ENTITY_PLAYER; bool isSwapped = !isVersusTearHit && other->type < type; Entity& first = isSwapped ? *other : *this; Entity& second = isSwapped ? *this : *other; Vector delta = vector_make(second.pos.x - first.pos.x, second.pos.y - first.pos.y); float reach = first.size + second.size; float distanceSquared = delta.y * delta.y + delta.x * delta.x; if (!(distanceSquared < reach * reach) || is_collision_ignored(first, second)) return false; first.isCollidesWithEntity = second.type != ENTITY_TEAR; second.isCollidesWithEntity = first.type != ENTITY_TEAR; float distance = std::sqrt(distanceSquared); if (distance < PUSH_MIN_DISTANCE) return true; Vector relative = vector_make(first.velocity.x - second.velocity.x, first.velocity.y - second.velocity.y); if (reach * 0.5f < vector_length(relative) && delta.y * relative.y + delta.x * relative.x < 0) { Vector relativeDirection = vector_normalized(relative); Vector deltaDirection = vector_normalized(delta); float dot = relativeDirection.y * deltaDirection.y + relativeDirection.x * deltaDirection.x; delta.y = dot * delta.y + dot * delta.y + delta.y; delta.x = delta.x + dot * delta.x + dot * delta.x; distance = std::sqrt(delta.y * delta.y + delta.x * delta.x); } float firstWeight = first.collision_mass_get(&second); float secondShare = firstWeight / (second.collision_mass_get(&first) + firstWeight); float firstShare = 1 - secondShare; float overlap = (reach - distance) / distance; bool isWallBound = first.type == ENTITY_PLAYER || (first.type == ENTITY_FAMILIAR && first.variant == FAMILIAR_PUNCHING_BAG); if (isWallBound && first.is_grid_collision_at(vector_make(-delta.x * firstShare * overlap, -delta.y * firstShare * overlap))) { secondShare = 1; firstShare = 0; } if (first.type == ENTITY_PLAYER) firstShare *= PLAYER_PUSH_SHARE; float relativeSpeed = vector_length( vector_make(second.velocity.x - first.velocity.x, second.velocity.y - first.velocity.y)); Vector impulse = vector_make(delta.x / distance * relativeSpeed, delta.y / distance * relativeSpeed); if (is_singe_ball(first) && is_singe_ball(second)) bounce_sound_play(SOUND_STONE_IMPACT, relativeSpeed); push_apply(first, {vector_make(-delta.x, -delta.y), vector_make(-impulse.x, -impulse.y), overlap, firstShare}); push_apply(second, {delta, impulse, overlap, secondShare}); return true; } // 006b25b0 Pushes a stretched entity and another apart along where their capsules overlap. Capsule is not written yet. void Entity::push_apart_capsule(Entity*) { reconstruction_missing("Entity::push_apart_capsule 006b25b0 (Capsule::Collide 00945470)"); } // 006b2ee0 Pushes the entity and another apart: as circles when neither is stretched, as capsules otherwise. void Entity::collide_with_entity(Entity* other) { if (is_unit_size(*this) && is_unit_size(*other)) push_apart_circle(other); else push_apart_capsule(other); } // 006b2f50 Makes two entities meet wherever they are: through their collision classes, or when low straight through // both HandleCollision calls. Whether the collision is ignored. bool Entity::ForceCollide(Entity* first, Entity* second, bool isLow) { g_forceCollideDepth++; bool isIgnored = false; if (!isLow) isIgnored = is_collision_ignored(*first, *second); else { bool isFirstIgnoring = first->HandleCollision(second, true); bool isSecondIgnoring = second->HandleCollision(first, false); isIgnored = isFirstIgnoring || isSecondIgnoring; } g_forceCollideDepth--; return isIgnored; } // 006b2390 Whether an entity is on the players' side, as ENTCOLL_PLAYEROBJECTS meets it: a player, tear, familiar, // knife or bomb, a charmed or friendly enemy, the Blood Puppy, Dark Esau and poop, or a projectile that hits enemies. bool is_player_object(Entity& entity) { switch (entity.type) { case ENTITY_PLAYER: case ENTITY_TEAR: case ENTITY_FAMILIAR: case ENTITY_BOMB: case ENTITY_KNIFE: return true; default: break; } bool isAlly = entity.HasEntityFlags(FLAG_CHARM | FLAG_FRIENDLY) || entity.type == ENTITY_BLOOD_PUPPY || entity.type == ENTITY_DARK_ESAU || entity.type == ENTITY_POOP; if (entity.IsEnemy() && isAlly) return true; return entity.type == ENTITY_PROJECTILE && (static_cast(entity).projectileFlags & PROJECTILE_HIT_ENEMIES); } // An entity's collision class, a charmed or friendly enemy's player-side class meeting all. static int collision_class_get(Entity& entity) { int collisionClass = entity.entityCollisionClass; bool isPlayerSide = collisionClass == ENTCOLL_PLAYERONLY || collisionClass == ENTCOLL_PLAYEROBJECTS; bool isAlly = Entity::is_enemy_type(entity.type) && entity.HasEntityFlags(FLAG_CHARM | FLAG_FRIENDLY); return isPlayerSide && isAlly ? ENTCOLL_ALL : collisionClass; } // 006b2410 Whether two entities pass through each other: by their collision classes (the first's player-only class // meets nothing), either being dead, or else either's HandleCollision ignoring the other. bool is_collision_ignored(Entity& first, Entity& second) { int firstClass = collision_class_get(first); int secondClass = collision_class_get(second); bool isNone = firstClass == ENTCOLL_NONE || secondClass == ENTCOLL_NONE; bool isNotPlayerObject = (firstClass == ENTCOLL_PLAYEROBJECTS && !is_player_object(second)) || (secondClass == ENTCOLL_PLAYEROBJECTS && !is_player_object(first)); bool isNotPlayer = firstClass == ENTCOLL_PLAYERONLY || (secondClass == ENTCOLL_PLAYERONLY && first.type != ENTITY_PLAYER); bool isNotEnemy = (firstClass == ENTCOLL_ENEMIES && !Entity::is_enemy_type(second.type)) || (secondClass == ENTCOLL_ENEMIES && !Entity::is_enemy_type(first.type)); if (first.isDead || second.isDead || isNone || isNotPlayerObject || isNotPlayer || isNotEnemy) return true; bool isFirstIgnoring = first.HandleCollision(&second, true); bool isSecondIgnoring = second.HandleCollision(&first, false); return isFirstIgnoring || isSecondIgnoring; }