AA 碰撞体 就是将所有的物体设置为矩形框进行碰撞计算。下面是代码

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.replica.replicaisland;
/**
* An Axis-Aligned rectangular collision volume.  This code treats other volumes as if they are
* also rectangles when calculating intersections.  Therefore certain types of intersections, such
* as sphere vs rectangle, may not be absolutely precise (in the case of a sphere vs a rectangle,
* for example, a new rectangle that fits the sphere is used to perform the intersection test, so
* there is some potential for false-positives at the corners).  However, for our purposes absolute
* precision isn't necessary, so this simple implementation is sufficient.
*/
public class AABoxCollisionVolume extends CollisionVolume {
private Vector2 mWidthHeight;
private Vector2 mBottomLeft;
public AABoxCollisionVolume(float offsetX, float offsetY, float width, float height) {
super();
mBottomLeft = new Vector2(offsetX, offsetY);
mWidthHeight = new Vector2(width, height);
}
public AABoxCollisionVolume(float offsetX, float offsetY, float width, float height,
int hit) {
super(hit);
mBottomLeft = new Vector2(offsetX, offsetY);
mWidthHeight = new Vector2(width, height);
}
@Override
public final float getMaxX() {
return mBottomLeft.x + mWidthHeight.x;
}
@Override
public final float getMinX() {
return mBottomLeft.x;
}
@Override
public final float getMaxY() {
return mBottomLeft.y + mWidthHeight.y;
}
@Override
public final float getMinY() {
return mBottomLeft.y;
}
/**
* Calculates the intersection of this volume and another, and returns true if the
* volumes intersect.  This test treats the other volume as an AABox.
* @param position The world position of this volume.
* @param other The volume to test for intersections.
* @param otherPosition The world position of the other volume.
* @return true if the volumes overlap, false otherwise.
*/
@Override
public boolean intersects(Vector2 position, FlipInfo flip, CollisionVolume other,
Vector2 otherPosition, FlipInfo otherFlip) {
final float left = getMinXPosition(flip) + position.x;
final float right = getMaxXPosition(flip) + position.x;
final float bottom = getMinYPosition(flip) + position.y;
final float top = getMaxYPosition(flip) + position.y;
final float otherLeft = other.getMinXPosition(otherFlip) + otherPosition.x;
final float otherRight = other.getMaxXPosition(otherFlip) + otherPosition.x;
final float otherBottom = other.getMinYPosition(otherFlip) + otherPosition.y;
final float otherTop = other.getMaxYPosition(otherFlip) + otherPosition.y;
final boolean result = boxIntersect(left, right, top, bottom,
otherLeft, otherRight, otherTop, otherBottom)
|| boxIntersect(otherLeft, otherRight, otherTop, otherBottom,
left, right, top, bottom);
return result;
}
/** Tests two axis-aligned boxes for overlap. */
private boolean boxIntersect(float left1, float right1, float top1, float bottom1,
float left2, float right2, float top2, float bottom2) {
final boolean horizontalIntersection = left1 < right2 && left2 < right1;
final boolean verticalIntersection = top1 > bottom2 && top2 > bottom1;
final boolean intersecting = horizontalIntersection && verticalIntersection;
return intersecting;
}
/** Increases the size of this volume as necessary to fit the passed volume. */
public void growBy(CollisionVolume other) {
final float maxX;
final float minX;
final float maxY;
final float minY;
if (mWidthHeight.length2() > 0) {
maxX = Math.max(getMaxX(), other.getMaxX());
minX = Math.max(getMinX(), other.getMinX());
maxY = Math.max(getMaxY(), other.getMaxY());
minY = Math.max(getMinY(), other.getMinY());
} else {
maxX = other.getMaxX();
minX = other.getMinX();
maxY = other.getMaxY();
minY = other.getMinY();
}
final float horizontalDelta = maxX - minX;
final float verticalDelta = maxY - minY;
mBottomLeft.set(minX, minY);
mWidthHeight.set(horizontalDelta, verticalDelta);
}
}

