Skip to content

Commit

Permalink
feat: allowed specifying BodyDef properties via constructor (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 29, 2022
1 parent 1c09a13 commit aa3f5f7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 37 deletions.
98 changes: 66 additions & 32 deletions packages/forge2d/lib/src/dynamics/body_def.dart
@@ -1,58 +1,92 @@
import '../../forge2d.dart';

/// A body definition holds all the data needed to construct a rigid body. You can safely re-use body
/// definitions. Shapes are added to a body after construction.
/// Holds all the data needed to construct a [Body].
///
/// You can safely re-use body definitions.
///
/// [Shape]s are added through [Fixture]s to a [Body] after construction via
/// [Body.createFixture].
class BodyDef {
/// The body type: static, kinematic, or dynamic. Note: if a dynamic body would have zero mass, the
/// mass is set to one.
BodyType type = BodyType.static;
BodyDef({
this.type = BodyType.static,
this.userData,
Vector2? position,
this.angle = 0.0,
Vector2? linearVelocity,
this.angularVelocity = 0.0,
this.linearDamping = 0.0,
this.angularDamping = 0.0,
this.allowSleep = true,
this.isAwake = true,
this.fixedRotation = false,
this.bullet = false,
this.active = true,
this.gravityScale = 1.0,
}) : position = position ?? Vector2.zero(),
linearVelocity = linearVelocity ?? Vector2.zero();

/// The body type: static, kinematic, or dynamic.
///
/// Note: A [BodyType.dynamic] body with zero mass, will have a mass of one.
BodyType type;

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

/// The world position of the body. Avoid creating bodies at the origin since this can lead to many
/// overlapping shapes.
Vector2 position = Vector2.zero();
/// The world position of the body.
///
/// Avoid creating bodies at the origin since this can lead to many
/// overlapping [Shape]s.
Vector2 position;

/// The world angle of the body in radians.
double angle = 0.0;
double angle;

/// The linear velocity of the body in world co-ordinates.
Vector2 linearVelocity = Vector2.zero();
Vector2 linearVelocity;

/// The angular velocity of the body.
double angularVelocity = 0.0;
double angularVelocity;

/// Linear damping is use to reduce the linear velocity. The damping parameter can be larger than
/// 1.0f but the damping effect becomes sensitive to the time step when the damping parameter is
/// large.
double linearDamping = 0.0;
/// Linear damping is use to reduce the linear velocity.
///
/// The damping parameter can be larger than 1.0f but the damping effect
/// becomes sensitive to the time step when the damping parameter is large.
double linearDamping;

/// Angular damping is use to reduce the angular velocity. The damping parameter can be larger than
/// 1.0f but the damping effect becomes sensitive to the time step when the damping parameter is
/// large.
double angularDamping = 0.0;
/// Angular damping is use to reduce the angular velocity.
///
/// The damping parameter can be larger than 1.0f but the damping effect
/// becomes sensitive to the time step when the damping parameter is large.
double angularDamping;

/// Set this flag to false if this body should never fall asleep. Note that this increases CPU
/// usage.
bool allowSleep = true;
/// Set this flag to false if this body should never fall asleep.
///
/// Note: Not alllowing a body to sleep increases CPU usage.
bool allowSleep;

/// Is this body initially sleeping?
bool isAwake = true;
bool isAwake;

/// Should this body be prevented from rotating? Useful for characters.
bool fixedRotation = false;
/// Should this body be prevented from rotating?
///
/// Useful for characters.
bool fixedRotation;

/// Is this a fast moving body that should be prevented from tunneling through other moving bodies?
/// Note that all bodies are prevented from tunneling through kinematic and static bodies. This
/// setting is only considered on dynamic bodies.
/// Is this a fast moving body that should be prevented from tunneling through
/// other moving bodies?
///
/// Note: All bodies are prevented from tunneling through [BodyType.kinematic]
/// and [BodyType.static] bodies. This setting is only considered on
/// [BodyType.dynamic] bodies.
///
/// @warning You should use this flag sparingly since it increases processing time.
bool bullet = false;
/// Warning: You should use this flag sparingly since it increases processing
/// time.
bool bullet;

/// Does this body start out active?
bool active = true;
bool active;

/// Experimental: scales the inertia tensor.
double gravityScale = 1.0;
double gravityScale;
}
20 changes: 15 additions & 5 deletions packages/forge2d/lib/src/dynamics/body_type.dart
@@ -1,5 +1,15 @@
/// The body type.
/// static: zero mass, zero velocity, may be manually moved
/// kinematic: zero mass, non-zero velocity set by user, moved by solver
/// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
enum BodyType { static, kinematic, dynamic }
import '../../forge2d.dart';

/// Defines the type of a [Body].
enum BodyType {
/// Defines a [Body] with zero mass, zero velocity, may be manually moved.
static,

/// Defines a [Body] with zero mass, non-zero velocity set by user, moved by
/// solver.
kinematic,

/// Defines a [Body] with positive mass, non-zero velocity determined by
/// forces, moved by solver.
dynamic,
}
10 changes: 10 additions & 0 deletions packages/forge2d/test/dynamics/body_def_test.dart
@@ -0,0 +1,10 @@
import 'package:forge2d/forge2d.dart';
import 'package:test/test.dart';

void main() {
group('BodyDef', () {
test('can be instantiated', () {
expect(BodyDef(), isA<BodyDef>());
});
});
}

0 comments on commit aa3f5f7

Please sign in to comment.