Android游戏框架之基础之AA碰撞系统

时间:2024-01-14 13:26:02

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. }