Refactorings, cleanup
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 17s

This commit is contained in:
Stephen Seo 2023-08-11 19:30:02 +09:00
parent d2d0f8373c
commit 64c5bcd1bc
4 changed files with 39 additions and 41 deletions

View file

@ -164,3 +164,15 @@ std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane) {
Vector3 operator+(const Vector3 &a, const Vector3 &b) { Vector3 operator+(const Vector3 &a, const Vector3 &b) {
return Vector3{a.x + b.x, a.y + b.y, a.z + b.z}; return Vector3{a.x + b.x, a.y + b.y, a.z + b.z};
} }
Vector3 operator-(const Vector3 &a, const Vector3 &b) {
return Vector3{a.x - b.x, a.y - b.y, a.z - b.z};
}
Vector3 operator*(Vector3 vec3, float factor) {
return Vector3Scale(vec3, factor);
}
Vector3 operator*(Matrix mat, Vector3 vec3) {
return Vector3Transform(vec3, mat);
}

View file

@ -32,5 +32,10 @@ extern std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane);
// extern Vector4 operator*(const Matrix &m, const Vector4 &v); // extern Vector4 operator*(const Matrix &m, const Vector4 &v);
extern Vector3 operator+(const Vector3 &a, const Vector3 &b); extern Vector3 operator+(const Vector3 &a, const Vector3 &b);
extern Vector3 operator-(const Vector3 &a, const Vector3 &b);
extern Vector3 operator*(Vector3 vec3, float factor);
extern Vector3 operator*(Matrix mat, Vector3 vec3);
#endif #endif

View file

