Skip to content

Commit

Permalink
Merge branch 'main' into libfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff committed Dec 22, 2024
2 parents 1183b98 + bd97652 commit c7ee274
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

# Build
- name: Setup sccache
uses: mozilla-actions/[email protected].6
uses: mozilla-actions/[email protected].7

- name: configure
run: nix develop --command just configure
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: DeterminateSystems/nix-installer-action@v16
- uses: DeterminateSystems/magic-nix-cache-action@v8

- uses: mozilla-actions/[email protected].6
- uses: mozilla-actions/[email protected].7

- name: build & test
run: |
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
[
vulkan-tools
llvmPackages_19.clang-tools
llvmPackages_17.libllvm
llvmPackages_19.libllvm
sccache

just
Expand Down
10 changes: 10 additions & 0 deletions wren/include/wren/scene/components/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ struct Transform {

return translate * rotate;
}

[[nodiscard]] auto right() const {
return (math::Quaternionf{rotation}.to_mat() * math::Vec3f{1, 0, 0})
.normalized();
}

[[nodiscard]] auto up() const {
return (math::Quaternionf{rotation}.to_mat() * math::Vec3f{0, 1, 0})
.normalized();
}
};

} // namespace wren::scene::components
23 changes: 20 additions & 3 deletions wren/src/scene/components/collider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,34 @@ auto BoxCollider2D::raycast(const Transform& transform,
math::Quaternionf{transform.rotation}.to_mat() * math::Vec3f{0, 0, 1};

float denominator = normal.dot(direction);
if (std::abs(denominator) < 0.0001) {
if (std::abs(denominator) < 1e-6) {
return {};
}

float t = normal.dot(transform.position - origin) / denominator;
// Calculate the position along the ray
float t = (transform.position - origin).dot(normal) / denominator;
if (t < 0) {
// Intersection points is behind the ray's origin
return {};
}

return origin + direction * t;
const auto point = origin + direction * t;
math::Vec3f projected_point = point - transform.position;
// projected_point -= projected_point.dot(normal) * normal;

const math::Vec3f left_edge = -(transform.right() * size.x() / 2);
const math::Vec3f right_edge = (transform.right() * size.x() / 2);
const math::Vec3f top_edge = (transform.up() * size.y() / 2);
const math::Vec3f bottom_edge = -(transform.up() * size.y() / 2);

if (projected_point.x() > left_edge.x() &&
projected_point.x() < right_edge.x() &&
projected_point.y() < top_edge.y() &&
projected_point.y() > bottom_edge.y()) {
return point;
}

return {};
}

} // namespace wren::scene::components
46 changes: 33 additions & 13 deletions wren_math/include/wren/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ template <typename T, std::size_t N>
struct Vec {
using vec_t = Vec<T, N>;

static auto UnitX() { return vec_t{1.0f}; }
static auto UnitY() { return vec_t{0.0f, 1.0f}; }
static auto UnitZ() { return vec_t{0.0f, 0.0f, 1.0f}; }
static auto unit_x() { return vec_t{1.0f}; }
static auto unit_y() { return vec_t{0.0f, 1.0f}; }
static auto unit_z() { return vec_t{0.0f, 0.0f, 1.0f}; }

Vec() : data() {}

Expand All @@ -22,8 +22,16 @@ struct Vec {
}
}

// template <typename... Args>
// Vec(Args&&... d) : data({{std::forward<Args>(d)...}}) {}
template <typename... Args>
Vec(Args... args)
requires(sizeof...(Args) > 1)
: data({std::forward<Args>(args)...}) {}

// Swizzles
[[nodiscard]] auto xyz() const {
static_assert(N > 3, "xyz swizzle requires 4 or more components ");
return Vec<T, 3>{data.at(0), data.at(1), data.at(2)};
}

