1
1
Fork 0
Browse Source

Demo of swinging string

thread-physics
= 4 years ago
parent
commit
5a5c91ce35
  1. 13
      draw.c
  2. 2
      draw.h
  3. 103
      game.c
  4. 23
      game.h
  5. 9
      vect.c
  6. 2
      vect.h

13
draw.c

@ -119,6 +119,19 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) { @@ -119,6 +119,19 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
SDL_SetRenderDrawColor(ren, 0, 255, 0, 255);
}
// draw strings
for (int i = 0; i < body->num_strings; i++) {
if (!body->strings[i].attached) {
continue;
}
printf("Draw string");
Vect B;
B = in_view(body->position);
Vect E;
E = in_view(body->strings[i].end_point);
SDL_RenderDrawLine(ren, B.x, B.y, E.x, E.y);
}
if (body->collision_poly_size == 1) {
SDL_Rect dot;
Vect V;

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"

103
game.c

@ -146,6 +146,31 @@ void get_new_physics(Body **phys) { @@ -146,6 +146,31 @@ void get_new_physics(Body **phys) {
/*physics->collision_poly[0].y = physics->y_pos;*/
/*}*/
int string_update_fixed(String *string) {
return 0;
}
int string_set_end(String *string, void *setting) {
string->end_point = *(Vect *)setting;
return 0;
}
String *get_fixed_strings(int number) {
String *strings = calloc(number, sizeof(String));
for (int i = 0; i < number; i++) {
strings[i].attached = false;
// takes self (String) and does nothing.
strings[i].update_end_point = string_update_fixed;
// takes self and vect to set string end point
strings[i].set_end_point = string_set_end;
}
return strings;
}
player_st get_player(int x, int y) {
/* creates player at given postion and zeroes physics */
@ -176,6 +201,19 @@ player_st get_player(int x, int y) { @@ -176,6 +201,19 @@ player_st get_player(int x, int y) {
player.physics->updateCollisionPoly = cast_update_collision_poly;
// swing rope
player.physics->num_strings = 1;
player.physics->max_strings = 1;
player.physics->strings = get_fixed_strings(1);
player.physics->strings->max_length = 890;
// testing
Vect end_pt;
end_pt.x = -100;
end_pt.y = 0;
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;
rect[1].x = 10; rect[1].y = -10;
@ -183,7 +221,8 @@ player_st get_player(int x, int y) { @@ -183,7 +221,8 @@ player_st get_player(int x, int y) {
rect[3].x = -10; rect[3].y = 10;
// gravity
set_motor_newtons(player.physics, M_GRAVITY, 0.0, player.physics->obj_mass * 4);
set_motor_newtons(player.physics, M_GRAVITY, 0.0,
player.physics->obj_mass * 4);
// walking motor
add_motor(player.physics, 0.0, player.physics->obj_mass * 9.81);
@ -602,7 +641,6 @@ void advance_thing(Body * thing) { @@ -602,7 +641,6 @@ void advance_thing(Body * thing) {
thing->updateCollisionPoly(thing);
}
thing->updateCollisionPoly(thing);
Vect translation;
Vect friction;
@ -663,6 +701,63 @@ void advance_thing(Body * thing) { @@ -663,6 +701,63 @@ void advance_thing(Body * thing) {
/*accel_thing(thing, rest.x, rest.y);*/
}
for (int i = 0; i < thing->num_strings; i++) {
if (!thing->strings[i].attached) {
continue;
}
double st_len = vect_distance(thing->next_position, thing->strings[i].end_point);
double max_len = thing->strings[i].max_length;
if (st_len > max_len) {
printf("STRING: %f, %f\n", st_len, max_len);
Vect end = thing->strings[i].end_point;
Vect position = thing->next_position;
Vect string;
string.x = end.x - position.x;
string.y = end.y - position.y;
double disp = st_len - max_len;
double angle = atan2(string.y, string.x);
double scale_factor = max_len / vect_mag(string);
Vect corrected_string;
corrected_string.x = cos(angle) * max_len;
corrected_string.y = sin(angle) * max_len;
printf("corrected string : %f %f\n", corrected_string.x, corrected_string.y);
thing->next_position = vect_scalar(thing->next_position,
scale_factor);
/*thing->next_position.x += cos(angle) * disp;*/
/*thing->next_position.y += sin(angle) * disp;*/
// move the thing back to the right position
/*Vect adj_pos = vect_add(vect_scalar(corrected_string, -1),*/
/*string);*/
/*adj_pos = vect_scalar(adj_pos, -1);*/
/*thing->position = vect_add(thing->position, adj_pos);*/
// set velocity to 0 in direction of string
Vect vel_correction = project_vect(thing->vel, string);
double dir;
double corr = vect_scalar_projection(thing->vel, string);
if (corr < 0) {
corr *= -1;
}
Vect v_corr;
v_corr.y = corr * sin(angle);
v_corr.x = corr * cos(angle);
thing->vel = vect_add(v_corr, thing->vel);
}
}
uint32_t now = SDL_GetTicks();
uint32_t time_delta = now - thing->last_advance_time ;
@ -799,9 +894,7 @@ void add_to_world(world_thing thing) { @@ -799,9 +894,7 @@ void add_to_world(world_thing thing) {
memcpy(world + things_in_world, &thing, sizeof(world_thing));
things_in_world += 1;
//printf("Added to world: %d\n", thing.kind);
}
void get_room(void) {
@ -835,7 +928,7 @@ void startgame(SDL_Renderer * ren) { @@ -835,7 +928,7 @@ void startgame(SDL_Renderer * ren) {
memset(world, 0, sizeof(world_thing) * 100);
SDL_GetRendererOutputSize(ren, &width, &height);
player = get_player(500,500);
player = get_player(100,1000);
world_thing player_world;

23
game.h

@ -45,8 +45,22 @@ typedef struct { @@ -45,8 +45,22 @@ typedef struct {
int y;
} Point;
typedef struct BodyStruct{
/* String structure, used as a sub-element of a Body, compressible, but not
* stretchable. */
struct String {
bool attached;
double max_length;
Vect end_point;
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
// string
};
typedef struct String String;
typedef struct BodyStruct{
// turn on dynamic physics
// For moving objects.
// eg. on for payer, off for walls
@ -78,7 +92,6 @@ typedef struct BodyStruct{ @@ -78,7 +92,6 @@ typedef struct BodyStruct{
//float y_acc;
/*------------------*/
// constants
double elasticity;
@ -89,7 +102,6 @@ typedef struct BodyStruct{ @@ -89,7 +102,6 @@ typedef struct BodyStruct{
int collision_shape_size;
void (*updateCollisionPoly)(struct BodyStruct *);
// fields
double glob_friction;
bool glob_gravity; // t/f
@ -100,6 +112,11 @@ typedef struct BodyStruct{ @@ -100,6 +112,11 @@ typedef struct BodyStruct{
int num_motors;
int max_motors;
Motor *motors;
int num_strings;
int max_strings;
String *strings;
} Body;
typedef struct {

9
vect.c

@ -47,3 +47,12 @@ double vect_scalar_projection(Vect V, Vect P) { @@ -47,3 +47,12 @@ double vect_scalar_projection(Vect V, Vect P) {
double vect_dir(Vect V) {
return atan2(V.y, V.x);
}
// distance between two points
double vect_distance(Vect A, Vect B) {
double y = (B.y - A.y);
double x = (B.x - A.x);
y = y * y;
x = x * x;
return sqrt(x + y);
}

2
vect.h

@ -30,4 +30,6 @@ Vect project_vect(Vect one, Vect two); @@ -30,4 +30,6 @@ Vect project_vect(Vect one, Vect two);
/* Return the scalar projection of V onto P */
double vect_scalar_projection(Vect V, Vect P);
/* Return the distance between two point vectors A and B */
double vect_distance(Vect A, Vect B);
#endif

Loading…
Cancel
Save