Computer Science Canada

2d array coordinates

Author:  Mr. T [ Thu Aug 10, 2006 7:26 pm ]
Post subject:  2d array coordinates

I am using a 15 by 30 grid (2d array) and I was wondering how i could determine the pixel coordinates of each individual block in the grid?

Author:  [Gandalf] [ Thu Aug 10, 2006 7:40 pm ]
Post subject: 

Simply multiply the index of each square by the size of the grid, for example:
code:
const GRID_SIZE := 25
var grid : array 1 .. 10, 1 .. 10 of
    record
        x, y : int
    end record
for x : 1 .. upper (grid, 1)
    for y : 1 .. upper (grid, 2)
        grid (x, y).x := x * GRID_SIZE
        grid (x, y).y := y * GRID_SIZE
        Draw.Box (grid (x, y).x, grid (x, y).y, grid (x, y).x + GRID_SIZE, grid (x, y).y + GRID_SIZE, black)
    end for
end for


If that's not the answer you were looking for, I suggest you be a little more specific in your question.

Author:  Mr. T [ Thu Aug 10, 2006 7:45 pm ]
Post subject:  Alex's opinion

Well, in my game I need to know if a ball has collided with a filled block in the grid. I need the specific block to be in pixel coordinates in order to determine if the ball coordinates = the block coordinates. In which case, a collision has occurred.

Author:  [Gandalf] [ Thu Aug 10, 2006 8:07 pm ]
Post subject: 

Ok, then use the method I posted above combined with classical rectangular collision detection. Right?

Author:  Mr. T [ Thu Aug 10, 2006 8:10 pm ]
Post subject:  Alex's Opinion

I dont see the neccesity for a record. Ive never used one before when dealign with a 2d array.

Author:  [Gandalf] [ Thu Aug 10, 2006 8:24 pm ]
Post subject: 

Umm... why not? There's no reason to not use one now.

Fine, if you really don't want to use a record, you can create a Grid class and start using OOP in your program. Or use parallel arrays, but that's really just a worse method of using a record.

Author:  Mr. T [ Thu Aug 10, 2006 8:26 pm ]
Post subject:  Alex's Opinion

I'm not looking for an alternate to records; rather I'm asking why use them at all in this case?

Author:  [Gandalf] [ Thu Aug 10, 2006 9:12 pm ]
Post subject: 

I, on the other hand, am asking: Why? Why not use them? If not now, then why ever use records? What's the point of records at all?

We're using a record because it shows that a grid has both x and y co-ordinates. If you use parallel arrays (don't use records) you have two completely unrelated variables being used together, which is hardly expressive. A class would work well too.

Author:  Windsurfer [ Fri Aug 11, 2006 6:06 pm ]
Post subject: 

Why not post your source code and let people help you find the best method?


: