mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
huge improvement
This commit is contained in:
@@ -20,50 +20,4 @@ namespace omath
|
||||
0.f
|
||||
};
|
||||
}
|
||||
|
||||
Vector3 Vector3::ForwardVector(const float pitch, const float yaw)
|
||||
{
|
||||
const auto cosPitch = std::cos(angles::DegreesToRadians(pitch));
|
||||
const auto sinPitch = std::sin(angles::DegreesToRadians(pitch));
|
||||
|
||||
const auto cosYaw = std::cos(angles::DegreesToRadians(yaw));
|
||||
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
||||
|
||||
|
||||
return
|
||||
{
|
||||
cosPitch*cosYaw,
|
||||
cosPitch*sinYaw,
|
||||
sinPitch
|
||||
};
|
||||
}
|
||||
|
||||
Vector3 Vector3::RightVector(const float pitch, const float yaw, const float roll)
|
||||
{
|
||||
const auto radPitch = angles::DegreesToRadians(pitch);
|
||||
const auto radYaw = angles::DegreesToRadians(yaw);
|
||||
const auto radRoll = angles::DegreesToRadians(roll);
|
||||
|
||||
const auto cosPitch = std::cos(radPitch);
|
||||
const auto sinPitch = std::sin(radPitch);
|
||||
|
||||
const auto cosYaw = std::cos(radYaw);
|
||||
const auto sinYaw = std::sin(radYaw);
|
||||
|
||||
const auto cosRoll = std::cos(radRoll);
|
||||
const auto sinRoll = std::sin(radRoll);
|
||||
|
||||
|
||||
return
|
||||
{
|
||||
sinRoll*sinPitch*cosYaw + cosRoll*sinYaw,
|
||||
sinRoll*sinPitch*sinYaw - cosRoll*cosYaw,
|
||||
-sinRoll*cosPitch
|
||||
};
|
||||
}
|
||||
|
||||
Vector3 Vector3::UpVector(float pitch, float yaw, float roll)
|
||||
{
|
||||
return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw));
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,13 @@
|
||||
|
||||
#include "omath/prediction/Projectile.hpp"
|
||||
#include <cmath>
|
||||
|
||||
#include <omath/engines/Source.hpp>
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||
{
|
||||
auto currentPos = m_origin + Vector3::ForwardVector(pitch, yaw) * m_launchSpeed * time;
|
||||
auto currentPos = m_origin + omath::source::ForwardVector({source::PitchAngle::FromDegrees(pitch), source::YawAngle::FromDegrees(yaw), source::RollAngle::FromDegrees(0)}) * m_launchSpeed * time;
|
||||
currentPos.z -= (gravity * m_gravityScale) * std::pow(time, 2.f) * 0.5f;
|
||||
|
||||
return currentPos;
|
||||
|
||||
@@ -6,49 +6,4 @@
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const ViewPort &viewPort,
|
||||
const float fov, const float near, const float far, const float lensZoom)
|
||||
{
|
||||
m_origin = position;
|
||||
m_viewAngles = viewAngles;
|
||||
m_viewPort = viewPort;
|
||||
m_fieldOfView = fov;
|
||||
m_nearPlaneDistance = near;
|
||||
m_farPlaneDistance = far;
|
||||
m_lensZoom = lensZoom;
|
||||
}
|
||||
|
||||
Mat<4, 4> Camera::GetViewMatrix() const
|
||||
{
|
||||
const auto forward = Vector3::ForwardVector(m_viewAngles.x, m_viewAngles.y);
|
||||
const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
|
||||
const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
|
||||
|
||||
return Mat<>::TranslationMat(-m_origin) * Mat<>::OrientationMat(forward, right, up);
|
||||
}
|
||||
|
||||
std::expected<Vector3, Error> Camera::WorldToScreen(const Vector3& worldPosition) const
|
||||
{
|
||||
const auto posVecAsMatrix = Mat<>::MatColumnFromVector(worldPosition);
|
||||
|
||||
|
||||
const auto projectionMatrix = Mat<>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
|
||||
m_nearPlaneDistance, m_farPlaneDistance, m_lensZoom);
|
||||
|
||||
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
|
||||
|
||||
if (projected.At(0, 3) == 0.f)
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
projected /= projected.At(0, 3);
|
||||
|
||||
if (projected.At(0, 0) < -1.f || projected.At(0, 0) > 1.f ||
|
||||
projected.At(0, 1) < -1.f || projected.At(0, 1) > 1.f ||
|
||||
projected.At(0, 2) < -1.f || projected.At(0, 2) > 1.f)
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
projected *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height);
|
||||
|
||||
return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user