1
1
Fork 0
Browse Source

minor

thread-physics
= 5 years ago
parent
commit
908610eb1c
  1. 3
      basic-lib.c
  2. 9
      draw.c
  3. 2
      draw.h
  4. 70
      game.c
  5. 6
      game.h
  6. 1
      main.c

3
basic-lib.c

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
#include <stdio.h>
#include <time.h>
#include <string.h>
@ -65,7 +64,7 @@ void join_strings(char * return_val, char * str_1, char * str_2) { @@ -65,7 +64,7 @@ void join_strings(char * return_val, char * str_1, char * str_2) {
while (*(str_1 + i) != '\0') {
combined[i] = *(str_1 + i);
i++;
}
}
while (*(str_2 + j) != '\0') {
combined[len1+j] = *(str_2 + j);

9
draw.c

@ -51,11 +51,11 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) { @@ -51,11 +51,11 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
/* draw the player as a coloured rect */
SDL_Rect player_rect;
player_rect.x = V.x - 5;
player_rect.y = V.y - 5;
player_rect.x = V.x -10;
player_rect.y = V.y -10;
player_rect.w = 10;
player_rect.h = 10;
player_rect.w = 20;
player_rect.h = 20;
if (red) {
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
@ -132,7 +132,6 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) { @@ -132,7 +132,6 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
for (int i = 1; i < body->collision_poly_size; i++) {
x_en = body->collision_poly[i].x -viewport_pos.x;
y_en = body->collision_poly[i].y -viewport_pos.y;
printf("colpoly: %d %d %d %d\n", x_st, y_st, x_en, y_en);
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;

2
draw.h

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

70
game.c

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "game.h"
player_st *glob_player;
void set_motor_newtons(Body *thing, int motorID, double x, double y);
void set_motor_max_velocity(Body *thing, int motorID, double max);
void get_new_physics(Body **phys);
// move the collision poly to the position of the player
@ -89,6 +89,9 @@ void get_new_physics(Body **phys) { @@ -89,6 +89,9 @@ void get_new_physics(Body **phys) {
memset(physics->motors, 0, sizeof(Motor) * 100);
physics->max_motors = 100;
physics->num_motors = 0;
// gravity
add_motor(physics, 0.0, 0.0);
// friction
add_motor(physics, 0.0, 0.0);
physics->updateCollisionPoly = default_update_collision_poly;
@ -221,9 +224,6 @@ Vect get_normal(Vect start, Vect end) { @@ -221,9 +224,6 @@ Vect get_normal(Vect start, Vect end) {
return norm;
}
// reference:
// http://www.dyn4j.org/2010/01/sat/#sat-inter
//
@ -246,26 +246,22 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -246,26 +246,22 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
one_position = vect_add(one_position, one->collision_poly[i]);
end = one->collision_poly[i];
axes[i-1] = get_normal(start, end);
printf(".%d\n", i-1);
start = end;
}
end = one->collision_poly[0];
axes[one->collision_poly_size - 1] = get_normal(start, end);
printf(".%d\n", one->collision_poly_size - 1);
start = two->collision_poly[0];
for (int i = 1; i < two->collision_poly_size; i++) {
two_position = vect_add(two_position, two->collision_poly[i]);
end = two->collision_poly[i];
axes[i - 1 + one->collision_poly_size] = get_normal(start, end);
printf(".%d\n", i-1 + one->collision_poly_size);
start = end;
}
end = two->collision_poly[0];
axes[two->collision_poly_size + one->collision_poly_size - 1] = get_normal(start, end);
printf(".%d\n",two->collision_poly_size + one->collision_poly_size - 1);
double *proj_one, *proj_two;
proj_one = proj_two = 0;
@ -280,13 +276,12 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -280,13 +276,12 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
if (proj_two) {
free(proj_two);
}
printf("Testing on axis: %f %f\n", axes[i].x, axes[i].y);
proj_one = project_col_poly(one, axes[i]);
proj_two = project_col_poly(two, axes[i]);
if ((proj_one[0] >= proj_two[1])
|| (proj_two[0] >= proj_one[1])) {
printf("not overlapping\n");
logwrite(INFO, "No collision");
free(axes);
free(proj_one);
free(proj_two);
@ -300,7 +295,7 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -300,7 +295,7 @@ 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])))) {
printf("COntained");
logwrite(INFO, "Contained collision");
double min = proj_one[0] - proj_two[0];
double max = proj_one[1] - proj_two[1];
@ -312,7 +307,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -312,7 +307,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
}
printf("OVERLAP : %e\n", overlap);
if (overlap < min_overlap && overlap > 0) {
min_overlap = overlap;
min_axis = axes[i];
@ -434,17 +428,17 @@ Wall *get_long_wall(int numNodes, int *nodes) { @@ -434,17 +428,17 @@ Wall *get_long_wall(int numNodes, int *nodes) {
return wallwall;
}
void accel_thing(Body * thing, float x, float y) {
void accel_thing(Body * thing, double x, double y) {
/* takes acceleration in m/s2 and converts to m/ms adding
* it to physics_thing
*/
// convert to m / millisecond
float x_adj = x / 1000.0;
float y_adj = y / 1000.0;
double x_adj = x / 1000.0;
double y_adj = y / 1000.0;
(*thing).acc.y += (float)y_adj;
(*thing).acc.x += (float)x_adj;
thing->acc.y += y_adj;
thing->acc.x += x_adj;
}
@ -489,10 +483,6 @@ void add_motor(Body *thing, double x, double y) { @@ -489,10 +483,6 @@ void add_motor(Body *thing, double x, double y) {
// basic collision handler for testing
//
// TODO: Maybe adding all the MTVs together for the body will make it work better,
// will this solve the problem of negative normals? noo.. idk. But anyway it is more
// efficient for processing many collisions because you only do the physics for an
// MTV once rather than for all of the collisions separately.
int process_collisions(Body *thing, Vect *trans) {
Vect translation;
translation.x = translation.y = 0;
@ -550,11 +540,11 @@ int process_collisions(Body *thing, Vect *trans) { @@ -550,11 +540,11 @@ int process_collisions(Body *thing, Vect *trans) {
void advance_thing(Body * thing) {
// TODO: fix ordering of collision detection + physics sim so that collisions
// are less bad.
// TODO: Implement broad phase and narrow phase, else massively speed up collisions somehow.
thing->acc.x = 0;
thing->acc.y = 0;
printf("Position: %f %f\n", thing->position.x, thing->position.y);
set_motor_newtons(thing, M_FRICTION, 0,0);
set_motor_max_velocity(thing, M_FRICTION, 0);
if (!thing->collision_poly[0].y) {
thing->updateCollisionPoly(thing);
@ -566,6 +556,7 @@ void advance_thing(Body * thing) { @@ -566,6 +556,7 @@ void advance_thing(Body * thing) {
thing->updateCollisionPoly(thing);
Vect translation;
Vect friction;
translation.x = translation.y = 0;
int numcols = 0;
if ((numcols = process_collisions(thing, &translation))) {
@ -589,12 +580,32 @@ void advance_thing(Body * thing) { @@ -589,12 +580,32 @@ void advance_thing(Body * thing) {
revert_vel.x = cos(vect_dir(translation)) * mag;
revert_vel.y = sin(vect_dir(translation)) * mag;
thing->vel.x -= revert_vel.x;
thing->vel.y -= revert_vel.y;
// accel
double norm = 9.81 * thing->obj_mass;
double fric_const = 0.52;
Vect x;
x.y = 0; x.x = 1;
if (vect_scalar_projection(thing->vel, translation) > 0) {
friction.x = translation.y;
friction.y = -translation.x;
} else {
friction.x = -translation.y;
friction.y = translation.x;
}
// force
friction.x = friction.x * norm * fric_const;
friction.y = friction.y * norm * fric_const;
printf("friction accel: %e %e\n", friction.x, friction.y);
printf("velocity: %e %e\n", thing->vel.x, thing->vel.y);
set_motor_newtons(thing, M_FRICTION, friction.x, friction.y);
set_motor_max_velocity(thing, M_FRICTION, vect_mag(project_vect(thing->vel, friction)));
// restitution force
Vect rest;
/*rest.x = 0;*/
/*rest.y = 0;*/
/*double impulse = thing->obj_mass * vect_mag(thing->vel) * thing->obj_elasticity;*/
@ -610,7 +621,6 @@ void advance_thing(Body * thing) { @@ -610,7 +621,6 @@ void advance_thing(Body * thing) {
thing->last_advance_time = SDL_GetTicks(); // in milliseconds
if (time_delta > 18) {
logwrite(ERROR, "Simulation too slow\n");
return;
}
// motors
@ -666,10 +676,10 @@ void advance_thing(Body * thing) { @@ -666,10 +676,10 @@ void advance_thing(Body * thing) {
/*}*/
if (fabs((*thing).vel.x) < 0.00001) {
if (fabsl((*thing).vel.x) < 0.001) {
(*thing).vel.x = 0;
}
if (fabs((*thing).vel.y) < 0.00001) {
if (fabsl((*thing).vel.y) < 0.001) {
(*thing).vel.y = 0;
}
@ -741,7 +751,7 @@ void add_to_world(world_thing thing) { @@ -741,7 +751,7 @@ void add_to_world(world_thing thing) {
}
void get_floor(void) {
int floorsize = 100;
int floorsize = 1000;
FloorPoly *polys = generate_floor_simple(floorsize);
Floor *floor = calloc(1, sizeof(Floor));
@ -796,7 +806,7 @@ void walk_player(int x, int y) { @@ -796,7 +806,7 @@ void walk_player(int x, int y) {
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 0, 100 * 10 * y);
return;
}
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 100 * x , 100 * y);
add_motor_newtons(glob_player->physics, M_PLAYER_WALK, 100 *10* x , 100 * y);
}
void step(int interval) {

6
game.h

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
@ -6,17 +5,16 @@ @@ -6,17 +5,16 @@
#include <math.h>
#include "vect.h"
#ifndef _DEFGAME
#define _DEFGAME
#include "garbo.h"
#include "draw.h"
enum motors {
M_GRAVITY = 0,
M_PLAYER_WALK = 1
M_FRICTION = 1,
M_PLAYER_WALK = 2
};
enum world_thing_kind {

1
main.c

@ -65,6 +65,7 @@ int main () { @@ -65,6 +65,7 @@ int main () {
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
queue_for_cleanup(win, WINDOW);
queue_for_cleanup(ren, RENDERER);

Loading…
Cancel
Save