Incomplete Collision Code

This commit is contained in:
2024-05-27 23:09:50 -04:00
parent 937b806b88
commit 16880cef46
6 changed files with 49 additions and 71 deletions

View File

@@ -1,6 +1,6 @@
#include <ball.h>
void Ball::pre_render() {
hVelocity = 1;
hVelocity = 0;
hMove(velAngle, hVelocity);
vMove(vVelocity);
}
@@ -14,7 +14,7 @@ Ball::Ball() {
name = "ball";
modelPath = "assets/models/sphere_lo.obj";
texturePath = "assets/textures/missing.png";
setScale(1.f);
setScale(1.0f);
hVelocity = 0;
vVelocity = 0;
}

View File

@@ -22,7 +22,7 @@ Cube::Cube() {
name = "cube";
modelPath = "assets/models/cube.obj";
texturePath = "assets/textures/multi/";
setScale(1.0f);
setScale(0.5f);
hVelocity = 0;
vVelocity = 0;
}

View File

@@ -1,5 +1,24 @@
#include <engine/collision.h>
VertexArray genIcosahedron(const Sphere* sphere) {
const float phi = (1.0 + std::sqrt(5.0)) / 2.0;
VertexArray result;
std::vector<Vertex> vertices = {
{-1, phi, 0}, { 1, phi, 0}, {-1, -phi, 0}, { 1, -phi, 0},
{ 0, -1, phi}, { 0, 1, phi}, { 0, -1, -phi}, { 0, 1, -phi},
{ phi, 0, -1}, { phi, 0, 1}, {-phi, 0, -1}, {-phi, 0, 1}
};
std::vector<unsigned int> indices = {
0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
};
result.vertices = vertices;
result.indices = indices;
return result;
}
AABB Collision::genMinimallyEnclosingAABB(const VertexArray* vA) {
float minX = std::numeric_limits<float>::max();
float minY = std::numeric_limits<float>::max();
@@ -69,6 +88,7 @@ VertexArray Collision::getDrawable(const Shape& collider) {
2,4, 5, 7,4, 7,6,0,2, 6,0, 6, 4,1, 3, 7,
1, 7, 5
};
result.vertices.push_back(s->minPoint);
result.vertices.emplace_back(s->maxPoint.x, s->minPoint.y, s->minPoint.z);
result.vertices.emplace_back(s->minPoint.x, s->maxPoint.y, s->minPoint.z);
@@ -76,11 +96,10 @@ VertexArray Collision::getDrawable(const Shape& collider) {
result.vertices.emplace_back(s->minPoint.x, s->minPoint.y, s->maxPoint.z);
result.vertices.emplace_back(s->maxPoint.x, s->minPoint.y, s->maxPoint.z);
result.vertices.emplace_back(s->minPoint.x, s->maxPoint.y, s->maxPoint.z);
result.vertices.push_back(s->maxPoint); // 7: Top-back-right
result.vertices.push_back(s->maxPoint);
for (unsigned int ind : indices)
result.indices.push_back(ind);
return result;
}
@@ -89,66 +108,15 @@ VertexArray Collision::getDrawable(const Shape& collider) {
return result;
}
if (const auto* s = dynamic_cast<const Sphere*>(&collider)) { //Returns a pre-generated VertexArray of an icosphere.
VertexArray result;
std::vector<Vertex> verts = {
{0.000f, -1.000f, 0.000f}, {0.724f, -0.447f, 0.526f},
{-0.276f, -0.447f, 0.851f}, {-0.894f, -0.447f, 0.000f},
{-0.276f, -0.447f, -0.851f}, {0.724f, -0.447f, -0.526f},
{0.276f, 0.447f, 0.851f}, {-0.724f, 0.447f, 0.526f},
{-0.724f, 0.447f, -0.526f},{0.276f, 0.447f, -0.851f},
{0.894f, 0.447f, 0.000f}, {0.000f, 1.000f, 0.000f},
{-0.162f, -0.851f, 0.500f}, {0.425f, -0.851f, 0.309f},
{0.263f, -0.526f, 0.809f}, {0.851f, -0.526f, 0.000f},
{0.425f, -0.851f, -0.309f}, {-0.526f, -0.851f, 0.000f},
{-0.688f, -0.526f, 0.500f}, {-0.162f, -0.851f, -0.500f},
{-0.688f, -0.526f, -0.500f}, {0.263f, -0.526f, -0.809f},
{0.951f, 0.000f, 0.309f}, {0.951f, 0.000f, -0.309f},
{0.000f, 0.000f, 1.000f}, {0.588f, 0.000f, 0.809f},
{-0.951f, 0.000f, 0.309f}, {-0.588f, 0.000f, 0.809f},
{-0.588f, 0.000f, -0.809f}, {-0.951f, 0.000f, -0.309f},
{0.588f, 0.000f, -0.809f}, {0.000f, 0.000f, -1.000f},
{0.688f, 0.526f, 0.500f}, {-0.263f, 0.526f, 0.809f},
{-0.851f, 0.526f, 0.000f}, {-0.263f, 0.526f, -0.809f},
{0.688f, 0.526f, -0.500f}, {0.162f, 0.851f, 0.500f},
{0.526f, 0.851f, 0.000f}, {-0.425f, 0.851f, 0.309f},
{-0.425f, 0.851f, -0.309f}, {0.162f, 0.851f, -0.500f}
};
if (const auto* s = dynamic_cast<const Sphere*>(&collider)) {
VertexArray result = genIcosahedron(s);
std::vector<unsigned int> indices = {
0, 13, 12, 1, 13, 15, 0, 12, 17,
0, 17, 19, 0, 19, 16, 1, 15, 22,
2, 14, 24, 3, 18, 26, 4, 20, 28,
5, 21, 30, 1, 22, 25, 2, 24, 27,
3, 26, 29, 4, 28, 31, 5, 30, 23,
6, 32, 37, 7, 33, 39, 8, 34, 40,
9, 35, 41, 10, 36, 38, 38, 41, 11,
38, 36, 41, 36, 9, 41, 41, 40, 11,
41, 35, 40, 35, 8, 40, 40, 37, 11,
40, 34, 37, 34, 7, 37, 37, 38, 11,
37, 33, 38, 33, 10, 38, 23, 36, 10,
23, 30, 36, 30, 9, 36, 31, 35, 9,
31, 28, 35, 28, 8, 35, 29, 34, 8,
29, 26, 34, 26, 7, 34, 27, 33, 7,
27, 24, 33, 24, 6, 33, 25, 32, 6,
25, 22, 32, 22, 10, 32, 30, 31, 9,
30, 21, 31, 21, 4, 31, 28, 29, 8,
28, 20, 29, 20, 3, 29, 26, 27, 7,
26, 18, 27, 18, 2, 27, 22, 23, 10,
22, 15, 23, 15, 5, 23, 16, 21, 5,
16, 19, 21, 19, 4, 21, 19, 20, 4,
19, 17, 20, 17, 3, 20, 17, 18, 3,
17, 12, 18, 12, 2, 18, 15, 16, 5,
15, 13, 16, 13, 0, 16, 12, 14, 2,
12, 13, 14, 13, 1, 14
};
if (s->Radius != 1.0f) //Scale
for (auto& vertex : result.vertices)
vertex = Vector3::Normalize(vertex) * s->Radius;
for (auto& v : verts) {
v *= s->Radius;
for (auto& v : result.vertices) //Translate
v += s->Position;
}
result.vertices = verts;
result.indices = indices;
return result;
}
}

View File

@@ -134,7 +134,7 @@ void Engine::initGL()
exit(0);
}
glColor4f(0.f, 0.f, 0.f, 0.f);
//JGL::J3D::DrawString3D(JGL::Colors::Blue, "JGL Sample Text", {0.5f, 0, 0.5f}, 0.1f);
JGL::J3D::DrawString3D(JGL::Colors::Blue, "JGL Sample Text", {0.5f, 0, 0.5f}, 0.1f);
//JGL::J2D::FillRect2D(JGL::Colors::Purples::BlueViolet, {0.f, -5.f}, {24, 24});
}

