1
1
Fork 0
Browse Source

Can now add swing ropes

thread-physics
alistair 4 years ago
parent
commit
132d5629bc
  1. 7
      Makefile
  2. 13
      controlscheme.c
  3. 24
      controlscheme.h
  4. 85
      game.c
  5. 4
      game.h
  6. 10
      main.c

7
Makefile

@ -10,8 +10,8 @@ run: all @@ -10,8 +10,8 @@ run: all
all: $(EXE)
$(EXE): main.o vect.o logger.o game.o garbo.o draw.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o vect.o game.o logger.o draw.o garbo.o
$(EXE): main.o vect.o logger.o game.o garbo.o draw.o controlscheme.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o vect.o game.o logger.o draw.o garbo.o controlscheme.o
main.o: main.c logger.h game.h garbo.h draw.h
$(CXX) $(CXXFLAGS) -c main.c
@ -31,6 +31,9 @@ draw.o: draw.c draw.h @@ -31,6 +31,9 @@ draw.o: draw.c draw.h
vect.o: vect.c vect.h
$(CXX) $(CXXFLAGS) -c vect.c
controlscheme.o: controlscheme.c controlscheme.h
$(CXX) $(CXXFLAGS) -c controlscheme.c
clean:
rm *.o && rm $(EXE)

13
controlscheme.c

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
#include "controlscheme.h"
struct InputMap input_map;
void get_input_map(void) {
input_map.player_down = SDL_SCANCODE_S;
input_map.player_up = SDL_SCANCODE_W;
input_map.player_right = SDL_SCANCODE_D;
input_map.player_left = SDL_SCANCODE_A;
input_map.player_rope = SDL_SCANCODE_E;
}

24
controlscheme.h

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
#include <SDL2/SDL.h>
enum Game_Interaction {
PLAYER_UP,
PLAYER_DOWN,
PLAYER_RIGHT,
PLAYER_LEFT
};
struct InputMap {
SDL_Scancode player_up;
SDL_Scancode player_down;
SDL_Scancode player_left;
SDL_Scancode player_right;
SDL_Scancode player_rope;
};
extern struct InputMap input_map;
void get_input_map(void);

85
game.c

