Android游戏框架之基础之AA碰撞系统
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碰撞系统的更多相关文章
- Android游戏框架Libgdx使用入门
转载自:http://blog.csdn.net/cping1982/article/details/6176191 Libgdx作者博客:http://www.badlogicgames.com/ ...
- 识货的拿走:Android游戏框架解读之总体结构
Android游戏开发的框架图无偿奉上.
- 5个最佳的Android测试框架(带示例)
谷歌的Android生态系统正在不断地迅速扩张.有证据表明,新的移动OEM正在攻陷世界的每一个角落,不同的屏幕尺寸.ROM /固件.芯片组以及等等等等,层出不穷.于是乎,对于Android开发人员而言 ...
- 八款常见的Android游戏引擎
原文地址:http://bbs.csdn.net/topics/380203732 1.Angle Angle是一款专为Android平台设计的,敏捷且适合快速开发的2D游戏引擎,基于OpenGL ...
- Android 游戏教程让人物动起来
在这里给大家分享Android游戏教程怎样让人物动起来,话不多说了,直接进入正题. 一. 准备工作 首先要准备好要使用的人物动作图和地形图.把它分割成16个不同的动作,循环播放同一行的4个不同 ...
- 记录一下八款开源 Android 游戏引擎
记录一下八款开源 Android 游戏引擎 虽然android学了点点,然后现在又没学了(我为啥这么没有恒心呢大哭).以后有时间还是要继续学android的,一定要啊!虽然现在没学android游戏编 ...
- [Android游戏开发]八款开源 Android 游戏引擎 (巨好的资源)
初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道该从何处入手,每当遇到自己无法解决的难题时,又往往会一边羡慕于 iPhone下有诸如Cocos2d-iphone之类的免费游戏引 ...
- 八款开源 Android 游戏引擎 (巨好的资源)
转载地址:http://software.intel.com/zh-cn/blogs/2012/01/13/android-4 初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道 ...
- Android 八款开源 Android 游戏引擎
原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...
随机推荐
- CH340在STM32实现一键下载电路
在做基于STM32的多功能MP3播放器的课题时,在程序下载这部分时借鉴了正点原子开发板上的一键下载电路,采用CH340G这款芯片设计. 在画PCB初期原理图部分,对采用CH340G设计的一键下载电路不 ...
- 九度 Online Judge 之《剑指 Offer》一书相关题目解答
前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...
- STL六大组件之——适配器代表大会
适配器也是一种常用的设计模式: 将一个类的接口转换为另一个类的接口,使得原本因接口不兼容而不能合作的两个类可以一起运作.STL提供三种适配器:改变容器接口的容器适配器.改变迭代器接口的迭代器适配器以及 ...
- seo技巧-2015/10/05
1.每页都要有它自己的文件名,并且有它自己的上级文件夹和它自己相关关键字. 2.建议在每页上使用一个的H1标签.我也试着使用许多H2 或H3的标签在页面内辅助构成正文内容. 3. 有时花费一点钱帮助你 ...
- Linux_系统信息
公司里一些仿真软件得进Linux系统,好奇公司用的什么Linux版本,于是搜罗了几个命令如下: 1 uname - Print system info -a, print all info -s, ...
- 安卓手机修改hosts攻略-摘自网络
Android手机是和Google帐号紧密联系的,由于$^&情况,很多时候Google帐号无法登录,导致Android市场无法使用.在电脑上我们通过修改Hosts方法可以解决Google帐号的 ...
- 【Maven】Maven下载源码和Javadoc的方法
1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...
- Linux下的crontab定时执行任务命令详解
在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...
- MacTerminal快捷键
[MacTerminal快捷键] 在Mac系统中并没有Home.End等键,所以在使用时并不是特别的顺手,但是有几个键位组合可以使Terminal的操作更加灵活方便. 1.将光标移动到行首:ctrl ...
- spring properties resolve 问题
在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...