View File

@@ -171,7 +171,7 @@ void Entity::render() {
glDisable(GL_LIGHTING);
glPushMatrix();
glColor4f(1, 0, 0, 1);
Collision::getDrawable(getSphere()).drawWireframe();
Collision::getDrawable(getSphere()).drawWireframe(); //TODO drawing this solid segfaults with useVBO because we attempt to draw buffer 0.
glColor4f(1, 1, 1, 1);
glPopMatrix();
glEnable(GL_LIGHTING);
@@ -219,6 +219,10 @@ void Entity::erase() {
AABB Entity::getAABB() {
AABB aabb = Collision::genMinimallyEnclosingAABB(getGeometry());
if (getScale() != 1)
aabb.minPoint *= scale,
aabb.maxPoint *= scale;
aabb.minPoint += position;
aabb.maxPoint += position;
return aabb;
@@ -226,6 +230,7 @@ AABB Entity::getAABB() {
Sphere Entity::getSphere() {
Sphere sphere = Collision::genMinimallyEnclosingSphere(getGeometry());
sphere.Radius *= scale;
sphere.Position = position;
return sphere;
}

View File

@@ -41,13 +41,7 @@ void VertexArray::draw() {
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x), //Vertices
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &texCoords[0].x), //Texture Coordinates
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]); //Indices
if (engine->useVBO) {
if (engine->useVBO && vbo !=0 && tbo !=0 && ebo !=0) {
//Vertices
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_FLOAT, 0, nullptr);
@@ -63,7 +57,18 @@ void VertexArray::draw() {
//Reset selected buffer.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
return;
}
//Immediate rendering.
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x), //Vertices
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &texCoords[0].x), //Texture Coordinates
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]); //Indices
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);