简体   繁体   中英

Wrong origin coordinates logic for polygon

what i have now in test project, where i try to fix this problem:

2 polygon objects

2 actors, each of actor contain one polygon object

1 array with actors, i run through it in render method and with use of shapeRenderer draw polygons

1 OrthographicCamera

1 Stage

1 ScreenViewport

The logic is next: i create polygon 1 and polygon 2 with the same size and same position at 0,0 .

Than i do setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2). Than i translate one polygon by 50 pixel on axis X and run setOrigin(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2). And i added InputListener for first polygon, when i click on it i do rotateBy on second actor with second polygon. My camera has Y-Up. On rotation second polygon think that his origin point is top right corner of the screen and do rotation around it.

Here is and gif of result: http://i.imgur.com/CDzqTta.gif

And scheme how it behave: http://i.imgur.com/dCB0HDX.jpg

And here is gif of its behave if i maximize window: http://i.imgur.com/6ZKn33d.gif . Here we can see that after resize, green polygon do rotation around the point which was before resize top right cornern of the screen.

Here is a test project with this problem https://www.dropbox.com/s/ti2iiwm2cbom62f/polygonRotation.zip?dl=0

Could anyone help to find out where is a problem?

Thank you.

Both your camera and your first polygon are in the position (0,0) .

Your camera is the same size as your screen. So:

setOrigin(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2);

sets the point to the top right corner of it.

If you want the point to be in the middle, then set it like this:

setOrigin(0, 0);

Alternatively, you can keep the origin point and just move the camera instead. So the world origin is in the Bottom Left corner of it.

camera.position.set(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2);

在此处输入图片说明

It seems like you misuse the setOrigin method. In your code in PolygonRotation.java :

public void create () {
  float w = Gdx.graphics.getWidth();
  float h = Gdx.graphics.getHeight();

  //...

  p1.setVertices(getVertices(0, 0, 20, 5));
  p2.setVertices(getVertices(0, 0, 20, 5));

  pActor pa1 = new pActor();
  pActor pa2 = new pActor();

  actors.add(pa1);
  actors.add(pa2);

  //...

  pa1.setPosition(w/2, h/2);

  //...

  pa2.setPosition(pa1.getX(), pa1.getY());

  //...

  pa2.setOrigin(pa1.getX(), pa1.getY()); // <-- This line

  //...

  drawViewportBox(w, h);
}

Origin is not the coordinates of polygon's center, but is rather the offset from it.

According to your code both polygons are positioned into center of the screen ( w/2 , h/2 ).

Then you set origin of w/2 , h/2 for second polygon which sums up with it's current position and equals to w and h . In other words origin is set to the right upper corner of the screen, which is clearly the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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