feat: stored WorldManifold

pull/267/head
alestiago 3 years ago
parent 3231b9a6e6
commit b13436c253

@ -1,4 +1,5 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template bumping_behavior}
@ -11,14 +12,17 @@ class BumpingBehavior extends ContactBehavior {
/// Determines how strong the bump is.
final double _strength;
/// This is used to recoginze the current state of a contact manifold in world
/// coordinates.
@visibleForTesting
final WorldManifold worldManifold = WorldManifold();
@override
void postSolve(Object other, Contact contact, ContactImpulse impulse) {
super.postSolve(other, contact, impulse);
if (other is! BodyComponent) return;
final worldManifold = WorldManifold();
contact.getWorldManifold(worldManifold);
other.body.applyLinearImpulse(
worldManifold.normal
..multiply(

@ -7,20 +7,17 @@ import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/src/components/bumping_behavior.dart';
import '../../helpers/helpers.dart';
import 'layer_test.dart';
class MockContactImpulse extends Mock implements ContactImpulse {}
class _MockContact extends Mock implements Contact {}
class MockManifold extends Mock implements Manifold {}
class _MockContactImpulse extends Mock implements ContactImpulse {}
class TestHeavyBodyComponent extends BodyComponent {
class _TestBodyComponent extends BodyComponent {
@override
Body createBody() {
final shape = CircleShape();
return world.createBody(
BodyDef(
type: BodyType.dynamic,
),
BodyDef(type: BodyType.dynamic),
)..createFixtureFromShape(shape, 20);
}
}
@ -32,7 +29,7 @@ void main() {
group('BumpingBehavior', () {
flameTester.test('can be added', (game) async {
final behavior = BumpingBehavior(strength: 0);
final component = TestBodyComponent();
final component = _TestBodyComponent();
await component.add(behavior);
await game.ensureAdd(component);
});
@ -40,43 +37,39 @@ void main() {
flameTester.testGameWidget(
'the bump is greater when the strengh is greater',
setUp: (game, tester) async {
final component1 = TestBodyComponent();
final behavior1 = BumpingBehavior(strength: 1);
final component1 = _TestBodyComponent();
final behavior1 = BumpingBehavior(strength: 1)
..worldManifold.normal.setFrom(Vector2.all(1));
await component1.add(behavior1);
final component2 = TestBodyComponent();
final behavior2 = BumpingBehavior(strength: 2);
final component2 = _TestBodyComponent();
final behavior2 = BumpingBehavior(strength: 2)
..worldManifold.normal.setFrom(Vector2.all(1));
await component2.add(behavior2);
final dummy1 = TestHeavyBodyComponent();
final dummy2 = TestHeavyBodyComponent();
final other1 = _TestBodyComponent();
final other2 = _TestBodyComponent();
await game.ensureAddAll([
component1,
component2,
dummy1,
dummy2,
other1,
other2,
]);
expect(dummy1.body.inverseMass, greaterThan(0));
expect(dummy2.body.inverseMass, greaterThan(0));
final contact = _MockContact();
final contactImpulse = _MockContactImpulse();
final contact = MockContact();
final manifold = MockManifold();
final contactImpulse = MockContactImpulse();
when(() => manifold.localPoint).thenReturn(Vector2.all(1));
when(() => contact.manifold).thenReturn(manifold);
behavior1.postSolve(dummy1, contact, contactImpulse);
behavior2.postSolve(dummy2, contact, contactImpulse);
behavior1.postSolve(other1, contact, contactImpulse);
behavior2.postSolve(other2, contact, contactImpulse);
expect(
dummy2.body.linearVelocity.x,
greaterThan(dummy1.body.linearVelocity.x),
other2.body.linearVelocity.x,
greaterThan(other1.body.linearVelocity.x),
);
expect(
dummy2.body.linearVelocity.y,
greaterThan(dummy1.body.linearVelocity.y),
other2.body.linearVelocity.y,
greaterThan(other1.body.linearVelocity.y),
);
},
);

Loading…
Cancel
Save