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

@@ -130,7 +130,7 @@ static bool indexContains(BzSpatialGridIndex index, i32 x, i32 y) {
return x >= index.minX && x <= index.maxX && y >= index.minY && y <= index.maxY;
}
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
BZ_ASSERT(entry && entry->id == id);
@@ -140,7 +140,7 @@ BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32
oldIndex.minY == newIndex.minY &&
oldIndex.maxX == newIndex.maxX &&
oldIndex.maxY == newIndex.maxY) {
return id;
return;
}
for (i32 y = oldIndex.minY; y <= oldIndex.maxY; y++) {
@@ -159,7 +159,6 @@ BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32
}
entry->index = newIndex;
return id;
}
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);

View File

@@ -45,7 +45,7 @@ void bzSpatialGridDestroy(BzSpatialGrid *grid);
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id);
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY);

View File

@@ -120,6 +120,10 @@ bool init(void *userData) {
ECS_OBSERVER(ECS, entitySpatialRemoved, EcsOnRemove, Position, SpatialGridID);
ECS_OBSERVER(ECS, pathRemoved, EcsOnRemove, Path);
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, AngularVelocity, SteeringOutput);
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Position, Rotation, Velocity, AngularVelocity, SteeringOutput, Path);
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain);

View File

@@ -35,6 +35,9 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
object.shape.sizeX, object.shape.sizeY);
ecs_set(ECS, e, SpatialGridID, {spatialID});
ecs_set(ECS, e, Rotation, {0.0f});
ecs_set(ECS, e, Velocity, {});
ecs_set(ECS, e, AngularVelocity, {0.0f});
ecs_set(ECS, e, SteeringOutput, {});
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
ecs_set(ECS, e, Animation, {
.firstFrame=(TextureRegion) {

View File

@@ -18,11 +18,19 @@
void entitySpatialRemoved(ecs_iter_t *it);
/* Observer (for releasing path objects)
* 0: Game (singleton)l for object pool
* 0: Game (singleton) for object pool
* 1: Path
*/
void pathRemoved(ecs_iter_t *it);
/*
* 0: Game (singleton) for entity map
* 1: Position
* 2: Size
* 3: Velocity (only entities with velocity change position)
* 4: SpatialGridID
*/
void entityUpdateSpatialID(ecs_iter_t *it);
/*
* 0: Game (singleton) for collisions

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) {