0

I changed box2d example to spawn circles. But the physic looks wrong. For example, when the circle stops jumping, it is not on the rectangle, but is immersed in the rectangle.

solidRect(600, 100, Colors.WHITE)
    .position(100, 412)
    .registerBodyWithFixture(
        type = BodyType.STATIC,
        friction = 0.2
    )

onClick {
    val pos = it.currentPosLocal
    
    circle(16.0)
        .position(pos.x, pos.y)
        .size(32,32) // does not affect
        .registerBodyWithFixture(
            type = BodyType.DYNAMIC,
            friction = 0.5,
            restitution = 0.5,
            density = 0.2
        )
}

Result Circle immersed in rectangle when using box2d in korge

1

vimuth
  • 5,064
  • 33
  • 79
  • 116

1 Answers1

0

I think the issue is not here, where you define the box2d circle, but later, when you render. Box2D for a circle shape has the position at the -center-. However, usually sprite libs have two coordinates, the bottom left and also the offset to the center of the shape. The bottom left is where the texture is rendered to, and the offset is used for rotation. This is true for libGDX sprites as here Explanation on libgdx draw method . You might not be using libGDX sprites of course.

Now admittedly as libGDX uses bottom left with y going up you get a rendering discrepency thats different from what you might imagine would happen if box2D center were declared to the renderer as bottom left of the image, but anyway I think the where the issue comes is the translation from box2D to wherever you are rendering, and not in the box2D code you have. So whichever method of rendering you are using there is a mistake in coordinate translation.

i.e. sprite bottom left should be position-(size/2)

londonBadger
  • 611
  • 2
  • 5
  • 5