Android游戏框架之基础之AA碰撞系统相关推荐

  1. Android DRM框架与基础知识

    Android DRM框架与基础知识 Android DRM框架 DRM框架的目的:能让安卓设备可以播放更多的内容,不同的内容和硬件设备可能使用的是不同的内容版权保护机制或者没有版权管理机制,但是安卓 ...

  2. Android游戏框架AndEngine使用入门

    项目站点:http://www.andengine.org 项目地址:http://code.google.com/p/andengine 示例地址:http://code.google.com/p/ ...

  3. Android游戏框架解读之总体结构

    Android游戏开发的框架图无偿奉上. 大小: 55.7 KB 查看图片附件

  4. 简单的android游戏框架——zgf

    一.zgf是什么 我花了一周时间整理了以前写的代码,发现很多代码可以重用,于是就做了一个简单的android游戏开发框架,名字叫zxx43 game framework,简称zgf,这个游戏框架使用起 ...

  5. 【android-tips】SurfaceView的制作android游戏框架介绍

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.介绍 我们知道android游戏主要包含两方面.一方面是控制类,这个通过一些循环以及监听机制来实现.另一方面 ...

  6. Android GreenDao框架使用 基础篇

    参考 官方文档 Github 一.准备工作 在工程的build.gradle文件中添加 repositories {...mavenCentral()}dependencies {...classpa ...

  7. 5个最佳的Android测试框架

    2019独角兽企业重金招聘Python工程师标准>>> 谷歌的Android生态系统正在不断地迅速扩张.有证据表明,新的移动OEM正在攻陷世界的每一个角落,不同的屏幕尺寸.ROM / ...

  8. 《Android游戏开发详解》一导读

    前 言 Android游戏开发详解 作为对编程知之甚少或者毫无所知的初学者,开始学习Android游戏开发,可能会觉得就像是穿越陌生的星际的旅程.有太多的事情要尝试,太多的知识要学习,令人遗憾的是,还 ...

  9. 技术转载:八款开源 Android 游戏引擎 (巨好的资源)

    作者: iamsheldon 链接:http://software.intel.com/zh-cn/blogs/2012/01/13/android-4/ 初学Android游戏开发的朋友,往往会显得 ...

最新文章

  1. 【青少年编程】【四级】创意画图
  2. python打印表情
  3. 不用库函数求平方根!
  4. 陌生人社会_陌生人之旅
  5. Collections.synchronizedList使用
  6. STM32F103:三.(2)红外接收
  7. 谁说 Vim 不好用?
  8. java基本数据类型填空题_java基本数据类型练习题
  9. MyBatis关联映射
  10. [NULL @ 000002d5c65b5180] missing picture in access unit
  11. 35 漂亮的单页网页设计
  12. python常用工具类
  13. 印象笔记Markdown的使用方法
  14. 游戏服务器——中心服
  15. 数据压缩(十四)——AR模型的参数估计阅读
  16. LED显示行业之知识大全4
  17. 雄关漫道真如铁,而今迈步从头越.
  18. ckeditor粘贴上传图片
  19. 动手训练属于自己的无人车,这个超强服务现已开源!
  20. 无人驾驶---1 激光雷达的地面-非地面分割和pcl_ros实践

热门文章

  1. asp.net 点击查询跳转到查询结果页面_个体户如何办理和查询定期定额业务?
  2. access哪个速度快 vfp_大学计算机二级考试,报考哪个科目比较好?
  3. go语言的iota是什么意思_关于Golang中的iota
  4. Word2013、2016中页码总页数设置为当前节总页数
  5. 3d旋转相册代码源码_实现可旋转的Reflection Probe(原创)
  6. 坐标系转换(镜像与对换)
  7. 画活动图教程_绘画教程116—传统的山水现代的刀画,看了就会的步骤图
  8. Iso时间转java instant,在java.util.Date和java.time.Instant之间转换古代日期时的差异
  9. 探究Java虚拟机栈
  10. 强悍的远程桌面管理器