Compare commits

...

4 Commits

2 changed files with 17 additions and 13 deletions

View File

@ -13,7 +13,8 @@ physics_t physics_default() {
return (physics_t) { return (physics_t) {
.type=COLLIDERTYPE_NONE, .type=COLLIDERTYPE_NONE,
.velocity_x = 0.f, .velocity_x = 0.f,
.velocity_y = 0.f .velocity_y = 0.f,
.solver = &solve_collision_slide
}; };
} }
@ -180,12 +181,11 @@ float get_solve_force(const object_t* a, const object_t* b, float* out_px, float
} }
} }
static inline void solve_collision_slide(object_t* left, object_t* right) {
void _solve_collision_slide(object_t* this, object_t* other) {
float dx, dy; float dx, dy;
const float d = get_solve_force(this, other, &dx, &dy); const float d = get_solve_force(left, right, &dx, &dy);
this->sprite.x += dx; left->sprite.x += dx;
this->sprite.y += dy; right->sprite.y += dy;
} }
static inline static inline
@ -198,12 +198,12 @@ void _solve_move(object_t* this) {
if(can_collide(other) && this != other && _collision_check(other, this)) { if(can_collide(other) && this != other && _collision_check(other, this)) {
object_broadcast_collision(other, this); object_broadcast_collision(other, this);
object_broadcast_collision(this, other); object_broadcast_collision(this, other);
_solve_collision_slide(this, other); this->physics.solver(this, other);
} }
} }
} }
void move_and_slide(object_t* this, float delta_time) { void physics_move(object_t* this, float delta_time) {
const float max_step_size = this->physics.max_interpolate_step_size; const float max_step_size = this->physics.max_interpolate_step_size;
// calculate step delta // calculate step delta
float dx = this->physics.velocity_x * delta_time, dy = this->physics.velocity_y * delta_time; float dx = this->physics.velocity_x * delta_time, dy = this->physics.velocity_y * delta_time;

View File

@ -5,7 +5,8 @@
typedef struct object_t object_t; typedef struct object_t object_t;
typedef void(*collided_fn)(object_t*, struct object_t*); typedef void(*collided_fn)(object_t*, object_t*);
typedef void(*solver_fn)(object_t* left, object_t* right);
typedef enum collider_type_t { typedef enum collider_type_t {
COLLIDERTYPE_MIN, COLLIDERTYPE_MIN,
@ -23,6 +24,7 @@ typedef struct circle_t {
typedef struct physics_t { typedef struct physics_t {
collider_type_t type; collider_type_t type;
collided_fn evt_collision; collided_fn evt_collision;
solver_fn solver;
float velocity_x, velocity_y; float velocity_x, velocity_y;
float max_interpolate_step_size; float max_interpolate_step_size;
union { union {
@ -31,15 +33,17 @@ typedef struct physics_t {
}; };
} physics_t; } physics_t;
physics_t physics_default(); extern physics_t physics_default();
void object_broadcast_evt_collision(object_t* this, object_t* other); extern void object_broadcast_evt_collision(object_t* this, object_t* other);
void physics_update(); extern void physics_update();
void move_and_slide(object_t* this, float delta_time); extern void physics_move(object_t* this, float delta_time);
extern short can_collide(const object_t* this); extern short can_collide(const object_t* this);
extern void solve_collision_slide(object_t* left, object_t* right);
extern float get_solve_force(const object_t* this, const object_t* other, float* solve_x, float* solve_y); extern float get_solve_force(const object_t* this, const object_t* other, float* solve_x, float* solve_y);
#endif /* _physics_h */ #endif /* _physics_h */