libgdx Fixed point after camera.rotateAround

自作多情 提交于 2019-12-12 05:03:56

问题


Good night friends.

I'm having trouble drawing a fixed point on the screen when the screen is rotated. I used the method "rotateAround" from the position of the player.

It seems to me. I have to rotate this fixed point also from the position of the player. I use this stretch learned here in stackoverflow.

 public void rotate(Vector3 position, Vector3 centerPoint){
    this.cosTemp = MathUtils.cosDeg(this.anguloAtual);
    this.senTemp = MathUtils.sinDeg(this.anguloAtual);

    this.xTemp = centerPoint.x + ((position.x - centerPoint.x) * this.cosTemp) - ((position.y - centerPoint.y) * this.senTemp);
    this.yTemp = centerPoint.y + ((position.y - centerPoint.y) * this.cosTemp) + ((position.x - centerPoint.x) * this.senTemp);

    position.set(this.xTemp, this.yTemp, 0);
}

In the drawing that the player on the screen. I used the position of the player, then called "camera.project" then the method "rotate". The fixed point appears, however it is not exactly fixed. I used the example of a fixed point slightly ahead of the player.

public void meDesenhar(SpriteBatch spriteBatch) {

    spriteBatch.begin();
    this.spritePlayer.setPosition(this.positionPlayer.x - (this.spritePlayer.getWidth() / 2),
                                    this.positionPlayer.y - this.spritePlayer.getHeight() / 2);

    this.spritePlayer.draw(spriteBatch);
    spriteBatch.end();

    originPosition.set(positionPlayer, 0);
    fixedPosition.set(positionPlayer.x, positionPlayer.y + 10, 0);

    cameraTemp.project(fixedPosition);
    cameraTemp.project(originPosition);

    cameraManagerTemp.rotate(fixedPosition, originPosition);
    Debugagem.drawPointInScreen(Color.BLUE, fixedPosition);
}

My questions:

1 - I am doing something wrong, or just it is a result of rounding? I realized when debugging. The position of the player changed a little every rotation after the "camera.project". Example position (540, 320) turned (539.99, 320.013)

2 - I tried using and enjoying the SpriteBatch the draw method to perform the rotation however, could not make the rotation from the player. I would arrive at the same result.

3 - Can I use two cameras? Each camera would be a layer. A camera at the map and the player would be. The other for fixed point. It's viable? I could not find any example that works with more than one camera at the same time. Anyone know any examples please. I'm not talking about huds or cameras to stage.

Video follows.

https://www.youtube.com/watch?v=1Vg8haN5ULE

Thank you.


回答1:


  1. It can be result of rounding because its moving a pixel.
  2. You can calculate rotation from the player but its not necessary.
  3. Of course you can use multiple cameras in your game and you should also in this case.

Its few screenshot from my old projects that i used multiple cameras

As you can see you can even use different type of cameras like ortho and perspective both 2D and 3D.

Just create new camera like first one and change projection matrix

camrotate = new OrthographicCamera(540, 960);
//...
camfixed =  new OrthographicCamera(540, 960);
//...

And in render method

batch.setProjectionMatrix(camrotate.combined);
batch.begin();
//draw in camrotate now
//...
//...
batch.end();

batch.setProjectionMatrix(camfixed.combined);
batch.begin();
//draw fixed elements now
//...
//...   
batch.end();
//add one more camera if you need

Edit: Change projection matrix outside of batch.begin()/end() otherwise the current batch will flushed.



来源:https://stackoverflow.com/questions/39028834/libgdx-fixed-point-after-camera-rotatearound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!