Programación Específica > Allegro

 Necesito Una Ayudita Sobre Colisiones

<< < (2/2)

turko666:
Mira a lo que me refiero...

P/D: no se puede adjuntar 2 imagenes en un mismo post?  :o

turko666:
afff se me acabaron las ideas... solo se me ocurren 2 cosas:

1ero(y absurda idea) : hacer una matriz aumentada 4 veces entonces al moverse 8 px todos los tiles ocuparan 16 posiciones de un arreglo

0 0 0 0  << asi seria un solo tile de 32px... usa 16 posiciones de memoria en vez de 1
0 0 0 0
0 0 0 0
0 0 0 0

Esa tecnica no puede fallar pero me parece demasiado antipráctico, ademas el uso de memoria se iria x las nubes en mapas grandes.


2do: hacer que cada tile (posicion de la matriz) utilize campos, o sea... si quiero subir (y-=8 px) que vea si la proxima posicion del arreglo es == 0 (como ya esta hecho) o si esa posicion +8px de x ==0 o si esa posicion -8px de x ==0... pero tampoco le encuentro la vuelta...

adjunto una imagen para explicar el problema... a partir del 0 y 16px de X positivo y 16px de y positivo hay colision... pero 16 px X negativo y 16 px Y negativo no hay colision alguna, se puede transpasar sin ningun problema ya uqe solo se fija si los proximos 32 px tienen un valor igual a 0... pero no se fija sus alrededores.

ibito:
Hola...
Talvez esto te sirva...

turko666:
mmm la verdad no lo entendi muy bien... igualmente gracias por la intencion  :D

de todas formas me ilumine y se me ocurrio como hacerlo... ahora anda de 10  :comp: lo que hice fue remplazar los px_rel y py_rel por px_array que seria la posicion del personaje en el arreglo... si el personaje se mueve 8 px px_array no se mueve... solo se mueve si px%32==0 osea si es divisible por 32... lo mismo py_array.

aca les copio el codigo por si no me entendieron asi lo prueban y ven a que me refiero :P ... el 3 es el personaje "graficamente" el 1 rojo es la posicion en la matriz del personaje...



--- Citar ---#include <allegro.h>
#include <stdlib.h>

#define TILESIZE      32
#define TILES_ANCHO  32
#define TILES_ALTO    24

BITMAP *buffer;
BITMAP *grid;
BITMAP *player;
BITMAP *tiles;

#define UP      0
#define DOWN    1
#define LEFT    2
#define RIGHT    3

//variables del personaje
int px, py, dir, frame,px_array,py_array,px_array_ant,py_array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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 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, 1, 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, 0, 0, 0, 0, 0 }};
//prototipo funciones
void inicia_todo();
void borra_todo();
void dibuja_mapa();
void mover(int d);
void dibuja_player();
void dibuja_todo();
void blitbuffer( BITMAP *bmp );

// ------------------------- estructura de funciones -------------------------------
void inicia_todo()
{
    allegro_init();
    install_keyboard();
    install_timer();
    install_mouse();
    set_color_depth( 16 );
    set_gfx_mode( GFX_AUTODETECT, 1024, 768, 0, 0);
   
    buffer=create_bitmap(SCREEN_W,SCREEN_H);
    clear(buffer);
   
    grid=create_bitmap(TILES_ANCHO * TILESIZE, TILES_ALTO * TILESIZE);
   
    player=load_bitmap("player.pcx",NULL);
    tiles=load_bitmap("tilemaps.pcx",NULL);
   
    px=32;
    py=32;
    dir=DOWN;
    frame=0;
    px_array=1;
    py_array=1;
}

// ---------------------------------------------------------------------
void borra_todo()
{
    if ( buffer )
        destroy_bitmap( buffer );

    if ( grid )
        destroy_bitmap( grid );
}

// -----------------------------------------------------------------------
void dibuja_player()
{
    textout(buffer,font,"1",px_array*TILESIZE,py_array*TILESIZE,-5789);
    textout(buffer,font,"3",px,py,-500);
}
// -----------------------------------------------------------------------
void dibuja_mapa()
{
  int i , j , x=0 , y=0;
      for(i=0;i<24;i++)
            {
                for(j=0;j<32;j++)
                      {
                      switch(mapa[j])
                            {
                            case 0: textout(grid,font,"0",j*TILESIZE,i*TILESIZE,255);break;
                            case 1: textout(grid,font,"1",j*TILESIZE,i*TILESIZE,5000);break;
                            case 2: blit(tiles,grid,x,0 * TILESIZE ,i * TILESIZE, j * TILESIZE, TILESIZE, TILESIZE);break;
                            }
                      }
            }
  blit( grid, buffer, 0, 0, 0, 0, TILES_ANCHO * TILESIZE, TILES_ALTO * TILESIZE );             
}
// -----------------------------------------------------------------------
void dibuja_todo()
{
    dibuja_mapa();
    dibuja_player();
    blitbuffer(buffer);
    show_mouse(screen);   
}
// ------------------------------------------------------------------------
void blitbuffer( BITMAP *bmp )
{
    acquire_screen();
    blit( bmp, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
    release_screen();
}     

// ------------------------------------------------------------------------
void inc_frame(int d)
{
if (dir==UP)
{
  frame++;
  if (frame>1) frame=0;
}
if (dir==DOWN)
{
  frame++;
  if (frame<4 || frame>5) frame=4;
}
if (dir==LEFT)
{
  frame++;
  if (frame<6 || frame>7) frame=6;
}
if (dir==RIGHT)
{
  frame++;
  if (frame<2 || frame>3) frame=2;
}
}
// --------------------------------------------------------------
void mover(int d)
{
    dir=d;
   inc_frame(dir);
   switch(dir)
    {
    case LEFT:  if(colision[py_array][px_array-1]!=0||px%TILESIZE!=0){px-=8;if(px%TILESIZE==0)px_array=px/32;}break;
    case RIGHT: if(colision[py_array][px_array+1]!=0||px%TILESIZE!=0){px+=8;if(px%TILESIZE==0)px_array=px/32;}break;
   case UP:    if(colision[py_array-1][px_array]!=0||py%TILESIZE!=0){py-=8;if(py%TILESIZE==0)py_array=py/32;}break;
   case DOWN:  if(colision[py_array+1][px_array]!=0||py%TILESIZE!=0){py+=8;if(py%TILESIZE==0)py_array=py/32;}break;
    }
}

// ---------------------- MAIN ------------------------------------
int main()
{
    bool done = false;
    bool mousemove = false;
    inicia_todo();

    while ( !done )
    {
        dibuja_todo();
        rest(30);

// ESC para salir

        if (key[ KEY_ESC ] ) done = true;
       
       
        if (key[ KEY_DOWN ] )mover(DOWN);
        if (key[ KEY_UP ] ) mover(UP);
        if (key[ KEY_LEFT ] ) mover(LEFT);
        if (key[ KEY_RIGHT ] ) mover(RIGHT);
       
    }
    borra_todo();
    return 0;
}
END_OF_MAIN();

--- Fin de la cita ---

turko666:
afff otro error, anda todo perfecto exepto cuando te moves en diagonal hacia los extremos del "cubo"  :angry:

Navegación

[0] Índice de Mensajes

[*] Página Anterior

Ir a la versión completa