feat: rigidbody is now constructed using a PhysicsEntity rather than a Transformable

This commit is contained in:
Sara 2023-11-27 17:38:22 +01:00
parent ece2a24a78
commit 163fac8f02
2 changed files with 7 additions and 6 deletions

View file

@ -6,7 +6,7 @@
#include "transformable.h" #include "transformable.h"
struct RigidBody { struct RigidBody {
Transformable transformable; PhysicsEntity owner;
float mass; float mass;
float bounce; float bounce;
@ -26,11 +26,11 @@ struct RigidBody {
List contacts; List contacts;
}; };
RigidBody* rigidbody_make(Transformable transform) { RigidBody* rigidbody_make(PhysicsEntity owner) {
RigidBody* self = malloc(sizeof(RigidBody)); RigidBody* self = malloc(sizeof(RigidBody));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for rigidbody"); ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for rigidbody");
*self = (RigidBody){ *self = (RigidBody){
.transformable = transform, .owner = owner,
.mass = 1.0f, .mass = 1.0f,
.bounce = 0.0f, .bounce = 0.0f,
@ -38,7 +38,7 @@ RigidBody* rigidbody_make(Transformable transform) {
.next_linear_force = ZeroVector, .next_linear_force = ZeroVector,
.last_linear_force = ZeroVector, .last_linear_force = ZeroVector,
.internal_transform = *transform.tc->get_transform(transform.data), .internal_transform = *owner.transformable->get_transform(owner.data),
.layers = 0x1, .layers = 0x1,
.collision_mask = 0x1, .collision_mask = 0x1,
@ -110,7 +110,8 @@ void rigidbody_integrate_forces(RigidBody* self) {
self->linear_velocity = velocity; self->linear_velocity = velocity;
self->internal_transform.position = position; self->internal_transform.position = position;
transformable_set_position(self->transformable, position); Transform* owner_trans = self->owner.transformable->get_transform(self->owner.data);
owner_trans->position = position;
self->last_linear_force = self->next_linear_force; self->last_linear_force = self->next_linear_force;
self->next_linear_force = ZeroVector; self->next_linear_force = ZeroVector;

View file

@ -18,7 +18,7 @@ typedef struct PhysicsEntity PhysicsEntity;
typedef void (*CollisionHandlerFn)(void* obj, List* collisions); typedef void (*CollisionHandlerFn)(void* obj, List* collisions);
// Referenced transform is stored but not owned by the rigidbody. // Referenced transform is stored but not owned by the rigidbody.
extern RigidBody* rigidbody_make(Transformable transform); extern RigidBody* rigidbody_make(PhysicsEntity owner);
extern void rigidbody_destroy(RigidBody* self); extern void rigidbody_destroy(RigidBody* self);
extern void rigidbody_add_contact(RigidBody* self, struct Collision hit); extern void rigidbody_add_contact(RigidBody* self, struct Collision hit);