Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2D Platformer - Collisions only working on two sides of an object. Why?

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 2D Platformer - Collisions only working on two sides of an object. Why?

    Hello, I am designing a 2D platformer game. Currently, the player moves around in stages, when the player moves off the screen, a new stage is loaded. The player can move around (and float - I have not implemented gravity) and I'm trying to code collision so that the player cannot move through any obstacles. Currently, it works on the left and top sides of every obstacle, however, on the bottom and right side, it does not work despite the code being the same.
    Here is my code for the Grogu (player) class:
    public class Grogu {
        //Image
        public Image currentGroguImage;
        String which = "Default";
        int step = 0;
        int frames = 0;
     
        //Position
        int x,y;
        int w,h;
     
        float xSpeed,ySpeed;
        Input input;
     
        public Grogu(int x, int y) {
            this.x = x;
            this.y = y;
            input = new Input(1);
            xSpeed = 0;
            ySpeed = 0;
     
        }
     
        public void update() {  
            move();
        }
     
        public void render(Graphics g) {
            //g.drawString("" + collision, Main.getScreenWidth()/2, Main.getScreenHeight()/2);
            g.drawImage(currentGroguImage,x, y);
        }
     
        public void move() {    
            x+=xSpeed;
            y+=ySpeed;
        }
     
        public void keyPressed(int key, char c)
        {
     
            if(key == Input.KEY_A) {
                xSpeed = -7;
            }
     
            if(key == Input.KEY_D) {
                xSpeed = 7;
            }
     
            if(key == Input.KEY_W) {
                ySpeed = -7;
            }
     
            if(key == Input.KEY_S) {
                ySpeed = 7;
            }
     
            if(key == Input.KEY_D) {
                which = "Right";
            }
            else if(key == Input.KEY_A) {
                which = "Left";
            }
        }
     
        public void keyReleased(int key, char c) {
            if(key == Input.KEY_D || key == Input.KEY_A) {
                which = "Default";
            }
            if(key == Input.KEY_A) {
                xSpeed = 0;
            }
     
            if(key == Input.KEY_D) {
                xSpeed = 0;
            }
     
            if(key == Input.KEY_W) {
                ySpeed = 0;
     
            }
     
            if(key == Input.KEY_S) {
                ySpeed = 0;
            }
        }
     
        public Rectangle getBounds() {
            Rectangle r = new Rectangle(x - (int)xSpeed,y - (int)ySpeed,currentGroguImage.getWidth() + (int)xSpeed * 2,currentGroguImage.getHeight() + (int)ySpeed * 2);
            return r;
        }
     
        public void setX(int s) {
            x = s;
        }
        public void setY(int s) {
            y = s;
        }
     
        public int getX() {
            return x;
        }
        public int getY() {
            return y;
        }
        public float getXSpeed() {
            return xSpeed;
        }
        public float getYSpeed() {
            return ySpeed;
        }
        public int getWidth() {
            return currentGroguImage.getWidth();
        }
        public int getHeight() {
            return currentGroguImage.getHeight();
        }
    }

    And here is my stage class, where the collisions are detected and dealt with:
    public class Stage {
        public ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
        public ArrayList<Enemy> enemies = new ArrayList<Enemy>();
        Grogu grogu;
        public Stage() {
            grogu = new Grogu(75,5*Main.getScreenHeight()/6 - Images.frontGrogu.getHeight()/2);
        }
        public void update() {
            checkCollisions();
            grogu.update();
     
        }
     
        public void render(Graphics g) {
     
            for(Obstacle o: obstacles) {
                o.render(g);
            }
     
            grogu.render(g);
        }
     
        public void keyPressed(int key, char c) {
            grogu.keyPressed(key, c);
        }
     
        public void keyReleased(int key, char c) {
            grogu.keyReleased(key, c);
        }
     
        private void checkCollisions() {
            for(Obstacle o: obstacles) {
                if(grogu.getBounds().intersects(o.getBounds())) {
                    o.setR();
                    if(grogu.getX() + grogu.getWidth() <= o.getX()) {
                        grogu.setX(grogu.getX() - (int)grogu.getXSpeed() - 1);
                    }
                    if(grogu.getX() > o.getX() + o.getWidth()) {
                        grogu.setX(grogu.getX() - (int)grogu.getXSpeed() + 1);
                    }
                    if(grogu.getY() + grogu.getHeight() <= o.getY()) {
                        grogu.setY(grogu.getY() - (int)grogu.getYSpeed() - 1);
                    }
                    if(grogu.getY() >= o.getY() + o.getHeight()) {
                        grogu.setY(grogu.getY() - (int)grogu.getYSpeed() + 1);
                    }
                    //return grogu.collision =true;
                }
            }
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2D Platformer - Collisions only working on two sides of an object. Why?

    How can the the program be compiled and executed for testing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. 2D Survival Platformer Help
    By Figgle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 18th, 2014, 10:05 AM
  2. Making Slopes for a Platformer Game
    By Gravity Games in forum Java Theory & Questions
    Replies: 4
    Last Post: November 8th, 2012, 07:22 PM
  3. Adding my own object to a Container...not working.
    By Tombomb in forum Object Oriented Programming
    Replies: 1
    Last Post: January 9th, 2012, 04:18 PM
  4. Collisions... working but not.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 24th, 2011, 08:05 PM
  5. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 09:47 AM

Tags for this Thread