Kinematic movement + path following

This commit is contained in:
2023-11-18 08:12:58 +01:00
parent 5cbb7b6c94
commit 03b5959eae
6 changed files with 75 additions and 7 deletions

View File

@@ -34,22 +34,76 @@ void pathRemoved(ecs_iter_t *it) {
}
}
void entityUpdateSpatialID(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *position = ecs_field(it, Position, 1);
Position *size = ecs_field(it, Position, 2);
//Velocity *velocity = ecs_field(it, Velocity, 3);
SpatialGridID *id = ecs_field(it, SpatialGridID, 4);
for (i32 i = 0; i < it->count; i++) {
Position pos = getBottomLeftPos(position[i], size[i]);
bzSpatialGridUpdate(game->entityGrid, id[i], pos.x, pos.y, size[i].x, size[i].y);
}
}
void entityUpdateKinematic(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
Position *position = ecs_field(it, Position, 1);
Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
f32 dt = it->delta_time;
for (i32 i = 0; i < it->count; i++) {
// and velocity and angular velocity
velocity[i] = Vector2Scale(velocity[i], 0.9f);
velocity[i].x += steeringOutput[i].linear.x * dt;
velocity[i].y += steeringOutput[i].linear.y * dt;
f32 maxSpeed = 15.0f;
velocity[i] = Vector2Clamp(velocity[i],
(Velocity) {-maxSpeed, -maxSpeed},
(Velocity){maxSpeed, maxSpeed});
angularVelocity[i] += steeringOutput[i].angular * dt;
steeringOutput[i] = (SteeringOutput){};
// Update position and rotation
position[i].x += velocity[i].x * dt * 10;
position[i].y += velocity[i].y * dt * 10;
rotation[i] += angularVelocity[i] * dt * 10;
}
}
void entityFollowPath(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
Position *position = ecs_field(it, Position, 1);
Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
Path *path = ecs_field(it, Path, 6);
for (i32 i = 0; i < it->count; i++) {
Position target = path[i].paths->waypoints[path[i].curWaypoint];
steeringOutput[i].linear.x = target.x - position[i].x;
steeringOutput[i].linear.y = target.y - position[i].y;
f32 dst = Vector2LengthSqr(steeringOutput[i].linear);
if (dst < 8.0f) {
path[i].curWaypoint++;
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
path[i].curWaypoint = 0;
path[i].paths = path[i].paths->next;
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
}
}
steeringOutput[i].linear = Vector2Normalize(steeringOutput[i].linear);
steeringOutput[i].linear = Vector2Scale(steeringOutput[i].linear, 10.0f);
}
}
void updateAnimations(ecs_iter_t *it) {