0

I'm making a flappy bird game, and I'm trying to make a statement where the bird will die if it touches one of the two pipes.

Here's my collision code which runs in the 'run' method.

    int appletsize_x = 800;
    int appletsize_y = 500;

    int x_pos = appletsize_x / 2;
    int y_pos = appletsize_y / 2;

    int x_pos2 = 100;
    int y_pos2 = -50;

    int x_pos6 = 100;
    int y_pos6 = 350;

      public void run ()
    {

            if (x_pos >= x_pos2
                    || (x_pos <= x_pos6))
            {
                collision = true;
                if (collision = true)
                {
                    startgame = false;
                }
            }
}

Of course there's more to it, I was just wondering how I'd make a collision detection for the bird and pipe.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58

1 Answers1

2

If those pipes are line shaped and if you know their line formula(or at least coords. of end points), then you can use "perpendicular distance to a line" calculation to know if it is close enough.

That is already answered: here

If a pipe needs to be a complex shape, and if you are ok with a particle simulation, here is very simple, inefficient but easy to use example that builds an object on a group of collidable objects and checks collision with a single object(bird):

import java.util.ArrayList;
import java.util.List;


public class FactoryClass {

    public class CollidableResult
    {
        public Collidable c;
        public boolean yesItIs;
    }

    public class Collidable
    {
        public float x;
        public float y;
        public static final float tolerance=600.001f; 
        public Collidable()
        {
            //just giving random starting coordinates for fun
            // so the object may not be a pipe with these. Please add some parameters
            // for your needs
            x=(float) (Math.random()*1000.0);
            y=(float) (Math.random()*1000.0);         
        }
        public CollidableResult isTooClose(Collidable c)
        {
            float deltax=(c.x - this.x);
            float deltay=(c.y - this.y);
            // checks if close enough
            if(Math.sqrt(deltax*deltax + deltay*deltay)<tolerance) 
            {
                CollidableResult cr=new CollidableResult();
                cr.yesItIs=true;
                cr.c=this;
                return cr;              
            }
            else
            {
                CollidableResult cr=new CollidableResult();
                cr.yesItIs=false;
                cr.c=null;
                return cr;   
            }
        }

        public List<Collidable> collide(List<Collidable> cl)
        {
            List<Collidable> c=new ArrayList<Collidable>();
            for(Collidable co:cl)
            {
                if(this.isTooClose(co).yesItIs)
                {
                    c.add(co);
                }
            }
            return c;       
        }
        public void die()
        {
            // AnimateDead();
            try {this.finalize();} catch (Throwable e) {e.printStackTrace();}
            System.gc();        
        }
        public void levelUp()
        { 
            System.out.println("Level Up! Hit points incremented by 12.");  
        }

    }

    public static void main(String[] args) {
        FactoryClass factory=new FactoryClass();
        List<Collidable> pointsOfAPipe = new ArrayList<Collidable>();
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));
        //...
        // build your pipe object using many colllidable points (can build complex shapes with this)
        //...
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));
        pointsOfAPipe.add(factory.new Collidable(/*parameters for pipe */));

        Collidable bird=factory.new Collidable();
        bird.x=100;
        bird.y=350;
        List<Collidable> collisionPoints = bird.collide(pointsOfAPipe);
        if(collisionPoints.size()>0)
        {
            System.out.println("Bird collides pipe on "+collisionPoints.size()+" different points and dies");
            bird.die();
        }
        else {System.out.println("Bird survived");bird.levelUp();}  
    }   
}

most of the time, output for me is:

Bird collides pipe on 4 different points and dies

You can add another class that integrates different collections to make even more complex scenarios such as rotating turrets mounted on a spaceship which fires beams to cut pipes and even collides other spaceships and birds.

Community
  • 1
  • 1
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97