summaryrefslogtreecommitdiff
path: root/2d/rigidbody/rigidbody_1/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2d/rigidbody/rigidbody_1/main.cpp')
-rw-r--r--2d/rigidbody/rigidbody_1/main.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/2d/rigidbody/rigidbody_1/main.cpp b/2d/rigidbody/rigidbody_1/main.cpp
new file mode 100644
index 0000000..26d24e2
--- /dev/null
+++ b/2d/rigidbody/rigidbody_1/main.cpp
@@ -0,0 +1,158 @@
+#include "../../../shared_cpp/OrthographicRenderer.h"
+#include "../../../shared_cpp/types.h"
+#include "../../../shared_cpp/WebglContext.h"
+#include "../../../shared_cpp/mathlib.h"
+#include "../../../shared_cpp/MainLoop.h"
+#include <cstdio>
+#include <cmath>
+#include <emscripten/html5.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <cmath>
+#include <cfloat>
+
+struct Rigidbody {
+ Vector2 force = { 0, 0 };
+ Vector2 velocity = { 0, 0 };
+ Vector2 position = { 0, 0 };
+ float32 mass = 1.f;
+
+ void reset() {
+ force = { 0, 0 };
+ velocity = { 0, 0 };
+ }
+
+ void applyForce(Vector2 f) {
+ force += f;
+ }
+
+ void applyGravity(float32 deltaTimeSeconds) {
+ velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds);
+ }
+
+ void update(float32 deltaTimeSeconds) {
+ applyGravity(deltaTimeSeconds);
+
+ Vector2 acceleration = force / mass;
+ velocity += (acceleration * deltaTimeSeconds);
+ position += (velocity * deltaTimeSeconds);
+ force = Vector2 { 0.f, 0.f };
+ }
+};
+
+struct Rectangle {
+ OrthographicShape shape;
+ Rigidbody body;
+
+ void load(OrthographicRenderer* renderer, Vector4 color, float32 width, float32 height) {
+ color = color.toNormalizedColor();
+
+ float32 halfWidth = width / 2.f;
+ float32 halfHeight = height / 2.f;
+
+ OrthographicVertex vertices[6];
+ vertices[0].position = Vector2 { -halfWidth, -halfHeight };
+ vertices[1].position = Vector2 { -halfWidth, halfHeight };
+ vertices[2].position = Vector2 { halfWidth, halfHeight };
+ vertices[3].position = Vector2 { -halfWidth, -halfHeight };
+ vertices[4].position = Vector2 { halfWidth, -halfHeight };
+ vertices[5].position = Vector2 { halfWidth, halfHeight };
+
+ for (int32 i = 0; i < 6; i++) {
+ vertices[i].color = color;
+ }
+
+ shape.load(vertices, 6, renderer);
+ body.reset();
+ }
+
+ void update(float32 dtSeconds) {
+ body.update(dtSeconds);
+ shape.model = Mat4x4().translateByVec2(body.position);
+ }
+
+ void render(OrthographicRenderer* renderer) {
+ shape.render(renderer);
+ }
+
+ void unload() {
+ shape.unload();
+ }
+};
+
+EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData);
+EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData);
+
+void load();
+void update(float32 time, void* userData);
+void unload();
+
+WebglContext context;
+OrthographicRenderer renderer;
+MainLoop mainLoop;
+Rectangle rectangle;
+
+int main() {
+ context.init("#gl_canvas");
+ emscripten_set_click_callback("#gl_canvas_play", NULL, false, onPlayClicked);
+ emscripten_set_click_callback("#gl_canvas_stop", NULL, false, onStopClicked);
+ return 0;
+}
+
+void load() {
+ renderer.load(&context);
+
+ rectangle.load(&renderer, Vector4 { 55.f, 235.f, 35.f, 255.f }, 32.f, 32.f);
+ rectangle.body.position = Vector2 { context.width / 3.f, context.height / 3.f };
+ rectangle.body.velocity = Vector2 { 100.f, 250.f };
+
+ mainLoop.run(update);
+}
+
+void update(float32 deltaTimeSeconds, void* userData) {
+ rectangle.update(deltaTimeSeconds);
+
+ // Check collisions with walls so we don't go out of the scene. Simply reflect here.
+ if (rectangle.body.position.x <= 0.f) {
+ rectangle.body.position.x = 0.f;
+ rectangle.body.velocity = rectangle.body.velocity - Vector2 { 1.f, 0.f } * (2 * (rectangle.body.velocity.dot(Vector2 { 1.f, 0.f })));
+ }
+ if (rectangle.body.position.y <= 0.f) {
+ rectangle.body.position.y = 0.f;
+ rectangle.body.velocity = rectangle.body.velocity - Vector2 { 0.f, 1.f } * (2 * (rectangle.body.velocity.dot(Vector2 { 0.f, 1.f })));
+ }
+ if (rectangle.body.position.x >= 640.f) {
+ rectangle.body.position.x = 640.f;
+ rectangle.body.velocity = rectangle.body.velocity - Vector2 { -1.f, 0.f } * (2 * (rectangle.body.velocity.dot(Vector2{ -1.f, 0.f })));
+ }
+ if (rectangle.body.position.y >= 480.f) {
+ rectangle.body.position.y = 480.f;
+ rectangle.body.velocity = rectangle.body.velocity - Vector2 { 0.f, -1.f } * (2 * (rectangle.body.velocity.dot(Vector2 { 0.f, -1.f }))) ;
+ }
+
+ // Renderer
+ renderer.render();
+ rectangle.render(&renderer);
+}
+
+void unload() {
+ mainLoop.stop();
+ renderer.unload();
+ rectangle.unload();
+}
+
+//
+// Interactions with DOM handled below
+//
+EM_BOOL onPlayClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) {
+ printf("Play clicked\n");
+
+ load();
+ return true;
+}
+
+EM_BOOL onStopClicked(int eventType, const EmscriptenMouseEvent* mouseEvent, void* userData) {
+ printf("Stop clicked\n");
+ unload();
+ return true;
+}