From f1c91f539cd1ba1b4deeeb7258c9bd44c87b7e03 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Wed, 3 Nov 2021 20:21:39 +0700 Subject: [PATCH] accelerate collision detection in x3 times (feat. Neirokan) --- ShooterConsts.h | 2 +- engine/Object.h | 2 ++ engine/Vec2D.cpp | 2 +- engine/Vec4D.cpp | 2 +- engine/physics/RigidBody.cpp | 10 ++++++---- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ShooterConsts.h b/ShooterConsts.h index c8ad50e..bd35522 100644 --- a/ShooterConsts.h +++ b/ShooterConsts.h @@ -21,7 +21,7 @@ namespace ShooterConsts { const std::string ABILITY_OBJ = "obj/ability.obj"; const std::string HILL_OBJ = "obj/hill.obj"; - const std::string GUN_OBJ = "obj/gun.obj"; + const std::string GUN_OBJ = "obj/weapon.obj"; const std::string GUN_FIRE_SOUND = "sound/weapons/gun.ogg"; const std::string GUN_RELOAD_SOUND = "sound/weapons/reload_gun.ogg"; const std::string AK47_OBJ = "obj/ak47.obj"; diff --git a/engine/Object.h b/engine/Object.h index d33ca85..ea2f9ac 100644 --- a/engine/Object.h +++ b/engine/Object.h @@ -105,6 +105,8 @@ public: [[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; } + [[nodiscard]] Matrix4x4 view() const { return Matrix4x4::View(left(), up(), lookAt(), position()); } + // OpenGL function [[nodiscard]] GLfloat *glModel() const; diff --git a/engine/Vec2D.cpp b/engine/Vec2D.cpp index 5db1e25..ef0fd37 100644 --- a/engine/Vec2D.cpp +++ b/engine/Vec2D.cpp @@ -66,7 +66,7 @@ double Vec2D::abs() const { Vec2D Vec2D::normalized() const { double vecAbs = abs(); if (vecAbs > Consts::EPS) { - return Vec2D(*this) / abs(); + return Vec2D(*this) / vecAbs; } else { return Vec2D(0); } diff --git a/engine/Vec4D.cpp b/engine/Vec4D.cpp index 6aa8423..f9a92a6 100644 --- a/engine/Vec4D.cpp +++ b/engine/Vec4D.cpp @@ -67,7 +67,7 @@ double Vec4D::abs() const { Vec4D Vec4D::normalized() const { double vecAbs = abs(); if (vecAbs > Consts::EPS) { - return Vec4D(*this) / abs(); + return Vec4D(*this) / vecAbs; } else { return Vec4D(1); } diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index ac008bc..0428e1f 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -16,14 +16,16 @@ RigidBody::RigidBody(ObjectNameTag nameTag, const std::string &filename, const V Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { Vec3D maxPoint{0, 0, 0}; + double maxDistance = -std::numeric_limits::max(); + Vec3D transformedDirection = (view() * direction).normalized(); + for (auto &tri : triangles()) { for (int i = 0; i < 3; i++) { - // TODO: multiplying model() * tri[i] costs too much time to compute - Vec3D point(model() * tri[i]); + Vec3D point = Vec3D(tri[i]); + double distance = point.dot(transformedDirection); - double distance = point.dot(direction.normalized()); if (distance > maxDistance) { maxDistance = distance; maxPoint = point; @@ -31,7 +33,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { } } - return maxPoint; + return model()*maxPoint + position(); } Vec3D RigidBody::_support(std::shared_ptr obj, const Vec3D &direction) {