Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2016
    Posts
    10
    Rep Power
    0

    Default Need Help With Collision Detection for My Java Game

    Hello, everybody! I'm currently making a simple indie stlye game in java. But I need help with the collision detection. I keep getting a null pointer exception and don't know why. Here is my code. Thanks


    Java Code:
    private boolean collision(float velX, float velY) {
    		int collisionX = ((int) x + (int) velX) / 64;
    		int collisionY = ((int) y + (int) velY) / 64;
    
    		boolean solid = false;
    		this.velX = velX;
    		this.velY = velY;
    
    		if (handler.getWorld().getTile(collisionX, collisionY).isSolid(true)) {
    			solid = true;
    		}
    		return solid;
    	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need Help With Collision Detection for My Java Game

    You should break up the statement at line 9 into individual variables and print out their contents. Handler could return null, getWorld() could return null. Etc.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Oct 2016
    Posts
    10
    Rep Power
    0

    Default Re: Need Help With Collision Detection for My Java Game

    Ok here:

    Java Code:
    private boolean collision(float velX, float velY) {
    		boolean solid = false;
    		this.velX = velX;
    		this.velY = velY;
    
    		if (handler.getWorld().getTile(((int) x + (int) velX) / 64,((int) y + (int) velY) / 64).isSolid(true)) {
    			solid = true;
    		}
    		return solid;
    	}

  4. #4
    Join Date
    Oct 2016
    Posts
    10
    Rep Power
    0

    Default Re: Need Help With Collision Detection for My Java Game

    Ok here

    Java Code:
    private boolean collision(float velX, float velY) {
    		
    		boolean solid = false;
    		this.velX = velX;
    		this.velY = velY;
    
    		if (handler.getWorld().getTile(((int) x + (int) velX) / 64,((int) y + (int) velY) / 64).isSolid(true)) {
    			solid = true;
    		}
    		return solid;
    	}
    I still got a null pointer exception error :(

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need Help With Collision Detection for My Java Game

    You did not do what I suggested. In fact, you did the opposite. I said BREAK up the statement. Like the following (I don't know the types so I will use letters of the alphabet. And next time you should also include the thrown exception trace.

    Java Code:
    System.out.println("handler = " + handler);
    X world = handler.getWorld();
    System.out.println("world = " + world);
    Y tile = world.getTile(collisionX, collisionY);
    System.out.println("tile = " + tile);
    Regards,
    Jim
    Last edited by jim829; 01-19-2017 at 07:16 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Oct 2016
    Posts
    10
    Rep Power
    0

    Default Re: Need Help With Collision Detection for My Java Game

    Thanks. Turns out Tile and World were null

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need Help With Collision Detection for My Java Game

    But you used world first so that is the initial problem. Tile was null simply because world couldn't get a value.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Join Date
    Oct 2016
    Posts
    10
    Rep Power
    0

    Default Re: Need Help With Collision Detection for My Java Game

    Oh. Didn't think about that. Thanks!

Similar Threads

  1. Java game collision detection need help!
    By NOOB-PROGRAMMER in forum New To Java
    Replies: 2
    Last Post: 01-18-2013, 04:16 PM
  2. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 12:45 PM
  3. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 03:29 PM
  4. Collision Detection for Billiards game
    By prashant in forum Java Gaming
    Replies: 3
    Last Post: 06-02-2011, 04:45 AM
  5. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 08:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •