From 05d805f03de713f12b4ef5e8f6edc6dcc71bbde8 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Thu, 13 Oct 2022 14:32:22 -0700 Subject: [PATCH] Add rotation --- gamestate.c | 41 +++++++++++++++++++++++++++-------------- main.c | 3 +++ types.h | 5 +++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/gamestate.c b/gamestate.c index e906b1b..945a11c 100644 --- a/gamestate.c +++ b/gamestate.c @@ -2,28 +2,41 @@ // do not use any global variables to process gamestate - -static void process_body(struct Body *body, float dt) +static void integrate_acceleration(struct Body *body, float dt) { - V2 current = body->position; - body->position = V2add(body->position, V2sub(current, body->old_position)); - body->position = V2add(body->position, V2scale(body->acceleration, dt*dt)); - body->old_position = current; -} + // position + { + V2 current = body->position; + body->position = V2add(body->position, V2sub(current, body->old_position)); + body->position = V2add(body->position, V2scale(body->acceleration, dt * dt)); + body->old_position = current; + } + // rotation + { + float current = body->rotation; + body->rotation = body->rotation + (current - body->old_rotation); + body->rotation = body->rotation + body->angular_acceleration * dt * dt; + body->old_rotation = current; + } +} -void process(struct GameState * gs, float dt) { - for(int i = 0; i < MAX_PLAYERS; i++) +void process(struct GameState *gs, float dt) +{ + for (int i = 0; i < MAX_PLAYERS; i++) { - struct Player * p = &gs->players[i]; - if(!p->connected) + struct Player *p = &gs->players[i]; + if (!p->connected) continue; p->body.acceleration = V2scale(p->input, 5.0f); - process_body(&p->body, dt); + p->body.angular_acceleration = p->input.x * 10.0f; + integrate_acceleration(&p->body, dt); } - for(int i = 0; i < gs->num_boxes; i++) + for (int i = 0; i < gs->num_boxes; i++) { - process_body(&gs->boxes[i].body, dt); + integrate_acceleration(&gs->boxes[i].body, dt); } + + } \ No newline at end of file diff --git a/main.c b/main.c index 0f49c1b..9402a40 100644 --- a/main.c +++ b/main.c @@ -195,7 +195,10 @@ static void frame(void) if (!p->connected) continue; sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f); + sgp_push_transform(); + sgp_rotate_at(p->body.rotation,p->body.position.x, p->body.position.y); sgp_draw_filled_rect(p->body.position.x - halfbox, p->body.position.y - halfbox, BOX_SIZE, BOX_SIZE); + sgp_pop_transform(); } // boxes diff --git a/types.h b/types.h index 2dd2b0b..0ecb010 100644 --- a/types.h +++ b/types.h @@ -34,7 +34,12 @@ struct Body { P2 position; P2 old_position; + + float rotation; + float old_rotation; + V2 acceleration; + float angular_acceleration; }; struct Player