mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/4jcraft.git
synced 2026-07-17 13:48:11 +00:00
wip: removing vec3 tls
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "../Platform/stdafx.h"
|
||||
#include "Vec3.h"
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include "AABB.h"
|
||||
|
||||
unsigned int Vec3::tlsIdx = 0;
|
||||
@@ -62,108 +64,99 @@ Vec3* Vec3::set(double x, double y, double z) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Vec3* Vec3::interpolateTo(Vec3* t, double p) {
|
||||
double xt = x + (t->x - x) * p;
|
||||
double yt = y + (t->y - y) * p;
|
||||
double zt = z + (t->z - z) * p;
|
||||
Vec3 Vec3::vectorTo(const Vec3& p) const { return {p.x - x, p.y - y, p.z - z}; }
|
||||
|
||||
return Vec3::newTemp(xt, yt, zt);
|
||||
Vec3 Vec3::normalize() const {
|
||||
double dist = std::sqrt(x * x + y * y + z * z);
|
||||
if (dist < 0.0001) return {0, 0, 0};
|
||||
|
||||
return {x / dist, y / dist, z / dist};
|
||||
}
|
||||
|
||||
Vec3* Vec3::vectorTo(Vec3* p) {
|
||||
return Vec3::newTemp(p->x - x, p->y - y, p->z - z);
|
||||
double Vec3::dot(const Vec3& p) const { return x * p.x + y * p.y + z * p.z; }
|
||||
|
||||
Vec3 Vec3::cross(const Vec3& p) const {
|
||||
return {y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x};
|
||||
}
|
||||
|
||||
Vec3* Vec3::normalize() {
|
||||
double dist = (double)(sqrt(x * x + y * y + z * z));
|
||||
if (dist < 0.0001) return Vec3::newTemp(0, 0, 0);
|
||||
return Vec3::newTemp(x / dist, y / dist, z / dist);
|
||||
Vec3 Vec3::add(double x, double y, double z) const {
|
||||
return {this->x + x, this->y + y, this->z + z};
|
||||
}
|
||||
|
||||
double Vec3::dot(Vec3* p) { return x * p->x + y * p->y + z * p->z; }
|
||||
|
||||
Vec3* Vec3::cross(Vec3* p) {
|
||||
return Vec3::newTemp(y * p->z - z * p->y, z * p->x - x * p->z,
|
||||
x * p->y - y * p->x);
|
||||
double Vec3::distanceTo(const Vec3& p) const {
|
||||
double xd = p.x - x;
|
||||
double yd = p.y - y;
|
||||
double zd = p.z - z;
|
||||
return std::sqrt(xd * xd + yd * yd + zd * zd);
|
||||
}
|
||||
|
||||
Vec3* Vec3::add(double x, double y, double z) {
|
||||
return Vec3::newTemp(this->x + x, this->y + y, this->z + z);
|
||||
}
|
||||
|
||||
double Vec3::distanceTo(Vec3* p) {
|
||||
double xd = p->x - x;
|
||||
double yd = p->y - y;
|
||||
double zd = p->z - z;
|
||||
return (double)sqrt(xd * xd + yd * yd + zd * zd);
|
||||
}
|
||||
|
||||
double Vec3::distanceToSqr(Vec3* p) {
|
||||
double xd = p->x - x;
|
||||
double yd = p->y - y;
|
||||
double zd = p->z - z;
|
||||
double Vec3::distanceToSqr(const Vec3& p) {
|
||||
double xd = p.x - x;
|
||||
double yd = p.y - y;
|
||||
double zd = p.z - z;
|
||||
return xd * xd + yd * yd + zd * zd;
|
||||
}
|
||||
|
||||
double Vec3::distanceToSqr(double x2, double y2, double z2) {
|
||||
double Vec3::distanceToSqr(const double x2, const double y2,
|
||||
const double z2) const {
|
||||
double xd = x2 - x;
|
||||
double yd = y2 - y;
|
||||
double zd = z2 - z;
|
||||
return xd * xd + yd * yd + zd * zd;
|
||||
}
|
||||
|
||||
Vec3* Vec3::scale(double l) { return Vec3::newTemp(x * l, y * l, z * l); }
|
||||
Vec3 Vec3::scale(const double l) const { return {x * l, y * l, z * l}; }
|
||||
|
||||
double Vec3::length() { return sqrt(x * x + y * y + z * z); }
|
||||
double Vec3::length() const { return sqrt(x * x + y * y + z * z); }
|
||||
|
||||
Vec3* Vec3::clipX(Vec3* b, double xt) {
|
||||
double xd = b->x - x;
|
||||
double yd = b->y - y;
|
||||
double zd = b->z - z;
|
||||
std::optional<Vec3> Vec3::clipX(const Vec3& b, const double xt) const {
|
||||
double xd = b.x - x;
|
||||
double yd = b.y - y;
|
||||
double zd = b.z - z;
|
||||
|
||||
if (xd * xd < 0.0000001f) return NULL;
|
||||
if (xd * xd < 0.0000001f) return std::nullopt;
|
||||
|
||||
double d = (xt - x) / xd;
|
||||
if (d < 0 || d > 1) return NULL;
|
||||
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
|
||||
if (d < 0 || d > 1) return std::nullopt;
|
||||
|
||||
return Vec3{x + xd * d, y + yd * d, z + zd * d};
|
||||
}
|
||||
|
||||
Vec3* Vec3::clipY(Vec3* b, double yt) {
|
||||
double xd = b->x - x;
|
||||
double yd = b->y - y;
|
||||
double zd = b->z - z;
|
||||
std::optional<Vec3> Vec3::clipY(const Vec3& b, const double yt) const {
|
||||
double xd = b.x - x;
|
||||
double yd = b.y - y;
|
||||
double zd = b.z - z;
|
||||
|
||||
if (yd * yd < 0.0000001f) return NULL;
|
||||
if (yd * yd < 0.0000001f) return std::nullopt;
|
||||
|
||||
double d = (yt - y) / yd;
|
||||
if (d < 0 || d > 1) return NULL;
|
||||
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
|
||||
if (d < 0 || d > 1) return std::nullopt;
|
||||
|
||||
return Vec3{x + xd * d, y + yd * d, z + zd * d};
|
||||
}
|
||||
|
||||
Vec3* Vec3::clipZ(Vec3* b, double zt) {
|
||||
double xd = b->x - x;
|
||||
double yd = b->y - y;
|
||||
double zd = b->z - z;
|
||||
std::optional<Vec3> Vec3::clipZ(const Vec3& b, const double zt) const {
|
||||
double xd = b.x - x;
|
||||
double yd = b.y - y;
|
||||
double zd = b.z - z;
|
||||
|
||||
if (zd * zd < 0.0000001f) return NULL;
|
||||
if (zd * zd < 0.0000001f) return std::nullopt;
|
||||
|
||||
double d = (zt - z) / zd;
|
||||
if (d < 0 || d > 1) return NULL;
|
||||
return Vec3::newTemp(x + xd * d, y + yd * d, z + zd * d);
|
||||
if (d < 0 || d > 1) return std::nullopt;
|
||||
|
||||
return Vec3{x + xd * d, y + yd * d, z + zd * d};
|
||||
}
|
||||
|
||||
std::wstring Vec3::toString() {
|
||||
static wchar_t buf[128];
|
||||
swprintf(buf, 128, L"(%f,%f,%f)", x, y, z);
|
||||
return std::wstring(buf);
|
||||
std::wstring Vec3::toString() const {
|
||||
return std::format(L"({},{},{})", x, y, z);
|
||||
}
|
||||
|
||||
Vec3* Vec3::lerp(Vec3* v, double a) {
|
||||
return Vec3::newTemp(x + (v->x - x) * a, y + (v->y - y) * a,
|
||||
z + (v->z - z) * a);
|
||||
Vec3 Vec3::lerp(const Vec3& v, const double a) const {
|
||||
return {x + (v.x - x) * a, y + (v.y - y) * a, z + (v.z - z) * a};
|
||||
}
|
||||
|
||||
void Vec3::xRot(float degs) {
|
||||
void Vec3::xRot(const float degs) {
|
||||
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless
|
||||
// wasting precision here
|
||||
double _sin = sin(degs);
|
||||
@@ -177,7 +170,7 @@ void Vec3::xRot(float degs) {
|
||||
z = zz;
|
||||
}
|
||||
|
||||
void Vec3::yRot(float degs) {
|
||||
void Vec3::yRot(const float degs) {
|
||||
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless
|
||||
// wasting precision here
|
||||
double _sin = sin(degs);
|
||||
@@ -191,7 +184,7 @@ void Vec3::yRot(float degs) {
|
||||
z = zz;
|
||||
}
|
||||
|
||||
void Vec3::zRot(float degs) {
|
||||
void Vec3::zRot(const float degs) {
|
||||
double _cos = cos(degs); // 4J - cos/sin were floats but seems pointless
|
||||
// wasting precision here
|
||||
double _sin = sin(degs);
|
||||
@@ -230,23 +223,24 @@ double Vec3::distanceTo(AABB* box) {
|
||||
return sqrt(xd * xd + yd * yd + zd * zd);
|
||||
}
|
||||
|
||||
Vec3* Vec3::closestPointOnLine(Vec3* p1, Vec3* p2) {
|
||||
Vec3* diff = newTemp(x - p1->x, y - p1->y, z - p1->z);
|
||||
Vec3* dir = newTemp(p2->x - p1->x, p2->y - p1->y, p2->z - p1->z);
|
||||
float dot1 = diff->dot(dir);
|
||||
Vec3 Vec3::closestPointOnLine(const Vec3& p1, const Vec3& p2) const {
|
||||
Vec3 diff = {x - p1.x, y - p1.y, z - p1.z};
|
||||
Vec3 dir = {p2.x - p1.x, p2.y - p1.y, p2.z - p1.z};
|
||||
float dot1 = diff.dot(dir);
|
||||
|
||||
if (dot1 <= 0.0f) return p1;
|
||||
|
||||
float dot2 = dir->dot(dir);
|
||||
float dot2 = dir.dot(dir);
|
||||
|
||||
if (dot2 <= dot1) return p2;
|
||||
|
||||
float t = dot1 / dot2;
|
||||
return newTemp(p1->x + t * dir->x, p1->y + t * dir->y, p1->z + t * dir->z);
|
||||
|
||||
return {p1.x + t * dir.x, p1.y + t * dir.y, p1.z + t * dir.z};
|
||||
}
|
||||
|
||||
double Vec3::distanceFromLine(Vec3* p1, Vec3* p2) {
|
||||
Vec3* closestPoint = closestPointOnLine(p1, p2);
|
||||
Vec3* diff =
|
||||
newTemp(x - closestPoint->x, y - closestPoint->y, z - closestPoint->z);
|
||||
return diff->length();
|
||||
double Vec3::distanceFromLine(const Vec3& p1, const Vec3& p2) const {
|
||||
Vec3 closestPoint = closestPointOnLine(p1, p2);
|
||||
Vec3 diff{x - closestPoint.x, y - closestPoint.y, z - closestPoint.z};
|
||||
return diff.length();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user