Sign in to follow this  

move around a map

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a pre-rendered 2D map of say 4 screen lengths, and want to move a sprite around the map on the screen, without walking on certain places eg rocks. So I need tomove the map on the screen in different directions. Is there a standard way to do this or is it more open to the programmer. Currently I can do this with a blotchy movement of 32 bits per move of the map, and it is taking nearly 1000 lines to do. I am splitting a map into tiles and having a sprite walk around without hitting things on the map. I haven't even started to make it OO which I doubt will change the prog length of about 1000 lines. Is there a standard way to do this problem in shorter code length?

Share this post


Link to post
Share on other sites
Firstly, it depends on the API you use. Do you use DirectX? OpenGL? SDL? Allegro? Windows GDI?

Secondly, we need to know, in a nutshell, how your drawing works. Do your sprites consist of textured polygons that you move with transformation matrices? Or do you copy pixel data from an image to the screen/back buffer? If the latter, it may be more difficult to achieve smooth scrolling.

Share this post


Link to post
Share on other sites
Sorry i didn't say i was using directx/c++ 'is there any other more professional tool?

I am getting about 500-600 frames per second with a large map and 1 sprite
Also I am using the sprite interface .

Share this post


Link to post
Share on other sites
Since (in other posts) I've already tried to explain how to use a map structure and draw your tiles from this each frame on the fly, I assume this is not the approach you want.

Could you post your existing drawing code so we can get more of an idea about what you are trying to do?

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Since (in other posts) I've already tried to explain how to use a map structure and draw your tiles from this each frame on the fly, I assume this is not the approach you want.

Could you post your existing drawing code so we can get more of an idea about what you are trying to do?


Hi and thanks for your help so far, I do appreciate it.

I had a more specific question this time so i thought I ask another one. I was hoping for a standard method and a tutorial to look up but if there isn't one I will have to make do.
A large pre-rendered map that a sprite moves around ,the map scrolls and the sprite interacts with the map.
Let me tinker a little further so I can ask better questions.

Share this post


Link to post
Share on other sites
I think you are complicating a simple problem. To move sprites (or any other objects) around simple keep track of a transformation matrix for that sprite. This transformation matrix can include as little information as the x,y,z position of the sprite or as complex as scalling and rotation as well.

When rendering simple translate your sprite by this transformation.


NOW For the camera. If you want the camera to follow a sprite simple called that sprites transformation first followed by your rendering commands.

IF you are not using the built in matrix functionality of your 3D API then something like this will suffice.

RenderMap(offsetx,offsety)
{
for (x->0 to mapsizex)
for (y->0 to mapsizey)
RenderTile(x+offsetx,y+offsety)
}

And the same applies for your sprite

