fix: based `BumpingBehavior` on world's normal (#267)

pull/269/head
Alejandro Santiago 3 years ago committed by GitHub
parent 60b71062b2
commit fc039ec082
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,15 +12,22 @@ 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;
contact.getWorldManifold(worldManifold);
other.body.applyLinearImpulse( other.body.applyLinearImpulse(
contact.manifold.localPoint worldManifold.normal
..normalize() ..multiply(
..multiply(Vector2.all(other.body.mass * _strength)), Vector2.all(other.body.mass * _strength),
),
); );
} }
} }

@ -7,22 +7,16 @@ 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() => world.createBody(
final shape = CircleShape(); BodyDef(type: BodyType.dynamic),
return world.createBody( )..createFixtureFromShape(CircleShape(), 1);
BodyDef(
type: BodyType.dynamic,
),
)..createFixtureFromShape(shape, 20);
}
} }
void main() { void main() {
@ -32,7 +26,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,16 +34,18 @@ 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 dummy1 = _TestBodyComponent();
final dummy2 = TestHeavyBodyComponent(); final dummy2 = _TestBodyComponent();
await game.ensureAddAll([ await game.ensureAddAll([
component1, component1,
@ -58,14 +54,8 @@ void main() {
dummy2, dummy2,
]); ]);
expect(dummy1.body.inverseMass, greaterThan(0)); final contact = _MockContact();
expect(dummy2.body.inverseMass, greaterThan(0)); 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); behavior1.postSolve(dummy1, contact, contactImpulse);
behavior2.postSolve(dummy2, contact, contactImpulse); behavior2.postSolve(dummy2, contact, contactImpulse);

Loading…
Cancel
Save