@ -162,8 +162,8 @@ int string_update_fixed(String *string) { @@ -162,8 +162,8 @@ int string_update_fixed(String *string) {
return 0;
}
int string_set_end(String *string, void *setting) {
string->end_point = *(Vect *)setting;
int string_set_end(String *string, Vect *setting) {
string->end_point = *setting;
return 0;
}
@ -218,12 +218,6 @@ player_st get_player(int x, int y) { @@ -218,12 +218,6 @@ player_st get_player(int x, int y) {
player.physics->strings = get_fixed_strings(1);
player.physics->strings->max_length = 210;
// testing
Vect end_pt;
end_pt.x = 2000;
end_pt.y = 3000;
player.physics->strings[0].set_end_point(player.physics->strings, &end_pt);
player.physics->strings[0].attached = true;
Vect *rect = player.physics->collision_shape;
rect[0].x = -10; rect[0].y = -10;
@ -578,6 +572,23 @@ void add_motor(Body *thing, double x, double y) { @@ -578,6 +572,23 @@ void add_motor(Body *thing, double x, double y) {
thing->num_motors += 1;
}
void add_rope(void) {
// testing
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);
player.physics->strings[0].attached = true;
}
void delete_rope(void) {
// testing
player.physics->strings[0].attached = false;
}
// basic collision handler for testing
//
int process_collisions(Body *thing, Vect *trans) {
@ -656,6 +667,8 @@ void advance_thing(Body * thing) { @@ -656,6 +667,8 @@ void advance_thing(Body * thing) {
Vect friction;
translation.x = translation.y = 0;
int numcols = 0;
// collisions
if ((numcols = process_collisions(thing, &translation))) {
double mag = vect_mag(translation);
@ -711,6 +724,7 @@ void advance_thing(Body * thing) { @@ -711,6 +724,7 @@ void advance_thing(Body * thing) {
/*accel_thing(thing, rest.x, rest.y);*/
}
// pendulums
for (int i = 0; i < thing->num_strings; i++) {
if (!thing->strings[i].attached) {
continue;
@ -719,6 +733,7 @@ void advance_thing(Body * thing) { @@ -719,6 +733,7 @@ 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);
if (st_len > max_len) {
Vect string_end = thing->strings[i].end_point;
Vect thing_position = thing->next_position;
@ -735,17 +750,15 @@ void advance_thing(Body * thing) { @@ -735,17 +750,15 @@ void advance_thing(Body * thing) {
thing->next_position.x += cos(angle) * disp;
thing->next_position.y += sin(angle) * disp;
Vect corrected_string;
corrected_string.x = cos(angle) * max_len;
corrected_string.y = sin(angle) * max_len;
/*Vect corrected_string;*/
/*corrected_string.x = cos(angle) * max_len;*/
/*corrected_string.y = sin(angle) * max_len;*/
// set velocity to 0 in direction of string
double corr_mag = vect_scalar_projection(thing->vel, string);
thing->vel.x -= corr_mag * cos(angle);
thing->vel.y -= corr_mag * sin(angle);
}
}
@ -783,8 +796,6 @@ void advance_thing(Body * thing) { @@ -783,8 +796,6 @@ void advance_thing(Body * thing) {
double acc_y = thing->motors[i].y / thing->obj_mass;
accel_thing(thing, acc_x, acc_y);
}
// printf("\n diff angle: %f pi: %f \n", dirV - dirF, M_PI);
// printf("Vel: %f, Force: %f\n\n", dirV, dirF);
}
// accelerate based on accel
@ -824,31 +835,6 @@ void advance_thing(Body * thing) { @@ -824,31 +835,6 @@ 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);
// revert if this caused collision
/*if (process_collisions(thing)) {*/
/*thing->x_pos = oldx;*/
/*thing->y_pos = oldy;*/
/*thing->updateCollisionPoly(thing);*/
/*return;*/
/*}*/
// wrap screen
/*if (thing->x_pos > width) {*/
/*thing->x_pos = 0;*/
/*}*/
/*if (thing->y_pos > height) {*/
/*thing->y_pos = 0;*/
/*}*/
/*if (thing->y_pos < 0) {*/
/*thing->y_pos = height;*/
/*}*/
/*if (thing->x_pos < 0) {*/
/*thing->x_pos = width;*/
/*}*/
}
@ -967,13 +953,14 @@ void get_room(void) { @@ -967,13 +953,14 @@ void get_room(void) {
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);
SDL_GetRendererOutputSize(ren, &width, &height);
player = get_player(2000,3000);
player = get_player(200,300);
world_thing player_world;
@ -1008,6 +995,22 @@ void walk_player(int x, int y) { @@ -1008,6 +995,22 @@ void walk_player(int x, int y) {
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 100 *10* x , 100 * y);
}
void handle_input_event(SDL_Event event) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.scancode == input_map.player_rope) {
add_rope();
printf("add rope\n");
}
break;
case SDL_KEYUP:
if (event.key.keysym.scancode == input_map.player_rope) {
delete_rope();
}
break;
}
}
void step(int interval) {
const uint8_t * keyboard;
keyboard = SDL_GetKeyboardState(NULL);

4
game.h

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#include "vect.h"
#include "garbo.h"
#include "draw.h"
#include "controlscheme.h"
enum motors {
M_GRAVITY = 0,
@ -55,7 +56,7 @@ struct String { @@ -55,7 +56,7 @@ struct String {
int (*update_end_point)(struct String*); // method to update the end pt
// say if string is attached
// to another object
int (*set_end_point)(struct String*, void *); // manually set the end point of
int (*set_end_point)(struct String*, Vect *); // manually set the end point of
// string
};
@ -163,6 +164,7 @@ typedef struct { @@ -163,6 +164,7 @@ typedef struct {
};
} world_thing;
void handle_input_event(SDL_Event event);
// add a motor to the world
void add_motor(Body *thing, double x, double y);

10
main.c

@ -9,9 +9,9 @@ @@ -9,9 +9,9 @@
#include <pthread.h>
#include "logger.h"
#include "game.h"
#include "draw.h"
#include "garbo.h"
#include "game.h"
#include "draw.h"
#include "garbo.h"
const int screen_width = 800;
const int screen_height = 600;
@ -93,6 +93,10 @@ int main () { @@ -93,6 +93,10 @@ int main () {
case SDL_QUIT:
close = 1;
break;
default:
handle_input_event (event);
break;
}
}

Loading…
Cancel
Save