Collision map

  • Thread starter Francois Lionet
  • Start date
F

Francois Lionet

Hello,

For the game I am writing, I need to handle a collision map. This collision
map is a simple array of bits representing an image, a bit set indicating
that there is a collision for this pixel. I am wondering on how to implement
this... I need to be able to scroll this array to follow the scrolling of
the game, to poke a shape into it, and retreive every pixel individually.
The size of this array can be quite big, bigger than the screen.
Should I do it by hand : reserve an array of bytes, and poke the shapes
myself, and do the collision detection. I fear that the scrolling would be
very slow, and that the overall performance would be bad. Is there a utility
class to shift the data in an array by an offset?
Should I use a BufferedImage of BINARY type? This would be really easy for
poking the shapes into it, and scrolling would be fast. But to get the data,
I would have to use the raster class, and the only way of getting the data
is to copy them into an array of Int (or float/double even worse), with one
int per pixel! Imagine the size of the array, and the time needed to copy
the data. Do you know if there is a way of getting the data in the raster
without copying them in an array?

What do you think? Do you think that the by hand method would be fast
enough? Do you know any method of getting access to the raster data without
copy?

Thanks, Francois
 
R

Robert Klemme

For the game I am writing, I need to handle a collision map. This collision
map is a simple array of bits representing an image, a bit set indicating
that there is a collision for this pixel. I am wondering on how to implement
this... I need to be able to scroll this array to follow the scrolling of
the game, to poke a shape into it, and retreive every pixel individually.
The size of this array can be quite big, bigger than the screen.
Should I do it by hand : reserve an array of bytes, and poke the shapes
myself, and do the collision detection. I fear that the scrolling would be
very slow, and that the overall performance would be bad. Is there a utility
class to shift the data in an array by an offset?
Should I use a BufferedImage of BINARY type? This would be really easy for
poking the shapes into it, and scrolling would be fast. But to get the data,
I would have to use the raster class, and the only way of getting the data
is to copy them into an array of Int (or float/double even worse), with one
int per pixel! Imagine the size of the array, and the time needed to copy
the data. Do you know if there is a way of getting the data in the raster
without copying them in an array?

What do you think? Do you think that the by hand method would be fast
enough? Do you know any method of getting access to the raster data without
copy?

Did you look at BitSet? I am not sure how that does for all the
operations you want, peeking and manipulating bits is certainly pretty
fast. And maybe you can do the scrolling by calculating offsets to indexes.

Kind regards

robert
 
P

Patricia Shanahan

Francois said:
Hello,

For the game I am writing, I need to handle a collision map. This collision
map is a simple array of bits representing an image, a bit set indicating
that there is a collision for this pixel. I am wondering on how to implement
this... I need to be able to scroll this array to follow the scrolling of
the game, to poke a shape into it, and retreive every pixel individually.
The size of this array can be quite big, bigger than the screen.
Should I do it by hand : reserve an array of bytes, and poke the shapes
myself, and do the collision detection. I fear that the scrolling would be
very slow, and that the overall performance would be bad. Is there a utility
class to shift the data in an array by an offset?

Is there any way you can use a base index, so that instead of shifting
data in the array you shift where you are looking? Can you pick a
maximum size during initialization?

If so, you could use an array as a circular buffer, and just adjust the
current base. You could wrap it in a class of your own that automates
mapping from the logical index to the actual index.

Consider using a java.util.BitSet as a circular buffer. It is very space
efficient, packing bits into a long[], 64 bits per element. The size in
bytes would be one eighth of the number of pixels. It already has
methods for setting and clearing blocks of consecutive bits that might
be useful when adding a shape.

If you do end up having to shift the data, rather than adjust the index,
look at System.arraycopy.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top