feat: Optimize rendering and physics with dirty chunk tracking and adaptive update rates

This commit introduces several performance optimizations:
- Implement chunk-based dirty rendering
- Add adaptive physics update rates
- Return modification status from element update functions
- Reduce unnecessary rendering and physics calculations
- Track world movement for efficient re-rendering

The key changes include:
1. Adding `dirtyChunks` and `worldMoved` tracking
2. Modifying element update functions to return modification status
3. Implementing adaptive physics update rates based on FPS
4. Skipping rendering for unchanged chunks
5. Reducing computational overhead in physics and rendering loops

These optimizations should significantly improve the simulation's performance, especially with large numbers of elements.
This commit is contained in:
Kacper Kostka (aider)
2025-04-05 15:35:19 +02:00
parent 1369822dc9
commit 60a1757ab1
4 changed files with 70 additions and 5 deletions

View File

@@ -13,6 +13,11 @@ function render() {
const key = getChunkKey(chunkX, chunkY);
// Skip rendering if the chunk hasn't changed and isn't marked as dirty
if (!dirtyChunks.has(key) && !worldMoved) {
continue;
}
if (!chunks.has(key)) continue;
const chunk = chunks.get(key);
@@ -115,8 +120,14 @@ function render() {
);
}
}
// Remove this chunk from the dirty list after rendering
dirtyChunks.delete(key);
}
// Reset world moved flag after rendering
worldMoved = false;
// Draw cursor position and update debug info
if (currentMouseX !== undefined && currentMouseY !== undefined) {
const worldX = Math.floor(currentMouseX / PIXEL_SIZE) + worldOffsetX;