1
1
Fork 0
Browse Source

fix swing rope

thread-physics
alistair 4 years ago
parent
commit
7cde63305a
  1. 1
      draw.c
  2. 110
      game.c
  3. 2
      main.c

1
draw.c

@ -45,7 +45,6 @@ Vect in_view(Vect V) { @@ -45,7 +45,6 @@ Vect in_view(Vect V) {
/* Game Specific rendering functions */
void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
logwrite(DEBUG, "Drawing player\n");
// translate to viewport
Vect V;

110
game.c

@ -2,10 +2,12 @@ @@ -2,10 +2,12 @@
#include "environment.h"
#define FLOOR_THICKNESS 200
#define BREAKPOINT *(int *)0 = 1;
#define MAX_ROPE_GRAB_LEN 800
GlobWorld world;
player_st *glob_player;
SDL_Renderer *debug_ren;
int level;
@ -776,48 +778,96 @@ void stop_pull_rope(void) { @@ -776,48 +778,96 @@ void stop_pull_rope(void) {
}
void add_rope(int mouse_x) {
void add_rope(int mouse_x, int mouse_y) {
if (player.physics->strings[0].attached) {
return;
}
Vect mouse;
mouse.x = mouse_x;
mouse.y = mouse_y;
mouse = vect_add(mouse, viewport_pos);
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.x = 0;
end.y = 0;
if (mouse_x != 0) {
end.x = mouse_x + viewport_pos.x;
}
struct room *room = world.uniques_index[ROOM_W]->room;
struct physics_collection floor = room->floor;
struct physics_collection ceil = room->floor;
struct physics_collection ceil = room->ceil;
int y = 0;
for (int i = 0; i < floor.numItems; i++) {
if (floor.items[i]->collision_poly[0].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;
Vect ray_e = player.physics->position;
Vect ray_s = mouse;
//double delt = ceiling->polys[i].right.x - end.x;
//interpolate_h(left, right)
// end.y = ceiling->polys[i].right.y - (int)(m * delt);
double x1 = ray_s.x;
double y1 = ray_s.y;
break;
double x2 = ray_e.x;
double y2 = ray_e.y;
int i;
struct physics_collection s;
bool found = false;
double len = MAX_ROPE_GRAB_LEN;
for (int j = 0; j < (floor.numItems + ceil.numItems - 2); j++) {
if (j >= floor.numItems - 1) {
s = ceil;
i = j - floor.numItems + 1;
} else {
s = floor;
i = j;
}
Vect wall_s = s.items[i]->position;
Vect wall_e = s.items[i+1]->position;
Vect intersect;
double x3 = wall_s.x;
double y3 = wall_s.y;
double x4 = wall_e.x;
double y4 = wall_e.y;
double t = ((x1 - x3)*(y3 - y4) - (y1 - y3)*(x3-x4))
/ ((x1-x2)*(y3-y4) - (y1 - y2)*(x3-x4));
double u = ((x1 - x2)*(y1 - y3) - (y1 - y2)*(x1 - x3))
/ ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
if (t < 0 || u > 0 || t > 1 || u < -1) {
continue;
} else {
intersect.x = ((x1*y2 - y1*x2)*(x3 - x4) -(x1 - x2)*(x3*y4 - y3*x4))
/ ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
intersect.y = ((x1*y2 - y1*x2)*(y3 - y4) - (y1 - y2) * (x3*y4 - y3 * x4))
/ ((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
Vect rope = vect_add(intersect, vect_scalar(start, -1));
double mag = vect_mag(rope);
if (mag < len) {
found = true;
end = intersect;
}
}
}
Vect rop = vect_add(end, vect_scalar(start, -1));
if (found) {
player.physics->strings[0].max_length = vect_mag(rop);
SDL_SetRenderDrawColor(debug_ren, 200, 255, 30, 255);
SDL_RenderDrawLine(debug_ren, start.x - viewport_pos.x,start.y-viewport_pos.y,
end.x - viewport_pos.x, end.y -viewport_pos.y);
player.physics->strings[0].set_end_point(player.physics->strings, &end);
player.physics->strings[0].attached = true;
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;
}
}
void delete_rope(void) {
@ -974,7 +1024,7 @@ void advance_thing(Body * thing) { @@ -974,7 +1024,7 @@ void advance_thing(Body * thing) {
thing->last_advance_time = now;
// cap the time delta: simulatino will slow down
printf("delta: %f\n", time_delta);
// printf("delta: %f\n", time_delta);
if (time_delta > 19) {
printf("ERROR too slow: %f\n", time_delta);
time_delta = 19;
@ -1170,7 +1220,6 @@ void advance_thing(Body * thing) { @@ -1170,7 +1220,6 @@ void advance_thing(Body * thing) {
}
/*void destroy_projectile(Projectile** proj) {*/
/*for (int i = 0; i < things_in_world; i ++) {*/
/*if (world[i].projectile == *proj) {*/
@ -1315,9 +1364,10 @@ GlobWorld create_world() { @@ -1315,9 +1364,10 @@ GlobWorld create_world() {
}
void startgame(SDL_Renderer * ren) {
logwrite(INFO, "STARTGAME");
get_input_map();
debug_ren = ren;
level = 2;
world = create_world();
@ -1363,9 +1413,7 @@ void handle_input_event(SDL_Event event) { @@ -1363,9 +1413,7 @@ void handle_input_event(SDL_Event event) {
SDL_Scancode sc = event.key.keysym.scancode;
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.scancode == input_map.player_rope) {
add_rope(0);
} if (sc == input_map.player_up) {
if (sc == input_map.player_up) {
walk_player(0, -1);
} if ( sc == input_map.player_left) {
walk_player(-1, 0);
@ -1391,7 +1439,7 @@ void handle_input_event(SDL_Event event) { @@ -1391,7 +1439,7 @@ void handle_input_event(SDL_Event event) {
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
add_rope(event.button.x);
add_rope(event.button.x, event.button.y);
if (event.button.button == SDL_BUTTON_RIGHT)
pull_rope(900);
break;

2
main.c

@ -113,8 +113,8 @@ int game(void) { @@ -113,8 +113,8 @@ int game(void) {
}
}
/* Redraw Screen */
redraw(ren);
step(10);
redraw(ren);
}
}

Loading…
Cancel
Save