Friction, velocity, rendering

This commit is contained in:
2025-10-09 09:44:17 -05:00
parent c31bcf7f6a
commit 7622f81309
7 changed files with 93 additions and 75 deletions

View File

@@ -8,32 +8,33 @@
#include "worldunit.h"
#include "assert/assert.h"
void worldChunkPosAdd(
worldchunkpos_t *pos,
const worldsubtile_t amt
) {
int8_t a = pos->subtile; // current signed subtile
int8_t b = amt; // signed delta
uint8_t unsignedAdded = (uint8_t)a + (uint8_t)b; // well-defined wrap add
int8_t r = (int8_t)unsignedAdded; // result in signed domain
void worldChunkPosAdd(worldchunkpos_t *pos, worldsubtile_t amt) {
assertNotNull(pos, "Position pointer cannot be NULL");
pos->subtile = r;
/*
int32_t sum = (int32_t)pos->subtile + (int32_t)amt;
int32_t shifted = sum + 128; // may be negative or large
int32_t carry = div_floor(shifted, 256); // tiles to add (can be neg)
int32_t rem = mod_floor(shifted, 256); // 0..255
int32_t new_sub = rem - 128; // back to [-128,127]
// Signed-overflow detection for a + b -> r:
// overflow if sign(a) == sign(b) and sign(r) != sign(a)
uint8_t ov = (uint8_t)((a ^ r) & (b ^ r)) >> 7; // 1 if overflow, else 0
// Direction of carry: +1 for b >= 0, -1 for b < 0 (computed branchlessly)
uint8_t neg = ((uint8_t)b) >> 7; // 0 if b>=0, 1 if b<0
int8_t dir = (int8_t)(1 - (neg << 1)); // +1 or -1
// Apply tile adjustment (mod-256 via uint8_t arithmetic)
pos->tile = (uint8_t)(pos->tile + (uint8_t)(ov * (uint8_t)dir));
pos->subtile = (int8_t)new_sub;
pos->tile = (uint8_t)((int32_t)pos->tile + carry); // wrap mod 256
*/
int32_t shiftedTotal = (int32_t)pos->subtile + (int32_t)amt + 128;
int32_t tileCarry = shiftedTotal >> 8; // divide by 256
int32_t wrappedSubtile = shiftedTotal - (tileCarry << 8);
pos->subtile = (int8_t)(wrappedSubtile - 128);
pos->tile = (uint8_t)(pos->tile + (uint8_t)tileCarry);
}
float_t worldChunkPosToF32(
const worldchunkpos_t pos,
const uint8_t tileSize
) {
return (float_t)(pos.tile * tileSize) + ((float_t)pos.subtile / 256.0f);
float_t worldChunkPosToF32(worldchunkpos_t pos, const uint8_t tileSize) {
const float scaleFactor = (float)tileSize * (1.0f / 256.0f);
return (
(float)pos.tile * (float)tileSize + ((float)pos.subtile + 128.0f) *
scaleFactor
);
}