Skip to content

Commit f1e5b3f

Browse files
committed
adds update_static_bounds
1 parent 4acda3e commit f1e5b3f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

echo/Body.hx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ class Body implements IDisposable {
283283
else shapes.push(shape);
284284
shape.set_parent(frame);
285285
dirty = true;
286+
update_static_bounds();
286287
}
287288
return shape;
288289
}
@@ -291,6 +292,7 @@ class Body implements IDisposable {
291292
if (shapes.remove(shape)) {
292293
shape.set_parent();
293294
dirty = true;
295+
update_static_bounds();
294296
}
295297
return shape;
296298
}
@@ -306,8 +308,7 @@ class Body implements IDisposable {
306308
shapes.resize(0);
307309
}
308310
/**
309-
* Gets the Body's position as a new `Vector2`.
310-
* @return Vector2
311+
* Gets the Body's position as a new `Vector2` (or sets the `Vector2`, if passed in).
311312
*/
312313
public function get_position(?vec2:Vector2):Vector2 return vec2 == null ? frame.offset.clone() : vec2.set(frame.offset.x, frame.offset.y);
313314

@@ -367,7 +368,18 @@ class Body implements IDisposable {
367368
* @return body.mass == 0
368369
*/
369370
public inline function is_static() return mass <= 0;
370-
371+
/**
372+
* If the Body is Static, update it's Quadtree Bounds.
373+
*/
374+
public inline function update_static_bounds() {
375+
if (is_static() && world != null) {
376+
bounds(quadtree_data.bounds);
377+
world.static_quadtree.update(quadtree_data);
378+
}
379+
}
380+
/**
381+
* Returns true if the Body has moved since the last `Physics.step()`.
382+
*/
371383
public inline function moved() return !x.equals(last_x, 0.001) || !y.equals(last_y, 0.001) || !rotation.equals(last_rotation, 0.001);
372384
/**
373385
* Disposes the Body. DO NOT use the Body after disposing it, as it could lead to null reference errors.
@@ -376,6 +388,7 @@ class Body implements IDisposable {
376388
remove();
377389
for (shape in shapes) shape.put();
378390
shapes = null;
391+
// TODO - dispose (or remove?) children Bodies
379392
velocity = null;
380393
max_velocity = null;
381394
drag = null;
@@ -405,10 +418,7 @@ class Body implements IDisposable {
405418
// TODO - Child Body transforms
406419
// sync_children();
407420

408-
if (is_static() && world != null) {
409-
bounds(quadtree_data.bounds);
410-
world.static_quadtree.update(quadtree_data);
411-
}
421+
update_static_bounds();
412422

413423
if (on_move != null) on_move(frame.offset.x, frame.offset.y);
414424
}
@@ -425,10 +435,7 @@ class Body implements IDisposable {
425435
// TODO - Child Body transforms
426436
// sync_children();
427437

428-
if (is_static() && world != null) {
429-
bounds(quadtree_data.bounds);
430-
world.static_quadtree.update(quadtree_data);
431-
}
438+
update_static_bounds();
432439

433440
if (on_move != null) on_move(frame.offset.x, frame.offset.y);
434441
}
@@ -440,6 +447,7 @@ class Body implements IDisposable {
440447
shapes[0] = value;
441448
shapes[0].set_parent(frame);
442449
dirty = true;
450+
update_static_bounds();
443451
return shapes[0];
444452
}
445453

@@ -452,10 +460,7 @@ class Body implements IDisposable {
452460
// TODO - Child Body transforms
453461
// sync_children();
454462

455-
if (is_static() && world != null) {
456-
bounds(quadtree_data.bounds);
457-
world.static_quadtree.update(quadtree_data);
458-
}
463+
update_static_bounds();
459464

460465
if (on_rotate != null) on_rotate(rotation);
461466
}
@@ -466,12 +471,13 @@ class Body implements IDisposable {
466471
inline function set_mass(value:Float):Float {
467472
if (value < 0.0001) {
468473
mass = inverse_mass = 0;
469-
if (world != null) {
470-
bounds(quadtree_data.bounds);
471-
world.static_quadtree.update(quadtree_data);
472-
}
474+
update_static_bounds();
473475
}
474476
else {
477+
// If the Body was previously Static, remove it from the static quadtree
478+
if (is_static() && world != null) {
479+
world.static_quadtree.remove(quadtree_data);
480+
}
475481
mass = value;
476482
inverse_mass = 1 / mass;
477483
}

0 commit comments

Comments
 (0)