alistair
5 years ago
11 changed files with 112 additions and 25 deletions
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
#include "game.h" |
||||
#include <SDL2/SDL.h> |
||||
#include <SDL2/SDL_image.h> |
||||
#include <stdbool.h> |
||||
#include <stdint.h> |
||||
|
||||
typedef struct { |
||||
int x_pos; |
||||
int y_pos; |
||||
|
||||
float x_vel; |
||||
float y_vel; |
||||
|
||||
float x_acc; |
||||
float y_acc; |
||||
|
||||
} physics_thing; |
||||
|
||||
/*
|
||||
*typedef enum { |
||||
* sdl_shape; |
||||
* image; |
||||
* |
||||
*} render_type; |
||||
*/ |
||||
|
||||
/*
|
||||
*typedef struct { |
||||
* char image_path[]; |
||||
* // poly
|
||||
*} sprite; |
||||
*/ |
||||
|
||||
|
||||
typedef struct { |
||||
bool has_physics; |
||||
physics_thing physics; |
||||
} player_st; |
||||
|
||||
player_st get_player(int x, int y) { |
||||
/* creates player at given postion and zeroes physics */ |
||||
|
||||
player_st player; |
||||
|
||||
player.physics.x_pos = x; |
||||
player.physics.y_pos = y; |
||||
player.physics.x_acc = 0; |
||||
player.physics.y_acc = 0; |
||||
player.physics.x_vel = 0; |
||||
player.physics.y_vel = 0; |
||||
|
||||
return (player); |
||||
} |
||||
|
||||
void accel_thing(physics_thing * thing, float x, float y) { |
||||
/* takes acceleration in m/s2 and converts to m/ms adding
|
||||
* it to physics_thing
|
||||
*/ |
||||
|
||||
long x_adj = (long ) x / 1000.0; |
||||
long y_adj = (long ) y / 1000.0; |
||||
(*thing).y_acc += (float)y_adj; |
||||
(*thing).x_acc += (float)x_adj; |
||||
} |
||||
|
||||
void advance_thing(physics_thing * thing) { |
||||
static uint32_t last_advance_time; |
||||
|
||||
uint16_t time_delta = last_advance_time - SDL_GetTicks(); |
||||
|
||||
// milliseconds
|
||||
last_advance_time = SDL_GetTicks(); |
||||
|
||||
(*thing).x_vel = (*thing).x_acc * (float)time_delta; |
||||
(*thing).y_vel = (*thing).y_acc * (float)time_delta; |
||||
|
||||
(*thing).x_pos = (*thing).x_acc * 1/2 * (float)time_delta;
|
||||
(*thing).y_pos = (*thing).y_acc * 1/2 * (float)time_delta;
|
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue