1
1
Fork 0
Browse Source

partial collision on scenev2

thread-physics
alistair 4 years ago
parent
commit
158387656b
  1. 7
      draw.c
  2. 2
      draw.h
  3. 82
      game.c

7
draw.c

@ -428,6 +428,13 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -428,6 +428,13 @@ void redraw_buffer(SDL_Renderer * ren) {
draw_collision_poly(ren, thing.floor->polys[i].physics);
}
break;
case ROOM_W:
for (int i = 0; i < thing.room->ceil.numItems; i++) {
draw_collision_poly(ren, thing.room->ceil.items[i]);
}
for (int i = 0; i < thing.room->floor.numItems; i++) {
draw_collision_poly(ren, thing.room->floor.items[i]);
}
}
}

2
draw.h

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
#ifndef _DEFDRAW
#define _DEFDRAW
#define SHOWCOLLISION 0
#define SHOWCOLLISION 1
#define SHOWFORCES 0
#include "garbo.h"

82
game.c

@ -515,8 +515,9 @@ int get_floor_ceiling() { @@ -515,8 +515,9 @@ int get_floor_ceiling() {
struct physics_collection floor;
struct physics_collection ceil;
floor.items = calloc(e.floor.size, sizeof(Body*));
ceil.items = calloc(e.floor.size, sizeof(Body*));
floor.items = calloc(e.floor.size - 1, sizeof(Body*));
ceil.items = calloc(e.ceil.size - 1, sizeof(Body*));
floor.numItems = e.floor.size - 1;
ceil.numItems = e.floor.size - 1;
@ -538,14 +539,17 @@ int get_floor_ceiling() { @@ -538,14 +539,17 @@ int get_floor_ceiling() {
Vect fthis = fseg->position;
Vect cthis = cseg->position;
Vect fnext = *(Vect *)arlst_get(&e.floor, i);
Vect cnext= *(Vect *)arlst_get(&e.ceil, i);
Vect fnext = *(Vect *)arlst_get(&e.floor, i + 1);
Vect cnext= *(Vect *)arlst_get(&e.ceil, i + 1);
double frise = fnext.y - fthis.y;
double frun = fnext.x - fthis.y;
double frun = fnext.x - fthis.x;
double crise = cnext.y - cthis.y;
double crun = cnext.x - cthis.y;
double crun = cnext.x - cthis.x;
double fdepth = fabs(frise) * 2 + 100;
double cdepth = -fabs(crise) * 2 + 100;
fseg->collision_shape[0].x = 0;
fseg->collision_shape[0].y = 0;
@ -554,10 +558,10 @@ int get_floor_ceiling() { @@ -554,10 +558,10 @@ int get_floor_ceiling() {
fseg->collision_shape[1].y = frise;
fseg->collision_shape[2].x = frun;
fseg->collision_shape[2].y = frise + FLOOR_THICKNESS;
fseg->collision_shape[2].y = frise + fdepth;
fseg->collision_shape[3].x = 0;
fseg->collision_shape[3].y = frise + FLOOR_THICKNESS;
fseg->collision_shape[3].y = frise + cdepth;
cseg->collision_shape[0].x = 0;
cseg->collision_shape[0].y = 0;
@ -566,15 +570,18 @@ int get_floor_ceiling() { @@ -566,15 +570,18 @@ int get_floor_ceiling() {
cseg->collision_shape[1].y = crise;
cseg->collision_shape[2].x = crun;
cseg->collision_shape[2].y = crise - FLOOR_THICKNESS;
cseg->collision_shape[2].y = crise + fdepth;
cseg->collision_shape[3].x = 0;
cseg->collision_shape[3].y = crise - FLOOR_THICKNESS;
cseg->collision_shape[3].y = crise + fdepth;
default_update_collision_poly(cseg);
default_update_collision_poly(fseg);
}
world_thing room_world;
room_world.kind = ROOM_W;
struct room* room = malloc(sizeof(struct room));
room->ceil = ceil;
room->floor = floor;
@ -789,6 +796,7 @@ int process_collisions(Body *thing, Vect *trans) { @@ -789,6 +796,7 @@ int process_collisions(Body *thing, Vect *trans) {
Vect translation;
translation.x = translation.y = 0;
Vect temptrans;
temptrans.x = temptrans.y = 0;
int num_cols = 0;
int num = 0;
@ -820,6 +828,40 @@ int process_collisions(Body *thing, Vect *trans) { @@ -820,6 +828,40 @@ int process_collisions(Body *thing, Vect *trans) {
translation = vect_add(translation, temptrans);
}
}
} else if (world.things[k].kind == ROOM_W) {
for (int i = 0; i < world.things[k].room->floor.numItems; i++) {
Body *s = world.things[k].room->floor.items[i];
if (s->position.x + E_ROOM_RES < viewport_pos.x) {
continue;
} else if (s->position.x > viewport_pos.x + width) {
continue;
} else {
num++;
}
if (check_collision(s, thing, &temptrans)) {
num_cols++;
thing->colliding = true;
translation = vect_add(translation, temptrans);
}
}
for (int i = 0; i < world.things[k].room->ceil.numItems; i++) {
Body *s = world.things[k].room->ceil.items[i];
if (s->position.x + E_ROOM_RES < viewport_pos.x) {
continue;
} else if (s->position.x > viewport_pos.x + width) {
continue;
} else {
num++;
}
if (check_collision(s, thing, &temptrans)) {
num_cols++;
thing->colliding = true;
translation = vect_add(translation, temptrans);
}
}
}
}
@ -849,11 +891,6 @@ void advance_thing(Body * thing) { @@ -849,11 +891,6 @@ void advance_thing(Body * thing) {
return;
}
if (!thing->collision_poly[0].y) {
thing->updateCollisionPoly(thing);
}
thing->updateCollisionPoly(thing);
Vect translation;
Vect friction;
translation.x = translation.y = 0;
@ -868,11 +905,16 @@ void advance_thing(Body * thing) { @@ -868,11 +905,16 @@ void advance_thing(Body * thing) {
}
double time_delta = now.tv_nsec - thing->last_advance_time.tv_nsec;
time_delta *= 0.5; // run at half speed for accuracy
if (now.tv_nsec < thing->last_advance_time.tv_nsec) {
time_delta = now.tv_nsec - (thing->last_advance_time.tv_nsec - 1000000000);
}
time_delta *= 0.000001; // convert to ms from ns
thing->last_advance_time = now;
printf("delta: %f\n", time_delta);
if (time_delta > 19) {
printf("ERROR too slow: %f\n", time_delta);
@ -1060,11 +1102,12 @@ void advance_thing(Body * thing) { @@ -1060,11 +1102,12 @@ void advance_thing(Body * thing) {
thing->next_position.x += (thing->vel.x * 1/2 * (double)time_delta);
thing->next_position.y += (thing->vel.y * 1/2 * (double)time_delta);
thing->position = thing->next_position;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
if (now.tv_nsec >= 1000000000) {
now.tv_nsec -= 1000000000;
if (!thing->collision_poly[0].y) {
thing->updateCollisionPoly(thing);
}
thing->last_advance_time = now;
thing->updateCollisionPoly(thing);
}
@ -1303,6 +1346,7 @@ void handle_input_event(SDL_Event event) { @@ -1303,6 +1346,7 @@ void handle_input_event(SDL_Event event) {
}
void step(int interval) {
if (player.physics->position.x > world.uniques_index[ROOM_W]->room
->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) {
next_level();

Loading…
Cancel
Save