auto at(std::size_t i) -> T& { return data.at(i); }
[[nodiscard]] auto at(std::size_t i) const { return data.at(i); }
Expand Down Expand Up @@ -56,9 +64,8 @@ struct Vec {

[[nodiscard]] constexpr auto dot(const vec_t& other) const {
T dot = 0;
const auto a = this->normalized();
const auto b = other.normalized();
for (std::size_t i = 0; i < N; i++) dot += a.data.at(i) * b.data.at(i);
for (std::size_t i = 0; i < N; i++)
dot += this->data.at(i) * other.data.at(i);
return dot;
}

Expand All @@ -74,6 +81,12 @@ struct Vec {
return v;
}

constexpr auto operator-=(const vec_t& other) {
for (std::size_t i = 0; i < N; i++) {
data.at(i) = data.at(i) - other.data.at(i);
}
}

constexpr auto operator-(const vec_t& other) const {
vec_t v{};
for (std::size_t i = 0; i < N; i++)
Expand Down Expand Up @@ -112,7 +125,12 @@ struct Vec {
std::array<T, N> data{};
};

// struct vec2i : Vec<int, 2> {
template <typename T, std::size_t N>
auto operator*(float scalar, Vec<T, N> vec) -> Vec<T, N> {
return vec * scalar;
}

// // struct vec2i : Vec<int, 2> {
// vec2i() : Vec<int, 2>() {}
// vec2i(int x, int y) : Vec<int, 2>({x, y}) {}
// vec2i(const Vec<int, 2>& other) : Vec<int, 2>(other) {}
Expand All @@ -123,18 +141,20 @@ struct Vec {

struct Vec2f : Vec<float, 2> {
Vec2f() : Vec<float, 2>() {}
Vec2f(float x, float y) : Vec<float, 2>({x, y}) {}
Vec2f(float x, float y) : Vec<float, 2>(x, y) {}
Vec2f(const Vec<float, 2>& other) : Vec<float, 2>(other) {}

[[nodiscard]] auto x() const { return data.at(0); }
auto x(float x) { data.at(0) = x; }
[[nodiscard]] auto y() const { return data.at(1); }
auto y(float y) { data.at(1) = y; }
};

struct Vec3f : Vec<float, 3> {
Vec3f() : Vec<float, 3>() {}
Vec3f(const auto& other) : Vec<float, 3>(other) {}
// Vec3f(float scalar) : Vec<float, 3>({scalar, scalar, scalar}) {}
Vec3f(float x, float y, float z) : Vec<float, 3>({x, y, z}) {}
Vec3f(float x, float y, float z) : Vec<float, 3>(x, y, z) {}
Vec3f(const Vec<float, 3>& other) : Vec<float, 3>(other) {}

[[nodiscard]] auto x() const { return data.at(0); }
Expand All @@ -154,9 +174,9 @@ struct Vec3f : Vec<float, 3> {
struct Vec4f : Vec<float, 4> {
Vec4f() : Vec<float, 4>() {}
Vec4f(const auto& other) : Vec<float, 4>(other) {}
Vec4f(float x, float y, float z, float w) : Vec<float, 4>({x, y, z, w}) {}
Vec4f(float x, float y, float z, float w) : Vec<float, 4>(x, y, z, w) {}
Vec4f(const Vec3f& vec, float w)
: Vec<float, 4>({vec.x(), vec.y(), vec.z(), w}) {}
: Vec<float, 4>(vec.x(), vec.y(), vec.z(), w) {}
Vec4f(const Vec<float, 4>& other) : Vec<float, 4>(other) {}
// Vec4f(std::array<float, 4> data) : Vec<float, 4>(data) {}

Expand Down
55 changes: 35 additions & 20 deletions wren_math/tests/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@
#include <wren/math/utils.hpp>
#include <wren/math/vector.hpp>

BOOST_AUTO_TEST_SUITE(VECTOR)
BOOST_AUTO_TEST_SUITE(vector_test_suite)

BOOST_AUTO_TEST_CASE(ADD_SUB) {
enum class OP { kAdd, kSub };
BOOST_AUTO_TEST_CASE(AddSub) {
enum class OP { Add, Sub };

struct Test {
wren::math::Vec2f a;
wren::math::Vec2f b;

wren::math::Vec2f expected;

OP op = OP::kAdd;
OP op = OP::Add;
};

std::array tests = {
Test{{5.0f, 5.0f}, {10.0f, 10.0f}, {15.0f, 15.0f}},
Test{{5.0f, 5.0f}, {10.0f, 10.0f}, {-5.0f, -5.0f}, OP::kSub},
Test{
.a = {5.0f, 5.0f},
.b = {10.0f, 10.0f},
.expected = {15.0f, 15.0f},
},
Test{.a = {5.0f, 5.0f},
.b = {10.0f, 10.0f},
.expected = {-5.0f, -5.0f},
.op = OP::Sub},
};

for (auto const& test : tests) {
for (const auto& test : tests) {
wren::math::Vec2f c;
switch (test.op) {
case OP::kAdd:
case OP::Add:
c = test.a + test.b;
break;
case OP::kSub:
case OP::Sub:
c = test.a - test.b;
break;
}
Expand All @@ -45,11 +52,19 @@ BOOST_AUTO_TEST_CASE(MUL) {
};

std::array tests = {
Test{{5, 5}, 10, {50, 50}},
Test{{5, 5}, -1, {-5, -5}},
Test{
.a = {5, 5},
.scalar = 10,
.expected = {50, 50},
},
Test{
.a = {5, 5},
.scalar = -1,
.expected = {-5, -5},
},
};

for (auto const& test : tests) {
for (const auto& test : tests) {
wren::math::Vec2f c = test.a * test.scalar;
BOOST_TEST(c == test.expected);
}
Expand All @@ -63,12 +78,12 @@ BOOST_AUTO_TEST_CASE(DOT) {
};

std::array tests = {
Test{{10, 10, 10}, {10, 10, 10}, 1},
Test{{10, 10, 10}, {15, 15, 15}, 1},
Test{{10, 10, 10}, {-10, -10, -10}, -1},
Test{.a = {0.5, 0.5, 0}, .b = {0.5, 0.5, 0}, .expected = 0.5},
Test{.a = {1, 1, 0}, .b = {1, 1, 0}, .expected = 2},
Test{.a = {0.5, 0.5, 0}, .b = {-0.5, -0.5, 0}, .expected = -0.5},
};

for (auto const& test : tests) {
for (const auto& test : tests) {
float got = test.a.dot(test.b);
BOOST_TEST(got == test.expected, boost::test_tools::tolerance(0.01F));
}
Expand All @@ -82,12 +97,12 @@ BOOST_AUTO_TEST_CASE(CROSS) {
};

std::array tests = {
Test{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}},
Test{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}},
Test{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}},
Test{.a = {1, 0, 0}, .b = {0, 1, 0}, .expected = {0, 0, 1}},
Test{.a = {0, 1, 0}, .b = {0, 0, 1}, .expected = {1, 0, 0}},
Test{.a = {0, 0, 1}, .b = {1, 0, 0}, .expected = {0, 1, 0}},
};

for (auto const& test : tests) {
for (const auto& test : tests) {
auto got = test.a % test.b;
BOOST_TEST(got == test.expected);
}
Expand Down
5 changes: 5 additions & 0 deletions wren_physics/include/wren/physics/ray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace wren::physics {
struct RayHit {
bool hit = false;
math::Vec3f point;

void reset() {
hit = false;
point = math::Vec3f{};
}
};

struct Ray {
Expand Down
Loading

0 comments on commit c7ee274

Please sign in to comment.