added custom collision solvers

This commit is contained in:
Sara 2023-10-22 11:25:44 +02:00
parent 788c1970e2
commit f373fada26
10 changed files with 116 additions and 76 deletions

View file

@ -1,6 +1,7 @@
#include "player.h"
#include "assets.h"
#include "debug.h"
#include "physics_entity.h"
#include "program.h"
#include "rigidbody.h"
#include "input.h"
@ -23,6 +24,27 @@ void player_spawn(Player* self, Vector at) {
rigidbody_get_transform(self->rigidbody)->position = at;
}
void player_collision_solver(Player* self, List* contacts) {
RigidBody* body = self->rigidbody;
Collision hit;
const Vector vel = vaddf(rigidbody_get_velocity(body), vmulff(rigidbody_get_force(body), delta_time()));
Vector avrg = ZeroVector;
int n = 0;
list_foreach(Contact, contact, contacts) {
hit = contact->hit;
Vector norm = vnormalizedf(hit.penetration_vector);
LOG_INFO("n %f %f", norm.x, norm.y);
float dot = fmaxf(0, vdotf(norm, hit.velocity));
avrg = vaddf(vmulff(avrg, (float)n), vsubf(vel, vmulff(norm, dot)));
++n;
avrg = vmulff(avrg, 1.0f / (float)n);
}
LOG_INFO("vel %f %f", vel.x, vel.y);
LOG_INFO("for %f %f", avrg.x, avrg.y);
// rigidbody_add_impulse(body, vinvf(avrg), 0);
default_contact_solver(Player_as_PhysicsEntity(self), contacts);
}
void player_start(Player* self) {
input_add_axis_action(SDL_SCANCODE_A, SDL_SCANCODE_D, &player_input_h);
input_add_axis_action(SDL_SCANCODE_S, SDL_SCANCODE_W, &player_input_v);