Starting basic physics, nothing fancy

This commit is contained in:
2023-02-20 00:29:28 -08:00
parent 4bfec16ba7
commit 9606b4dc9b
32 changed files with 779 additions and 286 deletions

View File

@ -14,9 +14,11 @@
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtx/component_wise.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/scalar_constants.hpp>
#include <glm/gtx/intersect.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/matrix_decompose.hpp>

View File

@ -1,12 +1,13 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
CubeMesh.cpp
TriangleMesh.cpp
QuadMesh.cpp
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
CubeMesh.cpp
TriangleMesh.cpp
QuadMesh.cpp
SphereMesh.cpp
)

View File

@ -13,8 +13,8 @@ void CubeMesh::buffer(
int32_t verticeStart, int32_t indiceStart
) {
assertNotNull(mesh);
mesh->bufferPositions(verticeStart, std::array<glm::vec3, CUBE_VERTICE_COUNT>{{
glm::vec3 positions[CUBE_VERTICE_COUNT] = {
pos,
glm::vec3(pos.x+size.x, pos.y, pos.z),
glm::vec3(pos.x, pos.y+size.y, pos.z),
@ -24,9 +24,9 @@ void CubeMesh::buffer(
glm::vec3(pos.x+size.x, pos.y, pos.z+size.z),
glm::vec3(pos.x, pos.y+size.y, pos.z+size.z),
pos + size
}});
};
mesh->bufferCoordinates(verticeStart,std::array<glm::vec2,CUBE_VERTICE_COUNT>{{
glm::vec2 coordinates[CUBE_VERTICE_COUNT] = {
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
@ -36,9 +36,9 @@ void CubeMesh::buffer(
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1)
}});
};
mesh->bufferIndices(indiceStart, std::array<meshindice_t, CUBE_INDICE_COUNT>{{
meshindice_t indices[CUBE_INDICE_COUNT] = {
// Back
verticeStart, verticeStart + 1, verticeStart + 3,
verticeStart, verticeStart + 2, verticeStart + 3,
@ -62,5 +62,9 @@ void CubeMesh::buffer(
// Bottom
verticeStart + 1, verticeStart, verticeStart + 4,
verticeStart + 1, verticeStart + 4, verticeStart + 5
}});
};
mesh->bufferPositions(verticeStart, positions, CUBE_VERTICE_COUNT);
mesh->bufferCoordinates(verticeStart, coordinates, CUBE_VERTICE_COUNT);
mesh->bufferIndices(indiceStart, indices, CUBE_INDICE_COUNT);
}

View File

@ -15,28 +15,24 @@ void QuadMesh::bufferQuadMeshWithZ(
) {
assertNotNull(mesh);
mesh->bufferPositions(
verticeStart, std::array<glm::vec3, QUAD_VERTICE_COUNT>{{
glm::vec3(xy0, z),
glm::vec3(xy1.x, xy0.y, z),
glm::vec3(xy0.x, xy1.y, z),
glm::vec3(xy1, z)
}}
);
glm::vec3 positions[QUAD_VERTICE_COUNT] = {
glm::vec3(xy0, z),
glm::vec3(xy1.x, xy0.y, z),
glm::vec3(xy0.x, xy1.y, z),
glm::vec3(xy1, z)
};
glm::vec2 coordinates[QUAD_VERTICE_COUNT] = {
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
};
meshindice_t indices[QUAD_INDICE_COUNT] = {
verticeStart, verticeStart + 1, verticeStart + 2,
verticeStart + 1, verticeStart + 2, verticeStart + 3
};
mesh->bufferCoordinates(
verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
}}
);
mesh->bufferIndices(
indiceStart, std::array<meshindice_t, QUAD_INDICE_COUNT>{{
verticeStart, verticeStart + 1, verticeStart + 2,
verticeStart + 1, verticeStart + 2, verticeStart + 3
}}
);
mesh->bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT);
mesh->bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT);
mesh->bufferIndices(indiceStart, indices, QUAD_INDICE_COUNT);
}
void QuadMesh::bufferQuadMesh(
@ -56,10 +52,11 @@ void QuadMesh::bufferCoordinates(
int32_t verticeStart
) {
assertNotNull(mesh);
mesh->bufferCoordinates(verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
glm::vec2 coordinates[QUAD_VERTICE_COUNT] = {
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
}});
};
mesh->bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT);
}
void QuadMesh::bufferPositions(
@ -67,14 +64,14 @@ void QuadMesh::bufferPositions(
glm::vec2 xy0, glm::vec2 xy1,
int32_t verticeStart
) {
mesh->bufferPositions(
verticeStart, std::array<glm::vec3, QUAD_VERTICE_COUNT>{{
glm::vec3(xy0, 0),
glm::vec3(xy1.x, xy0.y, 0),
glm::vec3(xy0.x, xy1.y, 0),
glm::vec3(xy1, 0)
}}
);
assertNotNull(mesh);
glm::vec3 positions[QUAD_VERTICE_COUNT] = {
glm::vec3(xy0, 0),
glm::vec3(xy1.x, xy0.y, 0),
glm::vec3(xy0.x, xy1.y, 0),
glm::vec3(xy1, 0)
};
mesh->bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT);
}
void QuadMesh::initQuadMesh(

View File

@ -0,0 +1,59 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SphereMesh.hpp"
using namespace Dawn;
void SphereMesh::createSphere(
Mesh *mesh,
float_t radius,
int32_t slices,
int32_t stacks
) {
std::vector<glm::vec3> positions;
// Create vertices
int32_t c = 0;
for (int32_t i = 0; i <= stacks; i++) {
float_t phi = M_PI * i / stacks;
float_t cosPhi = cos(phi);
float_t sinPhi = sin(phi);
for (int32_t j = 0; j <= slices; j++) {
float_t theta = 2 * M_PI * j / slices;
float_t cosTheta = cos(theta);
float_t sinTheta = sin(theta);
glm::vec3 v;
v.x = radius * sinPhi * cosTheta;
v.y = radius * sinPhi * sinTheta;
v.z = radius * cosPhi;
positions.push_back(v);
}
}
// Create indices
std::vector<meshindice_t> indices;
for (int32_t i = 0; i < stacks; i++) {
for (int32_t j = 0; j < slices; j++) {
meshindice_t p1 = i * (slices + 1) + j;
meshindice_t p2 = p1 + slices + 1;
indices.push_back(p1);
indices.push_back(p2);
indices.push_back(p1 + 1);
indices.push_back(p1 + 1);
indices.push_back(p2);
indices.push_back(p2 + 1);
}
}
mesh->createBuffers(positions.size(), indices.size());
mesh->bufferPositions(0, positions.data(), positions.size());
mesh->bufferIndices(0, indices.data(), indices.size());
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class SphereMesh {
public:
static void createSphere(
Mesh *mesh,
float_t radius,
int32_t slices,
int32_t stacks
);
};
}

View File

@ -9,19 +9,25 @@ using namespace Dawn;
void TriangleMesh::createTriangleMesh(Mesh *mesh) {
assertNotNull(mesh);
mesh->createBuffers(3, 3);
mesh->bufferPositions(0, std::array<glm::vec3, 3>{{
glm::vec3 positions[3] = {
glm::vec3(-0.5f, -0.5f, 0),
glm::vec3(0.5f, -0.5f, 0),
glm::vec3(0, 0.5f, 0)
}});
mesh->bufferCoordinates(0, std::array<glm::vec2, 3>{{
};
glm::vec2 coordinates[3] = {
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)
}});
mesh->bufferIndices(0, std::array<meshindice_t,3>{{
};
meshindice_t indices[3] = {
0, 1, 2
}});
};
mesh->createBuffers(3, 3);
mesh->bufferPositions(0, positions, 3);
mesh->bufferCoordinates(0, coordinates, 3);
mesh->bufferIndices(0, indices, 3);
}

