What is wrong in my libgdx box2d texture codes

Viewed 34

I am trying aling texture and a rectangle body.The problem is when body collide something texture is not rotating with body.It is rotating itself.I tried all method which I saw in internet. What do I do wrong.Thanks.

in show

        val props = startPoint.properties
        props2=props
        val x=props.get("x") as Float
        val y= props.get("y") as Float
        val width=props.get("width") as Float
        val height=props.get("height") as Float
        boxBody=tma.makeRectagleBody(x, y, width,height)
        box = Box(props.get("x") as Float, props.get("y") as Float, mainStage)
        box.setSize(props.get("width") as Float, props.get("height") as Float)

in render

 box.setPosition(boxBody.position.x * MyConstants.PPM - props2.get("width") as Float / 2, 
                    boxBody.position.y * MyConstants.PPM - props2.get("height") as Float / 2)
    box.setRotation(boxBody.getAngle() * MathUtils.radiansToDegrees)

make body method

fun makeRectagleBody(x: Float, y: Float, width: Float, height: Float):Body{

    val shapeRectangle = PolygonShape()
    shapeRectangle.setAsBox(genislik / 2 / MyConstants.PPM, yukseklik / 2 / MyConstants.PPM)
    val bdefRectangle = BodyDef()
    bdefRectangle.type = BodyDef.BodyType.DynamicBody
    bdefRectangle.position.set((x + genislik / 2) / MyConstants.PPM,
            (y + yukseklik / 2) / MyConstants.PPM)
    val bodyRectangle = world.createBody(bdefRectangle)
    bodyRectangle.createFixture(shapeRectangle, .5f)//Eğer densityyi ayarlamazsan karakter yine rahat hareket etmiyor. Dikkat et.
    return bodyRectangle

in Box

if (animation != null && isVisible) { batch.draw(animation!!.getKeyFrame(elapsedTime),x, y, originX, originY, width, height, scaleX, scaleY, rotation)
        }
        super.draw(batch, parentAlpha)

enter image description here enter image description here enter image description here

1 Answers

I solved my problem. by adding

box.setOrigin(box.getWidth()/2,box.getHeight()/2)

under

box.setRotation(boxBody.getAngle() * MathUtils.radiansToDegrees)
Related