From 365fb41831ef176299eb2088abd60c0a0172e5e2 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Thu, 7 Dec 2023 11:41:13 +0100 Subject: [PATCH] Remove rotation from steering behavior --- game/components.c | 6 ++---- game/components.h | 11 +++-------- game/main.c | 4 ++-- game/map_init.c | 11 +++++------ game/systems.h | 5 ++--- game/systems_entity.c | 24 +++++++++--------------- 6 files changed, 23 insertions(+), 38 deletions(-) diff --git a/game/components.c b/game/components.c index c58f77f..1b28e9a 100644 --- a/game/components.c +++ b/game/components.c @@ -18,9 +18,8 @@ ECS_COMPONENT_DECLARE(Position); ECS_COMPONENT_DECLARE(Size); ECS_COMPONENT_DECLARE(Velocity); ECS_COMPONENT_DECLARE(TargetPosition); +ECS_COMPONENT_DECLARE(Steering); ECS_COMPONENT_DECLARE(Rotation); -ECS_COMPONENT_DECLARE(AngularVelocity); -ECS_COMPONENT_DECLARE(SteeringOutput); ECS_COMPONENT_DECLARE(TextureRegion); ECS_COMPONENT_DECLARE(Animation); @@ -46,9 +45,8 @@ void initComponentIDs(ecs_world_t *ecs) { ECS_COMPONENT_DEFINE(ecs, Size); ECS_COMPONENT_DEFINE(ecs, Velocity); ECS_COMPONENT_DEFINE(ecs, TargetPosition); + ECS_COMPONENT_DEFINE(ecs, Steering); ECS_COMPONENT_DEFINE(ecs, Rotation); - ECS_COMPONENT_DEFINE(ecs, AngularVelocity); - ECS_COMPONENT_DEFINE(ecs, SteeringOutput); ECS_COMPONENT_DEFINE(ecs, TextureRegion); ECS_COMPONENT_DEFINE(ecs, Animation); diff --git a/game/components.h b/game/components.h index 6641c10..9b7efec 100644 --- a/game/components.h +++ b/game/components.h @@ -47,19 +47,14 @@ extern ECS_COMPONENT_DECLARE(Owner); typedef BzSpatialGridID SpatialGridID; extern ECS_COMPONENT_DECLARE(SpatialGridID); -typedef Vector2 Position, Size, Velocity, TargetPosition; +typedef Vector2 Position, Size, Velocity, TargetPosition, Steering; extern ECS_COMPONENT_DECLARE(Position); extern ECS_COMPONENT_DECLARE(Size); extern ECS_COMPONENT_DECLARE(Velocity); extern ECS_COMPONENT_DECLARE(TargetPosition); -typedef f32 Rotation, AngularVelocity; +extern ECS_COMPONENT_DECLARE(Steering); +typedef f32 Rotation; extern ECS_COMPONENT_DECLARE(Rotation); -extern ECS_COMPONENT_DECLARE(AngularVelocity); -typedef struct SteeringOutput { - Vector2 linear; - f32 angular; -} SteeringOutput; -extern ECS_COMPONENT_DECLARE(SteeringOutput); typedef struct TextureRegion { Texture2D texture; diff --git a/game/main.c b/game/main.c index 721b776..866f254 100644 --- a/game/main.c +++ b/game/main.c @@ -149,9 +149,9 @@ bool init(void *userData) { bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer); ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID); - ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, AngularVelocity, SteeringOutput); + ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, Steering); - ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Rotation, Velocity, TargetPosition, SteeringOutput); + ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Rotation, Velocity, TargetPosition, Steering); ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path); ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Velocity, AnimationType); diff --git a/game/map_init.c b/game/map_init.c index e9f0724..38cc254 100644 --- a/game/map_init.c +++ b/game/map_init.c @@ -37,8 +37,7 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) { 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, Steering, {}); ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)}); ecs_set(ECS, e, Animation, { .entityType=ENTITY_WORKER, @@ -48,10 +47,10 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) { .elapsed=i * 0.1f, }); ecs_set(ECS, e, AnimationType, {ANIM_IDLE}); - EntityArms arms = { - .left=ecs_new_id(ECS), - .right=ecs_new_id(ECS), - }; + //EntityArms arms = { + // .left=ecs_new_id(ECS), + // .right=ecs_new_id(ECS), + //}; //ecs_set_ptr(ECS, e, EntityArms, &arms); } return true; diff --git a/game/systems.h b/game/systems.h index ea6e457..d74c26c 100644 --- a/game/systems.h +++ b/game/systems.h @@ -44,8 +44,7 @@ void entityUpdateSpatialID(ecs_iter_t *it); * 1: Position * 2: Rotation * 3: Velocity - * 4: AngularVelocity - * 5: SteeringOutput + * 4: Steering */ void entityUpdateKinematic(ecs_iter_t *it); @@ -54,7 +53,7 @@ void entityUpdateKinematic(ecs_iter_t *it); * 2: Rotation * 3: Velocity * 4: TargetPosition - * 5: SteeringOutput + * 5: Steering */ void entityMoveToTarget(ecs_iter_t *it); diff --git a/game/systems_entity.c b/game/systems_entity.c index d334414..45fde7f 100644 --- a/game/systems_entity.c +++ b/game/systems_entity.c @@ -68,31 +68,25 @@ void entityUpdateKinematic(ecs_iter_t *it) { Position *position = ecs_field(it, Position, 1); Rotation *rotation = ecs_field(it, Rotation, 2); Velocity *velocity = ecs_field(it, Velocity, 3); - AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4); - SteeringOutput *steering = ecs_field(it, SteeringOutput, 5); + Steering *steering = ecs_field(it, Steering, 4); f32 dt = it->delta_time; for (i32 i = 0; i < it->count; i++) { // Update position and rotation // position += velocity * dt - // rotation += angularVelocity * dt position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt)); - //rotation[i] += angularVel[i] * dt; // Update velocity and angular velocity // velocity += steering.liner * dt - // angularVelocity += steering.angular * dt - velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i].linear, dt * 10)); - angularVel[i] += steering[i].angular * dt * 10; + velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i], dt * 10)); - if (Vector2LengthSqr(steering[i].linear) == 0) { + if (Vector2LengthSqr(steering[i]) == 0) { // Decay velocity velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f)); } - angularVel[i] *= 1 - (dt * 5.0f); // Reset steering - steering[i] = (SteeringOutput) {}; + steering[i] = Vector2Zero(); { const InputState *input = ecs_singleton_get(ECS, InputState); @@ -125,15 +119,15 @@ void entityMoveToTarget(ecs_iter_t *it) { Velocity *velocity = ecs_field(it, Velocity, 3); TargetPosition *targetPos = ecs_field(it, TargetPosition, 4); - SteeringOutput *steering = ecs_field(it, SteeringOutput, 5); + Steering *steering = ecs_field(it, Steering, 5); for (i32 i = 0; i < it->count; i++) { Position target = targetPos[i]; - steering[i].linear = Vector2Subtract(target, position[i]); - f32 dst = Vector2LengthSqr(steering[i].linear); + steering[i] = Vector2Subtract(target, position[i]); + f32 dst = Vector2LengthSqr(steering[i]); f32 maxAccel = 10.0f; - steering[i].linear = Vector2Normalize(steering[i].linear); - steering[i].linear = Vector2Scale(steering[i].linear, maxAccel); + steering[i] = Vector2Normalize(steering[i]); + steering[i] = Vector2Scale(steering[i], maxAccel); if (Vector2Length(velocity[i]) > 10.0f) { f32 rot = Vector2Angle(position[i], target);