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

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.replica.replicaisland;
  17. /**
  18. * An Axis-Aligned rectangular collision volume.  This code treats other volumes as if they are
  19. * also rectangles when calculating intersections.  Therefore certain types of intersections, such
  20. * as sphere vs rectangle, may not be absolutely precise (in the case of a sphere vs a rectangle,
  21. * for example, a new rectangle that fits the sphere is used to perform the intersection test, so
  22. * there is some potential for false-positives at the corners).  However, for our purposes absolute
  23. * precision isn't necessary, so this simple implementation is sufficient.
  24. */
  25. public class AABoxCollisionVolume extends CollisionVolume {
  26. private Vector2 mWidthHeight;
  27. private Vector2 mBottomLeft;
  28. public AABoxCollisionVolume(float offsetX, float offsetY, float width, float height) {
  29. super();
  30. mBottomLeft = new Vector2(offsetX, offsetY);
  31. mWidthHeight = new Vector2(width, height);
  32. }
  33. public AABoxCollisionVolume(float offsetX, float offsetY, float width, float height,
  34. int hit) {
  35. super(hit);
  36. mBottomLeft = new Vector2(offsetX, offsetY);
  37. mWidthHeight = new Vector2(width, height);
  38. }
  39. @Override
  40. public final float getMaxX() {
  41. return mBottomLeft.x + mWidthHeight.x;
  42. }
  43. @Override
  44. public final float getMinX() {
  45. return mBottomLeft.x;
  46. }
  47. @Override
  48. public final float getMaxY() {
  49. return mBottomLeft.y + mWidthHeight.y;
  50. }
  51. @Override
  52. public final float getMinY() {
  53. return mBottomLeft.y;
  54. }
  55. /**
  56. * Calculates the intersection of this volume and another, and returns true if the
  57. * volumes intersect.  This test treats the other volume as an AABox.
  58. * @param position The world position of this volume.
  59. * @param other The volume to test for intersections.
  60. * @param otherPosition The world position of the other volume.
  61. * @return true if the volumes overlap, false otherwise.
  62. */
  63. @Override
  64. public boolean intersects(Vector2 position, FlipInfo flip, CollisionVolume other,
  65. Vector2 otherPosition, FlipInfo otherFlip) {
  66. final float left = getMinXPosition(flip) + position.x;
  67. final float right = getMaxXPosition(flip) + position.x;
  68. final float bottom = getMinYPosition(flip) + position.y;
  69. final float top = getMaxYPosition(flip) + position.y;
  70. final float otherLeft = other.getMinXPosition(otherFlip) + otherPosition.x;
  71. final float otherRight = other.getMaxXPosition(otherFlip) + otherPosition.x;
  72. final float otherBottom = other.getMinYPosition(otherFlip) + otherPosition.y;
  73. final float otherTop = other.getMaxYPosition(otherFlip) + otherPosition.y;
  74. final boolean result = boxIntersect(left, right, top, bottom,
  75. otherLeft, otherRight, otherTop, otherBottom)
  76. || boxIntersect(otherLeft, otherRight, otherTop, otherBottom,
  77. left, right, top, bottom);
  78. return result;
  79. }
  80. /** Tests two axis-aligned boxes for overlap. */
  81. private boolean boxIntersect(float left1, float right1, float top1, float bottom1,
  82. float left2, float right2, float top2, float bottom2) {
  83. final boolean horizontalIntersection = left1 < right2 && left2 < right1;
  84. final boolean verticalIntersection = top1 > bottom2 && top2 > bottom1;
  85. final boolean intersecting = horizontalIntersection && verticalIntersection;
  86. return intersecting;
  87. }
  88. /** Increases the size of this volume as necessary to fit the passed volume. */
  89. public void growBy(CollisionVolume other) {
  90. final float maxX;
  91. final float minX;
  92. final float maxY;
  93. final float minY;
  94. if (mWidthHeight.length2() > 0) {
  95. maxX = Math.max(getMaxX(), other.getMaxX());
  96. minX = Math.max(getMinX(), other.getMinX());
  97. maxY = Math.max(getMaxY(), other.getMaxY());
  98. minY = Math.max(getMinY(), other.getMinY());
  99. } else {
  100. maxX = other.getMaxX();
  101. minX = other.getMinX();
  102. maxY = other.getMaxY();
  103. minY = other.getMinY();
  104. }
  105. final float horizontalDelta = maxX - minX;
  106. final float verticalDelta = maxY - minY;
  107. mBottomLeft.set(minX, minY);
  108. mWidthHeight.set(horizontalDelta, verticalDelta);
  109. }
  110. }

