libgdx - 组转换

时间:2014-03-14 14:13:31

标签: libgdx scene2d

我是LibGDX的新手。 我在show方法中使用actor创建了group并添加了listener(我在其中计算了旋转矩阵)并希望使用batch.setTransformMatrix(matrixResult)旋转group中的所有actor。但没有任何反应。如何强制旋转所有组的演员?

 @Override
   public void show() {
      camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      camera.position.set(0f, 0f, - 120);
      camera.lookAt(0,0,0);
      camera.near = 0.1f;
      camera.far = 300f;

      camera.update();
      stage = new Stage();
      shader = new ShaderProgram(vertexShader, fragmentShader);
      myBatch = new SpriteBatch();
      shapeRenderer = new ShapeRenderer();
      sphere = BuildSphere(5, 100);
      group = new Group(){
         @Override
         public void draw(Batch batch, float parentAlpha) {
            batch.setTransformMatrix(matrixResult);
            super.draw(batch, parentAlpha);

         }
      };

      group.setTouchable(Touchable.enabled);
      group.addListener(new InputListener(){

         @Override
         public boolean touchDown(InputEvent event, float x, float y,
               int pointer, int button) {
            Gdx.app.log("MOUSE_X_BEG", Float.toString(x));
            Gdx.app.log("MOUSE_Y_BEG", Float.toString(y));
            begX = x;
            begY = y;
            return true;
         }
         @Override
         public void touchDragged(InputEvent event, float x, float y,
               int pointer) {
            endX = x;
            endY = y;
            //isRotate = true;
            float translationX = endX-begX;
            float translationY = begY-endY;
            float length = (float)Math.sqrt(Math.pow(translationX,2) + Math.pow(translationY,2));
            Vector3 axis = new Vector3(0, 0, 0);
            if(length>0){
               if(begX-endX == 0){
                  axis.x = 1;
               }else{
                  float angle = (float) (Math.atan(translationY/translationX) + Math.PI/2.0*(translationX <0 ? -1 : 1));
                  axis.z = 1;
                  axis.x = (float) Math.cos(angle);
                  axis.y = (float) Math.sin(angle);
               }
            }
            quaternion = new Quaternion(axis, 0.02f*length);
            Matrix4 rotation = new Matrix4(quaternion);
            matrixResult = rotation.mul(matrixRotationOld);
            matrixRotationOld = matrixResult;


         }


      });
      //group.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      for (CenterCoordinate cenCoordinate : sphere.getPolygonCenterCoordinates()) {
         Vector3 vector = cenCoordinate.getCartesianCenter();
         Polygon polygon = new Polygon(1, cenCoordinate.getType().getCode(), cenCoordinate.getRadius(), cenCoordinate.getPsi(),
               cenCoordinate.getKsi(), vector, cenCoordinate.getRotation());

         group.addActor(polygon);
      }

      stage.addActor(group);
      Gdx.input.setInputProcessor(stage); 


   }

Polygon扩展了Actor绘制方法:

         @Override
  public void draw(Batch batch, float parentAlpha) {
     translation.setToTranslation(vertex.x, vertex.y, vertex.z);
     matrix.mul(translation);
     shader.begin();
     shader.setUniformMatrix("u_worldView", matrix);
     mesh.render(shader, GL20.GL_LINE_LOOP, 1, mesh.getNumVertices()-1);
     shader.end();
     matrix.idt();

  }

1 个答案:

答案 0 :(得分:0)

旋转batch不会旋转group。如果您想rotate rotate建议我Stage Stage的相机,并将SpriteBatch ProjectionMatrix s cam.combined设为rotate {1}}。但是Group只有rotateBy(degrees)使用setRotation(degrees)SpriteBatch方法。 旋转cameraActor实际上只是视图的更改,但旋转Group(在您的情况下为draw())会更改它们的逻辑。 在rotation方法中,您需要注意这个{{1}},然后您将获得所需的结果。