mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Handle removal of sprite physics bodies during the physics simulation
This commit is contained in:
parent
071201a5bb
commit
ab104c809d
@ -83,6 +83,7 @@ class TestBed extends NodeWithSize {
|
|||||||
|
|
||||||
void myCallback(PhysicsContactType type, PhysicsContact contact) {
|
void myCallback(PhysicsContactType type, PhysicsContact contact) {
|
||||||
print("CONTACT type: $type");
|
print("CONTACT type: $type");
|
||||||
|
contact.nodeB.removeFromParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handleEvent(SpriteBoxEvent event) {
|
bool handleEvent(SpriteBoxEvent event) {
|
||||||
|
@ -120,7 +120,7 @@ class PhysicsBody {
|
|||||||
|
|
||||||
void _detach() {
|
void _detach() {
|
||||||
if (_attached) {
|
if (_attached) {
|
||||||
_physicsNode.b2World.destroyBody(_body);
|
_physicsNode._bodiesScheduledForDestruction.add(_body);
|
||||||
_attached = false;
|
_attached = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,8 @@ class PhysicsNode extends Node {
|
|||||||
|
|
||||||
_ContactHandler _contactHandler;
|
_ContactHandler _contactHandler;
|
||||||
|
|
||||||
|
List<box2d.Body> _bodiesScheduledForDestruction = [];
|
||||||
|
|
||||||
double b2WorldToNodeConversionFactor = 500.0;
|
double b2WorldToNodeConversionFactor = 500.0;
|
||||||
|
|
||||||
Offset get gravity {
|
Offset get gravity {
|
||||||
@ -57,6 +59,9 @@ class PhysicsNode extends Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _stepPhysics(double dt) {
|
void _stepPhysics(double dt) {
|
||||||
|
// Remove bodies that were marked for destruction during the update phase
|
||||||
|
_removeBodiesScheduledForDestruction();
|
||||||
|
|
||||||
// Calculate a step in the simulation
|
// Calculate a step in the simulation
|
||||||
b2World.stepDt(dt, 10, 10);
|
b2World.stepDt(dt, 10, 10);
|
||||||
|
|
||||||
@ -71,6 +76,16 @@ class PhysicsNode extends Node {
|
|||||||
|
|
||||||
body._node._setRotationFromPhysics(degrees(b2Body.getAngle()));
|
body._node._setRotationFromPhysics(degrees(b2Body.getAngle()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove bodies that were marked for destruction during the simulation
|
||||||
|
_removeBodiesScheduledForDestruction();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _removeBodiesScheduledForDestruction() {
|
||||||
|
for (box2d.Body b2Body in _bodiesScheduledForDestruction) {
|
||||||
|
b2World.destroyBody(b2Body);
|
||||||
|
}
|
||||||
|
_bodiesScheduledForDestruction.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updatePosition(PhysicsBody body, Point position) {
|
void _updatePosition(PhysicsBody body, Point position) {
|
||||||
@ -257,6 +272,11 @@ class _ContactHandler extends box2d.ContactListener {
|
|||||||
b2Contact.isTouching(),
|
b2Contact.isTouching(),
|
||||||
b2Contact.isEnabled()
|
b2Contact.isEnabled()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (type == PhysicsContactType.postSolve) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Make callback
|
// Make callback
|
||||||
info.callback(type, contact);
|
info.callback(type, contact);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user