Skip to main content
gamedev.net
🔒 Locked

Splitting Up A Map

Started by Lith 1 reply 900+ views
Original Post
Lith
Lith
Hi,

In my platformer game, i split up the map file into "sectors." The size of 1 sector is the same size as the screen. When i draw and update game objects, i just deal with the ones that are in the same sector as the player and one sector in every direction(not diagonal). Like this:

[-][-][-][-][-][-]
[-][-][-][color="#ff0000"][-][-][-]
[color="#000000"][-][-][color="#ff0000"][-][color="#ffff00"][color="#0000ff"][-][[color="#ff0000"]-][-]
[-][-][-][color="#ff0000"][-][-][-]
[-][-][-][-][-][-]

[color="#0000ff"][-] [color="#000000"]- Player's sector
[color="#ff0000"][-] [color="#000000"]- Sectors to update

So, i need to calculate which sector the player is in from his coordinates.
I've tryed this:

SectorX = (int)player_struct->Position.X/G_SCREEN_WIDTH;
SectorY = (int)player_struct->Position.Y/G_SCREEN_HEIGHT;
player_struct -> CurrentSector = SectorX+(SectorY*CurrentMapFile.WidthInSectors);

It calculates the player's sector perfectly when at ground level. When i go up it goes into negative numbers. The origin of the screen is in the top left so sector 0,0 should be in the top left.

How can i correctly calculate the current sector?
simpler
simpler
The code you showed should work. Are you sure the player don't start in the top left sector?
Postie
Postie
I'm not sure what "ground level" means in the context of your example, though being a platform game I'm assuming its halfway down the Y coordinate (allowing you to have above and below ground areas). If your origin is in the top left, then Y will increase down the screen, and Y will decrease up the screen. If you're close to ground level, (which I'm assuming you've defined as Y = 0). then "up" from there will cause Y to become negative as you have noted.

In addition, you should be careful using the (int) cast in your calculation, unless your absolutely sure "Position" will always be positive.

If "Position" can be negative, the calculation will mess up around the origin. The correct way to handle the conversion under those circumstances is to use the Floor() function. A simple example of why this is the case is to consider what happens with the values -1 and +1 for X. Assuming [font="CourierNew, monospace"]G_SCREEN_WIDTH [/font]is more than 1, both will return 0, meaning there are 2 sectors with a SectorX of 0. Since Floor() always rounds down to the nearest integer, it correctly rounds to -1 for the X=-1 case.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.