Android游戏框架之基础之AA碰撞系统的更多相关文章

  1. Android游戏框架Libgdx使用入门

    转载自:http://blog.csdn.net/cping1982/article/details/6176191 Libgdx作者博客:http://www.badlogicgames.com/ ...

  2. 识货的拿走:Android游戏框架解读之总体结构

    Android游戏开发的框架图无偿奉上.

  3. 5个最佳的Android测试框架(带示例)

    谷歌的Android生态系统正在不断地迅速扩张.有证据表明,新的移动OEM正在攻陷世界的每一个角落,不同的屏幕尺寸.ROM /固件.芯片组以及等等等等,层出不穷.于是乎,对于Android开发人员而言 ...

  4. 八款常见的Android游戏引擎

    原文地址:http://bbs.csdn.net/topics/380203732 1.Angle  Angle是一款专为Android平台设计的,敏捷且适合快速开发的2D游戏引擎,基于OpenGL  ...

  5. Android 游戏教程让人物动起来

    在这里给大家分享Android游戏教程怎样让人物动起来,话不多说了,直接进入正题. 一. 准备工作     首先要准备好要使用的人物动作图和地形图.把它分割成16个不同的动作,循环播放同一行的4个不同 ...

  6. 记录一下八款开源 Android 游戏引擎

    记录一下八款开源 Android 游戏引擎 虽然android学了点点,然后现在又没学了(我为啥这么没有恒心呢大哭).以后有时间还是要继续学android的,一定要啊!虽然现在没学android游戏编 ...

  7. [Android游戏开发]八款开源 Android 游戏引擎 (巨好的资源)

    初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道该从何处入手,每当遇到自己无法解决的难题时,又往往会一边羡慕于 iPhone下有诸如Cocos2d-iphone之类的免费游戏引 ...

  8. 八款开源 Android 游戏引擎 (巨好的资源)

    转载地址:http://software.intel.com/zh-cn/blogs/2012/01/13/android-4 初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道 ...

  9. Android 八款开源 Android 游戏引擎

    原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...

随机推荐

  1. 比赛组队问题 --- 递归解法 --- java代码 --- 八皇后问题

    两队比赛,甲队为A.B.C3人,乙队为X.Y.Z3人.已知A不和X比,C不和X.Z比,请编程序找出3队赛手名单 采用了与八皇后问题相似的解法,代码如下: 如有疑问请链接八皇后问题的解法:http:// ...

  2. 谈谈final、finally和finalize

    final: final为修饰符, 如果类被声明为final,则不能派生新子类. 如果变量被声明为final,则必须在声明时初始化,在以后的引用只能读取,不可修改. 如果方法被final声明,则只能使 ...

  3. Hadoop-- MapReduce简单理解

    1.Hadoop和MapReduce概念 Mapreduce是一种模式. Hadoop是一种框架. Hadoop是一个实现了mapreduce模式的开源的分布式并行编程框架. 2.Hadoop框架   ...

  4. mysql根据时间查询前一天数据

    MySql数据库如何根据时间查询前一天的数据?本文整理了几个解决方法,有需要的朋友参考下.   本节内容:用MySql怎么根据时间查询前一天的数据. 例1: 代码示例: select * from t ...

  5. 构造数组的MaxTree

    题目 一个数组的MaxTree定义: 数组必须没有重复元素 MaxTree是一棵二叉树,数组的每一个值对应一个二叉树节点 包括MaxTree树在内且在其中的每一棵子树上,值最大的节点都是树的头 给定一 ...

  6. 如何通过Android Studio发布library到jCenter和Maven Central

    http://www.jianshu.com/p/3c63ae866e52# 在Android Studio里,如果你想引入任何library到自己的项目中,只需要很简单的在module的build. ...

  7. openjdk sunjdk区别

    使用过LINUX的人都应该知道,在大多数LINUX发行版本里,内置或者通过软件源安装JDK的话,都是安装的openjdk,那么到底什么是openjdk,它与sun jdk有什么关系和区别呢? 历史上的 ...

  8. mysql基础知识(3)--创建

    创建表: 基本形式 create  table  [if not  exists] 表名(字段列表, [约束或索引列表]) [表选项列表]; 说明:列表都是表示“多个”,相互之间用逗号分开. 字段基本 ...

  9. 转】Mahout分步式程序开发 聚类Kmeans

    原博文出自于: http://blog.fens.me/hadoop-mahout-kmeans/ 感谢! Mahout分步式程序开发 聚类Kmeans Hadoop家族系列文章,主要介绍Hadoop ...

  10. [原创]Devexpress XtraReports 系列 10 创建标签报表

    今天这篇是Dx Reports 基础初级系列的最后一篇了.以后如果有什么高级的应用,应该另开一个中级使用系列. 昨天发表了Devexpress XtraReports系列第九篇[原创]Devexpre ...