Skip to content

Commit

Permalink
docs(example): DraggableBox does not follow the cursor smoothly (#3204)
Browse files Browse the repository at this point in the history
This PR makes the `DraggableBox` movement more smooth and predictable
by:
1) adjusting `FixtureDef` and `MouseJointDef` parameters
2) setting the `MouseJoint` target to
`game.screenToWorld(event.deviceEndPosition)` rather than
`event.localEndPosition`

Fixes #3203

The comparison recordings are presented below:

<details closed><summary>GearJointExample</summary>

  | BEFORE | AFTER |
  |----------|----------|
| <video
src="https://github.com/flame-engine/flame/assets/30288967/8188ed18-e0aa-4c0f-a6c4-092015ba471c">
| <video
src="https://github.com/flame-engine/flame/assets/30288967/2388bf23-ab32-43a8-af88-a974687b6901">
|

</details>

<details closed><summary>PrismaticJointExample</summary>

  | BEFORE | AFTER |
  |----------|----------|
| <video
src="https://github.com/flame-engine/flame/assets/30288967/80ca56cd-7b7c-4a12-a551-37f268011bb0">
| <video
src="https://github.com/flame-engine/flame/assets/30288967/aaae0f7d-230b-47db-a1c2-cc39cace74e7">
|

</details>

<details closed><summary>PulleyJointExample</summary>

  | BEFORE | AFTER |
  |----------|----------|
| <video
src="https://github.com/flame-engine/flame/assets/30288967/1d40b0a0-57d2-444e-a324-63112548f9e3">
| <video
src="https://github.com/flame-engine/flame/assets/30288967/6deb1226-0f96-440f-ba72-14806612bd32">
|

</details>

<details closed><summary>RopeJointExample</summary>

  | BEFORE | AFTER |
  |----------|----------|
| <video
src="https://github.com/flame-engine/flame/assets/30288967/60b2ceee-4a41-4443-a2db-de5515270f18">
| <video
src="https://github.com/flame-engine/flame/assets/30288967/c06e35ff-2c59-4a02-b6bb-7611817a76c0">
|

</details>
  • Loading branch information
ksokolovskyi committed Jun 24, 2024
1 parent 28fd2a0 commit 6e517fa
Showing 1 changed file with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class Box extends BodyComponent {
Body createBody() {
final shape = PolygonShape()
..setAsBox(width / 2, height / 2, Vector2.zero(), 0);
final fixtureDef = FixtureDef(shape, friction: 0.3, density: 10);
final fixtureDef = FixtureDef(
shape,
friction: 0.3,
restitution: 0.2,
density: 10,
);
final bodyDef = BodyDef(
userData: this, // To be able to determine object in collision
position: startPosition,
Expand Down Expand Up @@ -63,33 +68,42 @@ class DraggableBox extends Box with DragCallbacks {
}

@override
bool onDragUpdate(DragUpdateEvent info) {
final target = info.localEndPosition;
void onDragStart(DragStartEvent event) {
super.onDragStart(event);

final target = game.screenToWorld(event.devicePosition);

final mouseJointDef = MouseJointDef()
..maxForce = body.mass * 300
..dampingRatio = 0
..frequencyHz = 20
..target.setFrom(body.position)
..maxForce = 5000 * body.mass
..dampingRatio = 0.1
..frequencyHz = 50
..target.setFrom(target)
..collideConnected = false
..bodyA = groundBody
..bodyB = body;
mouseJoint = MouseJoint(mouseJointDef);

world.createJoint(mouseJoint!);
}

@override
bool onDragUpdate(DragUpdateEvent event) {
mouseJoint?.setTarget(
game.screenToWorld(event.deviceEndPosition),
);

if (mouseJoint == null) {
mouseJoint = MouseJoint(mouseJointDef);
world.createJoint(mouseJoint!);
} else {
mouseJoint?.setTarget(target);
}
return false;
}

@override
void onDragEnd(DragEndEvent info) {
super.onDragEnd(info);
void onDragEnd(DragEndEvent event) {
super.onDragEnd(event);

if (mouseJoint == null) {
return;
}

_destroyJoint = true;
info.continuePropagation = false;
event.continuePropagation = false;
}
}

0 comments on commit 6e517fa

Please sign in to comment.