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

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

Loading…
Cancel
Save