RenderSprite(offsetx,offsety)
{
this->Sprite.Render(this->x+offsetx,this->y+offsety);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by dawidjoubert
I think you are complicating a simple problem. To move sprites (or any other objects) around simple keep track of a transformation matrix for that sprite. This transformation matrix can include as little information as the x,y,z position of the sprite or as complex as scalling and rotation as well.

When rendering simple translate your sprite by this transformation.


NOW For the camera. If you want the camera to follow a sprite simple called that sprites transformation first followed by your rendering commands.

IF you are not using the built in matrix functionality of your 3D API then something like this will suffice.

RenderMap(offsetx,offsety)
{
for (x->0 to mapsizex)
for (y->0 to mapsizey)
RenderTile(x+offsetx,y+offsety)
}

And the same applies for your sprite

RenderSprite(offsetx,offsety)
{
this->Sprite.Render(this->x+offsetx,this->y+offsety);
}


well i think I am already doing this. Each map tile has a position in a 1D structure, including the row and col it appears in the map. Go through 2 for loops and start displaying the 1d array with the map , as you just do some maths to get to the startrow,col in the array then print to screen from 0,0.

Share this post


Link to post
Share on other sites
I am getting now the problem of reading in a map 640 X 640 pixels, with each tile 32 pixels.
So I should have 200 tiles of 32 pixel X 32 pixels..
When i print out all the tiles I don't get the whole map but it looks like 2/3 of it. It looks like the map has been scaled up when reading in so i am confused, does anyone have an idea?
I read in like this


if (FAILED(hr= D3DXCreateTextureFromFile(g_pd3dDevice,
L"grass3.bmp",&g_BackGroundFile)))
return hr;

i=0;
for (int row=0; row < bRows; row++)
for (int col=0; col < bCols; col++)
{
g_back[i].Col = col;
g_back[i].Row = row;
g_back[i].xMapPos =col * 32;
g_back[i].yMapPos =row * 32;
g_back[i].CanMoveOnTile =true;
g_back[i].TilePixelHeight =32;
g_back[i].TilePixelWidth =32;
g_back[i].TileNumber =i;
g_back[i].TileType =1;

i++;
}

/////print out like this
count=0;
for (int j=0; j< screenRows;j++)
for (int i=0; i< screenCols;i++)

{
{

backRect.left=g_back[count].xMapPos ;
backRect.right=backRect.left+g_back[count].TilePixelWidth ;
backRect.top=g_back[count].yMapPos ;
backRect.bottom=backRect.top+g_back[count].TilePixelHeight ;


D3DXVECTOR3 pos2=D3DXVECTOR3(
i * 32 ,
j * 32 , 1);
g_Sprite->Draw(g_BackGroundFile,&backRect,NULL,&pos2,0xFFFFFFFF);

count+=1;

of course I should only get a 640 X 480 map on the screen due to the window size but it looks like it is about 380 X 400 of the actual mapsize.

Share this post


Link to post
Share on other sites
I had a look at the articles and can do what it says but here is a problem.
If you want smooth scrolling of a tiled map without a constant move ( and not really slow), you need to move the map a varying amount at a time or small amount a time (less than tile size).

This is where it gets tricky as i do now have a smooth scrolling map but you need extra code to move a less than a tile size.

eg 32 * 32 tile size or half that wont move smoothly at a constant each time.
Moving the map at 4 -32 pixels a time depending if you want to move fast or just a bit looks better.

If I move a map down i need to program an extra row to be drawn at the top.

Does anyone have a better logic for real time movement of a smooth scrolling map?

Share this post


Link to post
Share on other sites
Hey guys, this is Direct3D your using here! Why don't you just draw your tiles as quads with the appropriate textures on them and translate all of them however you like?!
In the end, this approach would give you good performance AND flexibility. Think about it: this would make collision detection etc. alot easier and you can scroll from as much as 10 tiles to 0.000000000001 tiles :)

Share this post


Link to post
Share on other sites
Quote:
Original post by godmodder
Hey guys, this is Direct3D your using here! Why don't you just draw your tiles as quads with the appropriate textures on them and translate all of them however you like?!
In the end, this approach would give you good performance AND flexibility. Think about it: this would make collision detection etc. alot easier and you can scroll from as much as 10 tiles to 0.000000000001 tiles :)


Interesting idea!

I thought a sprite is more suitable for speed and flexibility because it is designed for 2D.
Can you define the flexible vertex structure with extra members to cater for extra information?

Share this post


Link to post
Share on other sites
First of all, I don't think performance is going to be huge problem in a 2D game when it's accelerated on the GPU. I mean, no matter how large your 2D data sets are going to become, it'll be nowhere near their 3D counterparts.

Second, I think sprites would be more useful to draw 2D HUD's etc... In other words: they make it easier to draw 2D on top of 3D. I don't think they were even designed for performance critical code and the current drivers are allmost certainly not optimised for drawing them. Drawing a bunch of quads (were probably talking less than 500 quads here) is allways going to be faster.

You could probably store your tile info in just plain structures. This should give you a very flexible system to maintain.
Just a stupid example:

struct sTile
{
CVector3 position;
CVector3 *pVertices;
bool CanPlayerComeHere;
// ...
};

The only thing you should take care of is calculating the offsets for placing the tiles. If your tiles are 50 units large in width, you should place them on the x-axis like this: 50, 100, 150... (you get it)
Once you've calculated your offsets for x and y, you just place them into the vertices pointer of your tile structure and you're ready to go.

You want scrolling? Just translate all the tiles in the right direction. (you could also translate you're character ofcourse, but I like to rotate problems 90 degrees ;) )

You want collision detection? Just draw an invisible bounding box around tiles where the CanPlayerComeHere value == false; and check it. Presto!

Hope I've been able to help,
Jeroen

Share this post


Link to post
Share on other sites
Quote:
So I should have 200 tiles of 32 pixel X 32 pixels


If you have 640 x 640 like you said .. at 32 x 32 isnt that 20 tiles by 20 tiles so a total of 400 tiles? That may be why your only getting part of it to show.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this