Skip to content

Commit

Permalink
feat!: remove JointType (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 26, 2022
1 parent 6fdf564 commit 42a3952
Show file tree
Hide file tree
Showing 42 changed files with 603 additions and 141 deletions.
1 change: 0 additions & 1 deletion packages/forge2d/lib/forge2d.dart
Expand Up @@ -70,7 +70,6 @@ export 'src/dynamics/joints/gear_joint_def.dart';
export 'src/dynamics/joints/jacobian.dart';
export 'src/dynamics/joints/joint.dart';
export 'src/dynamics/joints/joint_def.dart';
export 'src/dynamics/joints/joint_type.dart';
export 'src/dynamics/joints/limit_state.dart';
export 'src/dynamics/joints/motor_joint.dart';
export 'src/dynamics/joints/motor_joint_def.dart';
Expand Down
Expand Up @@ -10,10 +10,6 @@ class ConstantVolumeJointDef<A extends Body> extends JointDef<A, A> {
final List<A> bodies = <A>[];
final List<DistanceJoint> joints = [];

ConstantVolumeJointDef() : super(JointType.constantVolume) {
collideConnected = false;
}

/// Adds a body to the group
///
/// @param argBody
Expand Down
Expand Up @@ -15,8 +15,6 @@ class DistanceJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The damping ratio. 0 = no damping, 1 = critical damping.
double dampingRatio = 0.0;

DistanceJointDef() : super(JointType.distance);

/// Initialize the bodies, anchors, and length using the world anchors.
///
/// @param b1 First body
Expand Down
Expand Up @@ -8,8 +8,6 @@ class FrictionJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The maximum friction torque in N-m.
double maxTorque = 0.0;

FrictionJointDef() : super(JointType.friction);

/// Initialize the bodies, anchors, axis, and reference angle using the world anchor and world
/// axis.
void initialize(A bodyA, B bodyB, Vector2 anchor) {
Expand Down
24 changes: 11 additions & 13 deletions packages/forge2d/lib/src/dynamics/joints/gear_joint.dart
Expand Up @@ -32,9 +32,6 @@ class GearJoint extends Joint {
final Joint _joint1;
final Joint _joint2;

final JointType _typeA;
final JointType _typeB;

// Body A is connected to body C
// Body B is connected to body D
final Body _bodyC;
Expand Down Expand Up @@ -71,13 +68,14 @@ class GearJoint extends Joint {
GearJoint(GearJointDef def)
: _joint1 = def.joint1,
_joint2 = def.joint2,
_typeA = def.joint1.type,
_typeB = def.joint2.type,
_bodyC = def.joint1.bodyA,
_bodyD = def.joint2.bodyA,
super(def) {
assert(_typeA == JointType.revolute || _typeA == JointType.prismatic);
assert(_typeB == JointType.revolute || _typeB == JointType.prismatic);
assert(
(_joint1 is RevoluteJoint || _joint1 is PrismaticJoint) &&
(_joint2 is RevoluteJoint || _joint2 is PrismaticJoint),
'A GearJoint can only be joint with RevoluteJoint and/or PrismaticJoint.',
);

double coordinateA, coordinateB;

Expand All @@ -90,7 +88,7 @@ class GearJoint extends Joint {
final xfC = _bodyC.transform;
final aC = _bodyC.sweep.a;

if (_typeA == JointType.revolute) {
if (_joint1 is RevoluteJoint) {
final revolute = def.joint1 as RevoluteJoint;
_localAnchorC.setFrom(revolute.localAnchorA);
localAnchorA.setFrom(revolute.localAnchorB);
Expand Down Expand Up @@ -124,7 +122,7 @@ class GearJoint extends Joint {
final xfD = _bodyD.transform;
final aD = _bodyD.sweep.a;

if (_typeB == JointType.revolute) {
if (_joint2 is RevoluteJoint) {
final revolute = def.joint2 as RevoluteJoint;
_localAnchorD.setFrom(revolute.localAnchorA);
localAnchorB.setFrom(revolute.localAnchorB);
Expand Down Expand Up @@ -228,7 +226,7 @@ class GearJoint extends Joint {

final temp = Vector2.zero();

if (_typeA == JointType.revolute) {
if (_joint1 is RevoluteJoint) {
_jvAC.setZero();
_jwA = 1.0;
_jwC = 1.0;
Expand All @@ -250,7 +248,7 @@ class GearJoint extends Joint {
_mass += _mC + _mA + _iC * _jwC * _jwC + _iA * _jwA * _jwA;
}

if (_typeB == JointType.revolute) {
if (_joint2 is RevoluteJoint) {
_jvBD.setZero();
_jwB = _ratio;
_jwD = _ratio;
Expand Down Expand Up @@ -399,7 +397,7 @@ class GearJoint extends Joint {
double jwA, jwB, jwC, jwD;
var mass = 0.0;

if (_typeA == JointType.revolute) {
if (_joint1 is RevoluteJoint) {
jvBC.setZero();
jwA = 1.0;
jwC = 1.0;
Expand Down Expand Up @@ -435,7 +433,7 @@ class GearJoint extends Joint {
coordinateA = (pA..sub(pC)).dot(_localAxisC);
}

if (_typeB == JointType.revolute) {
if (_joint2 is RevoluteJoint) {
jvBD.setZero();
jwB = _ratio;
jwD = _ratio;
Expand Down
2 changes: 0 additions & 2 deletions packages/forge2d/lib/src/dynamics/joints/gear_joint_def.dart
Expand Up @@ -13,6 +13,4 @@ class GearJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
///
/// @see GearJoint
double ratio = 0.0;

GearJointDef() : super(JointType.gear);
}
80 changes: 31 additions & 49 deletions packages/forge2d/lib/src/dynamics/joints/joint.dart
Expand Up @@ -4,33 +4,33 @@ import '../../../forge2d.dart';
/// joints also feature limits and motors.
abstract class Joint {
static T create<T extends Joint>(World world, JointDef def) {
if (T == ConstantVolumeJoint || def.type == JointType.constantVolume) {
if (T == ConstantVolumeJoint || def is ConstantVolumeJointDef) {
return ConstantVolumeJoint(world, def as ConstantVolumeJointDef) as T;
} else if (T == DistanceJoint || def.type == JointType.distance) {
} else if (T == DistanceJoint || def is DistanceJointDef) {
return DistanceJoint(def as DistanceJointDef) as T;
} else if (T == FrictionJoint || def.type == JointType.friction) {
} else if (T == FrictionJoint || def is FrictionJointDef) {
return FrictionJoint(def as FrictionJointDef) as T;
} else if (T == GearJoint || def.type == JointType.gear) {
} else if (T == GearJoint || def is GearJointDef) {
return GearJoint(def as GearJointDef) as T;
} else if (T == MotorJoint || def.type == JointType.motor) {
} else if (T == MotorJoint || def is MotorJointDef) {
return MotorJoint(def as MotorJointDef) as T;
} else if (T == MouseJoint || def.type == JointType.mouse) {
} else if (T == MouseJoint || def is MouseJointDef) {
return MouseJoint(def as MouseJointDef) as T;
} else if (T == PrismaticJoint || def.type == JointType.prismatic) {
} else if (T == PrismaticJoint || def is PrismaticJointDef) {
return PrismaticJoint(def as PrismaticJointDef) as T;
} else if (T == PulleyJoint || def.type == JointType.pulley) {
} else if (T == PulleyJoint || def is PulleyJointDef) {
return PulleyJoint(def as PulleyJointDef) as T;
} else if (T == RevoluteJoint || def.type == JointType.revolute) {
} else if (T == RevoluteJoint || def is RevoluteJointDef) {
return RevoluteJoint(def as RevoluteJointDef) as T;
} else if (T == RopeJoint || def.type == JointType.rope) {
} else if (T == RopeJoint || def is RopeJointDef) {
return RopeJoint(def as RopeJointDef) as T;
} else if (T == WeldJoint || def.type == JointType.weld) {
} else if (T == WeldJoint || def is WeldJointDef) {
return WeldJoint(def as WeldJointDef) as T;
} else if (T == WheelJoint || def.type == JointType.wheel) {
} else if (T == WheelJoint || def is WheelJointDef) {
return WheelJoint(def as WheelJointDef) as T;
} else {
throw ArgumentError(
'Invalid joint type $T with invalid joint definition ${def.type}',
'Invalid joint type $T with invalid joint definition $def',
);
}
}
Expand All @@ -39,7 +39,6 @@ abstract class Joint {
joint.destructor();
}

final JointType _type;
late Body bodyA;
late Body bodyB;

Expand All @@ -52,19 +51,13 @@ abstract class Joint {
Joint(JointDef def)
: assert(def.bodyA != def.bodyB),
localAnchorA = def.localAnchorA,
localAnchorB = def.localAnchorB,
_type = def.type {
localAnchorB = def.localAnchorB {
bodyA = def.bodyA;
bodyB = def.bodyB;
_collideConnected = def.collideConnected;
islandFlag = false;
}

/// get the type of the concrete joint.
///
/// @return
JointType get type => _type;

/// Whether the body is connected to the joint
bool containsBody(Body body) => body == bodyA || body == bodyB;

Expand Down Expand Up @@ -131,34 +124,23 @@ abstract class Joint {

_color.setFromRGBd(0.5, 0.8, 0.8);

switch (type) {
case JointType.distance:
debugDraw.drawSegment(p1, p2, _color);
break;

case JointType.pulley:
{
final pulley = this as PulleyJoint;
final s1 = pulley.getGroundAnchorA();
final s2 = pulley.getGroundAnchorB();
debugDraw.drawSegment(s1, p1, _color);
debugDraw.drawSegment(s2, p2, _color);
debugDraw.drawSegment(s1, s2, _color);
}
break;

case JointType.friction:
debugDraw.drawSegment(x1, x2, _color);
break;

case JointType.constantVolume:
case JointType.mouse:
// don't draw this
break;
default:
debugDraw.drawSegment(x1, p1, _color);
debugDraw.drawSegment(p1, p2, _color);
debugDraw.drawSegment(x2, p2, _color);
if (this is DistanceJoint) {
debugDraw.drawSegment(p1, p2, _color);
} else if (this is PulleyJoint) {
final pulley = this as PulleyJoint;
final s1 = pulley.getGroundAnchorA();
final s2 = pulley.getGroundAnchorB();
debugDraw.drawSegment(s1, p1, _color);
debugDraw.drawSegment(s2, p2, _color);
debugDraw.drawSegment(s1, s2, _color);
} else if (this is FrictionJoint) {
debugDraw.drawSegment(x1, x2, _color);
} else if (this is ConstantVolumeJoint || this is MouseJoint) {
// Don't draw these joints.
} else {
debugDraw.drawSegment(x1, p1, _color);
debugDraw.drawSegment(p1, p2, _color);
debugDraw.drawSegment(x2, p2, _color);
}
}
}
13 changes: 5 additions & 8 deletions packages/forge2d/lib/src/dynamics/joints/joint_def.dart
Expand Up @@ -4,27 +4,24 @@ import '../../../forge2d.dart';
/// [JointDef]s are used to construct [Joint]s.
///
/// All [Joint]s are connected between two different [Body]ies.
/// One [Body] may [BodyType.static].
/// One [Body] may be [BodyType.static].
///
/// [Joint]s between [BodyType.static] and/or [BodyType.kinematic] are allowed,
/// but have no effect and use some processing time.
///
/// If possible, avoid directly extending [JointDef]. Instead, extend from an
/// already defined [JointDef] implementation.
/// Don't subclass [JointDef]. Instead, extend from an already defined
/// [JointDef] subclass.
/// {@endtemplate}
abstract class JointDef<A extends Body, B extends Body> {
/// {@macro dynamics.joints.joint_def.type}
JointDef(this.type, [this.collideConnected = false]);
/// {@macro dynamics.joints.joint_def}
JointDef([this.collideConnected = false]);

/// The local anchor point relative to body1's origin.
final Vector2 localAnchorA = Vector2.zero();

/// The local anchor point relative to body2's origin.
final Vector2 localAnchorB = Vector2.zero();

/// The joint type is set automatically for concrete joint types.
JointType type;

/// Use this to attach application specific data to your joints.
Object? userData;

Expand Down
14 changes: 0 additions & 14 deletions packages/forge2d/lib/src/dynamics/joints/joint_type.dart

This file was deleted.

2 changes: 0 additions & 2 deletions packages/forge2d/lib/src/dynamics/joints/motor_joint_def.dart
Expand Up @@ -17,8 +17,6 @@ class MotorJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// Position correction factor in the range [0,1].
double correctionFactor = 0.3;

MotorJointDef() : super(JointType.motor);

void initialize(A bodyA, B bodyB) {
this.bodyA = bodyA;
this.bodyB = bodyB;
Expand Down
2 changes: 0 additions & 2 deletions packages/forge2d/lib/src/dynamics/joints/mouse_joint_def.dart
Expand Up @@ -14,6 +14,4 @@ class MouseJointDef<A extends Body, B extends Body> extends JointDef<A, B> {

/// The damping ratio. 0 = no damping, 1 = critical damping.
double dampingRatio = .7;

MouseJointDef() : super(JointType.mouse);
}
Expand Up @@ -32,8 +32,6 @@ class PrismaticJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The desired motor speed in radians per second.
double motorSpeed = 0.0;

PrismaticJointDef() : super(JointType.prismatic);

/// Initialize the bodies, anchors, axis, and reference angle using the world anchor and world
/// axis.
void initialize(A b1, B b2, Vector2 anchor, Vector2 axis) {
Expand Down
Expand Up @@ -19,9 +19,7 @@ class PulleyJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The pulley ratio, used to simulate a block-and-tackle.
double ratio = 1.0;

PulleyJointDef() : super(JointType.pulley) {
collideConnected = true;
}
PulleyJointDef() : super(true);

/// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
void initialize(
Expand Down
Expand Up @@ -31,8 +31,6 @@ class RevoluteJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The maximum motor torque used to achieve the desired motor speed. Usually in N-m.
double maxMotorTorque = 0.0;

RevoluteJointDef() : super(JointType.revolute);

/// Initialize the bodies, anchors, and reference angle using the world anchor.
///
/// @param b1
Expand Down
Expand Up @@ -7,7 +7,7 @@ class RopeJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// will have no effect.
double maxLength = 0.0;

RopeJointDef() : super(JointType.rope) {
RopeJointDef() : super() {
localAnchorA.setValues(-1.0, 0.0);
localAnchorB.setValues(1.0, 0.0);
}
Expand Down
2 changes: 0 additions & 2 deletions packages/forge2d/lib/src/dynamics/joints/weld_joint_def.dart
Expand Up @@ -10,8 +10,6 @@ class WeldJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// The damping ratio. 0 = no damping, 1 = critical damping.
double dampingRatio = 0.0;

WeldJointDef() : super(JointType.weld);

/// Initialize the bodies, anchors, and reference angle using a world anchor point.
void initialize(A bodyA, B bodyB, Vector2 anchor) {
this.bodyA = bodyA;
Expand Down
Expand Up @@ -24,7 +24,7 @@ class WheelJointDef<A extends Body, B extends Body> extends JointDef<A, B> {
/// Suspension damping ratio, one indicates critical damping
double dampingRatio = 0.0;

WheelJointDef() : super(JointType.wheel) {
WheelJointDef() : super() {
localAxisA.setValues(1.0, 0.0);
}

Expand Down
@@ -1,6 +1,5 @@
import 'package:forge2d/forge2d.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
import 'package:test/test.dart';

void main() {
group('ConstantVolumeJointDef', () {
Expand Down

0 comments on commit 42a3952

Please sign in to comment.