My edge collision algorithm is not working properly - java

if(pl.y+pl.height >= a.y && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y<=a.y) { //TOP
colUP=true;
}
else colUP=false;
if(pl.y <= a.y+a.height && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y+pl.height>=a.y+a.height) { //BOTTOM
colDOWN=true;
}
else colDOWN=false;
if(pl.x <= a.x+a.width && pl.x+pl.width>a.x+a.width && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //RIGHT
colRIGHT=true;
}
else colRIGHT=false;
if(pl.x+pl.width >= a.x && pl.x<a.x && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //LEFT
colLEFT=true;
}
else colLEFT=false;
I setup a debug that will tell me which of the 4 Booleans is being set to true, and they don't show that when I put the box 'pl' on top of box 'a' colUP is not equal to true, and they will only come true in weird instances where box 'pl' is colliding with several box 'a's , and the collision for a certain side might be true when it isn't but if colUP is true then colRIGHT is true for some reason. (This code is inside a for loop that goes through an array list of Rectangles and sets the current Rectangle equal to the variable 'a' so that a.x is the box's x position)

You have right logic but you set false for each condition separately. In reality all conditions should be true. So, use one boolean variable - isInRectangle=true; then check all conditions - left,right,top,bottom. If any is not true then isInRectangle=false;
It is simple AND logical operation for all 4 conditions.

Related

Java while(condition doesn't reach)?? Why

Why is it when I do this
while(word.charAt(left) == word.charAt(right) && left >= 0 && right < word.length()){
left--;
right++;
}
its says in the while conditions "left >= 0 && right < word.length()" condition doesn't reach, but when I do it like this
while(left >= 0 && right < word.length() && word.charAt(left) == word.charAt(right)){
left--;
right++;
}
The conditions are met.
Is there an order for while(loop conditions)???
The order does matter as conditions are checked from left to right. You're getting that message because if the previous condition evaluates to true, then those two conditions
left >= 0 && right < word.length()
will always evaluate to true. You probably want the latter (since you would want to check all the conditions).

Check if one java condition is subset of another condition

I have a use case where I have to check if one condition is subset of another condition. Meaning, the result set that satisfies condition 2 will be the subset of result set that satisfies condition 1.
I was checking and intellij does figures it out during code inspection
if (x < 7 && x > 1) {
return true;
} else if (x > 3 && x <6) {
return false;
}
Here during code inspection it does highlights that result of second condition is always false.
So, How it is implemented?
This range:
(x > 3 && x <6)
is a subrange of:
(x < 7 && x > 1)
so if the condition (x < 7 && x > 1)
is true then this condition (x > 3 && x <6) will not be reached
is false then this condition (x > 3 && x <6) is also false
So the meaning of code inspection is:
if (x > 3 && x <6) is reached then it is always false.
The right way to implement your logic should be to check first the subrange:
if (x > 3 && x <6) {
return false;
} else if (x > 1 && x < 7) {
return true;
} else {
return ....;
}

Line and point collision in java

I'm making a tron game and have everything except for the collision. Currently making it through MVC (required as it is an AP Compsci project) and I am having trouble with the collision between the trail and the bike. My idea is to have a point on the racer to which it collides with (the front point). The light trails are drawn with a list of points, the first point being the start point and each point after are added when the racer turns. The last point is the current location of the bike. It draws a line between each point in paintComponent, and also uses a method in the Line class (which contains the arraylist of points) to check collision. So far I have tried to see if the front point is on any of the axis (that is, if the x or y value is equal to any of the corresponding x or y pvalues on the line) and then check the opposite coordinate and check if the x or y is in between the x or y of two points on that axis, and return true or false if they are. Here is what I mean:
public boolean checkPoint(Point p)
{
for(int k = points.size() - 1; k > 0;k -= 2)
{
//if the x's are the same ( vertical ), check if within y's
if(p.getX() == points.get(k).getX() && p.getX() == points.get(k - 1).getX())
{
if(points.get(k).getY() > points.get(k - 1).getY()){
if(p.getY() < points.get(k).getY() && p.getY() > points.get(k-1).getY())
return true;
}
if(points.get(k).getY() < points.get(k - 1).getY()){
if(p.getY() > points.get(k).getY() && p.getY() < points.get(k-1).getY())
return true;
}
}
// if the y's are the same (horizontal), check if within x's
if(p.getY() == points.get(k).getY() && p.getY() == points.get(k - 1).getY())
{
if(points.get(k).getX() > points.get(k - 1).getX()){
if(p.getX() < points.get(k).getX() && p.getX() > points.get(k-1).getX())
return true;
}
if(points.get(k).getX() < points.get(k - 1).getX()){
if(p.getX() > points.get(k).getX() && p.getX() < points.get(k-1).getX())
return true;
}
}
}
}
The game either runs really slowly after a few seconds and works, or doesn't work at all. Am I going in the right direction, how should I go about this?

Highlighting "Legal Moves" In chess-like game on panel array- Java

this is more of a logic question than syntax but I'm sure you guys have better ways to do it than me anyway. So I have some pieces (JLabels) on a board (11x11 Grid of JPanels on a JPanel) all in a frame. I'm trying to highlight the "possible moves" for these pieces (I.E. how many panels they can cross in a move) very similar to in many online chessgames. I have the logic that checks if movement follows the rules, and it works perfect, I just can't seem to figure out how to find all of those legal moves at once and use them.
This is the code that I use to check if movement is legal:
public void movePieces(MouseEvent me)
{
MouseEvent e = me;
if (turnCount%2 == 1 && pieceCheck[(e.getY()/yInc)][(e.getX()/xInc)])
{
if (playerOne && (logic[(e.getY()/yInc)][(e.getX()/xInc)] == 0 || logic[(e.getY()/yInc)][(e.getX()/xInc)] == 2 || logic[(e.getY()/yInc)][(e.getX()/xInc)] == 4))
{
otherCount = logic[(e.getY()/yInc)][(e.getX()/xInc)];
tempX = e.getX();
tempY = e.getY();
paintPossible(tempX, tempY);
turnCount++;
}
if (!playerOne && (logic[(e.getY()/yInc)][(e.getX()/xInc)] == 1 || logic[(e.getY()/yInc)][(e.getX()/xInc)] == 3 || logic[(e.getY()/yInc)][(e.getX()/xInc)] == 5))
{
otherCount = logic[(e.getY()/yInc)][(e.getX()/xInc)];
tempX = e.getX();
tempY = e.getY();
turnCount++;
}
}
else if ((turnCount%2 == 0 && logic[(e.getY()/yInc)][(e.getX()/xInc)] == -1 && !pieceCheck[(e.getY()/yInc)][(e.getX()/xInc)]) && (Math.abs(tempX - e.getX()) <= xInc && (Math.abs(tempY - e.getY()) <= yInc) || ((Math.abs(tempX - e.getX()) <= 2*xInc) && (Math.abs(tempY - e.getY()) < yInc/4)) || ((Math.abs(tempX - e.getX()) < xInc/4) && (Math.abs(tempY - e.getY()) <= 2*yInc))))
{
panels[(e.getY()/yInc)][(e.getX()/xInc)].add(pieces[otherCount]);
logic[(e.getY()/yInc)][(e.getX()/xInc)] = otherCount;
pieceCheck[(e.getY()/yInc)][(e.getX()/xInc)] = true;
pieceCheck[tempY/yInc][tempX/xInc] = false;
panels[tempY/yInc][tempX/xInc].setBackground(veryDarkGray);
panels[tempY/yInc][tempX/xInc].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
playerOne = !playerOne;
turnCount++;
}
....
}
Basically the first if-statement and what's inside is for when you first click a piece, and then if it's your turn then it will set that piece as the next in line to put down, and grab the X-Y coordinates of where it is.
The else-if then activates the next time you click due to the rotation of my turnCount, and makes sure there's no piece where you're clicking and that the space is within a certain# of pixels to where your piece is before you move.
I need to take that same checking of validity in where you can move, and apply it in a separate method that will loop through the 2d array of JPanels that is my board and color the valid moves. The method would be called where "paintPossible(tempX, tempY)" is, and in the same spot in the "if" below.

Checking if Element Exists in Boolean Array

I took a programming class, and I'm revisiting old programs that I did not quite get right. This one is a Game Of Life program, and I have a question about code cleanup.
I need to make sure that an array element is in bounds before checking whether its neighbor's boolean value is true or false. I have a statement to check if firstGen[0][0]'s top-left (up one row, left one column) is in bounds. Is there an easier or more elegant way to check if an element is in bounds or to restrict the element checks to the boundaries of a given array without using four && conditionals per if statement?
Note that I have only changed the first if statement thus far, so there may be errors elsewhere. I also excluded the boundary checks for the other neighbors.
public static boolean[][] generation(boolean[][] firstGen)
{
int length = firstGen.length;
boolean[][] newGen = new boolean[length][length];
for (int j = 0; j < firstGen[0].length; j++)
{ for (int i = 1; i < firstGen.length; i++)
{
int count = 0;
if ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
{ if (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true
if ((newGen[i][j] == false) && (count == 3)) newGen[i][j] = true;
else if ((newGen[i][j] == true) && (count == 1)) newGen[i][j] = false;
else if ((newGen[i][j] == true) && (count > 3)) newGen[i][j] = false;
else break;
}
}
return newGen;
}
If i and j are in bounds, then you know for sure that i - 1 < length and j - 1 < length are both true.
Also:
i - 1 >= 0 can be written i > 0
if (condition == true) can be rewritten if (cond)
So you could replace:
if ((i-1 >= 0) && (i-1 < length) && (j-1 >= 0) && (j-1 < length)) //top-left element exists
{ if (newGen[i-1][j-1] == true) count++; } //increment `count` if top-left element is true
by:
//increment `count` if top-left element is true
if (i > 0 && j > 0 && newGen[i-1][j-1]) count++;
That's the best way I can think of to check if its out of bounds, but an alternative method in general, and one that I think gives programs like the Game of Life more exciting outcomes, is adding periodic boundaries. Basically this means that if you walk off one edge, you end up on the other side (like in pac-man). It sounds complicated, but really all it takes is the % function, which returns the remainder of division between the two numbers given.
So:
27 % 5 = 2;
So for adding periodic boundries you would update x and y positions like this:
x = (x + xStep + horizontalSize) % horizontalSize;
y = (y + yStep + verticalSize) % verticalSize;
Where xStep and yStep are +1 or -1 depending on what direction you want to go. (this works nicely with a for loop) The addition of the size is to make sure you go below zero when you get close to borders.
Then you never have to worry about messy border conditions, everything simply overlaps. No need to check each and every border. I hope this makes sense. Please ask for clarification if not. I've used this more for random walker programs but the idea is the same.

Categories