From b967bc45ec0718a0f22f7c734dfe2ca31739dd91 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Fri, 14 Oct 2022 23:52:07 -0700 Subject: [PATCH] Much better physics but still no rotation --- gamestate.c | 16 +++++++++------- main.c | 6 +++++- server.c | 49 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/gamestate.c b/gamestate.c index 85dbfc3..0952e63 100644 --- a/gamestate.c +++ b/gamestate.c @@ -39,11 +39,12 @@ static void integrate_acceleration(struct Body *body, float dt) static void modify_interval(struct Body *from, float *from_interval, V2 center, V2 axis) { + float halfbox = BOX_SIZE/2.0f; V2 points[4] = { - V2add(from->position, V2rotate((V2){.x = 0.5f, .y = -0.5f}, from->rotation)), // upper right - V2add(from->position, V2rotate((V2){.x = 0.5f, .y = 0.5f}, from->rotation)), // bottom right - V2add(from->position, V2rotate((V2){.x = -0.5f, .y = 0.5f}, from->rotation)), // lower left - V2add(from->position, V2rotate((V2){.x = -0.5f, .y = -0.5f}, from->rotation)), // upper left + V2add(from->position, V2rotate((V2){.x = halfbox, .y = -halfbox}, from->rotation)), // upper right + V2add(from->position, V2rotate((V2){.x = halfbox, .y = halfbox}, from->rotation)), // bottom right + V2add(from->position, V2rotate((V2){.x = -halfbox, .y = halfbox}, from->rotation)), // lower left + V2add(from->position, V2rotate((V2){.x = -halfbox, .y = -halfbox}, from->rotation)), // upper left }; for (int point_i = 0; point_i < 4; point_i++) { @@ -119,10 +120,11 @@ void process(struct GameState *gs, float dt) assert(from_interval[0] < from_interval[1]); assert(to_interval[0] < to_interval[1]); - if (to_interval[0] < from_interval[1]) // intersecting + if (from_interval[1] > to_interval[0]) // intersecting { - from->position = V2add(from->position, V2scale(axis, -0.5f)); - to->position = V2add(from->position, V2scale(axis, 0.5f)); + float intersection_depth = from_interval[1] - to_interval[0]; + from->position = V2add(from->position, V2scale(axis, intersection_depth*-0.5f)); + to->position = V2add(to->position, V2scale(axis, intersection_depth*0.5f)); } } } diff --git a/main.c b/main.c index 9402a40..53dd229 100644 --- a/main.c +++ b/main.c @@ -113,6 +113,10 @@ static void frame(void) Log("New client from host %x\n", event.peer->address.host); break; case ENET_EVENT_TYPE_RECEIVE: + // @Robust @BeforeShip use some kind of serialization strategy that checks for out of bounds + // and other validation instead of just casting to a struct + // "Alignment of structure members can be different even among different compilers on the same platform, let alone different platforms." + // ^^ need serialization strategy that accounts for this if multiple platforms is happening https://stackoverflow.com/questions/28455163/how-can-i-portably-send-a-c-struct-through-a-network-socket struct ServerToClient msg; if(event.packet->dataLength != sizeof(msg)) { @@ -151,7 +155,7 @@ static void frame(void) ENetPacket * packet = enet_packet_create((void*)&curmsg, sizeof(curmsg), ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT); enet_peer_send(peer, 0, packet); - process(&gs, (float)sapp_frame_duration()); + // process(&gs, (float)sapp_frame_duration()); } // drawing diff --git a/server.c b/server.c index e0445e7..eca85f0 100644 --- a/server.c +++ b/server.c @@ -15,17 +15,32 @@ void server(void *data) struct GameState gs = {0}; - gs.boxes[0] = (struct Box){ - .body = (struct Body){ - .position = (P2){.x = 0.75f, .y = 0.0}}, - }; - gs.boxes[0].body.old_position = gs.boxes[0].body.position; - gs.boxes[1] = (struct Box){ - .body = (struct Body){ - .position = (P2){.x = 0.75f, .y = 0.5f}}, - }; - gs.boxes[1].body.old_position = gs.boxes[1].body.position; - gs.num_boxes = 2; + // two boxes stacked on top + if (false) + { + gs.boxes[0] = (struct Box){ + .body = (struct Body){ + .position = (P2){.x = 0.75f, .y = 0.0}}, + }; + gs.boxes[0].body.old_position = gs.boxes[0].body.position; + gs.boxes[1] = (struct Box){ + .body = (struct Body){ + .position = (P2){.x = 0.75f, .y = 0.5f}}, + }; + gs.boxes[1].body.old_position = gs.boxes[1].body.position; + gs.num_boxes = 2; + } + + // one box + if (true) + { + gs.boxes[0] = (struct Box){ + .body = (struct Body){ + .position = (P2){.x = 0.75f, .y = 0.0}}, + }; + gs.boxes[0].body.old_position = gs.boxes[0].body.position; + gs.num_boxes = 1; + } if (enet_initialize() != 0) { @@ -72,8 +87,8 @@ void server(void *data) { case ENET_EVENT_TYPE_CONNECT: Log("A new client connected from %x:%u.\n", - event.peer->address.host, - event.peer->address.port); + event.peer->address.host, + event.peer->address.port); int64_t player_slot = -1; for (int i = 0; i < MAX_PLAYERS; i++) @@ -92,10 +107,10 @@ void server(void *data) else { event.peer->data = (void *)player_slot; - gs.players[player_slot] = (struct Player){ .body.position = (V2){ - .x = 0.0f, - .y = 1.0f*(float)player_slot, - }}; + gs.players[player_slot] = (struct Player){.body.position = (V2){ + .x = 0.0f, + .y = 1.0f * (float)player_slot, + }}; gs.players[player_slot].body.old_position = gs.players[player_slot].body.position; gs.players[player_slot].connected = true; }