1. Guest,
    If you need help getting to know Xenforo, please see our guide here. If you have feedback or questions, please post those here.
    Dismiss Notice

Math expertes, help me work out this equation?

Discussion in 'After Hours Lounge (Off Topic)' started by Ricardo C, Aug 8, 2005.

  1. Ricardo C

    Ricardo C Producer

    Joined:
    Feb 14, 2002
    Messages:
    5,060
    Likes Received:
    0
    Game development related question:

    Imagine a grid made up of 64-pixel wide squares. It's a 3D world, but z is always zero. The squares start at 0,0 and occupy only the +x/+y axes, no negative values are involved. There is a player standing on a random spot on the grid. I have a function that is in constant use by every square of the grid, in order to determine whether the player is on it or not. To do this, I'm using the following variables:

    holder.x and holder.y, which will store the squares x and y coordinates.

    my.x and my.y, which hold the square's coordinates

    player.x and player.y, which hold the player's position

    At the start of the function, I copy my.x and my.y's values to holder.x and holder.y. Then, I substract player.x from my.x and player.y from my.y. I compare the results with the original values stored in holder.x and holder.y. The formula I'm using is this:

    if(my.x < holder.x)&&(my.x >= holderx-63)&&(my.y < holder.y)&&(my.y >= holder.y-63)
    {
    do stuff;
    }

    The idea is that if the above conditions are true, then the player is within the square's boundaries, and so the function may proceed.

    The code compiles without errors, but the "stuff" isn't getting done, which makes me think I screwed up elsewhere in the code, but I wanted to ask as many people as possible to have a look at this, in case it's the formula that is wrong.

    If anyone could help with this, I'd be most grateful. And I'll give you a free copy of the finished game [​IMG]
     
  2. Ricardo C

    Ricardo C Producer

    Joined:
    Feb 14, 2002
    Messages:
    5,060
    Likes Received:
    0
    Problem solved [​IMG] Basically my equation muddied things up with too many variables. Someone suggested a much simpler version, and I'm back in business [​IMG]
     
  3. Joseph DeMartino

    Joseph DeMartino Lead Actor

    Joined:
    Jun 30, 1997
    Messages:
    8,313
    Likes Received:
    12
    Location:
    Florida
    Real Name:
    Joseph DeMartino
    Oh c'mon, you can't leave us hanging like that. At least post the simpler solution. A thousand anal-retentive math geeks are in agony out there. [​IMG]

    Joe
     
  4. Dave Poehlman

    Dave Poehlman Producer

    Joined:
    Mar 8, 2000
    Messages:
    3,817
    Likes Received:
    0
    How about a single variable with an array?

    my(x,y)

    By the way, are you making a Tron light-cycle game, Ricardo? [​IMG]
     
  5. Ricardo C

    Ricardo C Producer

    Joined:
    Feb 14, 2002
    Messages:
    5,060
    Likes Received:
    0
    3D Bomberman-type game, actually [​IMG] I need the bombs to be dropped in the middle of the "tile" the player is standing on, even if he's on the edge of it, or I would have all sorts of collision detection nightmares later.

    I could have also had the player be the one scanning the surface it's standing on, but since I started with the opposite way (hey I'm a newbie at this, cut me some slack [​IMG]), I wanted to make "my way" work rather than concede defeat. I'm not only bad at math, I'm also stubborn :b

    Joseph, the equation is this:

    if (my.x
     
  6. Haggai

    Haggai Producer

    Joined:
    Nov 3, 2003
    Messages:
    3,883
    Likes Received:
    0
    The "my" and "holder" variables seemed sort of redundant to me (I didn't see the thread until you posted about having figured it out, Ricardo), so I guess that was part of the problem. Still a little confused, though--you're just checking to see whether the player is on the square or not? Why aren't you just testing whether my.x = player.x and my.y = player.y?
     
  7. Ricardo C

    Ricardo C Producer

    Joined:
    Feb 14, 2002
    Messages:
    5,060
    Likes Received:
    0
    Because since the player's movement isn't locked to the grid, when he stands on a square (say 0,0), player.x can range from 0 to 63, same from player.y. I need to account for all posibilities.
     
  8. Haggai

    Haggai Producer

    Joined:
    Nov 3, 2003
    Messages:
    3,883
    Likes Received:
    0
    Well, I'm still confused about what you're trying to test for, and what the possibilities for the player are, but as long as you aren't, you're in business. [​IMG]
     
  9. TimDoss

    TimDoss Second Unit

    Joined:
    Jun 10, 1999
    Messages:
    298
    Likes Received:
    0
    if your x,y boundaries are [0-63],[0-63] then doesn't
    "x + 64" put you outside of it? Seems that could cause
    unwanted results. To put "player.x
     
  10. Jeff Gatie

    Jeff Gatie Lead Actor

    Joined:
    Aug 19, 2002
    Messages:
    6,531
    Likes Received:
    15

    Now whether he's using the values as an actual index to an array is a different story and if so, may lead to memory access violations depending on the language, but the logic of the above is OK for the 'if' statements he is using.
     
  11. Chris Lockwood

    Chris Lockwood Producer

    Joined:
    Apr 21, 1999
    Messages:
    3,215
    Likes Received:
    0
    > Why aren't you just testing whether my.x = player.x and my.y = player.y?

    Or something like this:
    my.x = trunc(player.x) and my.y = trunc(player.y)
     
  12. Ricardo C

    Ricardo C Producer

    Joined:
    Feb 14, 2002
    Messages:
    5,060
    Likes Received:
    0

    Nah, there's no arrays in use other than the vectors holding the coordinates of each in-game entity. The equation is checking against the actual world coordinates.
     

Share This Page