Polish kinematic movement
This commit is contained in:
@@ -61,67 +61,54 @@ void entityUpdateSpatialID(ecs_iter_t *it) {
|
||||
|
||||
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);
|
||||
Steering *steering = ecs_field(it, Steering, 4);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 2);
|
||||
Steering *steering = ecs_field(it, Steering, 3);
|
||||
Unit *unit = ecs_field(it, Unit, 4);
|
||||
|
||||
f32 dt = it->delta_time;
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
// Update position and rotation
|
||||
// position += velocity * dt
|
||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||
|
||||
// Update velocity and angular velocity
|
||||
// velocity += steering.liner * dt
|
||||
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i], dt * 10));
|
||||
steering[i] = Vector2Normalize(steering[i]);
|
||||
// velocity += steering * dt
|
||||
Vector2 accel = Vector2Scale(steering[i], dt);
|
||||
accel = Vector2Scale(accel, unit->acceleration);
|
||||
velocity[i] = Vector2Add(velocity[i], accel);
|
||||
|
||||
// Apply deceleration
|
||||
if (Vector2LengthSqr(steering[i]) == 0) {
|
||||
// Decay velocity
|
||||
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
|
||||
// velocity *= (1.0 - decel)
|
||||
velocity[i] = Vector2Scale(velocity[i], 1.0 - unit->deceleration);
|
||||
}
|
||||
|
||||
// Reset steering
|
||||
steering[i] = Vector2Zero();
|
||||
|
||||
{
|
||||
const InputState *input = ecs_singleton_get(ECS, InputState);
|
||||
|
||||
Vector2 mouse = input->mouseDownWorld;
|
||||
f32 rot = Vector2Angle(position[i], mouse) + 270 * DEG2RAD;
|
||||
//rotation[i] = rot;
|
||||
}
|
||||
|
||||
// Check for speeding and clip
|
||||
const f32 maxSpeed = 15.0f;
|
||||
const f32 maxSpeed = unit->maxSpeed;
|
||||
if (Vector2Length(velocity[i]) > maxSpeed) {
|
||||
velocity[i] = Vector2Normalize(velocity[i]);
|
||||
velocity[i] = Vector2Scale(velocity[i], maxSpeed);
|
||||
}
|
||||
|
||||
// position += velocity * dt
|
||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||
}
|
||||
}
|
||||
|
||||
void entityMoveToTarget(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);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 2);
|
||||
|
||||
TargetPosition *targetPos = ecs_field(it, TargetPosition, 4);
|
||||
Steering *steering = ecs_field(it, Steering, 5);
|
||||
TargetPosition *targetPos = ecs_field(it, TargetPosition, 3);
|
||||
Steering *steering = ecs_field(it, Steering, 4);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Position target = targetPos[i];
|
||||
steering[i] = Vector2Subtract(target, position[i]);
|
||||
f32 dst = Vector2LengthSqr(steering[i]);
|
||||
f32 maxAccel = 10.0f;
|
||||
steering[i] = Vector2Normalize(steering[i]);
|
||||
steering[i] = Vector2Scale(steering[i], maxAccel);
|
||||
|
||||
if (Vector2Length(velocity[i]) > 10.0f) {
|
||||
f32 rot = Vector2Angle(position[i], target);
|
||||
//rotation[i] = rot;
|
||||
}
|
||||
|
||||
if (dst < 8.0f) {
|
||||
if (dst < 2.0f) {
|
||||
// Arrived
|
||||
ecs_remove(ECS, it->entities[i], TargetPosition);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user