#### Archived

This topic is now archived and is closed to further replies.

# 2D Scrolling in DirectDraw ...help !

This topic is 6587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have written a fast 2D-Scroll-Engine with Tiles ! I have this variables: mapX; // 0 - 6400 (Coordinates on map) mapY; // 0 - 4800 (Coordinates on map) So I have a 10x10 Screen map !! ...but how I can put objects on the map, like people or walls etc. ??? Should I calculate the map-Coordiantes of the object in realPixel (Screen-Coordinates) ?? Can somebodey show me a little bit of a code for a 2D-Scroll-map WITH OBJECTS ??? :-) Thanks MFG IronBrain (the guru of speaking english)

##### Share on other sites
You should create a ''world'' coordinate system for your game. This would usually be measure in pixel sizes, but relative to the top left corner of the map rather than the screen.

So, your map spans 0-6400 in the X direction. If you wanted to place an object in the centre of the map, place it at X coordinate 3200.

When you come to draw the objects on screen, just subtract the current position of the map (i.e. the world coordinates in the top left of the visible map)

So if your map was displaying at 3000 for the leftmost X, then the object would be (3200-3000=200) at X position 200 on screen.

##### Share on other sites
I agree with TwoFlower, first you need to ''world'' coordinate system. I recommend getting away from pixel based coordinates and move into tile based ones instead, and use offsets of pixels to perform smooth scrolling.

Here''s what I did.

I created a map format that can be any size.
My maps are defined as a collection of mini maps, this way I can load these mini maps from disk as the player walks around the map. This allows for huge worlds. I call these mini maps as sections. My sections are 256x256 tiles each. Tiles can be any pixel size.

In my demo I have a map that is 3x3 sections long (768x768 tiles [world coordinates])

Now when I want to place an object in my map it must be specified in world coordinates. A wall at 0,0 world pos is stored in the map file. When my engine is scrolling around it keeps track the top left screen position and translates it into world positions. I then read the map file at those locations and display the objects accordingly.

In psudeo code:

main()
{

while(alive)
{
WorldPosX = ConvertToWorldPos(PlayerPositionX);
WorldPosY = ConvertToWorldPos(PlayerPositionY);

DrawBaseTiles(WorldPosX,WorldPosY,offsetx);
DrawObjects(WorldPosX,WorldPosY,offsety);

GetPlayerInput();

UpdatePlayerPositions();
//etc other game loop stuff ....
}

}

The key to smooth scrolling is the UpdateScreenOffsets() function.

This function simply adjusts offset x and y paramters so that your map can smooth scroll the amount of your tilesize in x & y directions.

If offsetX exceeds your width of your tilesize then you move your player worldcoordinates X by one. Similarly for Y, and similarly for the opposite directions of scrolling.

You can see this method in my demo, hit F11 to see players world coordinates and notice they update only when the player steps onto the next tile (when the offsetX and or offsetY values exceed my tile size)

You can get the demo here.

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013179
×