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

Thread: Java chunk negative number problem

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java chunk negative number problem

    I've got a tile based map, which is divided in chunks. I got a method, which puts tiles in this map and with positive numbers it's working. But when using negative numbers it wont work. This is my setTile method:
    public static void setTile(int x, int y, Tile tile)
        	{
        		int chunkX = (int) Math.floor((float) x / (float) Chunk.CHUNK_SIZE), chunkY = (int) Math.floor((float) y / (float) Chunk.CHUNK_SIZE);
     
                    IntPair intPair = new IntPair(chunkX, chunkY);
     
        			world.put(intPair, new Chunk(chunkX, chunkY));
     
        		world.get(intPair).setTile(x - chunkX * Chunk.CHUNK_SIZE, y - chunkY * Chunk.CHUNK_SIZE, tile);
        	}
    This is the setTile method in the chunk class (CHUNK_SIZE is a constant with the value 64):
    The tiles array is a 2 dimensional 64*64 array

    public void setTile(int x, int y, Tile t)
        	{
        		if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
        			tiles[x][y] = t;
        	}

    The setTile method places a tile in a chunk. But when using a negative value a weird result is created. With e.g. x 53 it should place the tile in the chunk at the x position 0 and the chunk-array position 53. With the x value 87 it should place it in the chunk at x position 1 and the chunk-array position 23. These two examples work. But when using e.g. -45 it should place it in the chunk at x -1 and chunk-array position 18 which is not the case.

    What's wrong with my code?


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

    Default Re: Java chunk negative number problem

    But when using negative numbers it wont work.
    What does "wont work" mean?
    If you get an error message, copy the full text and paste it here.
    Otherwise explain what happens.

    If needed, post a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java chunk negative number problem

    But when using a negative value a weird result is created.
    Sometimes the result for the chunk-array position is 128 sometimes 64.

    It's kind of hard to explain. I'm using chunks in a tile based game. When using a absolute x or y value, it should place it in a relative x or y value in the chunk. However, when using a negative value as x position (maybe y too, but I don't need negative y positions) the result is a strange value out of the bounds of the array.

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

    Default Re: Java chunk negative number problem

    Please post a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Bingo90 (October 17th, 2013)

  6. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java chunk negative number problem

    Wow... When programming the problem-showing-program the problem somehow solved itself

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

    Default Re: Java chunk negative number problem

    That makes it easier when you are able to find and solve your problems.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: March 18th, 2013, 05:49 PM
  2. Replies: 2
    Last Post: December 3rd, 2012, 01:02 PM
  3. number turning negative during calculation why???
    By derekxec in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 3rd, 2012, 11:35 AM
  4. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  5. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM

Tags for this Thread