Back to project page OpenGL-2D-Engine.
The source code is released under:
Apache License
If you think the Android project OpenGL-2D-Engine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.foolish.a2de.physics; //from w w w.j a va 2 s .com import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.graphics.RectF; import com.foolish.a2de.graphics.Rectangle; import com.foolish.a2de.graphics.Shape; public class SimpleRectanglePhysics implements IPhysics2D { private List<Rectangle> mPossibleCollisions; private boolean mIntersects = false; public SimpleRectanglePhysics(Rectangle... rects) { this(Arrays.asList(rects)); } public SimpleRectanglePhysics(List<Rectangle> rects) { mPossibleCollisions = rects; } public boolean addRect(Rectangle rect) { if (mPossibleCollisions == null) mPossibleCollisions = new ArrayList<>(); return mPossibleCollisions.add(rect); } public boolean removeRect(Rectangle rect) { if (mPossibleCollisions == null) return false; return mPossibleCollisions.remove(rect); } @Override public void applyPhysics(Shape shape) { Rectangle rect = (Rectangle) shape; rect.update(); mIntersects = false; for (Rectangle possibleCollision : mPossibleCollisions) { if (rect.intersects(possibleCollision)) { mIntersects = true; rect.translate(-rect.speed().x, -rect.speed().y); RectF boundsRect = rect.bounds(); RectF boundsCollision = possibleCollision.bounds(); if (boundsRect.bottom <= boundsCollision.top) { rect.translate(rect.speed().x, 0.0f); rect.setSpeed(rect.speed().x, 0.0f); rect.setGrounded(true); } else if (boundsRect.left >= boundsCollision.right) { rect.translate(0.0f, rect.speed().y); rect.setSpeed(0.0f, rect.speed().y); } else if (boundsRect.right <= boundsCollision.left) { rect.translate(0.0f, rect.speed().y); rect.setSpeed(0.0f, rect.speed().y); } else if (boundsRect.top >= boundsCollision.bottom) { rect.translate(rect.speed().x, 0.0f); rect.setSpeed(rect.speed().x, -0.01f); } } } if (!mIntersects) { float y = rect.speed().y; y -= 0.001f; if (y <= -0.02f) { y = -0.02f; } rect.setSpeed(rect.speed().x, y); } } }