1
1
Fork 0
Browse Source

rope now attaches to ceiling

thread-physics
alistair 4 years ago
parent
commit
c0576a2379
  1. 7
      draw.c
  2. 134
      game.c
  3. 23
      game.h
  4. 2
      main.c

7
draw.c

@ -281,10 +281,10 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -281,10 +281,10 @@ void redraw_buffer(SDL_Renderer * ren) {
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y);
for (int i=0; i<things_in_world; i++) {
for (int i=0; i<world.size; i++) {
world_thing thing;
thing = world[i];
printf("draw thing: %d\n, no %d of %d\n", thing.kind, thing.nid, things_in_world);
thing = world.things[i];
printf("draw thing: %d\n, no %d of %d\n", thing.kind, thing.nid, world.size);
switch (thing.kind) {
case PLAYER:
@ -304,7 +304,6 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -304,7 +304,6 @@ void redraw_buffer(SDL_Renderer * ren) {
break;
case CEILING:
for (int i = 0; i < thing.floor->numPolys; i++) {
// draw_floor(ren, &thing.floor->polys[i], true);
draw_floor(ren, &thing.floor->polys[i], false);
draw_collision_poly(ren, thing.floor->polys[i].physics);
}

134
game.c

@ -1,13 +1,12 @@ @@ -1,13 +1,12 @@
#include "game.h"
#define FLOOR_THICKNESS 200
GlobWorld world;
player_st *glob_player;
/* array of all the things in the world and their kinds */
world_thing *world;
int things_in_world;
int world_size;
//world_thing *world;
void startgame(SDL_Renderer * ren) ;
@ -72,7 +71,6 @@ void default_motor_curve(Motor *motor) { @@ -72,7 +71,6 @@ void default_motor_curve(Motor *motor) {
return;
}
FloorPoly* generate_floor_simple(int num_polys, bool extend_down, int st_height) {
FloorPoly *floor = calloc(num_polys, sizeof(FloorPoly));
Vect last, next;
@ -376,7 +374,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -376,7 +374,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
if ((proj_one[0] >= proj_two[1])
|| (proj_two[0] >= proj_one[1])) {
logwrite(INFO, "No collision");
free(axes);
free(proj_one);
free(proj_two);
@ -390,7 +387,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -390,7 +387,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
// one of the shapes is contained
if ((overlap > (proj_one[1] - proj_one[0])
|| (overlap > (proj_two[1] - proj_two[0])))) {
logwrite(INFO, "Contained collision");
double min = proj_one[0] - proj_two[0];
double max = proj_one[1] - proj_two[1];
@ -647,10 +643,37 @@ void add_rope(void) { @@ -647,10 +643,37 @@ void add_rope(void) {
if (player.physics->strings[0].attached) {
return;
}
Vect end_pt;
end_pt.x = player.physics->position.x + 100;
end_pt.y = player.physics->position.y - 100;
player.physics->strings[0].set_end_point(player.physics->strings, &end_pt);
Vect start = player.physics->position;
Vect end;
end.x = start.x + 300 * player.physics->vel.x;
if (end.x - start.x < 30) {
end.x = start.x + 90;
}
end.y = 0;
Floor *ceiling = world.uniques_index[CEILING]->floor;
int y = 0;
for (int i = 0; i < ceiling->numPolys; i++) {
if (ceiling->polys[i].right.x > end.x) {
double width = ceiling->polys[i].right.x - ceiling->polys[i].left.x;
double m = (double)(ceiling->polys[i].right.y - ceiling->polys[i].left.y)
/ width;
double delt = ceiling->polys[i].right.x - end.x;
end.y = ceiling->polys[i].right.y - (int)(m * delt);
printf("ROPEJASHD: %f, %f\n", end.y, delt);
break;
}
}
Vect rop = vect_add(end, vect_scalar(start, -1));
player.physics->strings[0].max_length = vect_mag(rop);
player.physics->strings[0].set_end_point(player.physics->strings, &end);
player.physics->strings[0].attached = true;
}
@ -667,29 +690,29 @@ int process_collisions(Body *thing, Vect *trans) { @@ -667,29 +690,29 @@ int process_collisions(Body *thing, Vect *trans) {
int num_cols = 0;
int num = 0;
for (int k = 0; k < things_in_world; k++) {
if (world[k].kind == STATIC_WALL_W
&& world[k].wall->physics->uid != thing->uid) {
if ((world[k].wall->physics->position.x - viewport_pos.x) > (width * 2)
|| world[k].wall->physics->position.x - viewport_pos.x < 0) {
for (int k = 0; k < world.size; k++) {
if (world.things[k].kind == STATIC_WALL_W
&& world.things[k].wall->physics->uid != thing->uid) {
if ((world.things[k].wall->physics->position.x - viewport_pos.x) > (width * 2)
|| world.things[k].wall->physics->position.x - viewport_pos.x < 0) {
continue;
} else {
num++;
}
if (check_collision(world[k].wall->physics, thing, &temptrans)) {
if (check_collision(world.things[k].wall->physics, thing, &temptrans)) {
num++;
thing->colliding = true;
translation = vect_add(translation, temptrans);
}
} else if (world[k].kind == FLOOR || world[k].kind == CEILING) {
for (int i = 0; i < world[k].floor->numPolys; i++) {
if ((world[k].floor->polys[i].physics->position.x - viewport_pos.x) > (2 *width)
|| (world[k].floor->polys[i].physics->position.x - viewport_pos.x) < -width) {
} else if (world.things[k].kind == FLOOR || world.things[k].kind == CEILING) {
for (int i = 0; i < world.things[k].floor->numPolys; i++) {
if ((world.things[k].floor->polys[i].physics->position.x - viewport_pos.x) > (2 *width)
|| (world.things[k].floor->polys[i].physics->position.x - viewport_pos.x) < -width) {
continue;
} else {
num++;
}
if (check_collision(world[k].floor->polys[i].physics, thing, &temptrans)) {
if (check_collision(world.things[k].floor->polys[i].physics, thing, &temptrans)) {
num_cols++;
thing->colliding = true;
translation = vect_add(translation, temptrans);
@ -802,7 +825,8 @@ void advance_thing(Body * thing) { @@ -802,7 +825,8 @@ void advance_thing(Body * thing) {
double st_len = vect_distance(thing->next_position, thing->strings[i].end_point);
double max_len = thing->strings[i].max_length;
printf("STRING len: %f, %f\n", st_len, max_len);
printf("strendpoint: %f:%f\n\n", thing->strings[i].end_point.x, thing->strings[i].end_point.y);
printf("strendpoint: %f:%f\n\n", thing->strings[i].end_point.x,
thing->strings[i].end_point.y);
if (st_len > max_len) {
Vect string_end = thing->strings[i].end_point;
Vect thing_position = thing->next_position;
@ -936,47 +960,61 @@ void get_projectile(Projectile** proj) { @@ -936,47 +960,61 @@ void get_projectile(Projectile** proj) {
void advance_things(void) {
int numcols;
Vect translation;
for (int i = 0; i < things_in_world; i++) {
switch (world[i].kind) {
for (int i = 0; i < world.size; i++) {
switch (world.things[i].kind) {
case PLAYER_W :
logwrite(INFO, "ADVANCE PLAYER\n");
advance_thing((world[i].player->physics));
advance_thing((world.things[i].player->physics));
break;
case STATIC_WALL_W:
logwrite(INFO, "ADVANCE WALL\n");
advance_thing(world[i].wall->physics);
advance_thing(world.things[i].wall->physics);
break;
case FLOOR:
for (int k = 0; k < world[i].floor->numPolys; k++) {
advance_thing(world[i].floor->polys[k].physics);
for (int k = 0; k < world.things[i].floor->numPolys; k++) {
advance_thing(world.things[i].floor->polys[k].physics);
}
break;
case CEILING:
for (int k = 0; k < world[i].floor->numPolys; k++) {
advance_thing(world[i].floor->polys[k].physics);
for (int k = 0; k < world.things[i].floor->numPolys; k++) {
advance_thing(world.things[i].floor->polys[k].physics);
}
break;
case PROJECTILE:
if ((numcols = process_collisions(world[i].projectile->physics,
if ((numcols = process_collisions(world.things[i].projectile->physics,
&translation))) {
world[i].projectile->on_collision(world[i].projectile);
world.things[i].projectile->on_collision(world.things[i].projectile);
}
world[i].projectile->on_step(world[i].projectile);
world.things[i].projectile->on_step(world.things[i].projectile);
}
}
}
bool unique_world_kind(enum world_thing_kind k) {
switch (k) {
case PLAYER_W:
case FLOOR:
case CEILING:
return true;
default:
return false;
}
}
// grow array of world things if needed
void add_to_world(world_thing thing) {
if (things_in_world == world_size) {
if (world.size == world.capacity) {
logwrite(INFO, "Increased world size.");
world = realloc(world, sizeof(world_thing) * (world_size*=2));
world.things = realloc(world.things, sizeof(world_thing) * (world.size*=2));
if (unique_world_kind(thing.kind)) {
}
}
thing.nid = things_in_world;
memcpy(world + things_in_world, &thing, sizeof(world_thing));
thing.nid = world.size;
memcpy(world.things + world.size, &thing, sizeof(world_thing));
world.uniques_index[thing.kind] = world.things + world.size;
things_in_world += 1;
world.size++;
//printf("Added to world: %d\n", thing.kind);
}
@ -1020,13 +1058,23 @@ void get_room(void) { @@ -1020,13 +1058,23 @@ void get_room(void) {
add_to_world(floorthing);
}
GlobWorld create_world() {
GlobWorld c;
c.things = malloc(sizeof(world_thing) * 100);
memset(c.things, 0, sizeof(world_thing) * 100);
c.capacity = 100;
c.size = 0;
c.uniques_index = calloc(3, sizeof(world_thing*));
return c;
}
void startgame(SDL_Renderer * ren) {
logwrite(INFO, "STARTGAME");
get_input_map();
things_in_world = 0;
world_size = 100;
world = malloc(sizeof(world_thing) * 100);
memset(world, 0, sizeof(world_thing) * 100);
world = create_world();
SDL_GetRendererOutputSize(ren, &width, &height);
player = get_player(200,300);

23
game.h

@ -20,13 +20,14 @@ enum motors { @@ -20,13 +20,14 @@ enum motors {
};
enum world_thing_kind {
PLAYER_W,
PLAYER_W = 0,
FLOOR = 1,
CEILING = 2,
STATIC_WALL_W,
FLOOR,
CEILING,
PROJECTILE
};
// used to exert a force on an object
typedef struct motorstruct {
double x; // positive is right
@ -169,16 +170,24 @@ typedef struct { @@ -169,16 +170,24 @@ typedef struct {
};
} world_thing;
struct world {
world_thing * things;
int size;
int capacity;
int modified;
world_thing** uniques_index;
};
typedef struct world GlobWorld;
extern GlobWorld world;
void handle_input_event(SDL_Event event);
// add a motor to the world
void add_motor(Body *thing, double x, double y);
/* array of all the things in the world and their kinds */
extern world_thing *world;
extern int things_in_world;
extern int world_size;
extern void startgame(SDL_Renderer * ren) ;
extern void process_keydown(SDL_Keysym key);
extern void process_keyup(SDL_Keysym key);

2
main.c

@ -112,7 +112,7 @@ int main () { @@ -112,7 +112,7 @@ int main () {
}
}
empty_cleanup_queue();
//empty_cleanup_queue();
logwrite(DEBUG, "Emptied cleanup queue\n");
return 0;
}

Loading…
Cancel
Save