I was just coding for a game I'm creating, and I discovered an exciting algo, that might help some of you. I don't know if this algo or the math calculations is already out there but here it is:




[size=9]
consider a 4x4 matrix that looks like this:

[ 0 ] [ 1 ] [ 2 ] [ 3 ]
[ 4 ] [ 5 ] [ 6 ] [ 7 ]
[ 8 ] [ 9 ] [ 10 ] [ 11 ]
[ 12 ] [ 13 ] [ 14 ] [ 15 ]
fig. 1.0

now this matrix corresponds to this matrix:

[0, 0] [0, 1] [0, 2] [0, 3]
[1, 0] [1, 1] [1, 2] [1, 3]
[2, 0] [2, 1] [2, 2] [2, 3]
[3, 0] [3, 1] [3, 2] [3, 3]
fig. 1.1
[/size]



Now the question is, how do you calculate the equivalent room locations(fig. 1.0) based only on the given x and y coordinates or should I say based only on fig. 1.1?

Answer: (Math)

Given:
# of X grids = 4
# of Y grids = 4
X coordinate = 3 (Just an example)
Y coordinate = 2 (Just an example)

Equation: Room Location = (X Coordinate * (# of X Grids - 1)) + X Coordinate + Y Coordinate




[size=9]
Solution: Room Location = (3 * (4 - 1)) + 3 + 2
= (3 * 3) + 3 + 2
= 9 + 3 + 2
= 14
[/size]



Now isn't coordinate (3, 2) equivalent to 14?

Answer: (Assembly)




[size=9]
.CONST

;4x4 Matrix

XGRID EQU 4
YGRID EQU 4

;Coordinate to find the exact room location
.DATA

XCOORDS DD 3
YCOORDS DD 2
...

.CODE

mov eax, XGRID
dec eax
mul XCOORDS
add eax, XCOORDS
add eax, YCOORDS

[/size]



In here, EAX now contains the parallel room location, which is 14. I'll be posting the game at the Game Programming Forum.

Hope that helps!!!


this doesn't only apply to 4x4 but to any size, whether it's 300x259...


Happy ASMing!!! :)
Posted on 2002-02-24 22:35:06 by stryker
Hello :)

Is it just me or isn't

xcoord*(xgrid-1) + xcoord + ycoord (1)

the same as

xcoord*xgrid+ycoord? (2)

When you do the expansion, the xcoord * -1 simply
cancels out with +xcoord. Why not save yourself the
add, and the decrement? Or do you happen to have
(xgrid-1) already calculated?

--Chorus
Posted on 2002-03-14 23:29:45 by chorus
Yes, you are right, it cancels each other out. Thanks for spotting that. :) I was not thinking of XCOORDS to be distributed for multiplication to the 2 numbers inside the (parentheses), I was thinking of doing the calculations inside the () first then mul then add. If you want to see a game implementing this concept, click here and look for 006.zip. You'll see the code on the bottom most part of the .asm file. :)

At last, somebody responded to this thread(The post is dated last February 24th). I thought nobody would even care to look at this "cheap math crap". :)
Posted on 2002-03-15 00:20:13 by stryker
:)

Cheap math crap is the best kinda stuff :) It's half the reason
I like assembly language. Right now I'm working on a 2d game
engine, which I just started (right now it's basically a DIB section
that changes colours). I'm doing it all software (and I'm planning
the same for a 3d engine soon). Why? cause I like the cheap
math crap. Getting DirectX to do everything for you is no fun.

Anyways, I'm just rambling. Take it easy.

--Chorus
Posted on 2002-03-15 10:54:25 by chorus
?
In your example you need just


mov eax,XGRID
mul XCOORDS
add eax,YCOORDS
Posted on 2002-03-15 22:33:22 by The Svin
:) stupid me, hmm!! why didn't I updated the code. I must have an alzheimer's disease. :)
Posted on 2002-03-16 00:21:35 by stryker