@ -257,9 +257,8 @@ bool TRunnerScreen::update(float dt) {
this->camera_target.z = zf; this->camera_target.z = zf;
if (idx != SURFACE_UNIT_WIDTH / 2 + if (idx != SURFACE_UNIT_WIDTH / 2 +
(SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) {
this->camera_pos = Vector3Add( this->camera_pos = (Vector3Normalize(this->camera_target) * 4.0F) +
Vector3Scale(Vector3Normalize(this->camera_target), 4.0F), this->camera_target;
this->camera_target);
this->camera_pos.y += 4.0F; this->camera_pos.y += 4.0F;
} else { } else {
this->camera_pos.x = 0.0F; this->camera_pos.x = 0.0F;
@ -319,17 +318,6 @@ bool TRunnerScreen::draw() {
Vector3{xf - 0.5F, current.sw, zf + 0.5F}, color); Vector3{xf - 0.5F, current.sw, zf + 0.5F}, color);
} }
// DrawModel(Model{.transform = TEMP_cube_model.transform * TEMP_matrix,
// .meshCount = TEMP_cube_model.meshCount,
// .materialCount = TEMP_cube_model.materialCount,
// .meshes = TEMP_cube_model.meshes,
// .materials = TEMP_cube_model.materials,
// .meshMaterial = TEMP_cube_model.meshMaterial,
// .boneCount = TEMP_cube_model.boneCount,
// .bones = TEMP_cube_model.bones,
// .bindPose = TEMP_cube_model.bindPose},
// Vector3{0.0F, 0.0F, -4.0F}, 1.0F, WHITE);
for (auto &walker : walkers) { for (auto &walker : walkers) {
walker.draw(TEMP_cube_model); walker.draw(TEMP_cube_model);
} }

View file

@ -155,10 +155,8 @@ void Walker::update(float dt, const std::array<BoundingBox, BBCount> &bbs,
// body to target pos // body to target pos
if ((flags & 3) == 2) { if ((flags & 3) == 2) {
float diff = Vector3Distance(target_body_pos, body_pos); float diff = Vector3Distance(target_body_pos, body_pos);
body_pos = body_pos = body_pos + Vector3Normalize(target_body_pos - body_pos) *
Vector3Add(body_pos, Vector3Scale(Vector3Normalize(Vector3Subtract( (dt * BODY_TARGET_SPEED);
target_body_pos, body_pos)),
dt * BODY_TARGET_SPEED));
if (Vector3Distance(target_body_pos, body_pos) > diff) { if (Vector3Distance(target_body_pos, body_pos) > diff) {
flags &= ~3; flags &= ~3;
body_pos = target_body_pos; body_pos = target_body_pos;
@ -179,42 +177,38 @@ void Walker::update(float dt, const std::array<BoundingBox, BBCount> &bbs,
Vector3 ideal_foot_pos; Vector3 ideal_foot_pos;
if ((flags & 0x18) == 0) { if ((flags & 0x18) == 0) {
// Is nw. // Is nw.
ideal_foot_pos = Vector3Transform( ideal_foot_pos =
Vector3Normalize(Vector3{-1.0F, 0.0F, -1.0F}), rotationMatrix); rotationMatrix * Vector3Normalize(Vector3{-1.0F, 0.0F, -1.0F});
} else if ((flags & 0x18) == 0x8) { } else if ((flags & 0x18) == 0x8) {
// Is ne. // Is ne.
ideal_foot_pos = Vector3Transform( ideal_foot_pos =
Vector3Normalize(Vector3{1.0F, 0.0F, -1.0F}), rotationMatrix); rotationMatrix * Vector3Normalize(Vector3{1.0F, 0.0F, -1.0F});
} else if ((flags & 0x18) == 0x10) { } else if ((flags & 0x18) == 0x10) {
// Is sw. // Is sw.
ideal_foot_pos = Vector3Transform( ideal_foot_pos =
Vector3Normalize(Vector3{-1.0F, 0.0F, 1.0F}), rotationMatrix); rotationMatrix * Vector3Normalize(Vector3{-1.0F, 0.0F, 1.0F});
} else if ((flags & 0x18) == 0x18) { } else if ((flags & 0x18) == 0x18) {
// Is se. // Is se.
ideal_foot_pos = Vector3Transform( ideal_foot_pos =
Vector3Normalize(Vector3{1.0F, 0.0F, 1.0F}), rotationMatrix); rotationMatrix * Vector3Normalize(Vector3{1.0F, 0.0F, 1.0F});
} }
ideal_foot_pos = ideal_foot_pos =
Vector3Add(body_pos_same_y, body_pos_same_y + (ideal_foot_pos * this->body_feet_radius);
Vector3Scale(ideal_foot_pos, this->body_feet_radius));
// Check if body is past threshold. // Check if body is past threshold.
if (Vector3Distance(ideal_foot_pos, leg_target) > if (Vector3Distance(ideal_foot_pos, leg_target) >
FEET_RADIUS_PLACEMENT_CHECK_SCALE * this->feet_radius) { FEET_RADIUS_PLACEMENT_CHECK_SCALE * this->feet_radius) {
should_lift = true; should_lift = true;
Vector3 diff = Vector3Subtract(this->target_body_pos, this->body_pos); Vector3 diff = this->target_body_pos - this->body_pos;
if (Vector3Length(diff) > 0.1F) { if (Vector3Length(diff) > 0.1F) {
Vector3 dir = Vector3Normalize(diff); Vector3 dir = Vector3Normalize(diff);
leg_target = leg_target =
Vector3Add(ideal_foot_pos, ideal_foot_pos +
Vector3Scale(dir, this->feet_radius * (dir * (this->feet_radius * FEET_RADIUS_PLACEMENT_SCALE));
FEET_RADIUS_PLACEMENT_SCALE));
} else { } else {
Vector3 dir = Vector3 dir = Vector3Normalize(ideal_foot_pos - leg_target);
Vector3Normalize(Vector3Subtract(ideal_foot_pos, leg_target));
leg_target = leg_target =
Vector3Add(ideal_foot_pos, ideal_foot_pos +
Vector3Scale(dir, this->feet_radius * (dir * (this->feet_radius * FEET_RADIUS_PLACEMENT_SCALE));
FEET_RADIUS_PLACEMENT_SCALE));
} }
// Get average .y of ground at target position. // Get average .y of ground at target position.
Ray downwards{.position = Vector3{leg_target.x, leg_target.y + 5.0F, Ray downwards{.position = Vector3{leg_target.x, leg_target.y + 5.0F,
@ -244,10 +238,9 @@ void Walker::update(float dt, const std::array<BoundingBox, BBCount> &bbs,
// Moving horizontally. // Moving horizontally.
float prev_dist = Vector3Distance( float prev_dist = Vector3Distance(
leg_pos, Vector3{leg_target.x, leg_pos.y, leg_target.z}); leg_pos, Vector3{leg_target.x, leg_pos.y, leg_target.z});
Vector3 dir = Vector3Normalize(Vector3Subtract( Vector3 dir = Vector3Normalize(
Vector3{leg_target.x, leg_pos.y, leg_target.z}, leg_pos)); Vector3{leg_target.x, leg_pos.y, leg_target.z} - leg_pos);
leg_pos = leg_pos = leg_pos + (dir * (dt * FEET_HORIZ_MOVE_SPEED));
Vector3Add(leg_pos, Vector3Scale(dir, dt * FEET_HORIZ_MOVE_SPEED));
if (Vector3Distance(leg_pos, Vector3{leg_target.x, leg_pos.y, if (Vector3Distance(leg_pos, Vector3{leg_target.x, leg_pos.y,
leg_target.z}) >= prev_dist) { leg_target.z}) >= prev_dist) {
leg_pos.x = leg_target.x; leg_pos.x = leg_target.x;