Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage.stageToScreenCoordinates returns incorrect value with y offset in viewport #5854

Closed
jusw85 opened this issue Nov 29, 2019 · 8 comments · Fixed by #5923
Closed

Stage.stageToScreenCoordinates returns incorrect value with y offset in viewport #5854

jusw85 opened this issue Nov 29, 2019 · 8 comments · Fixed by #5923

Comments

@jusw85
Copy link

jusw85 commented Nov 29, 2019

Issue details

Stage.stageToScreenCoordinates returns incorrect value with y offset in viewport

Reproduction steps/code

Create a 100x100 window.
Setup a stage with a viewport in the top quarter of the window i.e. width 100, height 25, y=75
stage.stageToScreenCoordinates returns incorrect values.

public void create() {
     Viewport viewport = new Viewport() {
         @Override
         public void update(int screenWidth, int screenHeight, boolean centerCamera) {
             assert screenWidth == 100 && screenHeight == 100;
             setScreenBounds(0, 75, 100, 25);
             setWorldSize(100, 25);
             apply(centerCamera);
         }
     };
     viewport.setCamera(new OrthographicCamera());
     Stage stage = new Stage(viewport);

     Vector2 tmpvec = new Vector2();

     tmpvec.set(0, 0);
     stage.getViewport().project(tmpvec);
     System.out.println(tmpvec); // prints (0, 75), correct so far

     tmpvec.set(0, 0);
     stage.stageToScreenCoordinates(tmpvec);
     System.out.println(tmpvec); // prints (0, -50), should be (0, 25)
 }

Due to the following line in Stage:

stageCoords.y = viewport.getScreenHeight() - stageCoords.y;

After getting the result of camera.project (in absolute screen coordinates with origin at bottom left), it converts to top left origin using viewport.screenHeight instead of actual window height.

Version of LibGDX and/or relevant dependencies

1.9.10

@Tom-Ski Tom-Ski closed this as completed Nov 29, 2019
@jusw85
Copy link
Author

jusw85 commented Nov 29, 2019

This is unrelated to the #5853

@jusw85
Copy link
Author

jusw85 commented Nov 29, 2019

With reference to the example code:

     tmpvec.set(0, 0);
     stage.stageToScreenCoordinates(tmpvec);
     System.out.println(tmpvec);

(0,0) is within the bounds of the window. Why is it returning (0,-50) which is outside the window?

@Tom-Ski Tom-Ski reopened this Nov 29, 2019
@Tom-Ski
Copy link
Member

Tom-Ski commented Nov 29, 2019

Yeah, this doesn't look right. Why do you expect 0,25 though?

@jusw85
Copy link
Author

jusw85 commented Nov 29, 2019

  1. The window has width=100, height=100.
  2. The viewport has screenWidth=100, screenHeight=25, screenX=0, screenY=75 i.e. top 1/4 of the window.
  3. (0, 0) in world coordinates should correspond to (0, 75) in screen coordinates (origin at bottom left). This is verified by stage.getViewport().project(tmpvec);. This calls camera.project.

Relevant camera.project documentation:

The screen coordinate system has its origin in the <b>bottom</b> left, with the y-axis pointing <b>upwards</b> and the x-axis pointing to the right.

Relevant code:

public Vector2 project (Vector2 worldCoords) {

public Vector3 project (Vector3 worldCoords, float viewportX, float viewportY, float viewportWidth, float viewportHeight) {

  1. stage.stageToScreenCoordinates has origin at top left. Therefore it should be (0,25) (or thereabouts), certainly not (0,-50).

stageToScreenCoordinates code, which uses viewport.screenHeight to convert the origin:

public Vector2 stageToScreenCoordinates (Vector2 stageCoords) {

@Tom-Ski
Copy link
Member

Tom-Ski commented Nov 29, 2019

stage.stageToScreenCoordinates shouldn't have origin at top left, so it shouldn't be 0,25. Screen is bottom left unless otherwise specified, and project is sticking to this standard. As with the previous issue, the documentation probably needs improving here also.

Anyway, this result seems wrong, so will have to do further testing.

@jusw85
Copy link
Author

jusw85 commented Nov 29, 2019

stage.stageToScreenCoordinates shouldn't have origin at top left

I reasoned this from the way the source code and actual tests work, specifically this line stageCoords.y = viewport.getScreenHeight() - stageCoords.y;.

It works perfectly fine, returning correct result with origin top left if viewport has no offset.

[edit] actually origin top left is used almost everywhere:

viewport.toScreenCoordinates (origin top left)

/** Transforms a point to real screen coordinates (as opposed to OpenGL ES window coordinates), where the origin is in the top
* left and the the y-axis is pointing downwards. */

camera.unproject (origin top left)

The x- and y-coordinate of vec are assumed to be in screen coordinates (origin is the top left corner, y pointing down, x pointing to the right)

@NathanSweet
Copy link
Member

NathanSweet commented Feb 29, 2020

Didn't mean for this to get closed (happened via GH's automatic magicke).

@NathanSweet NathanSweet reopened this Feb 29, 2020
mquickmann pushed a commit to mquickmann/libgdx that referenced this issue Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@NathanSweet @jusw85 @Tom-Ski and others