简体   繁体   English

无法将jbox2d角度转换为slick2d角度

[英]Trouble converting jbox2d angle to slick2d angle

UPDATE Slick and JBox use radians that go in opposite directions, that's why I was having trouble. 更新 Slick和JBox使用的弧度方向相反,这就是我遇到麻烦的原因。

I am making a game using JBox2D and Slick2D (per the title). 我正在使用JBox2D和Slick2D(按标题)制作游戏。 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. 但是,Jbox2D中的冲突不同步。 Here is the pattern I have found: 这是我发现的模式:

1 unit = pi/4 in slick 1个= pi / 4 in slick

Slick direction: 光滑方向:

7___0___1 7___0___1

6___.___2 6 ___.___ 2

5___4___3 5___4___3

Jbox Direction: Jbox方向:

5___0___3 5___0___3

2___.___6 2 ___.___ 6

7___4___1 7___4___1

Really, I have no idea what is going on. 真的,我不知道发生了什么。 Can somebody help? 有人可以帮忙吗?

Okay. 好的。 It turns out that even thought Slick's transform and JBox's angle are both radians, They go in opposite directions . 事实证明,即使认为Slick的变换和JBox的角度都是弧度, 它们的方向也相反 So, I made the below code with the .getWorldPosition instead of transform. 因此,我使用.getWorldPosition而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM