![]() |
|
#1
|
|||
|
|||
|
I realized that doing collision detection for semi-complicated walls in a 2D game would be a lot easier if I was using tiles rather than pixels. I haven't really found anything but tutorials for other languages than C that I only partially understand. What I have found so far, is that to make the map I would declare a two dimensional array of the size I want the map to be. This would be to store the map variables. Pretty much this: if (myArray[0][1]==0) { walkable=0;} (I know that's not exactly it but I'm pretty sure that you and I are getting the gist of what I mean from that snippet.) My questions are: where do I go from here, how do I set the tile sizes, what is a good tile size for the psp screen. If someone could advise me on these questions and/or point me in the direction of some good tutorials in C it would be great. |
|
#2
|
|||
|
|||
|
It would probably be a good idea to create a struct containing all the information a tile needs(e.g. walkable, position) then creating an array of this struct so you can use it like this:
Code:
typedef struct Tile {
u8 walkable;
u32 x, y;
} Tile;
Tile map[sizeX*sizeY] = {
{1, 0,0},
//etc...
};
|
|
#3
|
||||
|
||||
|
Well if you're using C++ take advantage of the object-oriented aspect as what you are planning to do is a prime example of why it can be beneficiary. Something worth looking into, even if you arent using SDL the concept itself is there
http://lazyfoo.net/SDL_tutorials/lesson29/index.php By using classes you can later subclass your 'Tile' class to create specialized tiles. The Tile baseclass could have a virtual function like 'OnPlayerEnter()' which could then be overriden by other classes to create traps that kill the player, teleporters that send the player across the map, or whatever. |
|
#4
|
|||
|
|||
|
Quote:
|
|
#5
|
||||
|
||||
|
Gblo6689 - Nahh don't bother learning SDL just for the sake of using that tutorial.
Use Oslib to begin with SDL is clunky and ugly anyways if you ask me.. If you don't understand C++ classes and the scope of that tutorial is just over your head, I recommend doing whatever you want to begin with to gain experience. Programming something the 'wrong' way is a good step towards understanding and comprehending the programming concept and game programming theory. Soon as you get those two (it'll just 'click') you build on your understanding by introducing new techniques and means of optimization and efficiency, ie object-orientation (classes), polymorphism (virtual functions), inheritance, etc.
|
|
#6
|
|||
|
|||
|
Quote:
But i have been working witht he image collision but no luck so i decided 32x32 tiles but what im trying to figure out is like how do i find that program and make my entire map an array so i can have it move up in the array rather then move pixals and just display the array with certain values for different areas |
|
#7
|
|||
|
|||
|
Code:
#include <oslib/oslib.h>
#include <math.h>
#define BLANK_TILE 0
#define MAX_MAP_X 49
#define MAX_MAP_Y 29
#define TILE_SIZE 10
//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
//declaration of the pointers of our images
OSL_IMAGE *background,*A,*B,*C;
int move=1;
int tile[MAX_MAP_Y][MAX_MAP_X];
int MAPS=1;
int Map1[28][49] =
{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,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ollision( float fighterposX, float fighterposY, float fighter2posX, float fighter2posY)
{
int collision,collide=0;
collision = 0;
fighterposY=B->y;
fighterposX=B->x;
fighter2posX=A->x;
fighter2posY=A->y;
float fighterwidth =16;
float fighterheight =16;
float fighter2width =32;
float fighter2height =32;
if ((fighterposX + fighterwidth > fighter2posX) &&
(fighterposX < fighter2posX + fighter2width) &&
(fighterposY + fighterheight > fighter2posY) &&
(fighterposY < fighter2posY + fighter2height))
{
collision = 1; collide=1;
}
return collision;
}
void drawMap()
{
int x, y;
/* Draw the map */
for (y=0;y<MAX_MAP_Y;y++)
{
for (x=0;x<MAX_MAP_X;x++)
{
if (Map1[y][x] != 0 && MAPS==1)
{
oslDrawImageXY(B, x * TILE_SIZE, y * TILE_SIZE);
}
if((FghrCollision( B->x , B->y, A->x, A->y )))oslPrintf_xy(10,90,"collided");
}
}
}
int main(int argc, char **argv)
{
//Initialization of the Oslib library
oslInit(0);
//Initialization of the graphics mode
oslInitGfx(OSL_PF_8888, 1);
oslInitConsole(); //Text
oslSetTransparentColor(RGB(255,0,255));
//loads our images into memory
A = oslLoadImageFile("A.png", OSL_IN_RAM, OSL_PF_5551);
B = oslLoadImageFile("brick2.png", OSL_IN_RAM, OSL_PF_5551);
C = oslLoadImageFile("C.png", OSL_IN_RAM, OSL_PF_5551);
background = oslLoadImageFile("background.png", OSL_IN_RAM, OSL_PF_5551);
//position
A->x =100;
A->y =150;
C->x=250;
C->y=120;
//main while loop
while (!osl_quit)
{
//To be able to draw on the screen
oslStartDrawing();
//Clear the screen
oslCls();
//initiate the PSP's buttons
oslReadKeys();
oslDrawImage(background);
oslDrawImage(C);
drawMap();
oslDrawImage(A);
if(osl_keys->held.left) {
A->x-=1;
}
if(osl_keys->held.right){
A->x+=1;
}
if(osl_keys->held.up) {
A->y-=1;
}
if(osl_keys->held.down){
A->y+=1;
}
//Ends drawing mode
oslEndDrawing();
//Synchronizes the screen
oslSyncFrame();
}
//Terminate the program
oslEndGfx();
oslQuit();
return 0;
}
|
|
#8
|
|||
|
|||
|
Quote:
Though my real problem is how much I abhor making sprites.
|
|
#9
|
|||
|
|||
|
ok, so after tinkering with drawing the tiles and what not I decided to then put them in structures and advance the code a little. This of coarse led me to my current state. I keep receiving compiling errors and after a few days of trying to figure it out myself I put it to everyone here. Here is the code:
(The lines with the errors are marked above their respective lines. errors at bottom.) struct square { int Walkable; } ; struct World { square Tile; int yPos, xPos; } stage[MAPWIDTH][MAPHEIGHT]; Line:96-> void DrawMap(){ int x,y; for (y=0; y<MAP_HEIGHT; y++){ for (x=0; x<MAP_WIDTH; x++){ /*switch (stage[y][x]){ case 1: oslSetImageTileSize(room_tileset, 0,0,16,16); oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT); stage[y][x].yPos=(y*TILE_HEIGHT); stage[y][x].xPos=(x*TILE_WIDTH); stage[y][x].Tile.Walkable=0; break; case 2: oslSetImageTileSize(room_tileset, 16,0,16,16); oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT); stage[y][x].yPos=(y*TILE_HEIGHT); stage[y][x].xPos=(x*TILE_WIDTH); stage[y][x].Tile.Walkable=1; break; default: oslDebug("Problem loading tiles to map"); break; }*/ Line:119-> if (stage[y][x]==1){ oslSetImageTileSize(room_tileset, 0,0,16,16); oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT); stage[y][x].yPos=(y*TILE_HEIGHT); stage[y][x].xPos=(x*TILE_WIDTH); stage[y][x].Tile.Walkable=0; } Line:126-> if (stage[y][x]==2){ oslSetImageTileSize(room_tileset, 0,0,16,16); oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT); stage[y][x].yPos=(y*TILE_HEIGHT); stage[y][x].xPos=(x*TILE_WIDTH); stage[y][x].Tile.Walkable=0; } else { oslDebug("Problem loading tiles to map"); } void initializeMap(){ while (initialize==0){ Line:162-> stage [MAPHEIGHT][MAPWIDTH]={ {02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,02,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,0 1,01,01,01,01,01,01,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,01,02}, {02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,0 2,02,02,02,02,02,02,02,02} }; DrawMap(); player->x=stage[1][1].xPos; player->y=stage[1][1].yPos; initialize=1; }; Line:187-> } 119:error: no match for "operator==" in 'stage[y][x] == 1' 126:error: no match for "operator==" in 'stage[y][x] == 2' 162:error: expected primary expression before '{' token 162:error: expected ';' before '}' token 187:error: expected '}' at end of input 187:error: expected '}' at end of input (also the switch that is in there for some reason will not work either but it did before. If anyone sees what would be wrong with that let me know also.) (If anyone can tell me how to place my code in a scrolling box it would be appreciated.) |
|
#10
|
|||
|
|||
|
Im not sure where you ment to close certain things but this should compile a bit less errored also not sure if the map should be like {01,02} but rather (01,02) but you know im not that good at c so =/
Code:
struct square {
int Walkable;
}
struct World {
square Tile;
int yPos, xPos;
}
stage[MAPWIDTH][MAPHEIGHT];
void DrawMap(){
int x,y;
for (y=0; y<MAP_HEIGHT; y++){
for (x=0; x<MAP_WIDTH; x++){
switch (stage[y][x]){
case 1:
oslSetImageTileSize(room_tileset, 0,0,16,16);
oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT);
stage[y][x].yPos=(y*TILE_HEIGHT);
stage[y][x].xPos=(x*TILE_WIDTH);
stage[y][x].Tile.Walkable=0;
break;
case 2:
oslSetImageTileSize(room_tileset, 16,0,16,16);
oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT);
stage[y][x].yPos=(y*TILE_HEIGHT);
stage[y][x].xPos=(x*TILE_WIDTH);
stage[y][x].Tile.Walkable=1;
break;
default:
oslDebug("Problem loading tiles to map");
break;
}
if (stage[y][x]==1){
oslSetImageTileSize(room_tileset, 0,0,16,16);
oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT);
stage[y][x].yPos=(y*TILE_HEIGHT);
stage[y][x].xPos=(x*TILE_WIDTH);
stage[y][x].Tile.Walkable=0;
}
if (stage[y][x]==2){
oslSetImageTileSize(room_tileset, 0,0,16,16);
oslDrawImageXY(room_tileset, x*TILE_WIDTH, y*TILE_HEIGHT);
stage[y][x].yPos=(y*TILE_HEIGHT);
stage[y][x].xPos=(x*TILE_WIDTH);
stage[y][x].Tile.Walkable=0;
}
else {
oslDebug("Problem loading tiles to map");
}
void initializeMap(){
while (initialize==0){
stage[MAPHEIGHT][MAPWIDTH]={
{02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,01,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,01,02},
{02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02}
}
DrawMap();
player->x=stage[1][1].xPos;
player->y=stage[1][1].yPos;
initialize=1;
}
}
}
}
Last edited by dark_banana; 06-11-2009 at 03:55 PM. |
![]() |
|
|
|||
|
|||
|
|
| Thread Tools | |
| Display Modes | |
|
|