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

Thread: Math Help

Threaded View

  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.

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