Signing up for an account is fast and free. As a member you can join in the conversation, enter contests and remove the pop-up ads that guests get. Click here to create your free account.

# Math expertes, help me work out this equation?

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

### #1 of 12 OFFLINE Ricardo C

Ricardo C

Producer

• 5,060 posts
• Join Date: Feb 14 2002

Posted August 08 2005 - 03:57 PM

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

Man, an hour wasted on this sig! Thanks, Toshiba!

### #2 of 12 OFFLINE Ricardo C

Ricardo C

Producer

• 5,060 posts
• Join Date: Feb 14 2002

Posted August 08 2005 - 07:47 PM

Problem solved Basically my equation muddied things up with too many variables. Someone suggested a much simpler version, and I'm back in business
Man, an hour wasted on this sig! Thanks, Toshiba!

### #3 of 12 OFFLINE Joseph DeMartino

Joseph DeMartino

• 8,303 posts
• Join Date: Dec 31 1969
• Real Name:Joseph DeMartino
• LocationFlorida

Posted August 09 2005 - 07:13 AM

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.

Joe

### #4 of 12 OFFLINE Dave Poehlman

Dave Poehlman

Producer

• 3,817 posts
• Join Date: Mar 08 2000

Posted August 09 2005 - 08:10 AM

How about a single variable with an array?

my(x,y)

By the way, are you making a Tron light-cycle game, Ricardo?

### #5 of 12 OFFLINE Ricardo C

Ricardo C

Producer

• 5,060 posts
• Join Date: Feb 14 2002

Posted August 09 2005 - 08:37 AM

3D Bomberman-type game, actually 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 ), 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 <= player.x && player.x < my.x+64 && my.y <= player.y && player.y < my.y+64)
{
do stuff;
}

No extra variables, no fuss. I can't believe I didn't figure it out the first time. I need Math for Über-Dummies :b

Man, an hour wasted on this sig! Thanks, Toshiba!

### #6 of 12 OFFLINE Haggai

Haggai

Producer

• 3,883 posts
• Join Date: Nov 03 2003

Posted August 09 2005 - 08:46 AM

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 of 12 OFFLINE Ricardo C

Ricardo C

Producer

• 5,060 posts
• Join Date: Feb 14 2002

Posted August 09 2005 - 08:51 AM

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.
Man, an hour wasted on this sig! Thanks, Toshiba!

### #8 of 12 OFFLINE Haggai

Haggai

Producer

• 3,883 posts
• Join Date: Nov 03 2003

Posted August 09 2005 - 08:58 AM

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.

### #9 of 12 OFFLINE TimDoss

TimDoss

Second Unit

• 298 posts
• Join Date: Jun 10 1999

Posted August 10 2005 - 02:11 AM

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 <= my.x+63"
and "player.y <= my.y+63" I think would lock you within the boundaries.

### #10 of 12 OFFLINE Jeff Gatie

Jeff Gatie

• 6,530 posts
• Join Date: Aug 19 2002

Posted August 10 2005 - 02:33 AM

Quote:
 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 <= my.x+63" and "player.y <= my.y+63" I think would lock you within the boundaries.

This:
Quote:
 player.y <= my.y+63

is the same as this:
Quote:
 player.y < my.y+64

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 of 12 OFFLINE Chris Lockwood

Chris Lockwood

Producer

• 3,215 posts
• Join Date: Apr 21 1999

Posted August 10 2005 - 07:56 AM

> 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 of 12 OFFLINE Ricardo C

Ricardo C

Producer

• 5,060 posts
• Join Date: Feb 14 2002

Posted August 10 2005 - 06:47 PM

Quote:
 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.

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.

Man, an hour wasted on this sig! Thanks, Toshiba!