JAVA开发之碰撞算法

/**
  *物体的碰撞检测方法
  */
    public class Aoo {
    protected int wideth;  //对象的宽
    protected int heigth;  //对象的高
    protected int x;       //对象的X坐标
    protected int y;       //对象的X坐标
    
    public boolean isHit(Aoo other) {
        //设置this为碰撞物   other为被碰撞物
        int x1 = this.x - other.wideth;//x1:this碰撞物的x的坐标-被撞物的宽
        int x2 = this.x + this.wideth;//x2:this碰撞物的x的坐标+碰撞物的宽
        int y1 = this.y - other.heigth;//y2:this碰撞物的y的坐标-被撞物的高
        int y2 = this.y + this.heigth;//y2:this碰撞物的y的坐标+碰撞物的高

        int x = other.x;//碰撞物的x坐标
        int y = other.y;//碰撞物的Y坐标

        //当碰撞物在x1<=x<=x2  并且y坐标在y1<=y<=y2 即为碰撞
        return x >= x1 && x <= x2 && y >= y1 && y <= y2;
       }
    }

 

posted @ 2022-05-20 20:54  Sentinel-163  阅读(227)  评论(0编辑  收藏  举报