1
votes

I am simply trying to make a generic way to check for collision between rectangles. If you hit the top of a rectangle stop moving. However my rectangles seem to stop regardless of the x coordinate. I have three rectangles falling and one static rectangle that doesn't move, they are supposed to all fall on top of this static rectangle however when I run the code this happens. What happens during execution

Here is the collision handler code

    float distanceY, furthestLeft;
public void update() {

    for (int i = 0; i < stageObjects.size(); i++) {
        iPos = stageObjects.get(i).getPosition();
        iDim = stageObjects.get(i).getDimensions();
        for(int k = 0; k < stageObjects.size(); k++){
            kPos = stageObjects.get(k).getPosition();
            kDim = stageObjects.get(k).getDimensions();
            if(k == i){
                continue;
            }
                distanceY = assignDy();
                furthestLeft = assignDx();
             //DistanceY is the subtraction of one objects y coordinate and 
            // if it equals 0 then they are colliding and the furthest left 
            // is the bottom right x coord of the furthest left objects x 
            //coord so it should check if this x is contained within an
            // objects left x coord to right x coord
            if(distanceY <= 1 && distanceY >= 0 && furthestLeft >= iPos.x && furthestLeft <= iPos.x + iDim.x){
                stageObjects.get(i).react(Tuples.HIT_BOTTOM);
                stageObjects.get(k).react(Tuples.HIT_FROM_TOP);
                System.out.println("Collision: " + stageObjects.get(i).toString() + " with " + 
                stageObjects.get(k).toString());
            }

        }
    }

}


}


public float assignDy(){
    if(kPos.y > iPos.y){
        return Math.abs(kPos.y - (iPos.y + iDim.y));
    }else
        return Math.abs(iPos.y - (kPos.y + kDim.y));
}

public float assignDx(){
    if(kPos.x > iPos.x){
        return kPos.x + kDim.x;
    }else
        return iPos.x + iDim.x;
}

The error lies here and here is the react method

    public void react(int occurence){
    if(occurence == Tuples.HIT_BOTTOM){
        velocity.y = 0;
    }
}

However, if they are further apart the code works perfectly look.

enter image description here

I have also noticed that the rectangle can fall through other rectangles if it is to the left of another rectangle, but if it further to the right at all it will get hung as if it landed on the rectangle. The only reason the above image worked is because the furthest right fell first, anything to the left to another rectangle will get hung as well if it falls after the rectangle to the left

I just don't see what exactly I am doing wrong any help is greatly appreciated!

1
It would be nice if you could add some comments to your code on what you think it should do, for example what your if condition does. - Pinkie Swirl
Thank you I will add that now @PinkieSwirl - Pookie
For example: distanceY is always positive, why do you test for distanceY >= -1 ? Did you change your code and did not adapt some stuff? - Pinkie Swirl
@PinkieSwirl thank you it should be zero changing that now! - Pookie
You have quite many errors in your code, to many to be pointed out by me, since I have problems to properly explain them. That's why I suggest the following, use Rectangle (a libgdx class) it comes with a overlaps method. Then you just have to make sure that you do not mix up which rectangle is on top/bottom (which you do). If you don't want to use it, take a look at the source code of it. To catch all possible "collisions" you need to add 4*2 checks for rectangles, two for left/right/top/bottom side, currently you only have 2 (which are not completely correct either). - Pinkie Swirl

1 Answers

1
votes

Change iPos in the condition to something more generic, assign a variable like assignDx and dy to ccheck if Ipos or kPos is what you need to check for like so

    public void assignOx(Vector2 ox){
    if(kPos.x > iPos.x){
        ox.x = iPos.x;
        ox.y = iPos.x + iDim.x;
    }else{
        ox.x = kPos.x;
        ox.y = kPos.x + kDim.x;
    }
}