Add entity physics
This commit is contained in:
@@ -143,6 +143,53 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||
}
|
||||
}
|
||||
void entityUpdatePhysics(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
HitBox *hitbox = ecs_field(it, HitBox, 2);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 4);
|
||||
|
||||
f32 dt = it->delta_time;
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
// Only update "stationary" entities
|
||||
bool stationary = Vector2Length(velocity[i]) <= 0.2f;
|
||||
Rectangle bounds = entityTransformHitBox(position[i], hitbox[i]);
|
||||
BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid, bounds.x, bounds.y,
|
||||
bounds.width, bounds.height);
|
||||
Vector2 dir = Vector2Zero();
|
||||
f32 slowDown = 0.0f;
|
||||
while (bzSpatialGridQueryNext(&spatialIt)) {
|
||||
ecs_entity_t other = *(ecs_entity_t *) spatialIt.data;
|
||||
if (other == it->entities[i])
|
||||
continue;
|
||||
Position otherPos;
|
||||
Rectangle otherBounds;
|
||||
if (!entityGetHitBox(other, &otherPos, &otherBounds))
|
||||
continue;
|
||||
|
||||
if (!CheckCollisionRecs(bounds, otherBounds)) {
|
||||
continue;
|
||||
}
|
||||
slowDown += 0.1f;
|
||||
Position dif = Vector2Subtract(otherPos, position[i]);
|
||||
//dif = Vector2Normalize(dif);
|
||||
dir = Vector2Add(dir, dif);
|
||||
}
|
||||
|
||||
if (stationary) {
|
||||
dir = Vector2Normalize(dir);
|
||||
dir = Vector2Scale(dir, 40);
|
||||
velocity[i] = Vector2Subtract(velocity[i], dir);
|
||||
}
|
||||
|
||||
slowDown = BZ_MIN(slowDown, 0.85f);
|
||||
if (!stationary && slowDown > 0.0f) {
|
||||
velocity[i] = Vector2Scale(velocity[i], 1 - slowDown);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void entityMoveToTarget(ecs_iter_t *it) {
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
|
||||
Reference in New Issue
Block a user