Skip to content

Commit

Permalink
Fix collision area from screen size to world size
Browse files Browse the repository at this point in the history
  • Loading branch information
iamoscarliang committed Jun 5, 2023
1 parent 49d4007 commit b270c60
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public final void start() {
public final void stop() {
if (mEngine.isRunning()) {
mEngine.stopGame();
mEngine.disposeGame();
onStop();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class Engine implements UpdateLoop.UpdateListener, DrawLoop.DrawListener,
public Engine(GameView gameView) {
mGameView = gameView;
mGameView.setListener(this);
// Init the collision area of QuadTree
mQuadTree.init(gameView.getWidth(), gameView.getHeight());
}
//========================================================

Expand Down Expand Up @@ -160,6 +158,9 @@ public void startGame() {
mGameView.getWidth(), mGameView.getHeight());
}

// Init the collision area to world width and height
mQuadTree.init(mCamera.getWorldWidth(), mCamera.getWorldHeight());

// Init the default TouchController
if (mTouchController == null) {
mTouchController = new SingleTouchController(mGameView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Collision(Collidable collidableA, Collidable collidableB) {
//--------------------------------------------------------
// Static methods
//--------------------------------------------------------
public static Collision init(Collidable collidableA, Collidable collidableB) {
public static Collision initCollision(Collidable collidableA, Collidable collidableB) {
if (COLLISION_POOL.isEmpty()) {
return new Collision(collidableA, collidableB);
}
Expand All @@ -38,7 +38,7 @@ public static Collision init(Collidable collidableA, Collidable collidableB) {
return c;
}

public static void returnToPool(Collision collision) {
public static void returnCollision(Collision collision) {
collision.mCollidableA = null;
collision.mCollidableB = null;
COLLISION_POOL.add(collision);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static boolean isCollisionsDetected(Collidable collidableA, Collidable co
return false;
}

// Get the CollisionBox bounds
// Get the HitBox bounds
Rect boundsA = collidableA.getCollisionHitBox().getCollisionBounds();
Rect boundsB = collidableB.getCollisionHitBox().getCollisionBounds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

public class QuadTree {

private static final int MAX_TREE_NODE = 12;
private static final int MAX_NODE = 12;

private final QuadTreeNode mRoot;

private final List<QuadTreeNode> mTreeNodePool = new ArrayList<>();
private final List<QuadTreeNode> mNodePool = new ArrayList<>();
private final List<Collision> mDetectedCollisions = new ArrayList<>();

//--------------------------------------------------------
Expand All @@ -27,8 +27,8 @@ public class QuadTree {
public QuadTree() {
mRoot = new QuadTreeNode(this);
// We add them to the pool now
for (int i = 0; i < MAX_TREE_NODE; i++) {
mTreeNodePool.add(new QuadTreeNode(this));
for (int i = 0; i < MAX_NODE; i++) {
mNodePool.add(new QuadTreeNode(this));
}
}
//========================================================
Expand All @@ -37,11 +37,7 @@ public QuadTree() {
// Getter and Setter
//--------------------------------------------------------
public int getPoolSize() {
return mTreeNodePool.size();
}

public QuadTreeNode getNode() {
return mTreeNodePool.remove(0);
return mNodePool.size();
}
//========================================================

Expand All @@ -55,7 +51,7 @@ public void init(int width, int height) {
public void checkCollision(Engine engine) {
// Clear the collisions from the previous loop
while (!mDetectedCollisions.isEmpty()) {
Collision.returnToPool(mDetectedCollisions.remove(0));
Collision.returnCollision(mDetectedCollisions.remove(0));
}
mRoot.checkCollision(engine, mDetectedCollisions);
}
Expand All @@ -68,8 +64,12 @@ public void removeCollidable(Collidable collidable) {
mRoot.getCollidables().remove(collidable);
}

public void returnToPool(QuadTreeNode treeNode) {
mTreeNodePool.add(treeNode);
public QuadTreeNode obtainNode() {
return mNodePool.remove(0);
}

public void returnNode(QuadTreeNode treeNode) {
mNodePool.add(treeNode);
}
//========================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ public List<Collidable> getCollidables() {
return mCollidables;
}

public Rect getArea(int area) {
int startX = mArea.left;
int startY = mArea.top;
int width = mArea.width();
int height = mArea.height();
switch (area) {
case 0:
return new Rect(startX, startY, startX + width / 2, startY + height / 2);
case 1:
return new Rect(startX + width / 2, startY, startX + width, startY + height / 2);
case 2:
return new Rect(startX, startY + height / 2, startX + width / 2, startY + height);
case 3:
return new Rect(startX + width / 2, startY + height / 2, startX + width, startY + height);
}

return null;
}

public void setArea(Rect area) {
mArea.set(area);
}
Expand All @@ -77,7 +58,7 @@ public void checkCollision(Engine engine, List<Collision> detectedCollisions) {
for (int j = i + 1; j < size; j++) {
Collidable collidableB = mCollidables.get(j);
if (CollisionAlgorithm.isCollisionsDetected(collidableA, collidableB)) {
Collision c = Collision.init(collidableA, collidableB);
Collision c = Collision.initCollision(collidableA, collidableB);
if (!hasBeenDetected(c, detectedCollisions)) {
detectedCollisions.add(c);
collidableA.onCollision(collidableB);
Expand All @@ -93,21 +74,40 @@ private void divideAndCheck(Engine engine, List<Collision> detectedCollisions) {
mChildren.clear();
// Add 4 children
for (int i = 0; i < 4; i++) {
mChildren.add(mParent.getNode());
mChildren.add(mParent.obtainNode());
}
// Check children collision
for (int i = 0; i < 4; i++) {
QuadTreeNode node = mChildren.get(i);
node.setArea(getArea(i));
node.checkObjects(mCollidables);
node.setArea(getChildArea(i));
node.initChildArea(mCollidables);
node.checkCollision(engine, detectedCollisions);
// Clear and return to the pool
node.getCollidables().clear();
mParent.returnToPool(node);
mParent.returnNode(node);
}
}

private Rect getChildArea(int area) {
int startX = mArea.left;
int startY = mArea.top;
int width = mArea.width();
int height = mArea.height();
switch (area) {
case 0:
return new Rect(startX, startY, startX + width / 2, startY + height / 2);
case 1:
return new Rect(startX + width / 2, startY, startX + width, startY + height / 2);
case 2:
return new Rect(startX, startY + height / 2, startX + width / 2, startY + height);
case 3:
return new Rect(startX + width / 2, startY + height / 2, startX + width, startY + height);
default:
throw new IllegalArgumentException("Collision area not found!");
}
}

private void checkObjects(List<Collidable> collidables) {
private void initChildArea(List<Collidable> collidables) {
mCollidables.clear();
int size = collidables.size();
for (int i = 0; i < size; i++) {
Expand All @@ -122,7 +122,8 @@ private void checkObjects(List<Collidable> collidables) {
private boolean hasBeenDetected(Collision collision, List<Collision> detectedCollisions) {
int size = detectedCollisions.size();
for (int i = 0; i < size; i++) {
if (detectedCollisions.get(i).equals(collision)) {
Collision c = detectedCollisions.get(i);
if (c.equals(collision)) {
return true;
}
}
Expand Down

0 comments on commit b270c60

Please sign in to comment.