Much better physics but still no rotation

main
Cameron Murphy Reikes 2 years ago
parent 23c2e18a70
commit b967bc45ec

@ -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));
}
}
}

@ -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

@ -15,6 +15,9 @@ void server(void *data)
struct GameState gs = {0};
// two boxes stacked on top
if (false)
{
gs.boxes[0] = (struct Box){
.body = (struct Body){
.position = (P2){.x = 0.75f, .y = 0.0}},
@ -26,6 +29,18 @@ void server(void *data)
};
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)
{
@ -92,9 +107,9 @@ void server(void *data)
else
{
event.peer->data = (void *)player_slot;
gs.players[player_slot] = (struct Player){ .body.position = (V2){
gs.players[player_slot] = (struct Player){.body.position = (V2){
.x = 0.0f,
.y = 1.0f*(float)player_slot,
.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;

Loading…
Cancel
Save