About to implement physics
This commit is contained in:
@ -15,18 +15,18 @@ bool_t Dawn::aabb3dIntersect(struct AABB3D cube1, struct AABB3D cube2) {
|
||||
);
|
||||
}
|
||||
|
||||
bool aabbSweep(
|
||||
const glm::vec3& pos1, const glm::vec3& size1, const glm::vec3& vel1,
|
||||
const glm::vec3& pos2, const glm::vec3& size2, const glm::vec3& vel2,
|
||||
float& fraction
|
||||
bool_t aabb3dSweep(
|
||||
struct AABB3D cube1, glm::vec3 velocity1,
|
||||
struct AABB3D cube2, glm::vec3 velocity2,
|
||||
glm::vec3 *normal
|
||||
) {
|
||||
glm::vec3 relVel = vel1 - vel2;
|
||||
glm::vec3 relPos = pos1 - pos2;
|
||||
|
||||
// If the relative velocity is zero, the cubes are already intersecting
|
||||
i (glm::length2(relVel) == 0.0f) {
|
||||
fraction = 0.0f;
|
||||
return true;
|
||||
if(glm::length2(relVel) == 0.0f) {
|
||||
fraction = 0.0f;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Expand the size of the cubes by the magnitude of the relative velocity
|
||||
@ -34,29 +34,29 @@ bool aabbSweep(
|
||||
glm::vec3 size2exp = size2 + glm::abs(relVel);
|
||||
|
||||
// Compute the times at which the cubes first and last overlap on each axis
|
||||
float tminx = (size1exp.x + size2exp.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
|
||||
float tmaxx = (size1.x + size2.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
|
||||
float tminy = (size1exp.y + size2exp.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
|
||||
float tmaxy = (size1.y + size2.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
|
||||
float tminz = (size1exp.z + size2exp.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
|
||||
float tmaxz = (size1.z + size2.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
|
||||
float_t tminx = (size1exp.x + size2exp.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
|
||||
float_t tmaxx = (size1.x + size2.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
|
||||
float_t tminy = (size1exp.y + size2exp.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
|
||||
float_t tmaxy = (size1.y + size2.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
|
||||
float_t tminz = (size1exp.z + size2exp.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
|
||||
float_t tmaxz = (size1.z + size2.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
|
||||
|
||||
// Find the earliest and latest times of overlap
|
||||
float tmin = glm::max(glm::max(glm::min(tminx, tmaxx), glm::min(tminy, tmaxy)), glm::min(tminz, tmaxz));
|
||||
float tmax = glm::min(glm::min(glm::max(tminx, tmaxx), glm::max(tminy, tmaxy)), glm::max(tminz, tmaxz));
|
||||
float_t tmin = glm::max(glm::max(glm::min(tminx, tmaxx), glm::min(tminy, tmaxy)), glm::min(tminz, tmaxz));
|
||||
float_t tmax = glm::min(glm::min(glm::max(tminx, tmaxx), glm::max(tminy, tmaxy)), glm::max(tminz, tmaxz));
|
||||
|
||||
// If the earliest time of overlap is greater than the length of the timestep, the cubes
|
||||
// will not collide during the timestep
|
||||
if (tmin > 1.0f) {
|
||||
fraction = 1.0f;
|
||||
return false;
|
||||
fraction = 1.0f;
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the latest time of overlap is less than or equal to zero, the cubes are already
|
||||
// colliding and will collide throughout the timestep
|
||||
if (tmax <= 0.0f) {
|
||||
fraction = 0.0f;
|
||||
return true;
|
||||
if(tmax <= 0.0f) {
|
||||
fraction = 0.0f;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Compute the fraction of the timestep at which the collision occurs
|
||||
|
@ -22,4 +22,9 @@ namespace Dawn {
|
||||
bool_t aabb3dIntersect(struct AABB3D cube1, struct AABB3D cube2);
|
||||
|
||||
|
||||
bool_t aabb3dSweep(
|
||||
glm::vec3 pos1, glm::vec3 size1, glm::vec3 vel1,
|
||||
glm::vec3 pos2, glm::vec3 size2, glm::vec3 vel2,
|
||||
float_t& fraction
|
||||
);
|
||||
}
|
@ -29,7 +29,6 @@ bool_t Dawn::raytestSphere(
|
||||
*normal = glm::normalize(*hit - sphere.center);
|
||||
*distance = t0;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -10,4 +10,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
CubeCollider.cpp
|
||||
CapsuleCollider.cpp
|
||||
SphereCollider.cpp
|
||||
CharacterController3D.cpp
|
||||
)
|
@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "CharacterController3D.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
CharacterController3D::CharacterController3D(SceneItem *item) :
|
||||
SceneItemComponent(item)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CharacterController3D::onStart() {
|
||||
useEvent([&](float_t delta){
|
||||
// Friction
|
||||
velocity -= glm::vec3(velocity.x, 0, velocity.z) * friction * delta;
|
||||
|
||||
// Gravity
|
||||
velocity += this->gravity * delta;
|
||||
|
||||
auto myCollider = item->getComponent<Collider3D>();
|
||||
auto colliders = getScene()->findComponents<Collider3D>();
|
||||
auto itCollider = colliders.begin();
|
||||
while(itCollider != colliders.end()) {
|
||||
(*itCollider)->
|
||||
++itCollider;
|
||||
}
|
||||
|
||||
// Move / Update
|
||||
transform->setLocalPosition(transform->getLocalPosition() + (velocity * delta));
|
||||
}, getScene()->eventSceneUpdate);
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "Collider3D.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class CharacterController3D : public SceneItemComponent {
|
||||
public:
|
||||
glm::vec3 velocity = glm::vec3(0, 0, 0);
|
||||
glm::vec3 gravity = glm::vec3(0, -1, 0);
|
||||
float_t friction = 12.0f;
|
||||
|
||||
CharacterController3D(SceneItem *item);
|
||||
|
||||
void onStart() override;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user