0

The effect to be achieved is to let the bodies drop down one by one.

The previous code was to set all the bodies DYNAMIC when they were created, when the index increased the position.y increased, this made them ordered in a high column, some bodies crossed through the other after they dropped down, so I choose the plan below.

The bodies' type are set to STATIC when they are created, and then animationTimer set the body to DYNAMIC one by one so that they keep a certain distance when they fall.

The problem is that the bodies are sometime fixed on the top (checked, still STATIC type), 1 out of 15-20 on my test. I can't figure out why this is happening, is it a problem of timestep or something else?

for (i in 0..20) {
    val bodyDef = BodyDef()
    bodyDef.type = BodyType.STATIC
    bodyDef.position = Vec2(3.6f, 0f)

    val shape = PolygonShape()
    shape.setAsBox(2.0f, 1.0f)

    val fixture = FixtureDef()
    fixture.shape = shape
    fixture.friction = 0.2f
    fixture.restitution = 0.1f
    fixture.density = 0.6f
    val body = mWorld!!.createBody(bodyDef)
    body.isFixedRotation = true
    body.createFixture(fixture)
    discBodies.add(body)
}

var i = 0
val animationTimer = Timer()
animationTimer.schedule(object: TimerTask() {
    override fun run() {
        val body = discBodies[i]
        body.type = BodyType.DYNAMIC
        body.isAwake = true
        body.applyForceToCenter(Vec2(0.0f, 3f))
        if (i == discBodies.size - 1) {
            animationTimer.cancel()
        }
        i += 1
    }
}, 0, 80)
jdleung
  • 1,088
  • 2
  • 10
  • 26

0 Answers0