I need to render a DistanceJoint, just a line connecting the 2 anchors, nothing fancy. I've seen something about a render method here but I coulen't figure out how to use it. I also don't mind using debug features. Thank you very much :)
Asked
Active
Viewed 135 times
1 Answers
1
Since you have tagged the question with Flame I'm guessing that you are using flame_forge2d
?
The link that you provide is for pure forge2d web, but you should be able to do it in a similar fashion in flame_forge2d
.
I would try something like this in your Forge2DGame
:
final _bodyARenderPosition = Vector2.zero();
final _bodyBRenderPosition = Vector2.zero();
@override
void render(Canvas c) {
super.render(c);
c.save();
c.scale(camera.zoom);
for (final joint in world.joints) {
_bodyARenderPosition
..setFrom(joint.bodyA.position)
..y *= -1;
_bodyBRenderPosition
..setFrom(joint.bodyB.position)
..y *= -1;
c.drawLine(
_bodyARenderPosition.toOffset(),
_bodyBRenderPosition.toOffset(),
debugPaint,
);
}
c.restore();
}

spydon
- 9,372
- 6
- 33
- 63
-
Hmm... drawLine is called on the correct joints but I can't see any lines. Also, debugPaint is white while the background is black and the positions of bodyA/B is different. – Adam Katav Jul 19 '22 at 19:18
-
Here's a link to my code, it's bad I know https://github.com/adamkatav/sipl_client/blob/main/lib/game.dart – Adam Katav Jul 19 '22 at 19:25
-
1It looks like you might be using a quite old version of flame_forge2d, try to invert the y-axis of a cloned version of the body position before drawing the line. (Or update to a newer version of `flame_forge2d`, but then you'll have some other things to sort out first since there has been breaking changes). – spydon Jul 19 '22 at 19:49
-
Updating didn't help but inverting the Y exis did Thank you very much! Why did it work? – Adam Katav Jul 19 '22 at 19:57
-
No problem! :) Right, because here we are looking directly at the body positions that are coming from `Forge2D` and not on the `BodyComponent` positions, `Forge2D` is using a flipped coordinate system, where Y is shrinking downwards instead of growing like it is on the canvas, that's why the positions have to be flipped when rendered. – spydon Jul 19 '22 at 20:02
-
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246591/discussion-between-adam-katav-and-spydon). – Adam Katav Jul 19 '22 at 20:05
-
Sorry, didn't mean to write that, though I do have another question if that's okay – Adam Katav Jul 19 '22 at 20:07