Skip to content

Commit

Permalink
feat: allowed specifying FixtureDef properties via constructor (#52)
Browse files Browse the repository at this point in the history
* feat: allowed specifying FixtureDef properties via constructor

* refactor!: made default friction 0

* feat: made friction default 0.2
  • Loading branch information
alestiago committed Mar 29, 2022
1 parent 6cf6503 commit 1c09a13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
38 changes: 25 additions & 13 deletions packages/forge2d/lib/src/dynamics/fixture_def.dart
@@ -1,29 +1,41 @@
import '../../forge2d.dart';

/// A fixture definition is used to create a fixture. This class defines an abstract fixture
/// definition. You can reuse fixture definitions safely.
/// Used to create a [Fixture].
///
/// You can reuse fixture definitions safely.
class FixtureDef {
/// The shape, this must be set. The shape will be cloned, so you can create the shape on the
FixtureDef(
this.shape, {
this.userData,
this.friction = 0.2,
this.restitution = 0,
this.density = 0,
this.isSensor = false,
Filter? filter,
}) : filter = filter ?? Filter();

/// The [Shape], this must be set.
///
/// The [Shape] will be [Shape.clone]d, so you can create the shape on the
/// stack.
Shape shape;

FixtureDef(this.shape);

/// Use this to store application specific fixture data.
Object? userData;

/// The friction coefficient, usually in the range [0,1].
double friction = 0.2;
double friction;

/// The restitution (elasticity) usually in the range [0,1].
double restitution = 0.0;
double restitution;

/// The density, usually in kg/m^2
double density = 0.0;
/// The density, usually in kg/m^2.
double density;

/// A sensor shape collects contact information but never generates a collision response.
bool isSensor = false;
/// A sensor shape collects contact information but never generates a
/// collision response.
bool isSensor;

/// Contact filtering data;
Filter filter = Filter();
/// Contact filtering data.
Filter filter;
}
24 changes: 24 additions & 0 deletions packages/forge2d/test/dynamics/fixture_def_test.dart
@@ -0,0 +1,24 @@
import 'package:forge2d/forge2d.dart';
import 'package:test/test.dart';

void main() {
group('FixtureDef', () {
group('can be instantiated', () {
test('when Shape is a ChainShape', () {
expect(FixtureDef(ChainShape()), isA<FixtureDef>());
});

test('when Shape is a CircleShape', () {
expect(FixtureDef(CircleShape()), isA<FixtureDef>());
});

test('when Shape is a EdgeShape', () {
expect(FixtureDef(EdgeShape()), isA<FixtureDef>());
});

test('when Shape is a PolygonShape', () {
expect(FixtureDef(PolygonShape()), isA<FixtureDef>());
});
});
});
}

0 comments on commit 1c09a13

Please sign in to comment.