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);    }}

更多相关文章

  1. android 手机管理软件 发布开源代码
  2. 实用代码3
  3. Android使用代码实现关机/重启
  4. 【知识点】android代码中设置margin
  5. Android蓝牙通信代码
  6. Android代码规范
  7. android典型代码系列(二十二)------按键使上面的EditText抖动

随机推荐

  1. 使用case语句编写一个系统服务脚本
  2. MySQL数据库入门——备份数据库
  3. Mac电脑设置语音详细度的方法
  4. Linux基础-21day-Linux系统LVM逻辑卷管理
  5. Python开发技巧:scrapy-redis爬虫如何发送
  6. 利用frp实现内网穿越
  7. 华为分析5.2.0重磅上线,四大亮点让数据洞
  8. 异步多个请求的处理
  9. 在苹果Mac上如何将旁白命令分配到键盘或
  10. JavaScript中的预解析(变量提升)介绍!