progress on physics

This commit is contained in:
Sara 2023-10-16 21:31:48 +02:00
parent 6cc16cf1f8
commit c7e6b2aa29
24 changed files with 635 additions and 173 deletions

View file

@ -1,37 +1,60 @@
#include "player.h"
#include "assets.h"
#include "debug.h"
#include "program.h"
#include "rigidbody.h"
#include "input.h"
#include "physics_world.h"
static Vector directional = ZeroVector;
static Spritesheet* spr_player_standing = NULL;
static
void player_input_h(int val) {
directional.x = val * 0.1f;
directional.x = val * 10.f;
}
static
void player_input_v(int val) {
directional.y = -val * 0.1f;
directional.y = -val * 10.f;
}
void player_spawn(Player* self, Vector at) {
player_start(self);
self->transform.position = at;
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);
}
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);
self->transform = IdentityTransform;
self->transform.scale = (Vector){4.f, 4.f};
spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
store_asset(self, free);
self->sprite = sprite_from_spritesheet(spr_player_standing, 0);
sprite_set_origin(self->sprite, (Vector){0.25f, 1.f});
self->rigidbody = rigidbody_make(Player_as_Transformable(self));
float ex_w = 0.1f;
float h = .75f;
float r = 0.05f;
self->shape = shape_new((Vector[]){
{r-ex_w, 0.f}, {-ex_w, -r},
{-ex_w, r-h}, {r-ex_w, -h},
{ex_w-r, -h}, {ex_w, r-h},
{ex_w, -r}, {ex_w-r, 0.f},
}, 8);
physics_world_add_entity(Player_as_PhysicsEntity(self));
}
void player_update(Player* self, float dt) {
Vector velocity = rigidbody_get_velocity(self->rigidbody);
ASSERT_RETURN(!visnanf(velocity),, "Velocity is NaN (2)");
velocity = vmovetowardsf(velocity, vmulff(directional, 100.f), 10000.f * dt);
ASSERT_RETURN(!visnanf(velocity),, "Velocity is NaN (3)");
rigidbody_set_velocity(self->rigidbody, velocity);
Vector velocity_target = {directional.x, directional.y};
rigidbody_accelerate(self->rigidbody, vmulff(vsubf(velocity_target, velocity), 200.f));
}
void player_collision(Player* self, Collision hit) {