Remove rotation from steering behavior

This commit is contained in:
2023-12-07 11:41:13 +01:00
parent e7e4d1e4ce
commit 365fb41831
6 changed files with 23 additions and 38 deletions

View File

@@ -18,9 +18,8 @@ ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Size); ECS_COMPONENT_DECLARE(Size);
ECS_COMPONENT_DECLARE(Velocity); ECS_COMPONENT_DECLARE(Velocity);
ECS_COMPONENT_DECLARE(TargetPosition); ECS_COMPONENT_DECLARE(TargetPosition);
ECS_COMPONENT_DECLARE(Steering);
ECS_COMPONENT_DECLARE(Rotation); ECS_COMPONENT_DECLARE(Rotation);
ECS_COMPONENT_DECLARE(AngularVelocity);
ECS_COMPONENT_DECLARE(SteeringOutput);
ECS_COMPONENT_DECLARE(TextureRegion); ECS_COMPONENT_DECLARE(TextureRegion);
ECS_COMPONENT_DECLARE(Animation); ECS_COMPONENT_DECLARE(Animation);
@@ -46,9 +45,8 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_COMPONENT_DEFINE(ecs, Size); ECS_COMPONENT_DEFINE(ecs, Size);
ECS_COMPONENT_DEFINE(ecs, Velocity); ECS_COMPONENT_DEFINE(ecs, Velocity);
ECS_COMPONENT_DEFINE(ecs, TargetPosition); ECS_COMPONENT_DEFINE(ecs, TargetPosition);
ECS_COMPONENT_DEFINE(ecs, Steering);
ECS_COMPONENT_DEFINE(ecs, Rotation); ECS_COMPONENT_DEFINE(ecs, Rotation);
ECS_COMPONENT_DEFINE(ecs, AngularVelocity);
ECS_COMPONENT_DEFINE(ecs, SteeringOutput);
ECS_COMPONENT_DEFINE(ecs, TextureRegion); ECS_COMPONENT_DEFINE(ecs, TextureRegion);
ECS_COMPONENT_DEFINE(ecs, Animation); ECS_COMPONENT_DEFINE(ecs, Animation);

View File

@@ -47,19 +47,14 @@ extern ECS_COMPONENT_DECLARE(Owner);
typedef BzSpatialGridID SpatialGridID; typedef BzSpatialGridID SpatialGridID;
extern ECS_COMPONENT_DECLARE(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(Position);
extern ECS_COMPONENT_DECLARE(Size); extern ECS_COMPONENT_DECLARE(Size);
extern ECS_COMPONENT_DECLARE(Velocity); extern ECS_COMPONENT_DECLARE(Velocity);
extern ECS_COMPONENT_DECLARE(TargetPosition); 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(Rotation);
extern ECS_COMPONENT_DECLARE(AngularVelocity);
typedef struct SteeringOutput {
Vector2 linear;
f32 angular;
} SteeringOutput;
extern ECS_COMPONENT_DECLARE(SteeringOutput);
typedef struct TextureRegion { typedef struct TextureRegion {
Texture2D texture; Texture2D texture;

View File

@@ -149,9 +149,9 @@ bool init(void *userData) {
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer); bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID); 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, entityFollowPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Velocity, AnimationType); ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Velocity, AnimationType);

View File

@@ -37,8 +37,7 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
ecs_set(ECS, e, SpatialGridID, {spatialID}); ecs_set(ECS, e, SpatialGridID, {spatialID});
ecs_set(ECS, e, Rotation, {0.0f}); ecs_set(ECS, e, Rotation, {0.0f});
ecs_set(ECS, e, Velocity, {}); ecs_set(ECS, e, Velocity, {});
ecs_set(ECS, e, AngularVelocity, {0.0f}); ecs_set(ECS, e, Steering, {});
ecs_set(ECS, e, SteeringOutput, {});
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)}); ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
ecs_set(ECS, e, Animation, { ecs_set(ECS, e, Animation, {
.entityType=ENTITY_WORKER, .entityType=ENTITY_WORKER,
@@ -48,10 +47,10 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
.elapsed=i * 0.1f, .elapsed=i * 0.1f,
}); });
ecs_set(ECS, e, AnimationType, {ANIM_IDLE}); ecs_set(ECS, e, AnimationType, {ANIM_IDLE});
EntityArms arms = { //EntityArms arms = {
.left=ecs_new_id(ECS), // .left=ecs_new_id(ECS),
.right=ecs_new_id(ECS), // .right=ecs_new_id(ECS),
}; //};
//ecs_set_ptr(ECS, e, EntityArms, &arms); //ecs_set_ptr(ECS, e, EntityArms, &arms);
} }
return true; return true;

