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. 实用代码3
  2. Android干坏事——禁止设备休眠
  3. Android: 自定义Tab样式
  4. android 显示特殊符号
  5. android中使用特殊符号
  6. android viewpage的使用
  7. OpenGL ES for Android(安卓)相机预览
  8. Android(安卓)开源组件无限循环ViewPager
  9. Android(安卓)五大布局

随机推荐

  1. Android5.0 新特性学习总结
  2. Flutter String格式化
  3. Android(安卓)上网流量监控方法
  4. Android(安卓)布局中位置和大小单位相关
  5. android studio运行时提示All packages a
  6. 我的Android进阶之旅------>Android之Ani
  7. Android总结 - System UI Mode
  8. Android(安卓)SDK Manager 和 ADT 更新
  9. Android周学习Step By Step(8)--Intent之启
  10. android 水下气泡上升界面效果