2 Commits

Author SHA1 Message Date
d1bb086daf temp intersect
Some checks failed
Linux arm64 / Build (push) Failing after 4m42s
2024-03-26 09:00:47 +01:00
7f5aee9e16 fix merge 2024-03-26 09:00:30 +01:00
3 changed files with 22 additions and 95 deletions

View File

@@ -192,35 +192,6 @@ constexpr Vec3<T> operator*=(const Vec3<T>& vect, T mult) {
return vect;
}
template <typename T>
constexpr Vec3<T> operator*=(T mult, const Vec3<T>& vect) {
vect = vect * mult;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x * other.x, vect.y * other.y, vect.z * other.z};
}
template <typename T>
Vec3<T>& operator*=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect * other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator/(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x / other.x, vect.y / other.y, vect.z / other.z};
}
template <typename T>
Vec3<T>& operator/=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect / other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*=(T mult, const Vec3<T>& vect) {
vect = vect * mult;
@@ -269,62 +240,6 @@ constexpr bool operator<(const Vec3<T>& vec3, const Vec3<T>& other) {
return vec3.x < other.x && vec3.y < other.y && vec3.z < other.z;
}
/**
* @brief returns the (signed) minimal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <typename T>
constexpr T ReduceMin(Vec3<T> v) {
return std::min(std::min(v.x, v.y), v.z);
}
/**
* @brief returns the (signed) maximal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <typename T>
constexpr T ReduceMax(Vec3<T> v) {
return std::max(std::max(v.x, v.y), v.z);
}
/**
* @brief returns the minimum of each coordinate in a new vector
*
* @tparam T
* @param self
* @param other
* @return constexpr Vec3<T>
*/
template <typename T>
constexpr Vec3<T> Min(const Vec3<T>& self, const Vec3<T>& other) {
return {
std::min(self.x, other.x),
std::min(self.y, other.y),
std::min(self.z, other.z),
};
}
/**
* @brief returns the maximum of each coordinate in a new vector
*
* @tparam T
* @param self
* @param other
* @return constexpr Vec3<T>
*/
template <typename T>
constexpr Vec3<T> Max(const Vec3<T>& self, const Vec3<T>& other) {
return {
std::max(self.x, other.x),
std::max(self.y, other.y),
std::max(self.z, other.z),
};
}
// Vec4
template <typename T>

View File

@@ -1,18 +1,28 @@
#include "blitz/maths/Physics.h"
#include "blitz/maths/Maths.h"
#include "blitz/misc/Log.h"
#include <cmath>
namespace blitz {
namespace maths {
float Intersects(const Ray& ray, const AABB& aabb) {
Vec3f dirFrac{
/**
* @brief Returns `true` if the half-line `ray` _strictly_ intersects with `aabb`,
* and `false` if it _strictly_ doesn't intersect.
* Note that if it only intersects with corners, edges, or sides of the box,
* or if any coordinate in `ray` is `NAN` or `inf` or if any coordinate in
* `aabb` is `NAN` the result is unspecified.
* */
float Distance(const Ray& ray, const AABB& aabb) {
// r.dir is unit direction vector of ray
Vec3f dirFrac = {
1.0f / ray.direction.x,
1.0f / ray.direction.y,
1.0f / ray.direction.z,
};
// lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
// r.org is origin of ray
float t1 = (aabb.from.x - ray.origin.x) * dirFrac.x;
float t2 = (aabb.to.x - ray.origin.x) * dirFrac.x;
float t3 = (aabb.from.y - ray.origin.y) * dirFrac.y;
@@ -24,17 +34,19 @@ float Intersects(const Ray& ray, const AABB& aabb) {
float tmax = std::min(std::min(std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
if (tmax < 0) {
return -1;
}
// if tmin > tmax, ray doesn't intersect AABB
if (tmin > tmax) {
return -1;
if (tmax < 0 || tmin > tmax) {
return -1.0f;
}
return tmin;
}
bool Intersects(const Ray& ray, const AABB& aabb) {
float distance = Distance(ray, aabb);
utils::LOG(std::to_string(distance));
return distance >= 0.0f;
}
} // namespace maths
} // namespace blitz

View File

@@ -47,7 +47,7 @@ void ServerGame::CheckShoot(game::PlayerID shooter, Vec3f position, float yaw, f
game::Player* shooterPlayer = GetPlayerById(shooter);
for (auto& [playerId, player] : GetPlayers()) {
if (playerId != shooter && maths::Intersects(shootRay, playerStaticAABB + player.GetPosition()) > 0.0f) {
if (playerId != shooter && maths::Intersects(shootRay, playerStaticAABB + player.GetPosition())) {
DamagePlayer(player, *shooterPlayer);
shootLanded = true;
}