Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok, so in my Java2D Game, using swing/awt (I hear it is not the best to combine those), I am trying to finish collisions.


So, the problem is, the "if (player.xvel < 0) { }" and the "if (player.xvel > 0) { }" in the Block class are still being executed even though the player hasn't run into the block horizontally. How can I make it so the code is executed when the player runs/walks into the block horizontally?

My code for the collide function is below, in Block.java:

    void collide(Player player) {  
        Rectangle player_hb = new Rectangle((int) player.x, (int) player.y, (int) player.width, (int) player.height);
        Rectangle block_hb = new Rectangle(x + Display.graphics_panel.WorldX, y, width, height);

        if (player_hb.intersects(block_hb)) {
            Rectangle intersection = (Rectangle) player_hb.createIntersection(block_hb);  

            if (player.yvel > 0) {
                player.jumping = false;
                player.y -= intersection.getHeight();
            }
            if (player.yvel < 0) {
                player.yvel = 0;
                player.y += intersection.getHeight(); 
            }
            /* Execute this code ONLY if player is intersecting a block HORIZONTALLY!! */
            if (player.xvel > 0) {
                System.out.println("You shouldn't see this unless you run into a block!" + i++);
            }
            if (player.xvel < 0) {
                System.out.println("You shouldn't see this unless you run into a block!" + i--);
            }
        }
    }   
share|improve this question

bumped to the homepage by Community 2 days ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

    
Also, should I be moving the world, or the player/camera. Right now I am moving the world as I do not know how to make a camera. – javaprogammer23 Jan 17 at 4:37
    
The camera is stationary, people usually move the world through the camera using matrices. – Bálint Jan 17 at 10:15
    
Is all of that code relevant? If not please only provide the relevant code for us to see. It becomes tedious solving your issue if we have to filter irrelevant code at the same time. – Charanor Jan 17 at 14:44
    
sorry, I will link the relevant code, and I do not know what matrices are – javaprogammer23 Jan 17 at 17:44

Ok, I finally found the solution! I made a hitbox for the x-axis so the x wouldn't interfere with the y. (by subtracting 10 from the y and 20 from the height) Now collisions are working abosolutely perfect!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.