CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Math Help

  1. #1
    Join Date
    Sep 2008
    Posts
    70

    Math Help

    Hello, i am having some problems. Hopefully i can explain it to you without getting you to confused.

    Basically i have a 2D game which has a total of 300 tiles in a 20 x 15 grid. Each tile is 32 pixels. I am trying to find the player position in one number. His x position is indicated by a variable called x likewise with this y position. But that is the total pixel position. So to get his actual tile number you need to divide by 32.

    So to find out his total position i basically need to get his x number. Then times it by 20 (the number of tiles on the line) for every row of y he is down. I have a formula that sort of works. The problem is that when i walk halfway down a tile going in the y direction it starts adding numbers to my total tile. I only want it to calculate when i have moved a entire 32 pixels.

    Here is my formula.

    playertile = ((y / 32) * 20 + 20) + ((x / 32)) - 20;

    I am aware it may be completely wrong as i suck at math. Even so this basically gets the right tile number if i am perfectly down 32 pixels. The problem is if im only down half a tile say 15 pixels it will still add some numbers.

    (+ 32 is becouse i want the first tile to be 1 and not 0)
    x + 32) / 32 gets the perfect x position
    x + 32) / 32 gets the perfect y position

    so how do i get my total position? i also tried : y + 32 / 32 * 20 + x + 32 / 32 but it was the same problem of moving halfway down a tile. I guess i need it to round to the nearest 32, whatever tile its closer to or something.



    If you are still confused basically i want the Y below to return 26. Or the b would return 300.

    xxxxxxxxxxxxxxxxxxxx
    xxxxxyxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxb


    Anyone no how i may go about this? Thanks. I know its confusing hopefully i explained it well enough for you to understand. Thanks for any help i receive .
    Last edited by marsh; January 12th, 2010 at 10:54 PM.

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Math Help

    Hi,

    As long as you use integer arithmetic, looks like that should work. Have you declared playertile, x & y as int?

    Alan

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Math Help

    If I'm following you, let's assume the values for for your position y are x = 200 and y = 50.

    Step 1 is finding the row number. You can do that with y / 32, which is 1 in this case. Multiply that by 20. That gives you
    (y / 32) * 20.

    Then you need to find the column number, which is x / 32, in this case 6. Add that to the previous result and you get

    ((y / 32) * 20) + (x / 32).

    Plugging in the numbers, you get
    ((50 / 32) * 20) + (200 / 32).
    (1 * 20) + (6)
    20 + 6
    26

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Math Help

    Hi marsh,

    Since
    playertile = ((y / 32) * 20) + (x / 32);
    is basically the same as:
    playertile = ((y / 32) * 20 + 20) + ((x / 32)) - 20;

    as GCDEF points out, it should work correctly.

    Alan

  5. #5
    Join Date
    Sep 2008
    Posts
    70

    Re: Math Help

    Quote Originally Posted by alanjhd08 View Post
    Hi,

    As long as you use integer arithmetic, looks like that should work. Have you declared playertile, x & y as int?

    Alan
    Sorry for wasting your guys time. I changed x and y to a float for some other calculations. So this must have broke the formula with the decimals. It works now thank you all very much.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured