Collision Problem
Author |
Message |
Panphobia

|
Posted: Sun Jan 20, 2013 3:08 am Post subject: Collision Problem |
|
|
Ok so as some of you might know for my project management summative for ICS 4U I am doing a rush hour game, basically in the original you slide cars either vertically or horizontally to clear the path such that your car can make it to the goal, we are done everything, but there are a few bugs, I fixed most of them but one remains a mystery, whenever I try to force one tree/car into another if i do it 2-3 times they will overlap, here is an example here is my code for the collision detection, if you need more code just tell me code: | public void mouseDragged(MouseEvent e) {
//when the mouse is dragged and component selected is not null, continue
if (componentName != null) {
//get the current mouse x and y assuming that it was clicked from the middle of the component
mouseX = e.getX() - carImage[Integer.parseInt(componentName)].getWidth() / 2;
mouseY = e.getY() - carImage[Integer.parseInt(componentName)].getHeight() / 2;
//get the direction of the selected component
direction = group.get(mapsIndex)[Integer.parseInt(componentName)].updown(Integer.toString(Integer.parseInt(componentName)));
//find the area that the selected object occupies
Rectangle or = carImage[Integer.parseInt(componentName)].getBounds();
//go through all other components
for (int x = 0; x < max; x++) {
//as long as the comparison is not made with itself or a nonexistant object, continue
if (x != Integer.parseInt(componentName) && carImage[x] != null) {
//get the area that the compared object occupies
Rectangle collide = carImage[x].getBounds();
//if the two areas intersect, make the selected car go back to where it was
if (or.intersects(collide)) {
mouseX = carImage[Integer.parseInt(componentName)].getX();
mouseY = carImage[Integer.parseInt(componentName)].getY();
}
}
}
//if the needed car reaches the exit, the map is won
if (componentName.equals("0") && carImage[0].getY() == 150 && carImage[0].getX() == 250 && end == false) {
//update the number of moves adn add one to reach the exit
counter += Math.abs((mouseX - startX) / 50) + Math.abs((mouseY - startY) / 50) + 1;
//find the percentage of moves relative to the minimum number of moves
double winStuff = (double) minMoves / (double) counter;
int winAmount;
//if the user got the minimum number of moves, make the won amount equal to 3 stars
if (winStuff == 1) {
winAmount = 3;
//if the user less than doubled the minimum number of moves, give the user 2 stars
} else if (winStuff > 0.5) {
winAmount = 2;
//otherwise, only give 1 star
} else {
winAmount = 1;
}
//tell the user that he/she has won the map and tell the user his/her score
JOptionPane.showMessageDialog(null, "Congrats! You won!\nYou made " + (counter) + " moves.\nYou got " + winAmount + " out of 3 stars!", "NICE!", JOptionPane.PLAIN_MESSAGE, new ImageIcon(System.getProperty("user.dir") + File.separator + "ha.gif"));
//add the number of stars won to the total score
score += winAmount;
//update the board
scoreBoard.setText("MOVES:\n\n" + counter + "\n\nMINIMUM\nMOVES:\n\n" + minMoves + "\n\nSCORE:\n\n" + score);
//set that it has been the end of the current map
end = true;
}
//if the direction is horizontal, make sure that the object is not dragged off the screen (right and left)
if (direction == true) {
if (mouseX < 50) {
mouseX = 50;
} else if (mouseX > 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth()) {
mouseX = 50 * 7 - carImage[Integer.parseInt(componentName)].getWidth();
}
//get the location of the mouse for the y axis
mouseY = carImage[Integer.parseInt(componentName)].getY();
//if the direction is vertical, make sure that the object is not dragged off the top and bottom of the screen
} else {
if (mouseY < 50) {
mouseY = 50;
} else if (mouseY > 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight()) {
mouseY = 50 * 7 - carImage[Integer.parseInt(componentName)].getHeight();
}
//get the location of the mouse for the x axis
mouseX = carImage[Integer.parseInt(componentName)].getX();
}
//update the component's location to where the mouse is
carImage[Integer.parseInt(componentName)].setLocation(mouseX, mouseY);
}
}
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Panphobia

|
Posted: Sun Jan 20, 2013 3:16 pm Post subject: Re: Collision Problem |
|
|
Well I do not know the problem because what it does if the current car/tree you are dragging is intersecting with another car/tree and then if it is it will make the tree that you are dragging go back to the original place it was at, here is the code for that, what i Think the problem is, is that the carImage x and y keep changing on how much you move it, and if you move slightly into another tree that is the new x and y, and it pushes it back there, so what I don't know is how to make it change the x and y if the move is valid code: | for (int x = 0; x <= max; x++) {
//as long as the comparison is not made with itself or a nonexistant object, continue
if (x != Integer.parseInt(componentName) && carImage[x] != null) {
//get the area that the compared object occupies
Rectangle collide = carImage[x].getBounds();
//if the two areas intersect, make the selected car go back to where it was
if (or.intersects(collide)) {
or.setBounds(x1, y1, width, height);
mouseX = carImage[Integer.parseInt(componentName)].getX();
mouseY = carImage[Integer.parseInt(componentName)].getY();
}
}
}
|
|
|
|
|
|
 |
Panphobia

|
Posted: Sun Jan 20, 2013 5:09 pm Post subject: RE:Collision Problem |
|
|
Problem solved!!! The problem was that I was basing the co-ordinates of the tree from the middle of the tree, and it would jump according to where you pressed it from, so i just changed this fact, and its fixed |
|
|
|
|
 |
Panphobia

|
Posted: Sun Jan 20, 2013 9:44 pm Post subject: RE:Collision Problem |
|
|
Ok so I fixed every single glitch, it does not glitch with the mouseX and y anymore, now it glitches if you drag a tree really fast into another one, they will overlap, is there anyway to slow down the cursor? i heard you can do it in c or c++, and if you can, is there a way to run c or c++ code with java? |
|
|
|
|
 |
DemonWasp
|
Posted: Sun Jan 20, 2013 10:47 pm Post subject: RE:Collision Problem |
|
|
No, you can't really slow down the cursor. I'm not even sure you can do that in C++ (you shouldn't be able to, though the mouse operates a bit differently in Direct3D or OpenGL contexts than usual, so maybe).
You can technically call C/C++ code from Java, but it's a bit of a mess and not ideal.
Maybe you should just make your dragging code work properly in the first place. |
|
|
|
|
 |
Panphobia

|
Posted: Sun Jan 20, 2013 11:06 pm Post subject: RE:Collision Problem |
|
|
It drags properly, and collides properly, only when you click and drag really fast does it overlap, i do not know why, but it does, strange... |
|
|
|
|
 |
|
|