158 lines
4.0 KiB
C
158 lines
4.0 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "entity/entitybase.h"
|
|
#include "physics/physicsshape.h"
|
|
#include "physics/physicsbodytype.h"
|
|
|
|
typedef struct {
|
|
physicsbodytype_t type;
|
|
physicsshape_t shape;
|
|
vec3 velocity;
|
|
float_t gravityScale;
|
|
bool_t onGround;
|
|
} entityphysics_t;
|
|
|
|
/**
|
|
* Initializes the physics component: allocates a body in PHYSICS_WORLD.
|
|
* Asserts if the world body limit is reached.
|
|
*/
|
|
void entityPhysicsInit(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Gets the underlying physics structure (temporarily) for the given entity.
|
|
* This is really just intended for doing operations faster than using the
|
|
* getters and setters, but it is preferred that you use those.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @return The physics component data for the given entity and component ID.
|
|
*/
|
|
entityphysics_t *entityPhysicsGet(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Sets the shape of the entity's physics body. This will not reset the body
|
|
* state, so if you change from a cube to a sphere, it will keep the same
|
|
* velocity and onGround state.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param shape The new shape to set on the physics body.
|
|
*/
|
|
void entityPhysicsSetShape(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const physicsshape_t shape
|
|
);
|
|
|
|
/**
|
|
* Gets the shape of the entity's physics body.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @return The shape of the physics body.
|
|
*/
|
|
physicsshape_t entityPhysicsGetShape(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Gets the velocity of the entity's physics body.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param dest The destination vec3 to write the velocity to.
|
|
*/
|
|
void entityPhysicsGetVelocity(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
vec3 dest
|
|
);
|
|
|
|
/**
|
|
* Sets the velocity of the entity's physics body. This is not an impulse, so
|
|
* it will be affected by mass and drag.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param velocity The new velocity to set on the physics body.
|
|
*/
|
|
void entityPhysicsSetVelocity(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
vec3 velocity
|
|
);
|
|
|
|
/**
|
|
* Applies an impulse to the entity's physics body. This is an immediate
|
|
* velocity change that is not affected by mass or drag. No-op on STATIC bodies.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param impulse The impulse to apply to the physics body.
|
|
*/
|
|
void entityPhysicsApplyImpulse(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
vec3 impulse
|
|
);
|
|
|
|
/**
|
|
* Returns true if the entity's physics body rested on a surface during the last
|
|
* step or move.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @return True if the body is on the ground, false otherwise.
|
|
*/
|
|
bool_t entityPhysicsIsOnGround(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Sets the body type of the entity's physics body.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param type The body type to set.
|
|
*/
|
|
void entityPhysicsSetBodyType(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const physicsbodytype_t type
|
|
);
|
|
|
|
/**
|
|
* Gets the body type of the entity's physics body.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @return The body type of the physics body.
|
|
*/
|
|
physicsbodytype_t entityPhysicsGetBodyType(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Releases the body slot back to PHYSICS_WORLD. Called automatically when
|
|
* the component is disposed via the component system.
|
|
*/
|
|
void entityPhysicsDispose(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|