Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Allegro] Grawitacja i kolizje

Ostatnio zmodyfikowano 2009-10-06 22:38
Autor Wiadomość
PhonniX
Temat założony przez niniejszego użytkownika
[Allegro] Grawitacja i kolizje
» 2009-10-01 18:57:04
Witam. Mam problem, chcę zrobić prostą platformówkę, zrobiłem kolizje za pomocą getpixel, i taką prostą grawitacje, ale kiedy wyskoczę i postać zaczepi o platformę nad to przenika przez nią. Jak temu zaradzić?
Oto mój kod
C/C++
#include <allegro.h>
#include <winalleg.h>
#include <windows.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>


float fTime, fNewTime, dt;
float fFPS, fFPSTime;
float dtFiz;
unsigned uFrames;

class ludz
{
public:
    int x;
    int y;
};
ludz Ludek;

void FPS()
{
   
    fNewTime = clock() / 1000.0f;
    dt = fNewTime - fTime;
    fTime = fNewTime;
   
    if( dt < 2 )
         dtFiz += dt;
   
    ++uFrames;
    fFPSTime += dt;
   
    if( fFPSTime >= 1.0f )
    {
       
        fFPS = uFrames / fFPSTime;
        uFrames = 0;
        fFPSTime = 0.0f;
       
    }
}

std::string IntToStr( int L )
{
    std::string tmp;
    itoa( L,( char * ) tmp.c_str(), 10 );
    std::string str = tmp.c_str();
    return str;
}



bool kolizja( int x1, int y1, int s1, int w1, int x2, int y2, int s2, int w2 )
{
    if( x2 <= x1 + s1 && x2 > x1 && y2 >= y1 && y2 <= y1 + w1 ) return true; else
    if( x2 <= x1 + s1 && x2 > x1 && y2 + w2 >= y1 && y2 + w2 <= y1 + w1 ) return true; else
    if( x2 + s2 <= x1 + s1 && x2 + s2 > x1 && y2 >= y1 && y2 <= y1 + w1 ) return true; else
    if( x2 + s2 <= x1 + s1 && x2 + s2 > x1 && y2 + w2 >= y1 && y2 + w2 <= y1 + w1 ) return true;
    else return false;
   
};

BITMAP * bufor = NULL;
BITMAP * ludek = NULL;
BITMAP * Teren = NULL;
float speed;
bool skacze;

int main()
{
    allegro_init();
    install_keyboard();
    set_color_depth( 16 );
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 );
    clear_to_color( screen, makecol( 128, 128, 128 ) );
   
   
    bufor = create_bitmap( 640, 480 );
    if( !bufor )
    {
        set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
        allegro_message( "Nie mogę utworzyć bufora !" );
        allegro_exit();
        return 0;
    }
   
    ludek = load_bmp( "ludek.bmp", default_palette );
    Teren = load_bmp( "teren.bmp", default_palette );
    skacze = false;
    Ludek.x = 400;
    Ludek.y = 100;
    int teren_x = 1;
    int teren_y = 1;
    int teren_w = 640;
    int teren_s = 480;
   
    while( true )
    {
        if( key[ KEY_ESC ] ) return false;
       
        FPS();
       
        int licz = dtFiz / 0.01f;
        for( int l = 0; l < licz; l++ )
        {
           
            if( key[ KEY_LEFT ] ) Ludek.x--;
           
            if( key[ KEY_RIGHT ] ) Ludek.x++;
           
            if( skacze == false ) { if( key[ KEY_UP ] ) { skacze = true; } }
            if(( getpixel( bufor, Ludek.x, Ludek.y ) != makecol( 150, 150, 150 ) ) ||
            ( getpixel( bufor, Ludek.x + 32, Ludek.y ) != makecol( 150, 150, 150 ) ) ||
            ( getpixel( bufor, Ludek.x, Ludek.y + 32 ) != makecol( 150, 150, 150 ) ) ||
            ( getpixel( bufor, Ludek.x + 32, Ludek.y + 32 ) != makecol( 150, 150, 150 ) ) )
            {
                Ludek.y -= 3;
                speed = 5;
            }
           
            if( skacze == true )
            {
                Ludek.y -= speed;
                speed -= 0.1;
            }
            if( speed < 0 )
            {
                skacze = false;
            }
            Ludek.y += 3;
            clear_to_color( bufor, makecol( 150, 150, 150 ) );
            masked_blit( ludek, bufor, 0, 0, Ludek.x, Ludek.y, 32, 32 );
            masked_blit( Teren, bufor, 0, 0, teren_x, teren_y, teren_w, teren_s );
            textout_ex( bufor, font, IntToStr( fFPS ).c_str(), 3, 190, makecol( 100, 100, 100 ), - 1 ); //FPS
        }
       
       
        dtFiz -=( int )( dtFiz / 0.01 ) * 0.01;
        blit( bufor, screen, 0, 0, 0, 0, 640, 480 );
    }
    destroy_bitmap( bufor );
    allegro_exit();
    return 0;
}
END_OF_MAIN();
Teren wygląda tak:
http://img406.imageshack.us/img406/9190/teren.png
P-10562
DejaVu
» 2009-10-06 22:38:55
Uhm... masakra :) temat kolizji to coś co na tym forum było rozwiązywane co najmniej 20 razy ;p kolizja 'by pixel' jest wolna, ale jeśli chcesz ją zastosować to polecam zacząć od napisania funkcji:
C/C++
bool czyKoliduja( int x1, int y1, BITMAP * img1, int x2, int y2, BITMAP * img2 );
Mając taką funkcję bez problemu powinieneś poradzić sobie z jej użyciem oraz będzie można coś dyskutować na temat poprawności jej implementacji jeśli coś nie zadziała :)
P-10675
« 1 »
  Strona 1 z 1