summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-10-23 15:13:20 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-10-23 15:13:20 -0400
commita214b4f8977a4b115710b5c9d152b392ac8e24f7 (patch)
tree2453ba5b5f6d852fabec6294bb6fe34992879cad
parenta003378e602107d421b819ac629493b5c6444762 (diff)
(mkosarek) Cube to better understand the bounds
-rw-r--r--3d/rigidbody/dist/output.js3
-rwxr-xr-x3d/rigidbody/dist/output.wasmbin62008 -> 67027 bytes
-rw-r--r--3d/rigidbody/main.cpp108
-rw-r--r--shared_cpp/Renderer3d.cpp2
4 files changed, 88 insertions, 25 deletions
diff --git a/3d/rigidbody/dist/output.js b/3d/rigidbody/dist/output.js
index e2fe7b4..28d7b0b 100644
--- a/3d/rigidbody/dist/output.js
+++ b/3d/rigidbody/dist/output.js
@@ -2474,6 +2474,8 @@ var ASM_CONSTS = {
GLctx.currentElementArrayBufferBinding = ibo ? (ibo.name | 0) : 0;
}
+ function _glBlendFunc(x0, x1) { GLctx['blendFunc'](x0, x1) }
+
function _glBufferData(target, size, data, usage) {
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
@@ -2965,6 +2967,7 @@ var asmLibraryArg = {
"glAttachShader": _glAttachShader,
"glBindBuffer": _glBindBuffer,
"glBindVertexArray": _glBindVertexArray,
+ "glBlendFunc": _glBlendFunc,
"glBufferData": _glBufferData,
"glClear": _glClear,
"glClearColor": _glClearColor,
diff --git a/3d/rigidbody/dist/output.wasm b/3d/rigidbody/dist/output.wasm
index 1bc13cc..2ac9b12 100755
--- a/3d/rigidbody/dist/output.wasm
+++ b/3d/rigidbody/dist/output.wasm
Binary files differ
diff --git a/3d/rigidbody/main.cpp b/3d/rigidbody/main.cpp
index f95e634..deac83e 100644
--- a/3d/rigidbody/main.cpp
+++ b/3d/rigidbody/main.cpp
@@ -38,8 +38,10 @@ struct Rigidbody3d {
void reset() {
numImpulses = 0;
- velocity = { 0, 0, 0};
- rotationalVelocity = { 0, 0, 0 };
+ velocity = Vector3();
+ rotationalVelocity = Vector3();
+ position = Vector3();
+ rotation = Quaternion();
}
void applyImpulse(Impulse i) {
@@ -103,13 +105,13 @@ struct Rigidbody3d {
};
// -- Sphere definition
-float32 bounds = 20.f;
+float32 bounds = 50.f;
struct Sphere {
Mesh3d mesh;
Rigidbody3d previousBody;
Rigidbody3d body;
- float32 radius = 5.f;
+ float32 radius = 1.f;
void load(Renderer3d* renderer) {
const float32 angleIncrements = 2.f;
@@ -119,6 +121,8 @@ struct Sphere {
Vertex3d* vertices = new Vertex3d[numVertices];
GLuint* indices = new GLuint[numIndices];
+ radius = body.mass; // Set the radius to be proportional to the mass
+
// Generate vertices and indices
GLint index = 0;
int32 vidx = 0;
@@ -169,11 +173,7 @@ struct Sphere {
mesh.load(vertices, numVertices, indices, numIndices, renderer);
- body.position = Vector3 { 0.f, 0.f, 0.f };
- body.velocity = Vector3 { 0.f, 0.f, 0.f };
-
- float32 singleFaceArea = radius;
- body.momentOfInertia = (body.mass * singleFaceArea) / 6.f;
+ body.momentOfInertia = (2.f / 5.f) * (body.mass * (radius * radius));
delete [] vertices;
delete [] indices;
@@ -183,31 +183,34 @@ struct Sphere {
previousBody = body;
body.update(dtSeconds);
+ Vector3 upper = body.position + Vector3(radius, radius, radius);
+ Vector3 lower = body.position - Vector3(radius, radius, radius);
+
// -- Constrain inside of the box
- if (body.position.x > bounds) {
- body.position.x = bounds;
+ if (upper.x > bounds) {
+ body.position.x = bounds - radius;
body.velocity.x = -body.velocity.x;
}
- else if (body.position.x < -bounds) {
- body.position.x = -bounds;
+ else if (lower.x < -bounds) {
+ body.position.x = -bounds + radius;
body.velocity.x = -body.velocity.x;
}
- if (body.position.y > bounds) {
- body.position.y = bounds;
+ if (upper.y > bounds) {
+ body.position.y = bounds - radius;
body.velocity.y = -body.velocity.y;
}
- else if (body.position.y < -bounds) {
- body.position.y = -bounds;
+ else if (lower.y < -bounds) {
+ body.position.y = -bounds + radius;
body.velocity.y = -body.velocity.y;
}
- if (body.position.z > bounds) {
- body.position.z = bounds;
+ if (upper.z > bounds) {
+ body.position.z = bounds - radius;
body.velocity.z = -body.velocity.z;
}
- else if (body.position.z < -bounds) {
- body.position.z = -bounds;
+ else if (lower.z < -bounds) {
+ body.position.z = -bounds + radius;
body.velocity.z = -body.velocity.z;
}
@@ -220,10 +223,52 @@ struct Sphere {
}
void unload() {
+ body.reset();
mesh.unload();
}
};
+struct Cube {
+ Mesh3d mesh;
+
+ void load(Renderer3d* renderer) {
+ Vertex3d vertices[8];
+ GLuint indices[] = {
+ 2, 6, 7, 2, 7, 3,
+ 6, 4, 5, 6, 5, 7,
+ 4, 0, 1, 4, 1, 5,
+ 0, 4, 6, 0, 6, 2,
+ 1, 5, 7, 1, 7, 3,
+ };
+
+ vertices[0].position = Vector3(-1.f, 1.f, 1.f);
+ vertices[1].position = Vector3(-1.f, -1.f, 1.f);
+ vertices[2].position = Vector3(1.f, 1.f, 1.f);
+ vertices[3].position = Vector3(1.f, -1.f, 1.f);
+ vertices[4].position = Vector3(-1.f, 1.f, -1.f);
+ vertices[5].position = Vector3(-1.f, -1.f, -1.f);
+ vertices[6].position = Vector3(1.f, 1.f, -1.f);
+ vertices[7].position = Vector3(1.f, -1.f, -1.f);
+
+ for (int idx = 0; idx < 8; idx++) {
+ vertices[idx].normal = Vector3(0, 1, 0);
+ vertices[idx].color = Vector4(randomFloatBetween(0, 1), randomFloatBetween(0, 1), randomFloatBetween(0, 1), 0.9f).normalize();
+ }
+
+ mesh.load(vertices, 8, indices, 30, renderer);
+ float32 scale = bounds;
+ mesh.model = Mat4x4().scale(Vector3(scale, scale, scale));//.translate(Vector3(0, 0, scale / 2.f));
+ }
+
+ void render(Renderer3d* renderer) {
+ mesh.render(renderer);
+ }
+
+ void unload() {
+ mesh.unload();
+ }
+};
+
// -- Intersection info
struct IntersectionResult {
bool intersect = false;
@@ -250,8 +295,9 @@ Camera3d camera;
MainLoop mainLoop;
bool isIntersectingPointer = false;
-const int32 numSpheres = 2;
+const int32 numSpheres = 3;
Sphere sphereList[numSpheres];
+Cube environmentCube;
int main() {
context.init("#gl_canvas");
@@ -264,20 +310,28 @@ int main() {
void load() {
renderer.load(&context);
- sphereList[0].load(&renderer);
sphereList[0].body.mass = 10.f;
sphereList[0].body.position = Vector3(10.f, 0, -5);
sphereList[0].body.velocity = Vector3(-10.f, 10.f, 10.f);
sphereList[0].body.rotationalVelocity = Vector3(1.f, 1.f, 1.f);
+ sphereList[0].load(&renderer);
- sphereList[1].load(&renderer);
sphereList[1].body.mass = 6.f;
sphereList[1].body.position = Vector3(-10.f, 0, 5);
sphereList[1].body.velocity = Vector3(10.f, -10.f, -10.f);
sphereList[1].body.rotationalVelocity = Vector3(1.f, 1.f, 1.f);
+ sphereList[1].load(&renderer);
+
+ sphereList[2].body.mass = 3.f;
+ sphereList[2].body.position = Vector3(-10.f, -10, 5);
+ sphereList[2].body.velocity = Vector3(10.f, 0.f, -10.f);
+ sphereList[2].body.rotationalVelocity = Vector3(1.f, 1.f, 1.f);
+ sphereList[2].load(&renderer);
+
+ environmentCube.load(&renderer);
camera.projection = Mat4x4().getPerspectiveProjection(0.1f, 10000.f, DEG_TO_RAD(45.f), 800.f / 600.f);
- camera.view = Mat4x4().translate({ 0, 0, -60.f });
+ camera.view = Mat4x4().translate({ 0, 0, -175.f });
mainLoop.run(update);
}
@@ -372,6 +426,8 @@ void update(float32 deltaTimeSeconds, void* userData) {
for (int32 idx = 0; idx < numSpheres; idx++) {
sphereList[idx].render(&renderer);
}
+
+ environmentCube.render(&renderer);
}
void unload() {
@@ -380,6 +436,8 @@ void unload() {
for (int32 idx = 0; idx < numSpheres; idx++) {
sphereList[idx].unload();
}
+
+ environmentCube.unload();
}
//
diff --git a/shared_cpp/Renderer3d.cpp b/shared_cpp/Renderer3d.cpp
index d6be3cb..ac2a0c5 100644
--- a/shared_cpp/Renderer3d.cpp
+++ b/shared_cpp/Renderer3d.cpp
@@ -62,6 +62,8 @@ void Renderer3d::render(Camera3d* camera) {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);