How to put objects and enemies in a 2d level?

Started by
8 comments, last by Winfield 10 years, 5 months ago
Okay so I made a level using a 2d map editor. Now when I get to the code I can make the world scroll relative to my characters position. So now I decide to code in a few object (coins, rings etc). I set their x and y position and find out they move along with my charcter rather than being in their own spots around the level. Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position. That seems to fix the problem, but now im experiencing slow downs. Is there a better way to do this?
Advertisement

Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position.

I understand you correctly that you loaded your map into an extra "backbuffer" of the same size as your map. On this "backbuffer" you blit all other objects which are not the player by hand ( hardcode instant of dynamiclly ) . Then you blit the map to the screen?

if that the case i would suggest you some kind of managerclass for your other entities. The basic thought behind you seperate more the logic of an entity from the drawfunctions.

The Manager handle the access to all entities. The logic manipulate the entities in that form that it looks like they would move or do what ever they supposed to do and after the logic finished her job the manager handover all objects e.g. to a seperate renderer. This same renderer should

also render you map. Then you have only to look in which order this renderer draw your objects to the screen!!! The only diffculty is now you have to setup up an own move logic for every entity like a very small and simple AI. or just a simple and stupid move pattern.

Then should everything work unrelated to your players movement....

Okay so I made a level using a 2d map editor. Now when I get to the code I can make the world scroll relative to my characters position. So now I decide to code in a few object (coins, rings etc). I set their x and y position and find out they move along with my charcter rather than being in their own spots around the level. Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position. That seems to fix the problem, but now im experiencing slow downs. Is there a better way to do this?

Sounds like your camera code is wrong.

All objects, including the player, should have a position in "global coordinates". Global coordinates are the coordinates from some fixed location in the loaded area (which might be the center of the area, or the upper left corner, or something like that).

Screen coordinates are relative to the location of the camera (the upper-left corner of the screen).

The camera itself has a global coordinate (i.e. it's at location (x,y) in the area).

You calculate screen coordinates by subtracting the camera position from the global coordinate of the object, and use this to draw.


screenPosOfObject = (myObject.globalPos - myCamera.globalPos);
draw(myObject, screenPosOfObject);

Keep positions in global positions, and only convert them to screen positions on-the-fly when you need them - it won't slow you down.

Don't use the buffer idea; as you found out, that'd burn through your framerate faster than napalm. wink.png


understand you correctly that you loaded your map into an extra "backbuffer" of the same size as your map. On this "backbuffer" you blit all other objects which are not the player by hand ( hardcode instant of dynamiclly ) . Then you blit the map to the screen?

Exactly

Okay so I made a level using a 2d map editor. Now when I get to the code I can make the world scroll relative to my characters position. So now I decide to code in a few object (coins, rings etc). I set their x and y position and find out they move along with my charcter rather than being in their own spots around the level. Okay, so I made one huge backbuffer that is the size of the level it self. Blit the level onto it, than the object as well. Than I blit that backbuffer to my normal buffer(which is the size of my screen) where my character is and move and blit relative to my characters position. That seems to fix the problem, but now im experiencing slow downs. Is there a better way to do this?

Sounds like your camera code is wrong.

All objects, including the player, should have a position in "global coordinates". Global coordinates are the coordinates from some fixed location in the loaded area (which might be the center of the area, or the upper left corner, or something like that).

Screen coordinates are relative to the location of the camera (the upper-left corner of the screen).

The camera itself has a global coordinate (i.e. it's at location (x,y) in the area).

You calculate screen coordinates by subtracting the camera position from the global coordinate of the object, and use this to draw.


screenPosOfObject = (myObject.globalPos - myCamera.globalPos);
draw(myObject, screenPosOfObject);

Keep positions in global positions, and only convert them to screen positions on-the-fly when you need them - it won't slow you down.

Don't use the buffer idea; as you found out, that'd burn through your framerate faster than napalm. wink.png

Ill try your suggestions, but would using mutliple threads help the framerate? Its chugging with my current algorithm

Ill try your suggestions, but would using mutliple threads help the framerate?


No, definitely not. 2D graphics are normally really really fast. smile.png

Multithreading doesn't gain instant speed - multithreading makes your code more complex, and done wrongly, it can even slow down your program or introduce more bugs that are even harder to find. If your 2D drawing is going slowly, then most likely something is wrong in the code, and that problem needs to be fixed directly, not coded over.

If I'm understanding your problem correctly, instead of drawing your level and items onto a back-buffer, and then drawing the back-buffer relative to your player, you need to just draw the objects relative to your camera in the first place (my previous post explains how). No extra buffers are necessary except your API's built-in backbuffer that prevents flickering - but you shouldn't ever have to touch that directly.

I got rid of the extra buffer, performance is back to normal. But im back at the start.

Okay here is what I do:

Here is how I set the camera up first of all:


//update the map scroll position
		//camera coordinates
		mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
		mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;

This is how I keep the camera on the screen:


//Avoid moving beyond the map edge
		if(mapxoff < 0) 
			mapxoff = 0;
		if(mapxoff > mapwidth*mapblockwidth - WIDTH)
			mapxoff = mapwidth*mapblockwidth - WIDTH;
		if(mapyoff < 0)
			mapyoff = 0;
		if(mapyoff > mapheight*mapblockheight - HEIGHT)
			mapyoff = mapheight*mapblockheight - HEIGHT;

Next this is how I set up the rings through the level


void initializeRings()
{
	int n;
	for(n = 0; n < RINGS; n++)
	{
		rings[n] = new SPRITE;
		rings[n]->alive = 1;
		rings[n]->x = n * 10;
		rings[n]->y = 340;
		rings[n]->curframe = 0;
		rings[n]->maxframe = 4;
		rings[n]->framecount = 0;
		rings[n]->framedelay = 7;
		rings[n]->width = ring_images[n]->w;
		rings[n]->height = ring_images[n]->h;
	}
}

Now this is how I draw the rings to level


void drawRings()
{
	int n;

	//loop through all ring object
	for(n=0; n<RINGS; n++)
    {
		//If the rings have not been collected yet
       if(rings[n]->alive)  
       { 
		   //Animate each ring
           if(++rings[n]->framecount > rings[n]->framedelay)
           {
                if(++rings[n]->curframe > rings[n]->maxframe)
                rings[n]->curframe = 0;
                rings[n]->framecount = 0;
           }
		  
		   //now draw each ring to the buffer
           draw_sprite(buffer, ring_images[rings[n]->curframe], rings[n]->x, rings[n]->y);
       }
    }
}

So what you are saying is that I get each ring's x and y position like this?

rings[n]->x - mapxoff;

rings[n]->y - mapyoff;

Hmm I cant seem to visualize it

My screen is 640*480

I wanna draw a ring at say coordinate 100, 100;

so 100 - maxoff?

servant, if you don't mind, can you give me a light code example using my rings objects?

So what you are saying is that I get each ring's x and y position like this?

rings[n]->x - mapxoff;
rings[n]->y - mapyoff;

Hmm I cant seem to visualize it
My screen is 640*480

I wanna draw a ring at say coordinate 100, 100;
so 100 - maxoff?


Yep. If a coin is at 100, and the camera is at 120, what is supposed to happen? The coin is 20 units to the left of the camera.
coin(100) - camera(120) = -20. Drawing a coin at -20, is drawing the coin 20 units to the left of the camera. It does what is intended.

If the coin is at 200, and the camera is at 120, what is supposed to happen? The coin is 80 units to the right of the camera.
coin(200) - camera(120) = 80. Drawing a coin at 80 is drawing the coin 80 units to the right of the camera. It works correctly, giving negative positions for objects to the left of the camera, and positive positions for objects to the right of the camera, which draw onscreen exactly as desired.

It's important that you do (objectPos - cameraPos) and not (cameraPos - objectPos) or it'd give the wrong results.

All your objects need to be stored with their global position on the map, but drawn with their local position. It's easiest, fast, and mentally makes sense one you comprehend it.

servant, if you don't mind, can you give me a light code example using my rings objects?


The exact same as what you already have, just subtract the camera position from any object in the game world.


void drawRings(int cameraX, int cameraY)
{
    //loop through all ring object
    for(int n = 0; n < RINGS; ++n)
    {
        //If the rings have not been collected yet
        if(rings[n]->alive)  
        { 
            //Animate each ring
            if(++rings[n]->framecount > rings[n]->framedelay)
            {
                if(++rings[n]->curframe > rings[n]->maxframe)
                    rings[n]->curframe = 0;
                
                rings[n]->framecount = 0;
            }
            
            //now draw each ring to the buffer
            draw_sprite(buffer, ring_images[rings[n]->curframe], (rings[n]->x - cameraX), (rings[n]->y - cameraY));
        }
    }
}

Your code was already pretty much correct, it was just that camera subtracting that it needed. smile.png

Yes it worked! Thank you so much for the help, Servant!

One easy pitfall is developing on the wrong machine. If you're building a 2D game from a massive gaming rig with a 2GB video card and eight cores, it's depressingly easy to end up with a sprite game that won't run on a two-year-old computer. If you can, make sure you're using an older system... that way you discover all the bottlenecks like the one you described above, instead of your players. ;)

This topic is closed to new replies.

Advertisement