Compare commits

..

No commits in common. "9a732ad82f4f3471b625bee8113d283c73f9a0e8" and "76bf6e81e59ef0929944ba6a079b48fd54f49978" have entirely different histories.

2 changed files with 13 additions and 17 deletions

View File

@ -13,8 +13,7 @@ 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
}; };
} }
@ -181,11 +180,12 @@ float get_solve_force(const object_t* a, const object_t* b, float* out_px, float
} }
} }
void solve_collision_slide(object_t* left, object_t* right) { static inline
void _solve_collision_slide(object_t* this, object_t* other) {
float dx, dy; float dx, dy;
const float d = get_solve_force(left, right, &dx, &dy); const float d = get_solve_force(this, other, &dx, &dy);
left->sprite.x += dx; this->sprite.x += dx;
right->sprite.y += dy; this->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);
this->physics.solver(this, other); _solve_collision_slide(this, other);
} }
} }
} }
void physics_move(object_t* this, float delta_time) { void move_and_slide(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,8 +5,7 @@
typedef struct object_t object_t; typedef struct object_t object_t;
typedef void(*collided_fn)(object_t*, object_t*); typedef void(*collided_fn)(object_t*, struct 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,
@ -24,7 +23,6 @@ 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 {
@ -33,17 +31,15 @@ typedef struct physics_t {
}; };
} physics_t; } physics_t;
extern physics_t physics_default(); physics_t physics_default();
extern void object_broadcast_evt_collision(object_t* this, object_t* other); void object_broadcast_evt_collision(object_t* this, object_t* other);
extern void physics_update(); void physics_update();
extern void physics_move(object_t* this, float delta_time); void move_and_slide(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 */