Java applet中的碰撞检测帮助

时间:2015-01-13 01:22:42

标签: java applet collision-detection collision

我正在制作一个飞扬的鸟类游戏,而我正试图做出一个声明,如果它接触到两个管道中的一个,它将会死亡。

这是我在'run'方法中运行的碰撞代码。

    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;
                }
            }
}

当然还有更多内容,我只是想知道如何对鸟和管道进行碰撞检测。

1 个答案:

答案 0 :(得分:2)

如果这些管道是线形的,并且如果你知道它们的线公式(或者至少是端点的坐标),那么你可以使用&#34;垂直距离线#34;计算以确定它是否足够接近。

已经回答:here

如果管道需要是一个复杂的形状,如果你可以使用粒子模拟,这里是一个非常简单,低效但易于使用的例子,它在一组可碰撞对象上构建一个对象并检查与一个碰撞对象(鸟):

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();}  
    }   
}

大部分时间,我的输出是:

Bird collides pipe on 4 different points and dies

你可以添加另一个集合不同集合的类来制作更复杂的场景,例如安装在太空船上的旋转炮塔,它可以发射光束来切割管道,甚至可以碰撞其他宇宙飞船和鸟类。