Rework input

This commit is contained in:
2023-12-05 14:38:11 +01:00
parent 8d01f328f9
commit ae1c21bdfc
6 changed files with 69 additions and 26 deletions

View File

@@ -64,34 +64,43 @@ 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 *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4);
SteeringOutput *steering = 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;
// 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;
if (Vector2LengthSqr(steering[i].linear) == 0) {
// Decay velocity
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
}
// Reset steering
steering[i] = (SteeringOutput) {};
// Check for speeding and clip
const f32 maxSpeed = 15.0f;
if (Vector2Length(velocity[i]) > maxSpeed) {
velocity[i] = Vector2Normalize(velocity[i]);
velocity[i] = Vector2Scale(velocity[i], maxSpeed);
}
// Update flipX
ecs_entity_t entity = it->entities[i];
if (ecs_has(it->world, entity, TextureRegion)) {
TextureRegion *text = ecs_get_mut(it->world, entity, TextureRegion);
text->flipX = velocity[i].x < 0.0f;
text->flipX = velocity[i].x < 0.01f;
}
}
}
@@ -101,16 +110,16 @@ void entityFollowPath(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 *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4);
SteeringOutput *steering = 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;
steering[i].linear.x = target.x - position[i].x;
steering[i].linear.y = target.y - position[i].y;
f32 dst = Vector2LengthSqr(steeringOutput[i].linear);
f32 dst = Vector2LengthSqr(steering[i].linear);
if (dst < 8.0f) {
path[i].curWaypoint++;
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
@@ -122,8 +131,8 @@ void entityFollowPath(ecs_iter_t *it) {
}
}
steeringOutput[i].linear = Vector2Normalize(steeringOutput[i].linear);
steeringOutput[i].linear = Vector2Scale(steeringOutput[i].linear, 10.0f);
steering[i].linear = Vector2Normalize(steering[i].linear);
steering[i].linear = Vector2Scale(steering[i].linear, 10.0f);
}
}
@@ -201,6 +210,19 @@ void renderColliders(ecs_iter_t *it) {
}
}
void renderRotationDirection(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
Rotation *rot = ecs_field(it, Rotation, 2);
for (i32 i = 0; i < it->count; i++) {
Vector2 v = {0.0f, 4.0f};
v = Vector2Rotate(v, rot[i]);
v = Vector2Add(v, pos[i]);
DrawCircle(v.x, v.y, 1.0f, RED);
}
}
void renderDebugPath(ecs_iter_t *it) {
Path *path = ecs_field(it, Path, 1);