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 9 of 9

Thread: Help with collision!

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Post Help with collision!

    Hi, I'm making a simple game, but I have problems with my collision detection! My world is based on tiles with currently four different types: FLOOR, WALL, VOID and PIT.
    You shouldn't be able to walk over WALL or VOID and you should only be able to get over PIT if you're jumping! Here's my problem:

    It miscalculates wich type of tile I am actually standing on! For example:

    If I'm standing on a FLOOR Tile it may say that I'm standing on an VOID tile 3 tiles away, but it isn't that it calculates each void tile 1 tile away from the actual tile, It doesn't have that kind of pattern!

    This is my code that checks if a player can be on a specified tile or not:

    public boolean canBeAt(Entity par0Entity, int par1, int par2) {
     
    		int var0 = (int) Math.floor((par1 / 32));
    		int var1 = (int) Math.floor((par2 / 32));
     
    		//System.out.println(var0 + " " + var1);
     
    		Tile var2 = world[var0][var1];
    		TileType var3 = var2.getType();
     
    		System.out.println(var3);
     
    		if (var3 == TileType.VOID || var3 == TileType.WALL) return false;
     
    		if (var3 == TileType.PIT && par0Entity.isInContactWithGround()) return false;
     
    		return true;
    	}
     
    	public BufferedImage getTexture(int par0) {
     
    		return tileTextures[par0];
    	}

    Since my tiles are 32*32 and since my tiles are stored in a Tile[][] were the first bracket is the y axis and the second is the x axis I thought i would do like this:

    Divide the players x axis(par1) with 32 then floor the value into an integer and use this as the x axis in the Tile array, then do the same thing with the player y axis(par2), but for some reason I cant explain
    it doesn't work!

    Please help me!
    Last edited by Dr.Code; November 4th, 2012 at 02:52 PM. Reason: Not enough information!


  2. #2

    Default Re: Help with collision!

    You haven't provided enough information for someone to extrapolate the answer to your problem. You didn't even state what type of error you're getting. Is the comparison not working? For instance you're on a regular tile for the ground and it says you're in a pit? Well the problem may lay in the look-up tile array you have.

  3. The Following User Says Thank You to Programming_Hobbyist For This Useful Post:

    Dr.Code (November 4th, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with collision!

    Quote Originally Posted by Programming_Hobbyist View Post
    You haven't provided enough information for someone to extrapolate the answer to your problem. You didn't even state what type of error you're getting. Is the comparison not working? For instance you're on a regular tile for the ground and it says you're in a pit? Well the problem may lay in the look-up tile array you have.
    Im sorry if my message was unclear! I will edit it now!

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with collision!

    int var0 = (int) Math.floor((par1 / 32));
    (par1 / 32) does not return a double because both operands are integers. So math.floor is not required. Nor is the cast to int at that point.
    So:
    int var0 = par1/32;
    is the same thing as above.

    Does this give you the values you thought it would? What does that commented sysout show?

  6. The Following User Says Thank You to jps For This Useful Post:

    Dr.Code (November 4th, 2012)

  7. #5
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with collision!

    Quote Originally Posted by jps View Post
    Does this give you the values you thought it would? What does that commented sysout show?
    I just realized it do not! The x axis is moved forward 3 stepes(where i should get the value 3 i get 0) and the y axis is moved back 2 stepes (where I should get 0 i get 2) I will look further in the problem!

  8. #6
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with collision!

    SOMEBODY PLEASE HIT ME IN THE HEAD! I have been wrong all along! the look-up Tile array is [y-axis][x-axis] not [x-axis][y-axis]! OMG! But that is easily fixed with a little tweaking on the rendering... But it still doesn't work with the tile collision!

  9. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with collision!

    But that is easily fixed with a little tweaking on the rendering... But it still doesn't work with the tile collision!
    That does not tell us much and you should post the code with your question.

  10. #8
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with collision!

    Quote Originally Posted by jps View Post
    That does not tell us much and you should post the code with your question.
    I am still pretty sure that the error is in the canBeAt() method in the first post!

  11. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with collision!

    Quote Originally Posted by Dr.Code View Post
    I am still pretty sure that the error is in the canBeAt() method in the first post!
    Well you are only talking about 6 lines of code. Brute Force!!!
    sysout every variable in the method between every line of code in the method.

    There is no way we can do anything for you without enough code to compile and run that shows the problem.



    What about this:
    int var0 = (int) Math.floor((par1 / 32));
    int var1 = (int) Math.floor((par2 / 32));
    Have you adjusted these lines? If not do it.
    Then post the code after adjustments so we are all looking at the same code.

    Tile var2 = world[var0][var1];
    Nothing allows any of us to test what this does....

    TileType var3 = var2.getType();
    Nothing allows any of us to test what this does.... Plus this could be wrong from the line above it....

    From those two lines through the remainder of the method, noting can be correct or assumed correct until the above lines have been tested and verified.

Similar Threads

  1. Tile Collision
    By Tim_Wilson in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 07:40 PM
  2. Need help with object collision
    By xDeadScYthe in forum AWT / Java Swing
    Replies: 3
    Last Post: March 23rd, 2012, 07:10 AM
  3. Collision
    By risen375 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2011, 07:02 AM
  4. Collision Detecting
    By iams3b in forum AWT / Java Swing
    Replies: 0
    Last Post: February 6th, 2011, 01:48 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM

Tags for this Thread