feat(physics): reworked physics to use colliders, allowing for bodies with multiple colliders

This commit is contained in:
Sara 2023-11-25 21:52:12 +01:00
parent 84a940d046
commit 240186c8de
13 changed files with 137 additions and 132 deletions

View file

@ -27,12 +27,7 @@ Player* MakePlayer() {
*self = (Player) {
.transform = IdentityTransform,
.rigidbody = NULL,
.collisionShape = shape_new((Vector[]){
MakeVector(-0.15, -0.15),
MakeVector( 0.15, -0.15),
MakeVector( 0.15, 0.0),
MakeVector(-0.15, 0.0)
}, 4),
.physicsCollider = NULL,
.playerInput = playerinput_new(self, -1),
.moveInput = ZeroVector,
.attackInput = 0,
@ -45,8 +40,14 @@ Player* MakePlayer() {
};
self->rigidbody = rigidbody_make(Player_as_Transformable(self));
self->physicsCollider = collider_new(self->rigidbody, shape_new((Vector[]){
MakeVector(-0.15, -0.065),
MakeVector( 0.15, -0.065),
MakeVector( 0.15, 0.065),
MakeVector(-0.15, 0.065)
}, 4), 0, 0x1);
sprite_set_origin(self->sprite, MakeVector(0.45f, 1.f));
sprite_set_origin(self->sprite, MakeVector(0.45f, 0.925f));
self->idle = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Idle.png", IVectorFrom(512)), 1.5f, LoopMode_Loop);
self->walk = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Walk.png", IVectorFrom(512)), 5.f, LoopMode_Loop);
@ -70,8 +71,8 @@ Player* SpawnPlayer(Vector location) {
}
void DestroyPlayer(Player* self) {
collider_destroy(self->physicsCollider);
rigidbody_destroy(self->rigidbody);
shape_destroy(self->collisionShape);
playerinput_drop(self->playerInput);
@ -105,7 +106,7 @@ void PlayerUpdate(Player* self, float deltaTime) {
void PlayerDraw(Player* self) {
animation_sprite_draw(self->currentAnimation, &self->transform);
shape_draw(self->collisionShape, self->transform);
shape_draw(collider_get_shape(self->physicsCollider), self->transform);
}
Transform* PlayerGetTransform(Player* self) {
@ -116,9 +117,5 @@ RigidBody* PlayerGetRigidBody(Player* self) {
return self->rigidbody;
}
Shape* PlayerGetCollisionShape(Player* self) {
return self->collisionShape;
}
void PlayerOnCollision(Player* self, Collision collision) {}
void PlayerOnOverlap(Player* self, PhysicsEntity other) {}
void PlayerOnOverlap(Player* self, Collider* other) {}

View file

@ -10,7 +10,7 @@
#include "transform.h"
#include "player_input.h"
#include "rigidbody.h"
#include "shape.h"
#include "collider.h"
extern const Vector PLAYER_SPEED;
@ -18,7 +18,7 @@ typedef struct Player {
Transform transform;
RigidBody* rigidbody;
Shape* collisionShape;
Collider* physicsCollider;
PlayerInput* playerInput;
@ -52,9 +52,8 @@ void PlayerDraw(Player* self);
Transform* PlayerGetTransform(Player* self);
RigidBody* PlayerGetRigidBody(Player* self);
Shape* PlayerGetCollisionShape(Player* self);
void PlayerOnCollision(Player* self, Collision collision);
void PlayerOnOverlap(Player* self, PhysicsEntity other);
void PlayerOnOverlap(Player* self, Collider* other);
static long PlayerGetDepth(Player* self) { return -(int)(self->transform.position.y * 1000); }
@ -75,7 +74,6 @@ impl_Transformable_for(Player,
impl_PhysicsEntity_for(Player,
PlayerGetRigidBody,
PlayerGetCollisionShape,
PlayerOnCollision,
PlayerOnOverlap
)

View file

@ -10,15 +10,16 @@ Prop* MakeProp(Sprite* sprite, Shape* shape) {
.transform = IdentityTransform,
.sprite = sprite,
.rigidbody = NULL,
.collisionShape = shape
.collisionShape = NULL
};
self->rigidbody = rigidbody_make(Prop_as_Transformable(self));
self->collisionShape = collider_new(self->rigidbody, shape, 0, 0x1);
rigidbody_set_static(self->rigidbody, 1);
sprite_set_origin(self->sprite, MakeVector(0.5f, 1.0f));
return self;
}
Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape) {
Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape, Vector origin) {
Prop* self = MakeProp(sprite, shape);
self->transform.position
@ -27,14 +28,16 @@ Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape) {
game_world_add_entity(Prop_as_BehaviourEntity(self));
physics_world_add_entity(Prop_as_PhysicsEntity(self));
sprite_set_origin(self->sprite, origin);
return self;
}
void DestroyProp(Prop* self) {
sprite_destroy(self->sprite);
collider_destroy(self->collisionShape);
rigidbody_destroy(self->rigidbody);
shape_destroy(self->collisionShape);
free(self);
}
@ -43,11 +46,11 @@ void PropUpdate(Prop* self, float deltaTime) {}
void PropDraw(Prop* self) {
sprite_draw(self->sprite, self->transform);
shape_draw(self->collisionShape, self->transform);
shape_draw(collider_get_shape(self->collisionShape), self->transform);
}
void PropOnCollision(Prop* self, Collision collision) {}
void PropOnOverlap(Prop* self, PhysicsEntity other) {}
void PropOnOverlap(Prop* self, Collider* other) {}
Transform* PropGetTransform(Prop* self) {
return &self->transform;
@ -56,7 +59,3 @@ Transform* PropGetTransform(Prop* self) {
RigidBody* PropGetRigidBody(Prop* self) {
return self->rigidbody;
}
Shape* PropGetCollisionShape(Prop* self) {
return self->collisionShape;
}

View file

@ -8,17 +8,17 @@
#include "transform.h"
#include "sprite.h"
#include "rigidbody.h"
#include "shape.h"
#include "collider.h"
typedef struct Prop {
Transform transform;
Sprite* sprite;
RigidBody* rigidbody;
Shape* collisionShape;
Collider* collisionShape;
} Prop;
Prop* MakeProp(Sprite* sprite, Shape* shape);
Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape);
Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape, Vector origin);
void DestroyProp(Prop* self);
void PropStart(Prop* self);
@ -26,11 +26,10 @@ void PropUpdate(Prop* self, float deltaTime);
void PropDraw(Prop* self);
void PropOnCollision(Prop* self, Collision collision);
void PropOnOverlap(Prop* self, PhysicsEntity other);
void PropOnOverlap(Prop* self, Collider* other);
Transform* PropGetTransform(Prop* self);
RigidBody* PropGetRigidBody(Prop* self);
Shape* PropGetCollisionShape(Prop* self);
static long PropGetDepth(Prop* self) { return -(int)(self->transform.position.y * 1000); }
@ -40,7 +39,6 @@ impl_Transformable_for(Prop,
impl_PhysicsEntity_for(Prop,
PropGetRigidBody,
PropGetCollisionShape,
PropOnCollision,
PropOnOverlap
)

View file

@ -8,11 +8,12 @@ void play() {
SpawnProp(MakeVector(2.f, 0.f),
sprite_from_spritesheet(spritesheet_load("assets/bag.png", IVectorFrom(512)), 0),
shape_new((Vector[]){
MakeVector(-0.25, -0.1),
MakeVector(0.25, -0.1),
MakeVector(0.25, 0.0),
MakeVector(-0.25, 0.0)
}, 4)
MakeVector(-0.2, -0.1),
MakeVector( 0.2, -0.1),
MakeVector( 0.2, 0.05),
MakeVector(-0.2, 0.05)
}, 4),
MakeVector(0.5f, .95f)
);
SpawnPlayer(ZeroVector);
}