1

I have parent classes that create shape entities and I can choose to make the entity a collidable entity or a Non-Collidable Entity. So far, in this project, Circles and Rectangles are collidables. Whereas, the ellipse is a Non-Collidable. The funny thing is even though I have created multiple circles and rectangles. Only 1 pair of circles and rectangles are collidable whereas the rest isn't. I am really confused as I thought the child always inherits the property from the parent. Therefore, if all shapes have the same parent. I do not see how is it possible that the child does not behave the same way.

// CIRCLE ENTITY

public class daCircle extends Collidable{

    protected float circleRadius;
    protected int upKey;
    protected int downKey;
    protected int leftKey;
    protected int rightKey;

    public daCircle(int circleXPos, int circleYPos, float circleRadius, int circleSpeed, Color circleColor, 
    int upKey, int downKey, int leftKey, int rightKey) {
        super(circleXPos, circleYPos, circleColor, circleSpeed);
        this.circleRadius = circleRadius;
        super.keyMovementList.set(0, upKey);
        super.keyMovementList.set(1, downKey);
        super.keyMovementList.set(2, leftKey);
        super.keyMovementList.set(3, rightKey);
    }
}

// Collidable

public void checkForCollision(ArrayList<GeometricObjects> listOfAllShapes, String direction) {      
        for (GeometricObjects daShape : listOfAllShapes) {
            if (daShape instanceof Collidable) {
                Iterator<GeometricObjects> iter = listOfAllShapes.iterator();
                while(iter.hasNext()) {
                    GeometricObjects nextShape = iter.next();
                    if (daShape != nextShape) {
                        if (daShape instanceof daCircle && nextShape instanceof daCircle) {
                            setCirclesCollisionFlag(isCirclesColliding((daCircle)daShape, (daCircle)nextShape, direction));
                        }
                        if (daShape instanceof daRect && nextShape instanceof daRect) {
                            setRectanglesCollisionFlag(isRectanglesColliding((daRect)daShape, (daRect)nextShape, direction));
                        }
                        if (daShape instanceof daCircle && nextShape instanceof daRect ) {
                            setCircleRectangleCollisionFlag(circIntersectRect((daCircle)daShape, (daRect)nextShape, direction));
                        } 
                    } 
                } 
            }
        }
    }

// Controller

