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 搜索引擎的文章,于是搜索了, ...
随机推荐
- ctype库试运行
from ctypes import * msvcrt=cdll.msvcrt message_string="Hello world!\n" msvcrt.wprintf(&qu ...
- Web开发中设置快捷键来增强用户体验
从事对日外包一年多以来,发现日本的无论是WinForm项目还是Web项目都注重快捷键的使用,日本人操作的时候都喜欢用键盘而不是用鼠标去点,用他们的话来说"键盘永远比鼠标来的快",所 ...
- (转载)OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类,今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray ...
- 刚刚大学毕业,自己搭网站遇到的问题 一:tomcat中同时部署两个项目的问题
最近直接把两个项目打成war包在tomcat下发布,出现了很多莫名奇妙的问题,就是不能发布成功,只能有一个项目能成功,在网上查了很多方法,以为是两个项目中jar包出现冲突,也按照网上的方法把两个项目中 ...
- IDEA使用docker进行调试
背景 手头有个任务,需要用java通过jni调用一个开源算法库gmssl的功能,但是gmssl只提供了源码,需要编译后才能使用.按照通常的做法,我们会部署好centos的虚拟机和开发环境,安装好gms ...
- JPA多对多@manytomany注解配置实例
维护端注解 @ManyToMany (cascade = CascadeType.REFRESH) @JoinTable (//关联表 name = "student_teacher&quo ...
- HDU1150Machine Schedule(二分图最大匹配的DFS解法)
题目大意就是说有两台机器,分别有n,m种模式可以调节,有k个工作,某一个工作i可以在第一台机器的a[i]模式下或第二台机器的b[i]模式下工作,两台机器的初始模式为0,问如何分配这K件工作使得两台机器 ...
- ELF学习--重定位文件
add.c int data = 1;int bss;const int rodata = 1;int add(int num1, int num2){ int sum = 0; sum = num1 ...
- angular实践第一弹:选项卡开发
在学习angular的过程中,实践是最好的方法. 在开发选项卡的过程中,不需要再像jquery一样以DOM操作为核心,那什么样的情况是以DOM操作为核心的Jquery的思想呢? 一想到改变什么,就想设 ...
- CSS实现未知高度图文混合垂直居中
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-26) CSS实现未知高度图文混合垂直居中,阅读CSS实现未知高度图文混合垂直居中. IE6,IE7,FF3测试通过 ...