added solver field to physics_t, exposed solve_collision_slide to make it work

pull/11/head
Sara 2023-06-25 18:12:38 +02:00
parent 76bf6e81e5
commit 7a2cb3cd5c
2 changed files with 11 additions and 4 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,14 @@ 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(this, other, &dx, &dy);
this->sprite.x += dx; this->sprite.x += dx;
this->sprite.y += dy; this->sprite.y += dy;
const float d = get_solve_force(left, right, &dx, &dy);
left->sprite.x += dx;
right->sprite.y += dy;
} }
static inline static inline
@ -198,7 +201,7 @@ 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);
} }
} }
} }

View File

@ -6,6 +6,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*, 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 +25,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 {
@ -40,6 +43,7 @@ void physics_update();
void move_and_slide(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 */