Removed physics for now.
This commit is contained in:
97
archive/physics/2d/Box.cpp
Normal file
97
archive/physics/2d/Box.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Box.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
bool_t boxIsPointInside(glm::vec2 point, glm::vec2 min, glm::vec2 max) {
|
||||
return (
|
||||
point.x >= min.x && point.x <= max.x &&
|
||||
point.y >= min.y && point.y <= max.y
|
||||
);
|
||||
}
|
||||
|
||||
bool_t Dawn::boxCheckCollision(
|
||||
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
|
||||
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB,
|
||||
glm::vec2 velocity,
|
||||
glm::vec2 &normal,
|
||||
float_t &entryTime,
|
||||
float_t &exitTime,
|
||||
glm::vec2 &entryPoint,
|
||||
glm::vec2 &exitPoint
|
||||
) {
|
||||
// Calculate the time intervals of overlap in the x and y axes
|
||||
glm::vec2 invEntry = glm::vec2(0.0f);
|
||||
glm::vec2 invExit = glm::vec2(0.0f);
|
||||
|
||||
if (velocity.x >= 0.0f) {
|
||||
invEntry.x = ((minB.x + posB.x) - (maxA.x + posA.x)) / velocity.x;
|
||||
invExit.x = ((maxB.x + posB.x) - (minA.x + posA.x)) / velocity.x;
|
||||
} else {
|
||||
invEntry.x = ((maxB.x + posB.x) - (minA.x + posA.x)) / velocity.x;
|
||||
invExit.x = ((minB.x + posB.x) - (maxA.x + posA.x)) / velocity.x;
|
||||
}
|
||||
|
||||
|
||||
if (velocity.y >= 0.0f) {
|
||||
invEntry.y = ((minB.y + posB.y) - (maxA.y + posA.y)) / velocity.y;
|
||||
invExit.y = ((maxB.y + posB.y) - (minA.y + posA.y)) / velocity.y;
|
||||
} else {
|
||||
invEntry.y = ((maxB.y + posB.y) - (minA.y + posA.y)) / velocity.y;
|
||||
invExit.y = ((minB.y + posB.y) - (maxA.y + posA.y)) / velocity.y;
|
||||
}
|
||||
|
||||
// Calculate the time of entry and exit for each axis
|
||||
glm::vec2 entry = glm::max(invEntry, glm::vec2(0.0f));
|
||||
glm::vec2 exit = glm::min(invExit, glm::vec2(1.0f));
|
||||
|
||||
// Find the time of entry and exit
|
||||
entryTime = glm::max(entry.x, entry.y);
|
||||
exitTime = glm::min(exit.x, exit.y);
|
||||
|
||||
// If there is no overlap in either axis, there is no collision
|
||||
if (entryTime > exitTime || entry.x < 0.0f && entry.y < 0.0f || entry.x > 1.0f || entry.y > 1.0f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculate the normal of the collision
|
||||
if (entry.x > entry.y) {
|
||||
if (invEntry.x < 0.0f) {
|
||||
normal = glm::vec2(1.0f, 0.0f);
|
||||
} else {
|
||||
normal = glm::vec2(-1.0f, 0.0f);
|
||||
}
|
||||
} else {
|
||||
if (invEntry.y < 0.0f) {
|
||||
normal = glm::vec2(0.0f, 1.0f);
|
||||
} else {
|
||||
normal = glm::vec2(0.0f, -1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the entry and exit points
|
||||
entryPoint = posA + velocity * entryTime;
|
||||
exitPoint = posA + velocity * exitTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t Dawn::boxIsBoxColliding(
|
||||
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
|
||||
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB
|
||||
) {
|
||||
// Check for no overlap in X axis
|
||||
if (posA.x + maxA.x < posB.x + minB.x || posB.x + maxB.x < posA.x + minA.x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for no overlap in Y axis
|
||||
if (posA.y + maxA.y < posB.y + minB.y || posB.y + maxB.y < posA.y + minA.y) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
48
archive/physics/2d/Box.hpp
Normal file
48
archive/physics/2d/Box.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
bool_t boxCheckCollision(
|
||||
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
|
||||
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB,
|
||||
glm::vec2 velocity,
|
||||
glm::vec2 &normal,
|
||||
float_t &entryTime,
|
||||
float_t &exitTime,
|
||||
glm::vec2 &entryPoint,
|
||||
glm::vec2 &exitPoint
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks if two boxes are colliding.
|
||||
*
|
||||
* @param posA Position of the first box.
|
||||
* @param minA Minimum point on the first box.
|
||||
* @param maxA Maximum point on the first box.
|
||||
* @param posB Position of the second box.
|
||||
* @param minB Minimum point on the second box.
|
||||
* @param maxB Maximum point on the second box.
|
||||
* @return True if the boxes are colliding with each other, false otherwise.
|
||||
*/
|
||||
bool_t boxIsBoxColliding(
|
||||
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
|
||||
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks if a given point is within the 2D Boundaries of an object.
|
||||
*
|
||||
* @param point Point to test.
|
||||
* @param min Minimum point on the box.
|
||||
* @param max Maximum point on the box.
|
||||
* @return True if the point is within the box.
|
||||
*/
|
||||
static bool_t boxIsPointInside(glm::vec2 point, glm::vec2 min, glm::vec2 max);
|
||||
}
|
11
archive/physics/2d/CMakeLists.txt
Normal file
11
archive/physics/2d/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Box.cpp
|
||||
Ray2D.cpp
|
||||
)
|
12
archive/physics/2d/Circle.hpp
Normal file
12
archive/physics/2d/Circle.hpp
Normal file
@ -0,0 +1,12 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "assert/assert.hpp
|
||||
|
||||
namespace Dawn {
|
||||
|
||||
}
|
13
archive/physics/2d/Physics2D.hpp
Normal file
13
archive/physics/2d/Physics2D.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
static inline glm::vec2 physics3Dto2D(glm::vec3 v) {
|
||||
return glm::vec2(v.x, v.z);
|
||||
}
|
||||
}
|
0
archive/physics/2d/PhysicsGrid.hpp
Normal file
0
archive/physics/2d/PhysicsGrid.hpp
Normal file
27
archive/physics/2d/Ray2D.cpp
Normal file
27
archive/physics/2d/Ray2D.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Ray2D.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
Ray2D::Ray2D(glm::vec2 pos, glm::vec2 dir) :
|
||||
position(pos),
|
||||
direction(dir)
|
||||
{
|
||||
}
|
||||
|
||||
bool_t Ray2D::intersects(struct Ray2D ray, glm::vec2 *out) {
|
||||
glm::vec2 j = this->position - ray.position;
|
||||
float_t f = -ray.direction.x * direction.y + direction.x * ray.direction.y;
|
||||
float_t s = (-direction.y * j.x + direction.x * j.y) / f;
|
||||
float_t t = (ray.direction.x * j.y - ray.direction.y * j.x) / f;
|
||||
if(s >= 0 && s <= 1 && t >= 0 && t <= 1) {
|
||||
*out = position + (t * direction);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
31
archive/physics/2d/Ray2D.hpp
Normal file
31
archive/physics/2d/Ray2D.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct Ray2D {
|
||||
const glm::vec2 position;
|
||||
const glm::vec2 direction;
|
||||
|
||||
/**
|
||||
* Constructs a new Ray 2D.
|
||||
*
|
||||
* @param position Position of this ray in 2D space.
|
||||
* @param direction Direction from the origin in 2D space of this ray.
|
||||
*/
|
||||
Ray2D(glm::vec2 position, glm::vec2 direction);
|
||||
|
||||
/**
|
||||
* Checks whether one ray is intersecting with another ray.
|
||||
*
|
||||
* @param ray The ray to check if this ray is intersecting.
|
||||
* @param out The point in space where the two rays intersect.
|
||||
* @return True if they intersect, otherwise false.
|
||||
*/
|
||||
bool_t intersects(struct Ray2D ray, glm::vec2 *out);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user