Solved

# JS Matrix question

Posted on 2009-04-12
233 Views
I am making a simple game. It is a grid 8x8 , the good guy occupies on square, the bad guy another.

I need to make sure when the good guy chooses his initial square that my script does not put the bad guy within 4 squares of him.

I am stumped on how to do this!

Can anyone give me an example????
0
Question by:edvinson
• 4
• 4
• 3
• +1

LVL 41

Expert Comment

Given row, and column values (each from 1 to 8) you can use the following to determine the distance.

``````  function distance( r1, c1, r2, c2 ) {

var a = Math.abs( r1 - r2 )

var b = Math.abs( c1 - c2 )

return Math.floor( Math.sqrt( ( a * a ) + ( b * b ) ) )

}
``````
0

LVL 13

Expert Comment

Just in case you are wondering how HonorGod came up with that function:

The function uses the "distance" formula which is a derivative of Pythagoras' Theorm (http://en.wikipedia.org/wiki/Pythagorean_theorem).
0

LVL 31

Expert Comment

You are on a grid, I think you want this measure of distance

function distance( x1, y1, x2, y2 ) {
var dx = Math.abs( x1 - x2 )
var dy = Math.abs(y1 - y2 )

if ( a > b) return a + Math.abs( a - b )
else     return b + Math.abs( a - b )

}
0

LVL 31

Expert Comment

gah!!  sorry I have it the wrong way round, it should read

function distance( x1, y1, x2, y2 ) {
var dx = Math.abs( x1 - x2 )
var dy = Math.abs(y1 - y2 )

if ( a < b) return a + Math.abs( a - b )
else     return b + Math.abs( a - b )

}
0

LVL 31

Accepted Solution

GwynforWeb earned 300 total points
OK now I am messing this thread up with my typos. I am so sorry. Since you are a grid the answer is this. (final answer)

function distance( x1, y1, x2, y2 ) {
var a= Math.abs( x1 - x2 )
var b = Math.abs(y1 - y2 )

if ( a > b)  return a
else     return b

}

ie the min distance you travel is the max of the differences in the coordinates.
0

LVL 13

Assisted Solution

numberkruncher earned 200 total points
I am not sure what GwynforWeb's solution is supposed to do as it completely ignores the variables dx and dy??

When using radial collision detection (i.e. making sure that a bad guy is not placed within 4 squares of good guy) then you simply need to check the distance.

So (using the image as an example):

Good (3, 3)           (col, row)
A (1, 1)
B (4, 2)
C (8, 5)

distance from Good to A = 2
distance from Good to B = 1
distance from Good to C = 5

Your code simply needs to enumerate the grid whilst comparing distance. The following is an example of how you could do this.
``````function isCellEmpty(col, row) {

// Return false if there is an enemy or player in cell!

return true;

}

function findPosition(playerPos) {

for (var ri = 0; ri < 8; ++ri)

for (var ci = 0; ci < 8; ++ci)

if (isCellEmpty(ci, ri) && distance(ri, ci, playerPos.r, playerPos.c) >= 4)

return { r: ri, c: ci};

// No places available!

return null;

}

function foo() {

var playerPos = { r: 2, c: 2 };

var enemyPos = findPosition(playerPos);

}
``````
dist.jpg
0

LVL 41

Expert Comment

Gwen, you're right. This is correct

function D2( r1, c1, r2, c2 ) {
return Math.max( Math.abs( r1 - r2 ), Math.abs( c1 - c2 ) )
}

``````<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<title> 8 x 8 </title>

<body>

<script type='text/javascript'>

function D1( r1, c1, r2, c2 ) {

var a = Math.abs( r1 - r2 )

var b = Math.abs( c1 - c2 )

return Math.floor( Math.sqrt( ( a * a ) + ( b * b ) ) )

}

function D2( r1, c1, r2, c2 ) {

return Math.max( Math.abs( r1 - r2 ), Math.abs( c1 - c2 ) )

}

function init() {

var total = diff = 0

for ( var r1 = 1; r1 < 9; r1++ ) {

for ( var c1 = 1; c1 < 9; c1++ ) {

for ( var r2 = 1; r2 < 9; r2++ ) {

for ( var c2 = 1; c2 < 9; c2++ ) {

if ( ( r1 != r2 ) || ( c1 != c2 ) ) {

var d1 = D1( r1, c1, r2, c2 )

var d2 = D2( r1, c1, r2, c2 )

if ( d1 != d2 ) {

document.write( r1 + ', ' + c1 + ' ' + r2 + ', ' + c2 + ' ' + d1 + ' ' + d2 + '<br>' )

diff++

}

total++

}

}

}

}

}

document.write( '<br><br>Total = ' + total + '  diff = ' + diff )

}

init()

</script>

</body>

</html>
``````
0

LVL 31

Expert Comment

If movement is between grid cells and diagonal movement is allowed then numberkruncher's diagram  seems wrong. And my solution of the max of the difference in the 2 coords is correct.
0

LVL 13

Expert Comment

GwynforWeb:  Yes, your 3rd response for maximum difference is correct; your earlier two responses didn't make a lot of sense, but you corrected those whilst I was writing my response.

Both approaches work, but GwynforWeb's approach uses less processing power.

I cannot see any problems with the diagram...
0

LVL 1

Author Comment

as i am fairly new to game prog, can anyone tell me which solution would be best for just passing the good guy coords and the bad guys?
0

LVL 41

Expert Comment

I am biased, and would vote for http://#a24127605 with partial credit to
http:#//a24127680 and http://#a24127663 (for the nice graphics)
0

LVL 41

Expert Comment

http:#a24127605 with partial credit to http:#a24127680 and http:#a24127663 (for the nice graphics)
0

## Featured Post

### Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…