Trouble converting jbox2d angle to slick2d angle

69 views Asked by At

UPDATE Slick and JBox use radians that go in opposite directions, that's why I was having trouble.

I am making a game using JBox2D and Slick2D (per the title). So, because I couldn't find anything online about it, I wrote a bunch of code from scratch to convert between them. However, it seems as though the angles are different, even though both documentations say they use radians.

Here is my code:

//In the update function
angle = (float) (angle % 2*Math.PI);
mass = player.getMass();
position = player.getPosition();

if(input.isKeyDown(inputLeft)){
    angle-=0.015f*turnBlocks.size()/mass;        //turning, pt1
} else if(input.isKeyDown(inputRight)){
    angle+=0.015f*turnBlocks.size()/mass;
}

player.setTransform(position, angle);           //turning, pt2

if(input.isKeyDown(inputForward)){
    float xv = (float)(0.25f * Math.sin(angle) * 
                      thrustBlocks.size() / mass);  //Converting angle to vector
    float yv = (float)(0.25f * Math.cos(angle) * 
                       thrustBlocks.size() / mass);
    Vec2 curVel = player.getLinearVelocity();
    xv = xv + curVel.x;
    yv = yv + curVel.y;
    player.setLinearVelocity(new Vec2(xv, yv));
}

and

//In the render function
g.setColor(Color.gray);

for(int mass = 0; mass < massBlocks.size(); mass++){
    float boxx = (float)massBlocks.get(mass)[0];
    float boxy = (float)massBlocks.get(mass)[1];
    int[] slicklist = tr.toSlick(position.x+boxx, position.y+boxy);
    boxx = (float)slicklist[0];
    boxy = (float)slicklist[1];

    float[] ps = {boxx-tr.xscale/2, boxy-tr.yscale/2, 
              boxx+tr.xscale/2, boxy-tr.yscale/2, 
              boxx+tr.xscale/2, boxy+tr.yscale/2, 
              boxx-tr.xscale/2, boxy+tr.yscale/2};
    Polygon p = new Polygon(ps);
    //turning, pt3  
    g.fill(p.transform(Transform.createRotateTransform(radAngle, slickx, slicky)));
}

When I run the above code (with the rest of it), I get the player block(s) moving in the direction it shows it is facing. However, the collision in Jbox2D is out of sync. Here is the pattern I have found:

1 unit = pi/4 in slick

Slick direction:

7___0___1

6___.___2

5___4___3

Jbox Direction:

5___0___3

2___.___6

7___4___1

Really, I have no idea what is going on. Can somebody help?

1

There are 1 answers

0
Blue On

Okay. It turns out that even thought Slick's transform and JBox's angle are both radians, They go in opposite directions. So, I made the below code with the .getWorldPosition instead of transform.

        float localJBoxX = thrustBlocks.get(count)[0];
        float localJBoxY = thrustBlocks.get(count)[1];

        float[] localEndCoords = {localJBoxX+0.5f, localJBoxY+0.5f,
                                    localJBoxX-0.5f, localJBoxY+0.5f,
                                    localJBoxX-0.5f, localJBoxY-0.5f,
                                    localJBoxX+0.5f, localJBoxY-0.5f};

        float[] slickCoords = new float[localEndCoords.length];

        for(byte point = 0; point<localEndCoords.length/2; point++){

            Vec2 localPoint = new Vec2(localEndCoords[point*2], localEndCoords[point*2+1]);

            slickCoords[point*2] = (float)tr.toSlick(player.getWorldPoint(localPoint).x, player.getWorldPoint(localPoint).y)[0];
            slickCoords[point*2+1] = (float)tr.toSlick(player.getWorldPoint(localPoint).x, player.getWorldPoint(localPoint).y)[1];
        }

        Polygon box = new Polygon(slickCoords);
        g.fill(box.transform(new Transform()));  //as to return a shape