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 @   Sentinel-163  阅读(232)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
点击右上角即可分享
微信分享提示