Android 冲撞检测
2013-11-29 23:20
134 查看
虽然应该不是最优的,但是是逻辑较简单的那种
逻辑是:检查物体的4个点是否有至少一个在另外一个物体里,有就相撞了。
//冲撞检测
public boolean collidesWith(Layer otherSprite) {
if (otherSprite == null) {
throw new NullPointerException();
}
// 可见物体才需要相撞
if (otherSprite.isVisible() && this.isVisible()){
float otherX = otherSprite.getX();
float otherY = otherSprite.getY();
float otherWidth = otherSprite.getWidth();
float otherHeight = otherSprite.getHeight();
float thisX = getX();
float thisY = getY();
float thisWidth = getWidth();
float thisHeight = getHeight();
//判断this物体的4个顶点坐标是否在Other里面,就为碰撞
boolean f1 = (thisX + thisWidth) > otherX && (thisX + thisWidth) < (otherX + otherWidth) && (thisY + thisHeight) > otherY && (thisY + thisHeight) < (otherY + otherHeight) ;
boolean f2 = thisX > otherX && thisX < (otherX + otherWidth) && (thisY + thisHeight) > otherY && (thisY + thisHeight) < (otherY + otherHeight);
boolean f3 = (thisX + thisWidth) > otherX && (thisX + thisWidth) < (otherX + otherWidth) && thisY > otherY && thisY < (otherY + otherHeight);
boolean f4 = thisX > otherX && thisX < (otherX + otherWidth) && thisY > otherY && thisY < (otherY + otherHeight);
if(f1||f2||f3||f4){
return true;
}
}
return false;
}
逻辑是:检查物体的4个点是否有至少一个在另外一个物体里,有就相撞了。
//冲撞检测
public boolean collidesWith(Layer otherSprite) {
if (otherSprite == null) {
throw new NullPointerException();
}
// 可见物体才需要相撞
if (otherSprite.isVisible() && this.isVisible()){
float otherX = otherSprite.getX();
float otherY = otherSprite.getY();
float otherWidth = otherSprite.getWidth();
float otherHeight = otherSprite.getHeight();
float thisX = getX();
float thisY = getY();
float thisWidth = getWidth();
float thisHeight = getHeight();
//判断this物体的4个顶点坐标是否在Other里面,就为碰撞
boolean f1 = (thisX + thisWidth) > otherX && (thisX + thisWidth) < (otherX + otherWidth) && (thisY + thisHeight) > otherY && (thisY + thisHeight) < (otherY + otherHeight) ;
boolean f2 = thisX > otherX && thisX < (otherX + otherWidth) && (thisY + thisHeight) > otherY && (thisY + thisHeight) < (otherY + otherHeight);
boolean f3 = (thisX + thisWidth) > otherX && (thisX + thisWidth) < (otherX + otherWidth) && thisY > otherY && thisY < (otherY + otherHeight);
boolean f4 = thisX > otherX && thisX < (otherX + otherWidth) && thisY > otherY && thisY < (otherY + otherHeight);
if(f1||f2||f3||f4){
return true;
}
}
return false;
}
相关文章推荐
- Android 手势检测实战 打造支持缩放平移的图片预览效果(上)
- 算法移植优化(三)android dlib 人脸检测使用
- DDMS 检测不到 android 设备(红米手机)。
- Android系统的Root权限获取与检测
- Android App退出检测
- Android Studio select deployment target 检测不到模拟器
- 探讨Android 6.0及以上新权限系统的检测与处理
- Android中的人脸检测(静态和动态)
- android 自动检测版本升级
- Android静态安全检测 -> HTTPS敏感数据劫持漏洞
- Android程序检测网络是否可用
- 【Android】检测是否处于Wifi环境,利用WebView实现浏览器app
- Android静态安全检测 -> Content Provider组件本地SQL注入漏洞
- 值得推荐的Android应用性能检测工具列表(转)
- Android 内存检测工具
- Android平台上利用opencv进行图像的边沿检测
- Android检测服务是否运行
- Android摇晃检测ShakeDetector
- Androin学习笔记四十二:Java android Socket通信检测(server)连接是否断开
- Android apk版本检测及更新