Files
armorpaint/paint/sources/physics_body.c
T

77 lines
2.1 KiB
C
Raw Normal View History

2026-03-06 12:56:38 +01:00
2026-06-05 14:48:27 +02:00
#ifdef WITH_PHYSICS
2026-03-06 12:56:38 +01:00
2026-03-07 21:12:59 +01:00
#include "../libs/asim.h"
2026-04-21 10:52:27 +02:00
#include "global.h"
2026-03-07 21:12:59 +01:00
2026-03-06 12:56:38 +01:00
physics_body_t *physics_body_create() {
physics_body_t *body = GC_ALLOC_INIT(physics_body_t, {0});
return body;
}
void physics_body_init(physics_body_t *body, object_t *obj) {
any_imap_set(physics_body_object_map, obj->uid, body);
body->obj = obj;
transform_compute_dim(obj->transform);
2026-04-21 10:52:27 +02:00
body->dimx = obj->transform->dim.x;
body->dimy = obj->transform->dim.y;
body->dimz = obj->transform->dim.z;
2026-03-06 12:56:38 +01:00
f32 scale_pos = 1.0;
2026-03-07 21:12:59 +01:00
i16_array_t *posa = NULL;
u32_array_t *inda = NULL;
2026-03-06 12:56:38 +01:00
if (body->shape == PHYSICS_SHAPE_MESH || body->shape == PHYSICS_SHAPE_HULL || body->shape == PHYSICS_SHAPE_TERRAIN) {
2026-04-21 10:52:27 +02:00
mesh_object_t *mo = obj->ext;
mesh_data_t *data = mo->data;
vec4_t scale = obj->transform->scale;
2026-03-06 12:56:38 +01:00
2026-05-12 13:58:45 +02:00
if (obj->parent != NULL) {
scale.x *= obj->parent->transform->scale.x;
scale.y *= obj->parent->transform->scale.y;
scale.z *= obj->parent->transform->scale.z;
}
2026-04-21 10:52:27 +02:00
i16_array_t *positions = mesh_data_get_vertex_array(data, "pos")->values;
u32_array_t *indices0 = data->index_array;
2026-03-06 12:56:38 +01:00
2026-04-21 10:52:27 +02:00
scale_pos = scale.x * data->scale_pos;
posa = positions;
inda = indices0;
2026-03-06 12:56:38 +01:00
}
vec4_t loc = obj->transform->loc;
body->_body = asim_body_create(body->shape, body->mass, body->dimx, body->dimy, body->dimz, loc.x, loc.y, loc.z, posa, inda, scale_pos);
}
2026-05-11 19:48:30 +02:00
void physics_body_remove(physics_body_t *body) {
2026-03-07 21:12:59 +01:00
if (body == NULL) {
2026-03-06 12:56:38 +01:00
return;
}
2026-05-11 19:48:30 +02:00
imap_delete(physics_body_object_map, body->obj->uid);
2026-03-06 12:56:38 +01:00
asim_body_remove(body->_body);
}
void physics_body_apply_impulse(physics_body_t *body, vec4_t dir) {
asim_body_apply_impulse(body->_body, dir.x, dir.y, dir.z);
}
void physics_body_sync_transform(physics_body_t *body) {
transform_t *transform = body->obj->transform;
asim_body_sync_transform(body->_body, transform->loc, transform->rot);
}
void physics_body_update(physics_body_t *body) {
if (body->shape == PHYSICS_SHAPE_MESH) {
return; ////
}
transform_t *transform = body->obj->transform;
2026-03-08 08:39:29 +01:00
asim_body_get_pos(body->_body, &transform->loc);
asim_body_get_rot(body->_body, &transform->rot);
2026-03-06 12:56:38 +01:00
transform_build_matrix(transform);
}
#endif