Skip to content

Commit 5ebd52e

Browse files
obiotclaude
andauthored
Fix floating containers with Infinity bounds not rendering (#1356)
* Fix floating containers with Infinity bounds not rendering (HUD missing) After the Container refactor defaulting dimensions to Infinity, floating containers (like the HUD UIContainer) had empty bounds (min=Infinity, max=-Infinity). camera.isVisible() checked bounds.left <= camera.width, which is Infinity <= 800 = false, so the container was never updated and its children never rendered. Fix: treat floating objects with non-finite bounds as always visible. Also adjust platformer score position for the new baseline alignment. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: narrow cleared-bounds check, add test coverage - Check for exact cleared bounds sentinel (min=Infinity, max=-Infinity) instead of generic !isFinite() to avoid masking NaN bugs - Add tests for floating objects with cleared bounds visibility Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6db3d9b commit 5ebd52e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

packages/examples/src/examples/platformer/entities/HUD.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class UIContainer extends Container {
167167
this.name = "HUD";
168168

169169
// add our child score object at position
170-
this.addChild(new ScoreItem(-10, -10));
170+
this.addChild(new ScoreItem(-10, -30));
171171

172172
// add our audio control object
173173
this.addChild(new AudioControl(36, 56));

packages/melonjs/src/camera/camera2d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ export default class Camera2d extends Renderable {
779779
// floating objects use screen coordinates, check against the
780780
// camera viewport area (0, 0, camera.width, camera.height)
781781
const objBounds = obj.getBounds();
782+
if (
783+
objBounds.left === Infinity &&
784+
objBounds.top === Infinity &&
785+
objBounds.right === -Infinity &&
786+
objBounds.bottom === -Infinity
787+
) {
788+
// containers with no explicit size (cleared Infinity bounds) are always visible
789+
return true;
790+
}
782791
return (
783792
objBounds.left <= this.width &&
784793
objBounds.right >= 0 &&

packages/melonjs/tests/camera.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,29 @@ describe("Camera2d", () => {
229229
});
230230
});
231231

232+
describe("isVisible() with cleared/infinite bounds", () => {
233+
it("should treat floating object with cleared bounds as visible", () => {
234+
const { camera } = setup();
235+
camera.reset(0, 0);
236+
// simulate a floating container with Infinity dimensions (cleared bounds)
237+
const container = new Renderable(0, 0, Infinity, Infinity);
238+
container.floating = true;
239+
// clear bounds to the sentinel state (min=Infinity, max=-Infinity)
240+
container.getBounds().clear();
241+
expect(camera.isVisible(container)).toEqual(true);
242+
});
243+
244+
it("should treat non-floating object with cleared bounds as not visible", () => {
245+
const { camera } = setup();
246+
camera.reset(0, 0);
247+
camera.setBounds(0, 0, 2000, 2000);
248+
const obj = new Renderable(0, 0, 10, 10);
249+
obj.floating = false;
250+
obj.getBounds().clear();
251+
expect(camera.isVisible(obj)).toEqual(false);
252+
});
253+
});
254+
232255
describe("reset()", () => {
233256
it("should reset the camera position to 0,0 by default", () => {
234257
const { camera } = setup();

0 commit comments

Comments
 (0)