Files
armorpaint/paint/sources/physics_sim.c
T

137 lines
3.4 KiB
C
Raw Normal View History

2026-03-09 13:17:15 +01:00
#include "global.h"
2026-04-05 23:00:47 +02:00
any_array_t *sim_transforms;
bool sim_initialized = false;
2026-04-05 16:09:00 +02:00
2026-03-06 12:56:38 +01:00
void sim_init() {
if (sim_initialized) {
return;
}
physics_world_create();
sim_initialized = true;
}
void sim_update() {
render_path_raytrace_ready = false;
if (sim_running) {
// if (render_path_raytrace_frame != 1) {
// return;
// }
object_t_array_t *objects = map_keys(sim_object_script_map);
for (i32 i = 0; i < objects->length; ++i) {
object_t *o = objects->buffer[i];
2026-03-10 21:00:00 +01:00
char *s = any_map_get(sim_object_script_map, o);
char *addr = i64_to_string((i64)(o->transform));
s = string("{let transform=%s;%s}", addr, s);
2026-03-23 23:20:02 +01:00
// minic_eval(s);
2026-03-06 12:56:38 +01:00
}
physics_world_t *world = physics_world_active;
physics_world_update(world);
iron_delay_idle_sleep();
if (sim_record) {
render_target_t *rt = any_map_get(render_path_render_targets, "last");
buffer_t *pixels = gpu_get_texture_pixels(rt->_image);
2026-03-10 21:00:00 +01:00
#ifdef IRON_BGRA
2026-03-06 12:56:38 +01:00
export_arm_bgra_swap(pixels);
2026-03-10 21:00:00 +01:00
#endif
2026-03-06 12:56:38 +01:00
// iron_mp4_encode(pixels);
}
}
}
void sim_play() {
sim_running = true;
if (sim_record) {
if (string_equals(project_filepath, "")) {
2026-03-15 11:37:45 +01:00
console_error(tr("Save project first"));
2026-03-06 12:56:38 +01:00
sim_record = false;
return;
}
2026-03-10 21:00:00 +01:00
char *path = string("%s/output.mp4", path_base_dir(project_filepath));
2026-03-06 12:56:38 +01:00
render_target_t *rt = any_map_get(render_path_render_targets, "last");
// iron_mp4_begin(path, rt._image.width, rt._image.height);
}
// Save transforms
gc_unroot(sim_transforms);
2026-03-08 08:39:29 +01:00
sim_transforms = any_array_create_from_raw((void *[]){}, 0);
2026-03-06 12:56:38 +01:00
gc_root(sim_transforms);
mesh_object_t_array_t *pos = project_paint_objects;
for (i32 i = 0; i < pos->length; ++i) {
2026-04-05 23:00:47 +02:00
mat4_t *m = gc_alloc(sizeof(mat4_t));
memcpy(m->m, pos->buffer[i]->base->transform->local.m, sizeof(m->m));
2026-03-06 12:56:38 +01:00
any_array_push(sim_transforms, m);
}
}
void sim_stop() {
sim_running = false;
if (sim_record) {
// iron_mp4_end();
}
// Restore transforms
mesh_object_t_array_t *pos = project_paint_objects;
for (i32 i = 0; i < pos->length; ++i) {
2026-04-05 23:00:47 +02:00
transform_set_matrix(pos->buffer[i]->base->transform, *(mat4_t *)sim_transforms->buffer[i]);
2026-03-06 12:56:38 +01:00
physics_body_t *pb = any_imap_get(physics_body_object_map, pos->buffer[i]->base->uid);
2026-03-07 21:12:59 +01:00
if (pb != NULL) {
2026-03-06 12:56:38 +01:00
physics_body_sync_transform(pb);
}
}
}
void sim_add_body(object_t *o, physics_shape_t shape, f32 mass) {
physics_body_t *body = physics_body_create();
body->shape = shape;
body->mass = mass;
physics_body_init(body, o);
}
void sim_remove_body(i32 uid) {
physics_body_remove(uid);
}
void sim_duplicate() {
2026-04-18 14:14:54 +02:00
if (g_context->selected_object == NULL) {
return;
}
2026-03-06 12:56:38 +01:00
// Mesh
2026-04-05 16:41:00 +02:00
mesh_object_t *so = g_context->selected_object->ext;
2026-03-06 12:56:38 +01:00
mesh_object_t *dup = scene_add_mesh_object(so->data, so->material, so->base->parent);
transform_set_matrix(dup->base->transform, so->base->transform->local);
any_array_push(project_paint_objects, dup);
2026-03-10 21:00:00 +01:00
dup->base->name = so->base->name;
2026-03-06 12:56:38 +01:00
// Physics
physics_body_t *pb = any_imap_get(physics_body_object_map, so->base->uid);
2026-03-07 21:12:59 +01:00
if (pb != NULL) {
2026-03-06 12:56:38 +01:00
physics_body_t *pbdup = physics_body_create();
pbdup->shape = pb->shape;
pbdup->mass = pb->mass;
physics_body_init(pbdup, dup->base);
}
_tab_scene_paint_object_length++;
tab_scene_sort();
}
void sim_delete() {
2026-04-05 16:41:00 +02:00
mesh_object_t *so = g_context->selected_object->ext;
2026-03-06 12:56:38 +01:00
array_remove(project_paint_objects, so);
mesh_object_remove(so);
sim_remove_body(so->base->uid);
_tab_scene_paint_object_length--;
tab_scene_sort();
}