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:flutter/material.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template bumping_behavior}
@ -11,15 +12,22 @@ 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;
contact.getWorldManifold(worldManifold);
other.body.applyLinearImpulse(
contact.manifold.localPoint
..normalize()
..multiply(Vector2.all(other.body.mass * _strength)),
worldManifold.normal
..multiply(
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 '../../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,
),
)..createFixtureFromShape(shape, 20);
}
Body createBody() => world.createBody(
BodyDef(type: BodyType.dynamic),
)..createFixtureFromShape(CircleShape(), 1);
}
void main() {
@ -32,7 +26,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,16 +34,18 @@ 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 dummy1 = _TestBodyComponent();
final dummy2 = _TestBodyComponent();
await game.ensureAddAll([
component1,
@ -58,14 +54,8 @@ void main() {
dummy2,
]);
expect(dummy1.body.inverseMass, greaterThan(0));
expect(dummy2.body.inverseMass, greaterThan(0));
final contact = MockContact();
final manifold = MockManifold();
final contactImpulse = MockContactImpulse();
when(() => manifold.localPoint).thenReturn(Vector2.all(1));
when(() => contact.manifold).thenReturn(manifold);
final contact = _MockContact();
final contactImpulse = _MockContactImpulse();
behavior1.postSolve(dummy1, contact, contactImpulse);
behavior2.postSolve(dummy2, contact, contactImpulse);

Loading…
Cancel
Save