0
\$\begingroup\$

I am making a very simple demo in AndEngine in which I have three rectangles: rect1, rect2 and rect3 in an ArrayList.

I register onAreaTouch as follows:

 @Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
        final float pTouchAreaLocalY) {

    switch (pSceneTouchEvent.getAction()) {

    case TouchEvent.ACTION_DOWN:
        // this.setScale(1.0f);
        this.mGrabbed = true;
        break;
    case TouchEvent.ACTION_MOVE:
        if (this.mGrabbed) {
            for (int i = 0; i < rectangles.size(); i++) {

                int index = rectangles.indexOf(this);

                if (i != index) {

                    if (this.collidesWith(rectangles.get(i))) {
                        // should not overlap
                    } else {
                        this.setPosition(pSceneTouchEvent.getX() - 90, pSceneTouchEvent.getY() - 90);
                    }
                }
            }
        }
        break;
    case TouchEvent.ACTION_UP:
        if (this.mGrabbed) {
            this.mGrabbed = false;
        }
        break;
    }
    return true;
}

Rectangles are now overlapping with one another. I don't want this behavior, instead I'd like it if rect1 collides with rect2 then rect1 should not move further, but can move in any other direction.

[Cross-Posted here]

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

In order to solve this you need to implement something between the touch position and the position of the object.

Instead of setting the position directly to the new touch position you'll want to first test to see if the touch position is valid. If the new position is free from overlap, then you're done and you can move the rectangle there. However, if the rectangle being there would cause overlap you need to find out how far from the shape's current position it can go before causing overlap (math's your friend here) and only move it that far. If it's already against a shape and you drag into it, it will keep trying to move in, realizing it can only move 0 in that direction and give up. This method also has the added benefit of allowing you to slide the rectangle against an object it can't pass through with no further code.

A warning however, if you are working on a drag and move around model and not just a "place in a valid location" one, only focusing on where the shape will end up will allow you to "jump" other objects by moving all the way to the other side of them. If you want to avoid this you'll need to do some form of sweep collision detection where you check to see if the shape carved out by the rectangle's movement would collide with any of the shapes, and then stop/modify it's destination based on the point of first collision.

\$\endgroup\$
0
\$\begingroup\$

Save the last position of the rectangles before moving them as a variable. When collision is detected, determine the reverse direction of movement of each rectangle. Then move them back in that direction the amount of the overlap. The rectangles will be touching, but not overlapping.

Very fast moving shapes will require a different method. Hopefully you dpn't need to use very fast shapes.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .