WIP on physics/stage BSP stuff

This commit is contained in:
2026-06-27 13:16:28 -04:00
parent cb878cfaa5
commit 462fb12bd5
10 changed files with 130 additions and 66 deletions

View File

@@ -1,7 +1,9 @@
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <string.h>
#include <akerror.h>
#include <akstdlib.h>
#include <akgl/physics.h>
#include <akgl/game.h>

View File

@@ -25,12 +25,14 @@ akgl_RenderBackend *renderer;
akgl_PhysicsBackend *physics;
SDL_FRect *camera;
akgl_Tilemap *gamemap;
akgl_Stage *stage;
// Default objects
akgl_RenderBackend _akgl_renderer;
akgl_PhysicsBackend _akgl_physics;
SDL_FRect _akgl_camera;
akgl_Tilemap _akgl_gamemap;
akgl_Stage _akgl_stage;
MIX_Audio *bgm = NULL;
MIX_Mixer *akgl_mixer = NULL;
@@ -124,6 +126,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_game_init()
renderer = &_akgl_renderer;
physics = &_akgl_physics;
gamemap = &_akgl_gamemap;
stage = &_akgl_stage;
PASS(e, akgl_game_state_unlock());
SUCCEED_RETURN(e);

View File

@@ -10,6 +10,7 @@
#include <akgl/staticstring.h>
#include <akgl/iterator.h>
#include <akgl/error.h>
#include <akgl/stage.h>
akgl_Actor HEAP_ACTOR[AKGL_MAX_HEAP_ACTOR];
akgl_Sprite HEAP_SPRITE[AKGL_MAX_HEAP_SPRITE];
@@ -18,7 +19,7 @@ akgl_Character HEAP_CHARACTER[AKGL_MAX_HEAP_CHARACTER];
akgl_String HEAP_STRING[AKGL_MAX_HEAP_STRING];
aksl_ListNode HEAP_LIST[AKGL_MAX_HEAP_LIST];
aksl_TreeNode HEAP_TREE[AKGL_MAX_HEAP_TREE];
aksl_ListNode HEAP_TREE_LEAVES[AKGL_MAX_HEAP_TREE];
akgl_BSPLeaf HEAP_TREE_LEAVES[AKGL_MAX_HEAP_TREE];
void *AKGL_LIST_SENTINEL = (void *)1; /** Sentinel value used for aksl_ListNode objects to determine if they are available */
@@ -28,6 +29,8 @@ akerr_ErrorContext *akgl_heap_init()
int i = 0;
akerr_name_for_status(AKGL_ERR_SDL, "SDL Error");
PASS(errctx, akgl_heap_init_actor());
PASS(errctx, akgl_heap_init_list());
PASS(errctx, akgl_heap_init_tree());
for ( i = 0; i < AKGL_MAX_HEAP_SPRITE; i++) {
memset(&HEAP_SPRITE[i], 0x00, sizeof(akgl_Sprite));
}
@@ -59,6 +62,7 @@ akerr_ErrorContext *akgl_heap_init_tree(void)
for ( int i = 0; i < AKGL_MAX_HEAP_TREE; i++) {
memset(&HEAP_TREE[i], 0x00, sizeof(aksl_TreeNode));
HEAP_TREE[i].leaf = AKGL_LIST_SENTINEL;
memset(&HEAP_TREE_LEAVES[i], 0x00, sizeof(akgl_BSPLeaf));
}
SUCCEED_RETURN(e);
}
@@ -108,7 +112,7 @@ akerr_ErrorContext *akgl_heap_next_tree(aksl_TreeNode **dest)
{
PREPARE_ERROR(errctx);
for (int i = 0; i < AKGL_MAX_HEAP_TREE; i++ ) {
if ( HEAP_TREE[i].leaf != &AKGL_LIST_SENTINEL ) {
if ( HEAP_TREE[i].leaf != AKGL_LIST_SENTINEL ) {
continue;
}
*dest = &HEAP_TREE[i];
@@ -132,7 +136,7 @@ akerr_ErrorContext *akgl_heap_release_list(aksl_ListNode *list)
FAIL_ZERO_RETURN(e, list, AKERR_NULLPOINTER, "list");
list->next = NULL;
list->prev = NULL;
list->data = (void *)&AKGL_LIST_SENTINEL;
list->data = (void *)AKGL_LIST_SENTINEL;
SUCCEED_RETURN(e);
}
@@ -149,10 +153,13 @@ akerr_ErrorContext *akgl_heap_release_tree(aksl_TreeNode *tree)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, tree, AKERR_NULLPOINTER, "tree");
akgl_BSPLeaf *leaf = NULL;
tree->left = NULL;
tree->right = NULL;
if ( tree->leaf != NULL && tree->leaf->actors != NULL ) {
FAIL_RETURN(e, AKERR_VALUE, "Can't release tree node %p until its leaf actors %p is released and leaf->actors == NULL", tree, tree->leaf->actors);
tree->parent = NULL;
leaf = (akgl_BSPLeaf *)tree->leaf;
if ( tree->leaf != NULL && leaf->actors != NULL ) {
FAIL_RETURN(e, AKERR_VALUE, "Can't release tree node %p until its leaf actors %p is released and leaf->actors == NULL", tree, leaf->actors);
} else if ( tree->leaf != NULL ) {
memset((void *)tree->leaf, 0x00, sizeof(akgl_BSPLeaf));
}
@@ -192,7 +199,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_iter_tree_release(aksl_TreeNode *pt
FAIL_ZERO_RETURN(e, ptr, AKERR_NULLPOINTER, "ptr");
if ( ptr->leaf != AKGL_LIST_SENTINEL && ptr->leaf != NULL ) {
PASS(e, akgl_heap_release_list(ptr->leaf));
ptr->leaf = NULL;
//ptr->leaf = NULL;
}
PASS(e, akgl_heap_release_tree(ptr));
SUCCEED_RETURN(e);

View File

@@ -6,6 +6,7 @@
#include <akgl/error.h>
#include <akgl/heap.h>
#include <akgl/registry.h>
#include <akgl/stage.h>
akerr_ErrorContext AKERR_NOIGNORE *akgl_physics_null_gravity(akgl_PhysicsBackend *self, akgl_Actor *actor, float32_t dt)
{
@@ -91,9 +92,17 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_physics_arcade_move(struct akgl_PhysicsB
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, self, AKERR_NULLPOINTER, "self");
FAIL_ZERO_RETURN(e, actor, AKERR_NULLPOINTER, "actor");
akgl_Sprite *curspr = NULL;
actor->x += actor->vx * dt;
actor->y += actor->vy * dt;
actor->z += actor->vz * dt;
// Set the actor's bounding box for physics
if ( actor->basechar != NULL ) {
PASS(e, actor->basechar->sprite_get(actor->basechar, actor->state, &curspr));
actor->bbox = (SDL_FRect){actor->x, actor->y, curspr->width, curspr->height};
} else {
actor->bbox = (SDL_FRect){actor->x, actor->y, 0, 0};
}
SUCCEED_RETURN(e);
}

View File

@@ -1,4 +1,5 @@
#include <SDL3/SDL.h>
#include <akstdlib.h>
#include <akgl/stage.h>
#include <akgl/actor.h>
#include <akgl/heap.h>
@@ -19,6 +20,9 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d(akgl_Stage *self)
memset((void *)self, 0x00, sizeof(akgl_Stage));
self->partition = akgl_stage_2d_partition;
self->partition_actor = akgl_stage_2d_bsp_move;
memset((void *)&self->bsp, 0x00, sizeof(aksl_TreeNode));
self->bsp.leaf = &self->_rootleaf;
memset((void *)&self->_rootleaf, 0x00, sizeof(akgl_BSPLeaf));
SUCCEED_RETURN(e);
}
@@ -43,6 +47,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_partition_switchdirection(uint8
} else {
FAIL_RETURN(e, AKERR_VALUE, "Unknown partition direction %d", direction);
}
SUCCEED_RETURN(e);
}
/**
@@ -67,25 +72,29 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp_actoriter(aksl_ListNode *no
bool e2intersect;
akgl_BSPContext *context = NULL;
akgl_Actor *actor = NULL;
aksl_ListNode *dest = NULL;
aksl_TreeNode *dest = NULL;
aksl_ListNode *newnode = NULL;
akgl_BSPLeaf *leaf = NULL;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, node, AKERR_NULLPOINTER, "node");
FAIL_ZERO_RETURN(e, data, AKERR_NULLPOINTER, "data");
FAIL_ZERO_RETURN(e, node->data, AKERR_NULLPOINTER, "node->data");
FAIL_ZERO_RETURN(e, context->left, AKERR_NULLPOINTER, "left");
FAIL_ZERO_RETURN(e, context->right, AKERR_NULLPOINTER, "right");
FAIL_ZERO_RETURN(e, context->parent, AKERR_NULLPOINTER, "parent");
FAIL_ZERO_RETURN(e, context->left->leaf.actors, AKERR_NULLPOINTER, "left->leaf");
FAIL_ZERO_RETURN(e, context->right->leaf.actors, AKERR_NULLPOINTER, "right->leaf");
FAIL_ZERO_RETURN(e, context->parent->leaf.actors, AKERR_NULLPOINTER, "parent->leaf");
context = (akgl_BSPContext *)data;
actor = (akgl_Actor *)node->data;
e1intersect = SDL_HasRectIntersectionFloat(&context->left.extent, &actor->bbox);
e2intersect = SDL_HasRectIntersectionFloat(&context->right.extent, &actor->bbox);
FAIL_ZERO_RETURN(e, context->left, AKERR_NULLPOINTER, "left");
FAIL_ZERO_RETURN(e, context->right, AKERR_NULLPOINTER, "right");
FAIL_ZERO_RETURN(e, context->parent, AKERR_NULLPOINTER, "parent");
FAIL_ZERO_RETURN(e, ((akgl_BSPLeaf *)context->left->leaf)->actors, AKERR_NULLPOINTER, "left->leaf");
FAIL_ZERO_RETURN(e, ((akgl_BSPLeaf *)context->right->leaf)->actors, AKERR_NULLPOINTER, "right->leaf");
FAIL_ZERO_RETURN(e, ((akgl_BSPLeaf *)context->parent->leaf)->actors, AKERR_NULLPOINTER, "parent->leaf");
leaf = (akgl_BSPLeaf *)context->left->leaf;
e1intersect = SDL_HasRectIntersectionFloat(&leaf->extent, &actor->bbox);
leaf = (akgl_BSPLeaf *)context->right->leaf;
e2intersect = SDL_HasRectIntersectionFloat(&leaf->extent, &actor->bbox);
if ( e1intersect && e2intersect ) {
// Actors who cross left/right boundaries get left in the context of their parent, so both
@@ -100,7 +109,8 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp_actoriter(aksl_ListNode *no
if ( dest != NULL ) {
PASS(e, akgl_heap_next_list(&newnode));
newnode->data = node->data;
PASS(e, aksl_list_push(dest->leaf.actors, newnode));
leaf = (akgl_BSPLeaf *)dest->leaf;
PASS(e, aksl_list_push(leaf->actors, newnode));
actor->bsphome = dest;
actor->bsplistnode = newnode;
}
@@ -129,7 +139,8 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp_actoriter(aksl_ListNode *no
*/
akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp(aksl_TreeNode *root, aksl_ListNode *actors, SDL_FRect extents, uint8_t direction, uint8_t depth, uint8_t maxdepth)
{
akgl_BSPContext context;
akgl_BSPContext context = {0, 0, 0};
akgl_BSPLeaf *leaf = NULL;
// Until we have reached maxdepth levels of depth
PREPARE_ERROR(e);
@@ -143,58 +154,67 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp(aksl_TreeNode *root, aksl_L
// (the top half of a vertical division is "left", bottom half is "right")
if ( root->left == NULL ) {
PASS(e, akgl_heap_next_tree((aksl_TreeNode **)&root->left));
PASS(e, akgl_heap_next_list((aksl_ListNode **)&root->left->leaf->actors));
context.left = root->left;
PASS(e, akgl_heap_next_list((aksl_ListNode **)&((akgl_BSPLeaf *)root->left->leaf)->actors));
root->left->parent = root;
leaf = root->left->leaf;
if ( direction == AKGL_STAGE_PARTITION_HORIZONTAL ) {
context.left.extent.x = extents.x;
context.left.extent.y = extents.y;
context.left.extent.w = extents.w;
context.left.extent.h = (extents.h / 2);
leaf->extent.x = extents.x;
leaf->extent.y = extents.y;
leaf->extent.w = extents.w;
leaf->extent.h = (extents.h / 2);
} else if ( direction == AKGL_STAGE_PARTITION_VERTICAL ) {
context.left.extent.x = extents.x;
context.left.extent.y = extents.y;
context.left.extent.w = (extents.w / 2);
context.left.extent.h = extents.h;
leaf->extent.x = extents.x;
leaf->extent.y = extents.y;
leaf->extent.w = (extents.w / 2);
leaf->extent.h = extents.h;
} else {
FAIL_RETURN(e, AKERR_VALUE, "Invalid partition direction %d", direction);
}
}
if ( root->right == NULL ) {
PASS(e, akgl_heap_next_tree((aksl_TreeNode **)&root->right));
PASS(e, akgl_heap_next_list((aksl_ListNode **)&root->right->leaf->actors));
context.right = root->right;
PASS(e, akgl_heap_next_list((aksl_ListNode **)&((akgl_BSPLeaf *)root->right->leaf)->actors));
root->right->parent = root;
leaf = root->right->leaf;
if ( direction == AKGL_STAGE_PARTITION_HORIZONTAL ) {
context.right.extent.x = extents.x;
context.right.extent.y = extents.y + (extents.h / 2);
context.right.extent.w = extents.w;
context.right.extent.h = (extents.h / 2);
leaf->extent.x = extents.x;
leaf->extent.y = extents.y + (extents.h / 2);
leaf->extent.w = extents.w;
leaf->extent.h = (extents.h / 2);
} else if ( direction == AKGL_STAGE_PARTITION_VERTICAL ) {
context.right.extent.x = extents.x + (extents.w / 2);
context.right.extent.y = extents.y;
context.right.extent.w = (extents.w / 2);
context.right.extent.h = extents.h;
leaf->extent.x = extents.x + (extents.w / 2);
leaf->extent.y = extents.y;
leaf->extent.w = (extents.w / 2);
leaf->extent.h = extents.h;
} else {
FAIL_RETURN(e, AKERR_VALUE, "Invalid partition direction %d", direction);
}
}
if ( root->leaf->actors == NULL ) {
PASS(e, akgl_heap_next_list((aksl_ListNode **)&root->leaf->actors));
context.parent = root;
if ( ((akgl_BSPLeaf *)root->leaf)->actors == NULL ) {
PASS(e, akgl_heap_next_list((aksl_ListNode **)&((akgl_BSPLeaf *)root->leaf)->actors));
}
context.left = root->left;
context.right = root->right;
context.parent = root;
// - Test (SDL_HasIntersection) all actor objects in the current working space
// against the SDL_Frects for both halves. If they intersect, push the actors
// down into the leaf linked lists for each half
PASS(e, aksl_list_iterate(actors, &akgl_stage_2d_bsp_actoriter, &context));
ATTEMPT {
CATCH(e, aksl_list_iterate(actors, &akgl_stage_2d_bsp_actoriter, &context));
} CLEANUP {
} PROCESS(e) {
} HANDLE(e, AKERR_NULLPOINTER) {
SDL_Log("Empty actor list");
} FINISH(e, true);
PASS(e, akgl_stage_2d_partition_switchdirection(direction, &direction));
// - Recurse down into the left BSP node, changing our partitioning
// (vertical->horizontal, horizontal->vertical etc)
PASS(e, akgl_stage_2d_bsp(root->left, root->left->leaf->actors, context.left.extent, direction, depth + 1, maxdepth));
PASS(e, akgl_stage_2d_bsp(root->left, ((akgl_BSPLeaf *)root->left->leaf)->actors, ((akgl_BSPLeaf *)context.left->leaf)->extent, direction, depth + 1, maxdepth));
// - Recurse down into the right BSP node
// (vertical->horizontal, horizontal->vertical etc)
PASS(e, akgl_stage_2d_bsp(root->right, root->right->leaf->actors, context.right.extent, direction, depth + 1, maxdepth));
PASS(e, akgl_stage_2d_bsp(root->right, ((akgl_BSPLeaf *)root->right->leaf)->actors, ((akgl_BSPLeaf *)context.right->leaf)->extent, direction, depth + 1, maxdepth));
SUCCEED_RETURN(e);
}
@@ -234,7 +254,10 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_partition(akgl_Stage *self)
FAIL_ZERO_RETURN(e, self, AKERR_NULLPOINTER, "self");
PASS(e, akgl_stage_2d_bspfree(self));
self->bsp.leaf = &self->_rootleaf;
memset((void *)&self->_rootleaf, 0x00, sizeof(akgl_BSPLeaf));
// Build a linked list of all actors currently initialized
PASS(e, akgl_heap_next_list(&actors));
curnode = actors;
@@ -255,6 +278,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_partition(akgl_Stage *self)
* a simple Mario style sidescroller or even a JRPG, but for something like
* a bullet hell, it will be essential.
*/
((akgl_BSPLeaf *)self->bsp.leaf)->extent = (SDL_FRect){0, 0, camera->w, camera->h};
ATTEMPT {
CATCH(e, akgl_stage_2d_bsp(
&self->bsp,
@@ -318,7 +342,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp_move(akgl_Stage *self, akgl
} else {
// Actor already exists somewhere in the BSP tree
// 1. Is it still in the extents for its bsphome? If so, do nothing.
if ( SDL_HasRectIntersectionFloat(&actor->bsphome->leaf->extent, &actor->bbox) ) {
if ( SDL_HasRectIntersectionFloat(&((akgl_BSPLeaf *)actor->bsphome->leaf)->extent, &actor->bbox) ) {
SUCCEED_RETURN(e);
}
// 2. If not, pop the actor out of its current BSP actor list, and start walking up.
@@ -327,20 +351,20 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_stage_2d_bsp_move(akgl_Stage *self, akgl
tmp = actor->bsplistnode;
while ( curnode != NULL ) {
// 3. Is it within the current BSP node's extents? If not, walk up and repeat.
if ( SDL_HasRectIntersectionFloat(&curnode->leaf->extent, &actor->bbox) ) {
if ( SDL_HasRectIntersectionFloat(&((akgl_BSPLeaf *)curnode->leaf)->extent, &actor->bbox) ) {
// 4. Find which extent it matches.
// FIXME : akgl_BSPContext should be replaced with aksl_TreeNode at this point, it's fully redundant
context.left = curnode->left;
context.right = curnode->right;
context.parent = curnode->parent;
PASS(e, akgl_stage_2d_bsp_actoriter(tmp, actor->bsplistnode, &context));
PASS(e, akgl_stage_2d_bsp_actoriter(actor->bsplistnode, &context));
}
curnode = curnode->parent;
}
// Release the old actor node, it will have been moved to a new one by now
if ( tmp == actor->bsplistnode ) {
// Odd ...
SDL_Log("Expected actor %d to be moved to a different BSP node after moving, but it still has the same list node...\n", actor);
SDL_Log("Expected actor %p to be moved to a different BSP node after moving, but it still has the same list node...\n", actor);
} else {
PASS(e, akgl_heap_release_list(tmp));
}