
-----------------------------------
Panphobia
Sun Jan 20, 2013 3:08 am

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 http://i48.tinypic.com/2ry146r.png 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);
        }
    }
[/code]

-----------------------------------
Panphobia
Sun Jan 20, 2013 3:16 pm

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 