0
\$\begingroup\$

I am trying to make my Desktop game playable on IOS. I have a Button.cs class, which has this code in it:

   public bool IsPressed(TouchCollection touches)
    {
        for (int i = 0; i < touches.Count; i++)
        {
            if (TouchTriggerRect.Contains(touches[i].Position.X, touches[i].Position.Y))
            {
                return true;
            }
        }

        return false;
    }

Where TouchTriggerRect is a rectangle describing the button coordinates on the screen

I have noticed that the IsPressed method only works on some buttons (specifically, on all the buttons on the left side of the screen in Landscape orientation)

After some debugging, I understood that the IsPressed method doesn't work on the buttons placed on the right side of the screen because touches made there are getting offset by some random X and Y

Any suggestions on how to fix this bug? Or what can be causing this problem?

P.S I am using a Monogame.Extended.Camera2D in my game

P.S.S I have tried to build a simpler app, which draws red circles at all the touch locations it receives, and it workes perfectly. So, the problem is not in my devices

P.S.S.S This is how I get my touch Input: touches = TouchPanel.GetState();

UPD:

This is the code I use for my transformations:

var bounds = camera.BoundingRectangle;

Vector2 pos;

//The button is basically a rectangle, so I give it an X, a Y, a Width and a Height
JumpButton = new Button(bounds.Right - 80, bounds.Bottom - 74, 64, 64);
pos = camera.WorldToScreen(JumpButton.X, JumpButton.Y);
JumpButton.ScreenX = pos.X;
JumpButton.ScreenY = pos.Y;
//Same for all the other buttons 

And this is the code for TouchTriggerRect

public Rectangle TouchTriggerRect => new Rectangle((int)ScreenX, (int)ScreenY, Width, Height);
\$\endgroup\$
5
  • \$\begingroup\$ I'm sure the X and Y values are not random. Have you checked if this is some issue with screen vs world coordinates, world vs. camera or something like that? Do you have to transform the result first? \$\endgroup\$
    – Christian
    Jun 21, 2018 at 13:55
  • \$\begingroup\$ @Christian Well, maybe they are not random, but there is still an offset, which is not normal. Yes, I do take my camera into consideration and transform the coordinates (pretty sure I am doing it right, since it works when I touch the left side of the screen) \$\endgroup\$
    – Philippe
    Jun 21, 2018 at 14:00
  • \$\begingroup\$ I suspect that on the left side, there is still an offset, just not big enough to impact anything, and it grows larger the bigger X gets. If you're certain it's not caused by a wrong transormation though, I have no other idea right now, sorry. \$\endgroup\$
    – Christian
    Jun 21, 2018 at 14:11
  • \$\begingroup\$ Please post the code you're using to transform the coordinates in regards to the camera. That's most likely the cause. \$\endgroup\$ Jun 21, 2018 at 23:38
  • \$\begingroup\$ @craftworkgames posted it! \$\endgroup\$
    – Philippe
    Jun 22, 2018 at 14:29

2 Answers 2

1
\$\begingroup\$

Okay, well your transformation code is very strange. It looks like you're trying to transform the position of the button rather than the position of the mouse or touch coordinates.

The way you need to think about this stuff is that your positioning things in "virtual space" and letting the viewport adapter and camera transform things into "world space" or "screen space".

To illustrate my point let's pretend this image is our "virtual space".

virtual space vs scren space

Our "virtual" screen is 200x150 pixels. We place our button at 50,50 and the button is 100x50 pixels in size.

However, when we render our virtual space on the screen what we end up doing is scaling everything up to the screen resolution or window size. So now our 200x150 image is actually rendered at 800x600 pixels.

On the actual screen our button is drawn at 200,200 and is 400x200 pixels in size.

But here's the kicker. When we touch the center of the screen at 400,300 we're getting our touch / mouse coordinates in "screen space". What we want to do now is translate our input coordinates back into "virtual space".

This is the purpose of the WorldToScreen method.

var vp = camera.WorldToScreen(touches[i].Position.X, touches[i].Position.Y);

Now that our touch position is back into virtual space we can simply check to see if it's inside our button. We don't need to transform the size or position of the button because it's already defined in virtual space.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your answer, the problem was actually in my device! \$\endgroup\$
    – Philippe
    Jun 26, 2018 at 18:11
0
\$\begingroup\$

Problem solved by rebooting the mac, the device and the computer with VS

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .