Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 ISDCaptain01   Members   -  Reputation: 1443

Like
0Likes
Like

Posted 05 November 2013 - 01:22 PM

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?

Sponsor:

#2 exOfde   Members   -  Reputation: 810

Like
0Likes
Like

Posted 05 November 2013 - 01:52 PM


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....


Edited by exOfde, 05 November 2013 - 02:11 PM.


#3 Servant of the Lord   Crossbones+   -  Reputation: 20899

Like
3Likes
Like

Posted 05 November 2013 - 02:01 PM

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


Edited by Servant of the Lord, 05 November 2013 - 02:04 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 ISDCaptain01   Members   -  Reputation: 1443

Like
0Likes
Like

Posted 05 November 2013 - 06:46 PM


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



#5 ISDCaptain01   Members   -  Reputation: 1443

Like
0Likes
Like

Posted 05 November 2013 - 06:48 PM

 

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



#6 Servant of the Lord   Crossbones+   -  Reputation: 20899

Like
0Likes
Like

Posted 05 November 2013 - 10:25 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 ISDCaptain01   Members   -  Reputation: 1443

Like
0Likes
Like

Posted 06 November 2013 - 01:54 AM

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?


Edited by ISDCaptain01, 06 November 2013 - 02:16 AM.


#8 Servant of the Lord   Crossbones+   -  Reputation: 20899

Like
5Likes
Like

Posted 06 November 2013 - 10:21 AM

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


Edited by Servant of the Lord, 06 November 2013 - 10:27 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#9 ISDCaptain01   Members   -  Reputation: 1443

Like
1Likes
Like

Posted 07 November 2013 - 12:03 AM

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



#10 Winfield   Members   -  Reputation: 179

Like
0Likes
Like

Posted 13 November 2013 - 12:35 PM

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. ;)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS