help please.

This is a discussion on help please. within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a 2d game like mario.and I am doing a collision detection of the player with ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    help please.

    I am trying to make a 2d game like mario.and I am doing a collision detection of the player with the bricks.so I made a class bricks.
    here it is:
    Code:
    // CLASS BRICKS
    class bricks{
        float b_a;
        float b_b;
    public:
        bricks(){};
     //drawing brick function   
        void draw_brick(float brick_a,float brick_b){
            b_a=brick_a;
            b_b=brick_b;
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D,_textureId_bricks);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
            glBegin(GL_POLYGON);
    
            glTexCoord2f(0.0, 0.0);
            glVertex2f(100.0+brick_a,100.0+brick_b);
            glTexCoord2f(1.0, 0.0);
            glVertex2f(200.0+brick_a,100.0+brick_b);
            glTexCoord2f(1.0, 1.0);
            glVertex2f(200.0+brick_a,130.0+brick_b);
            glTexCoord2f(0.0,1.0);
            glVertex2f(100.0+brick_a,130.0+brick_b);
            
    glEnd();
            glDisable(GL_TEXTURE_2D);
        
        }
        
       //collision detection check
    void check(){
    //xmid player's mid in horizontal direction
    //ymid player's mid in vertical direction
        if((xmid>(100.0+b_a) && xmid<(200.0+b_a)  &&  ymid<=(130.0+b_b) && ymid>=(130.0+b_b)) || (xmid>0.0  && xmid<800  &&  ymid<=20 && ymid>=20))
        downEnable=0;
    else
    downEnable=1;
    }
    };

    Code:
    //open gl my display function which is called after every 33ns.acts as a while loop.
    void myDisplayFunction()
    {
        glClear( GL_COLOR_BUFFER_BIT );
    
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, _textureId);
        
        //Bottom
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glBegin(GL_POLYGON);
        glTexCoord2f( 0 , 0);
        glVertex2f(0.0+x, 0.0);
    
        glTexCoord2f( 1 , 0);
        glVertex2f(800.0+x, 0.0);
    
        glTexCoord2f( 1 , 1);
        glVertex2f(800.0+x, 800.0);
    
        glTexCoord2f( 0 , 1);
        glVertex2f(0.0+x, 800.0);
       
    
        glEnd();
        texture t1;
     
    xmid=float(player_x +player_x1)/2.0;
    ymid=float(player_y+player_y2)/2.0;
    b[0].draw_brick(50,50);
    b[0].check();
    //These are brick drawing functions!
    
    b[1].draw_brick(200,200);
    b[2].draw_brick(390,390);
    //these are brick collision checking functions.
    b[2].check();
    b[1].check();
    
    
      //not for ur use.  
    
       /* t1.display_scene();
            p1.draw();
        glDisable(GL_TEXTURE_2D);
        glutSwapBuffers();
        // i+=10;
    
        g1.g(0.5);
        printf("\n.");
    */
    }
    I want that Once I make a brick object.The collision detection code works for it.but it is working for one object only.

    Help please.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    What if your first one enables down and then the next two checks quickly disable it.
    I assume down tries to place the character under the brick he/she collides with.

    If the first collision detects and enables down you need the second and third collision to not disable it. But you do need to set a flag when no collisions are detected to disable it. So maybe dont have the check itself do anything other than return true and false and have your game code look at all the collisions and if any are true then enable down and if ALL are false then disable it.

    You should also notice just using the center point of the character won't always work. Maybe look up the bounding box method.
    Last edited by Lesshardtofind; 12-22-2012 at 01:32 PM. Reason: fist post was before coffee

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    if "else" statement is removed ,then gravity factor gets zero and the player does not come back and .i can't get ur other point.



    Please help be with the code.My display function is acting like a while loop,it is calling after every 33ns.so I think it should check all the objects for collision.

    provided coordinates are correct?

    please check the coordinates thing.can u?
    Last edited by danishzaidi; 12-22-2012 at 01:42 PM. Reason: first post was before tea :D

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Your display function shouldn't need to do collision checks. You need to start thinking about seperating concepts because if you don't do so from the start then things will get more difficult.

    My other suggestion was to make Brick.Check() a boolean rather than a void having it manipulate data itself. Then you would replace the code

    Code:
    downEnable = 0;
    with
    Code:
    return true;
    and
    Code:
    else
      downEnable = 1;
    with
    Code:
    return false;
    then your collision check could look like this
    Code:
    bool ColDetected = false;
    for(int x = 0; x < B.size(); x++){
      if(B[x].check()){
        ColDetected = true;
        downEnable = 0;
      }
    }
    if(!ColDetected)
      downEnable = 1;
    Last edited by Lesshardtofind; 12-22-2012 at 03:22 PM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    ok thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21