View File

@@ -44,8 +44,7 @@ void entityUpdateSpatialID(ecs_iter_t *it);
* 1: Position * 1: Position
* 2: Rotation * 2: Rotation
* 3: Velocity * 3: Velocity
* 4: AngularVelocity * 4: Steering
* 5: SteeringOutput
*/ */
void entityUpdateKinematic(ecs_iter_t *it); void entityUpdateKinematic(ecs_iter_t *it);
@@ -54,7 +53,7 @@ void entityUpdateKinematic(ecs_iter_t *it);
* 2: Rotation * 2: Rotation
* 3: Velocity * 3: Velocity
* 4: TargetPosition * 4: TargetPosition
* 5: SteeringOutput * 5: Steering
*/ */
void entityMoveToTarget(ecs_iter_t *it); void entityMoveToTarget(ecs_iter_t *it);

View File

@@ -68,31 +68,25 @@ void entityUpdateKinematic(ecs_iter_t *it) {
Position *position = ecs_field(it, Position, 1); Position *position = ecs_field(it, Position, 1);
Rotation *rotation = ecs_field(it, Rotation, 2); Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3); Velocity *velocity = ecs_field(it, Velocity, 3);
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4); Steering *steering = ecs_field(it, Steering, 4);
SteeringOutput *steering = ecs_field(it, SteeringOutput, 5);
f32 dt = it->delta_time; f32 dt = it->delta_time;
for (i32 i = 0; i < it->count; i++) { for (i32 i = 0; i < it->count; i++) {
// Update position and rotation // Update position and rotation
// position += velocity * dt // position += velocity * dt
// rotation += angularVelocity * dt
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt)); position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
//rotation[i] += angularVel[i] * dt;
// Update velocity and angular velocity // Update velocity and angular velocity
// velocity += steering.liner * dt // velocity += steering.liner * dt
// angularVelocity += steering.angular * dt velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i], dt * 10));
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i].linear, dt * 10));
angularVel[i] += steering[i].angular * dt * 10;
if (Vector2LengthSqr(steering[i].linear) == 0) { if (Vector2LengthSqr(steering[i]) == 0) {
// Decay velocity // Decay velocity
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f)); velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
} }
angularVel[i] *= 1 - (dt * 5.0f);
// Reset steering // Reset steering
steering[i] = (SteeringOutput) {}; steering[i] = Vector2Zero();
{ {
const InputState *input = ecs_singleton_get(ECS, InputState); 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); Velocity *velocity = ecs_field(it, Velocity, 3);
TargetPosition *targetPos = ecs_field(it, TargetPosition, 4); 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++) { for (i32 i = 0; i < it->count; i++) {
Position target = targetPos[i]; Position target = targetPos[i];
steering[i].linear = Vector2Subtract(target, position[i]); steering[i] = Vector2Subtract(target, position[i]);
f32 dst = Vector2LengthSqr(steering[i].linear); f32 dst = Vector2LengthSqr(steering[i]);
f32 maxAccel = 10.0f; f32 maxAccel = 10.0f;
steering[i].linear = Vector2Normalize(steering[i].linear); steering[i] = Vector2Normalize(steering[i]);
steering[i].linear = Vector2Scale(steering[i].linear, maxAccel); steering[i] = Vector2Scale(steering[i], maxAccel);
if (Vector2Length(velocity[i]) > 10.0f) { if (Vector2Length(velocity[i]) > 10.0f) {
f32 rot = Vector2Angle(position[i], target); f32 rot = Vector2Angle(position[i], target);