(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