public void movementController(ArrayList<GeometricObjects> allGeometricShapes) { 
        int temporaryObjSpeedHolder = 0;

        GeometricObjects daObject = (GeometricObjects) this;
        if (daObject instanceof Collidable) {
            collidableShape = (Collidable) daObject;
            daObject = collidableShape;
        }

        // if (Gdx.input.isTouched()) {
        //     daObject.setXPos(Gdx.input.getX());
        //     daObject.setYPos(Gdx.graphics.getHeight() - Gdx.input.getY());
        // }

        if(Gdx.input.isKeyPressed(keyMovementList.get(0))){
            if (daObject instanceof Collidable) {
                collidableShape.checkForCollision(allGeometricShapes, "UP");
            }
            
            if (circlesAreColliding || rectanglesAreColliding || circleRectangleAreColliding) {
                temporaryObjSpeedHolder = incomingCollisionSpeed;
            } else {
                temporaryObjSpeedHolder = objectSpeed;
            }

            daObject.yPos += temporaryObjSpeedHolder;
        
        } else if(Gdx.input.isKeyPressed(keyMovementList.get(1))){
            if (daObject instanceof Collidable) {
                collidableShape.checkForCollision(allGeometricShapes, "DOWN");
            }

            if (circlesAreColliding || rectanglesAreColliding || circleRectangleAreColliding) {
                temporaryObjSpeedHolder = incomingCollisionSpeed;
            } else {
                temporaryObjSpeedHolder = objectSpeed;
            }

            daObject.yPos -= temporaryObjSpeedHolder;

        }

        if(Gdx.input.isKeyPressed(keyMovementList.get(2))){
            if (daObject instanceof Collidable) {
                collidableShape.checkForCollision(allGeometricShapes, "LEFT");
            }

            if (circlesAreColliding || rectanglesAreColliding || circleRectangleAreColliding) {
                temporaryObjSpeedHolder = incomingCollisionSpeed;
            } else {
                temporaryObjSpeedHolder = objectSpeed;
            }

            daObject.xPos -= temporaryObjSpeedHolder;

        } else if(Gdx.input.isKeyPressed(keyMovementList.get(3))){
            if (daObject instanceof Collidable) {
                collidableShape.checkForCollision(allGeometricShapes, "RIGHT");
            }

            if (circlesAreColliding || rectanglesAreColliding || circleRectangleAreColliding) {
                temporaryObjSpeedHolder = incomingCollisionSpeed;
            } else {
                temporaryObjSpeedHolder = objectSpeed;
            }

            daObject.xPos += temporaryObjSpeedHolder;
        }

I have created many circles and rectangles and discovered that only the latest created shapes are able to collide. I thought that maybe since the shapes are all on different layers that it might not be able to collide. I really dk what else I can be testing to see why it is the way it is. If you guys got suggestions of testing. I'm all for it!

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Feb 02 '23 at 17:59
  • Do you need this as additional check in your //Collidable code for all the possibilities ```daShape instanceof daRect && nextShape instanceof daCircle``` because they might not necessarily always be in this order ```daShape instanceof daCircle && nextShape instanceof daRect ``` i.e you might find a rect before a circle as you iterate which would be missed. – londonBadger Feb 02 '23 at 23:54
  • @londonBadger You are absolutely right. One of the problems I have now is that my circles are able to collide with my rectangle but my rectangles are not able to collide with my circle(Code crashes). I believe this has something to do with my conditional checks or at least the math logic of circIntersectRect. I am still trying to find ways to get my Rectangle to collide with my circle but i'm at an impasse. If you got any idea, I would love to try them! – CrazeyFazey Feb 03 '23 at 07:24
  • If you are suspicious about your intersection test (and all this is axis-aligned), replace it with this one which seems optimised as well https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection – londonBadger Feb 03 '23 at 17:10
  • @londonBadger That is the one I am using! XD UPDATE: I manage to get the Rectangle intersecting with the circle by tweaking the algorithm a little and account for the move position of the rectangle. However, I have a slight issue whereas if the rectangle collides into a shape from the left or right, they are unable to back out. Weird, I'll keep trying and update my progress! – CrazeyFazey Feb 04 '23 at 20:03

1 Answers1

0

The problem might be your checkForCollision method.

You are iterating over all GeometricObjects in listOfAllShapes. Then you set the collision flag for the current iteration of the GeometricObject:

setCirclesCollisionFlag(isCirclesColliding((daCircle)daShape, (daCircle)nextShape, direction));

So if any collision is detected the flag will be set to true. But if the next object doesn't collide, the flag is reset to false. Therefore it seems that only the last shapes of each type in the iteration list can collide.

To fix this you need to change the lines set...CollisionFlag ... to not reset the flag if it was already set to true. Also you need to reset the flags once before the start of the loop.

A solution could look like this:

public void checkForCollision(ArrayList<GeometricObjects> listOfAllShapes, String direction) {
    // initially reset the flags before the loop
    setCirclesCollisionFlag(false);
    setRectanglesCollisionFlag(false);
    setCircleRectangleCollisionFlag(false);

    for (GeometricObjects daShape : listOfAllShapes) {
        if (daShape instanceof Collidable) {
            Iterator<GeometricObjects> iter = listOfAllShapes.iterator();
            while(iter.hasNext()) {
                GeometricObjects nextShape = iter.next();
                if (daShape != nextShape) {
                    if (daShape instanceof daCircle && nextShape instanceof daCircle //
                        && isCirclesColliding((daCircle)daShape, (daCircle)nextShape, direction)) { // only set to true, but never reset to false here
                        setCirclesCollisionFlag(true);
                    }
                    if (daShape instanceof daRect && nextShape instanceof daRect //
                        && isRectanglesColliding((daRect)daShape, (daRect)nextShape, direction)) { // only set to true, but never reset to false here
                        setRectanglesCollisionFlag(true);
                    }
                    if (daShape instanceof daCircle && nextShape instanceof daRect //
                        && circIntersectRect((daCircle)daShape, (daRect)nextShape, direction)) { // only set to true, but never reset to false here
                        setCircleRectangleCollisionFlag(true);
                    } 
                } 
            } 
        }
    }
}
Tobias
  • 2,547
  • 3
  • 14
  • 29
  • Hi there, I have tried your solution and played around with it a bit. Fully implementing the changes you have suggested. I implemented part of your solution as shown here - https://imgur.com/a/CkJgIGf This fixed the problem of the circle not being able to collide with the second rectangle. Interesting enough, if I were to make changes to my isCirclesColliding and isRectanglesColliding collision check. When my circles collide with a circle or rectangle collide with a rectangle. They freeze in place. Probably has something to do with my conditions for movement. But thx for your help. – CrazeyFazey Feb 03 '23 at 07:15