1 Replies - 660 Views - Last Post: 21 January 2016 - 10:04 AM Rate Topic: -----

#1 Sabotender   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-January 16

Need help with euclidean algorithm for game scrolling

Posted 19 January 2016 - 11:37 PM

Hiya, I'm looking for a some help with a bit of maths for my game. I have an array of tiles labeled from 0 to 14079 that are arranged like so on the attached image. I need to find out how this translates in the viewport space, as there are far too many unnecessary tiles to draw in a single call. The player moves around in the virtual space, but only the portion around the viewport, or camera is drawn, so I have to figure out which tiles in the array correspond to the tiles in virtual space, which then correspond to the screen coordinates of the viewport. Sorry it is difficult for me to explain it, I am not sure I can do better than what I just attempted, but I did attach that image to better illustrate what I am wanting. Please note, that image is not to scale at all. If you have any questions, I think it would be better for you ask directly.

Thanks for looking!

Btw, I am not asking for bunches of code, I just need the algorithm so I can translate things properly, and then I can continue with my game. These types of mats are lost to me. I just cannot see the answer. The closest I have gotten was something like... tileY * SCREEN_WIDTH + tileX, but that is about it.

Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with euclidean algorithm for game scrolling

#2 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Need help with euclidean algorithm for game scrolling

Posted 21 January 2016 - 10:04 AM

The answer is "your yellow area".

Even though the array appears to be two dimensional, all arrays are really one dimensional. So, you have to have the math to calculate the positions of the view port.

The upper left corner is one tile. So that should be trivial. The far right should be on the same row and the column should be the first tile plus the width of the view port in tiles.

You may need to convert between tiles and pixels depending on what you're doing. A tile should have a fixed pixel width.

The second row should be the hardest to find. I believe that's the first tile plus the width of the entire array, that is the width of the virtual space. In your drawing, that's 8. So if you count 8 plus the first yellow tile, you should get the second row. Add the width to that (8) to get the next row. Do that for how ever many rows you want.

Oh. And I would let the virtual space be virtual rather than drawing it. So, pixels would not matter for the virtual space. Only the drawn space would have to be converted to pixels.

For example, when trying to determine pixel coordinates for the first tile of the second row, I would make it the height of a tile plus 1 pixel down and 0 across. The next tile would be the same down and the width of a tile plus 1 across. Next tile would be the same except the width of two tiles plus one across.

If you end up with blank spots on the edges of the screen because of the way you are doing the scrolling, you might consider overdrawing it by drawing things off screen. What I mean is drawing an area that is actually larger than what is on screen by maybe a couple of tiles. That way if half a tile is off screen it still gets drawn.

This post has been edited by BBeck: 21 January 2016 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1