View File

@ -109,6 +109,17 @@ namespace Dawn {
return glm::vec2(getAxis(negativeX, positiveX), getAxis(negativeY, positiveY));
}
/**
* Returns the 2D Axis for the given binds.
*
* @param x X Axis bind.
* @param y Y Axis bind.
* @return 2D vector of the two given input binds.
*/
glm::vec2 getAxis2D(inputbind_t x, inputbind_t y) {
return glm::vec2(getValue(x), getValue(y));
}
/**
* Returns true if the given bind is currently being pressed (a non-zero
* value).

View File

@ -0,0 +1,14 @@
// 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 AABB3D {
glm::vec3 min;
glm::vec3 max;
};
}

View File

@ -0,0 +1,10 @@
# 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
Ray3D.cpp
)

View File

@ -0,0 +1,14 @@
// 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 PhysicsSphere {
glm::vec3 center;
float_t radius;
};
}

View File

@ -0,0 +1,15 @@
// 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 PhysicsTriangle {
glm::vec3 v0;
glm::vec3 v1;
glm::vec3 v2;
};
}

View File

@ -0,0 +1,106 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Ray3D.hpp"
using namespace Dawn;
struct Ray3D {
glm::vec3 origin;
glm::vec3 direction;
};
struct PhysicsSphere {
glm::vec3 center;
float_t radius;
};
bool_t Dawn::raytestSphere(
struct Ray3D ray,
struct PhysicsSphere sphere,
glm::vec3 *hit,
glm::vec3 *normal
) {
glm::vec3 h, n;
auto result = glm::intersectRaySphere(
ray.origin, ray.direction,
sphere.center, sphere.radius,
h, n
);
if(hit != nullptr) *hit = h;
if(normal != nullptr) *normal = n;
return result;
}
bool_t Dawn::raytestTriangle(
struct Ray3D ray,
struct PhysicsTriangle triangle,
glm::vec2 *hit,
float_t *distance
) {
glm::vec2 h;
float_t d;
auto result = glm::intersectRayTriangle(
ray.origin, ray.direction,
triangle.v0, triangle.v1, triangle.v2,
h, d
);
if(hit != nullptr) *hit = h;
if(distance != nullptr) *distance = d;
return result;
}
bool_t Dawn::raytestAABB(
struct Ray3D ray,
struct AABB3D box,
glm::vec3 *point,
glm::vec3 *normal,
float_t *distance
) {
// Compute the inverse direction of the ray, for numerical stability
glm::vec3 invDir(1.0f / ray.direction.x, 1.0f / ray.direction.y, 1.0f / ray.direction.z);
// Compute the t-values for the two intersection candidates
glm::vec3 tMin = (box.min - ray.origin) * invDir;
glm::vec3 tMax = (box.max - ray.origin) * invDir;
// Make sure tMin is less than or equal to tMax for all components
glm::vec3 t1 = glm::min(tMin, tMax);
glm::vec3 t2 = glm::max(tMin, tMax);
float tNear = glm::compMax(t1);
float tFar = glm::compMin(t2);
// If tNear is greater than or equal to tFar, there is no intersection
if (tNear >= tFar) return false;
// If tFar is negative, the ray is pointing away from the box
if(tFar < 0.0f) return false;
// Compute the hit point and normal
glm::vec3 hitPoint = ray.origin + tNear * ray.direction;
if(point != nullptr) *point = hitPoint;
if(distance != nullptr) *distance = tNear;
if(normal != nullptr) {
if (hitPoint.x == box.min.x) {
*normal = glm::vec3(-1, 0, 0);
} else if (hitPoint.x == box.max.x) {
*normal = glm::vec3(1, 0, 0);
} else if (hitPoint.y == box.min.y) {
*normal = glm::vec3(0, -1, 0);
} else if (hitPoint.y == box.max.y) {
*normal = glm::vec3(0, 1, 0);
} else if (hitPoint.z == box.min.z) {
*normal = glm::vec3(0, 0, -1);
} else if (hitPoint.z == box.max.z) {
*normal = glm::vec3(0, 0, 1);
}
}
// The ray intersects the box
return true;
}

View File

@ -0,0 +1,40 @@
// 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 "PhysicsTriangle.hpp"
#include "PhysicsSphere.hpp"
#include "AABB3D.hpp"
namespace Dawn {
struct Ray3D {
glm::vec3 origin;
glm::vec3 direction;
};
bool_t raytestSphere(
struct Ray3D ray,
struct PhysicsSphere sphere,
glm::vec3 *hit,
glm::vec3 *normal
);
bool_t raytestTriangle(
struct Ray3D ray,
struct PhysicsTriangle triangle,
glm::vec2 *hit,
float_t *distance
);
bool_t raytestAABB(
struct Ray3D ray,
struct AABB3D box,
glm::vec3 *point,
glm::vec3 *normal,
float_t *distance
);
}

View File

@ -1,10 +1,13 @@
# 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
PhysicsManager.cpp
)
# 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
PhysicsManager.cpp
)
# Subdirs
add_subdirectory(3d)

View File

@ -16,6 +16,9 @@ namespace Dawn {
public SceneItemPrefab<SimpleSpinningCubePrefab>
{
public:
MeshHost *meshHost;
SimpleTexturedMaterial *material;
static std::vector<Asset*> prefabAssets(AssetManager *man) {
return std::vector<Asset*>();
}
@ -27,9 +30,9 @@ namespace Dawn {
void prefabInit(AssetManager *man) override {
auto meshRenderer = this->addComponent<MeshRenderer>();
auto meshHost = this->addComponent<MeshHost>();
auto spinning = this->addComponent<ExampleSpin>();
auto material = this->addComponent<SimpleTexturedMaterial>();
meshHost = this->addComponent<MeshHost>();
// auto spinning = this->addComponent<ExampleSpin>();
material = this->addComponent<SimpleTexturedMaterial>();
meshHost->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(&meshHost->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);

View File

@ -43,7 +43,7 @@ namespace Dawn {
// Shared
float_t clipNear = 0.001f;
float_t clipFar = 1000.0f;
float_t clipFar = 50.0f;
/**
* Create a new Camera Component.

View File

@ -1,4 +1,13 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# 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
RayTester3D.cpp
)
# Subdirs
add_subdirectory(collider)

View File

@ -0,0 +1,42 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "RayTester3D.hpp"
#include "scene/Scene.hpp"
using namespace Dawn;
RayTester3D::RayTester3D(SceneItem *item) : SceneItemComponent(item) {
}
void RayTester3D::onStart() {
this->getScene()->eventSceneUpdate.addListener(this, &RayTester3D::onUpdate);
}
void RayTester3D::onUpdate() {
prefab->transform.setLocalPosition(glm::vec3(0, 0, 0));
box.max = glm::vec3( 0.5f, 0.5f, 0.5f);
box.min = glm::vec3(-0.5f, -0.5f, -0.5f);
auto mouse = this->getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y);
mouse *= 2.0f;
mouse -= glm::vec2(1, 1);
glm::vec3 pos = glm::vec3(mouse.x * camera->orthoRight, mouse.y * camera->orthoBottom, 0.0f);
ray.direction = glm::vec3(0, 0, -15);
ray.origin = pos;
// prefab->transform.setLocalPosition(pos);
// ray.origin = glm::vec3(0, 0, 5);
bool_t x = raytestAABB(ray, box, &hit, &normal, &distance);
if(x) {
prefab->material->color = COLOR_RED;
} else {
prefab->material->color = COLOR_WHITE;
}
}

View File

@ -0,0 +1,27 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "physics/3d/Ray3D.hpp"
#include "scene/components/display/Camera.hpp"
namespace Dawn {
class RayTester3D : public SceneItemComponent {
public:
SimpleSpinningCubePrefab *prefab;
Camera *camera;
struct AABB3D box;
struct Ray3D ray;
glm::vec3 hit;
glm::vec3 normal;
float_t distance;
RayTester3D(SceneItem *item);
void onStart() override;
void onUpdate();
};
}

View 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
Collider3D.cpp
CubeCollider.cpp
)

View File

@ -0,0 +1,12 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Collider3D.hpp"
using namespace Dawn;
Collider3D::Collider3D(SceneItem *item) : SceneItemComponent(item) {
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "physics/3d/Ray3D.hpp"
namespace Dawn {
struct Collider3DRayResult {
glm::vec3 normal;
glm::vec3 point;
};
class Collider3D : public SceneItemComponent {
public:
Collider3D(SceneItem *item);
virtual bool_t raycast(struct Ray3D ray, struct Collider3DRayResult *out) = 0;
};
}

View File

@ -0,0 +1,16 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeCollider.hpp"
using namespace Dawn;
CubeCollider::CubeCollider(SceneItem *item) : Collider3D(item) {
}
bool_t CubeCollider::raycast(struct Ray3D ray, struct Collider3DRayResult *out){
return false;
}

View File

@ -0,0 +1,19 @@
// 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 CubeCollider : public Collider3D {
public:
glm::vec3 center = glm::vec3(0, 0, 0);
glm::vec3 size = glm::vec3(1, 1, 1);
CubeCollider(SceneItem *item);
bool_t raycast(struct Ray3D ray, struct Collider3DRayResult *out)override;
};
}