#method to detect and prevent collision with rectangle not working

11 messages · Page 1 of 1 (latest)

pearl rose
#

(rec.x1, rec.y1) is the bottom left corner of the rect, (rec.x2, rec.y2) is top right
all the print statements i embedded print out when they should, but the position changes and velocity changes dont seem to work, should i be structuring this differently or have i just missed something here?

// just to clarify, detection of the collisions works fine, but the line at the end: currPos.set(lastPos); is the one that isnt working for some reason

the following method is run 30x / sec to check for collisions with rectangle objects

public void rectCollision() {
        scene.GameRectObjs.forEach(rec -> {
            if (rec.x1 < currPos.x && currPos.x < rec.x2 && rec.y1 < currPos.y && currPos.y < rec.y2) {
                // check if player is inside the rect
                Vector2d newPos = new Vector2d(currPos);
                if (lastPos.y > rec.y2) { // check if player was above rectangle
                    newPos.y = (rec.y2 + radius); // put player on top of rectangle
                    vel.y = 0;
                    System.out.println("top detect");
                } else if (lastPos.y < rec.y1) { // check if player was below rect
                    newPos.y = rec.y1 - radius; // put player on bottom of rect
                    vel.y = 0;
                    System.out.println("bottom detect");
                }
                if (lastPos.x < rec.x1) { // check if player was left of rect
                    newPos.x = rec.x1 - radius; // put player to left of rect
                    vel.x = 0;
                    System.out.println("left detect");
                } else if (lastPos.x > rec.x2) { // check if player was right of rect
                    newPos.x = rec.x2 + radius; // put player to right of rect
                    vel.x = 0;
                    System.out.println("right detect");
                }
                currPos.set(newPos);
            }
        });
    }

thank you kindly for your help

  • a tired CS student
tall mirageBOT
#

This post has been reserved for your question.

Hey @pearl rose! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pearl rose
#

oh and when the player hits the bottom of a rectangle, it works but not reliably

tall mirageBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

fossil fractal
#

First of all, make a method in your rectangle, or even a static method, to detect if rectangles overlap, and test that, and then your code will look a lot cleaner.

Second, this approach fails if the two rectangles are moving sufficiently fast. A better approach is to cast a ray out from the center of the rectangle and find anything that it would have collided with if it had moved in its desired velocity vector in the next tick.

tall mirageBOT
tall mirageBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

pearl rose