summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-12-07 21:17:12 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-12-07 21:17:12 -0500
commit3268bcf528723c6a6bde373707db8f7f234ec2ea (patch)
tree0ea0b6c83814386c70005af5f10c095521f0ec3b
parente8869220f5a5b245be1a89106c77f50ec07624fd (diff)
moving platforms
-rw-r--r--sim.cpp38
-rw-r--r--sim.hpp3
2 files changed, 25 insertions, 16 deletions
diff --git a/sim.cpp b/sim.cpp
index d2ae5ba..a1b3f49 100644
--- a/sim.cpp
+++ b/sim.cpp
@@ -245,12 +245,14 @@ static b2Body *platform_to_body(State *state, Platform *platform) {
v2 center = v2_scale(platform->center, B2_SCALE);
b2BodyDef body_def;
+ body_def.type = b2_kinematicBody;
body_def.position.Set(center.x, center.y);
b2Body *body = world->CreateBody(&body_def);
b2PolygonShape shape;
shape.SetAsBox(half_size, state->platform_thickness, b2Vec2(0, 0), platform->angle);
body->CreateFixture(&shape, 0.0f);
+ body->SetLinearVelocity(b2Vec2(0, 10.0f));
return body;
}
@@ -340,17 +342,6 @@ void sim_frame(Frame *frame) {
state->platform_thickness = 0.005f;
- { // initialize platforms
- state->nplatforms = 2;
- Platform *p = &state->platforms[0];
- p->center = V2(0.5f, 0.5f);
- p->angle = PIf * 0.46f;
- p->size = 0.3f;
- ++p;
- p->center = V2(0.4f, 0.5f);
- p->angle = PIf * 0.54f;
- p->size = 0.3f;
- }
ball->radius = 0.02f;
ball->pos = V2(0.5f, 0.8f);
@@ -358,8 +349,9 @@ void sim_frame(Frame *frame) {
b2Vec2 gravity(0, -30.0f);
b2World *world = state->world = new b2World(gravity);
+
b2BodyDef ground_body_def;
- ground_body_def.position.Set(0.0f, -10.0f);
+ ground_body_def.position.Set(0.0f, -1000.0f);
b2Body *ground_body = world->CreateBody(&ground_body_def);
b2PolygonShape ground_shape;
@@ -382,9 +374,20 @@ void sim_frame(Frame *frame) {
ball_body->CreateFixture(&ball_fixture);
- for (u32 i = 0; i < state->nplatforms; ++i)
- platform_to_body(state, &state->platforms[i]);
-
+ { // initialize platforms
+ state->nplatforms = 2;
+ Platform *p = &state->platforms[0];
+ p->center = V2(0.5f, 0.5f);
+ p->angle = PIf * 0.46f;
+ p->size = 0.3f;
+ p->body = platform_to_body(state, p);
+ ++p;
+ p->center = V2(0.4f, 0.5f);
+ p->angle = PIf * 0.54f;
+ p->size = 0.3f;
+ p->body = platform_to_body(state, p);
+ }
+
state->initialized = true;
#if DEBUG
state->magic_number = MAGIC_NUMBER;
@@ -413,6 +416,11 @@ void sim_frame(Frame *frame) {
b2Vec2 ball_pos = ball->body->GetPosition();
ball->pos.x = ball_pos.x * B2_INV_SCALE;
ball->pos.y = ball_pos.y * B2_INV_SCALE;
+ for (Platform *platform = state->platforms, *end = platform + state->nplatforms; platform != end; ++platform) {
+ b2Vec2 platform_pos = platform->body->GetPosition();
+ platform->center.x = platform_pos.x * B2_INV_SCALE;
+ platform->center.y = platform_pos.y * B2_INV_SCALE;
+ }
}
platforms_render(state, state->platforms, state->nplatforms);
diff --git a/sim.hpp b/sim.hpp
index a4824c8..176e879 100644
--- a/sim.hpp
+++ b/sim.hpp
@@ -1,4 +1,4 @@
-#include <Box2D.h>
+#include <Box2D/Box2D.h>
// enums with a specified width are a clang C extension & available in C++11
#if defined __clang__ || __cplusplus >= 201103L
#define ENUM_U8 typedef enum : u8
@@ -105,6 +105,7 @@ typedef struct {
} ShaderBall;
typedef struct {
+ b2Body *body;
v2 center;
float size;
float angle;