What's new

Math expertes, help me work out this equation? (1 Viewer)

Ricardo C

Senior HTF Member
Joined
Feb 14, 2002
Messages
5,068
Real Name
Ricardo C
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 :D
 

Ricardo C

Senior HTF Member
Joined
Feb 14, 2002
Messages
5,068
Real Name
Ricardo C
Problem solved :D Basically my equation muddied things up with too many variables. Someone suggested a much simpler version, and I'm back in business :)
 

Joseph DeMartino

Senior HTF Member
Joined
Jun 30, 1997
Messages
8,311
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. :)

Joe
 

Dave Poehlman

Senior HTF Member
Joined
Mar 8, 2000
Messages
3,813
How about a single variable with an array?

my(x,y)

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

Ricardo C

Senior HTF Member
Joined
Feb 14, 2002
Messages
5,068
Real Name
Ricardo C
3D Bomberman-type game, actually :D 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 :D), 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
 

Haggai

Senior HTF Member
Joined
Nov 3, 2003
Messages
3,883
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?
 

Ricardo C

Senior HTF Member
Joined
Feb 14, 2002
Messages
5,068
Real Name
Ricardo C
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.
 

Haggai

Senior HTF Member
Joined
Nov 3, 2003
Messages
3,883
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. :D
 

TimDoss

Second Unit
Joined
Jun 10, 1999
Messages
298
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
 

Jeff Gatie

Senior HTF Member
Joined
Aug 19, 2002
Messages
6,531

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.
 

Chris Lockwood

Senior HTF Member
Joined
Apr 21, 1999
Messages
3,215
> 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)
 

Ricardo C

Senior HTF Member
Joined
Feb 14, 2002
Messages
5,068
Real Name
Ricardo C

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.
 

Users who are viewing this thread

Sign up for our newsletter

and receive essential news, curated deals, and much more







You will only receive emails from us. We will never sell or distribute your email address to third party companies at any time.

Forum statistics

Threads
357,037
Messages
5,129,279
Members
144,286
Latest member
acinstallation172
Recent bookmarks
0
Top