
I order you to forgive yourself!
I'm new to the concept of a convolution matrix. It's very interesting to read about it. For now, I just coded my own thing quickly. I'm not using walls or obstacles yet. I just hard-coded a temporary movement matrix. I based my code on this:
http://angeljohnsy.blogspot.com/2012/09/image-dilation-without-using-imdilate.html
class BitmapSolver
{
/**
* Goal: Using bitmap image dilation operation, we can solve a level.
**/
int[,] area;
int[,] walls;
List<int[,]> obstacles;
int[,] movement;
public BitmapSolver(Level l, int width, int height) {
int tileWidth = l.getTerrain().GetLength(0);
int tileHeight = l.getTerrain().GetLength(1);
//New arrays in C# are always filled with 0s.
area = new int[tileWidth * width, tileHeight * height];
walls = new int[tileWidth * width, tileHeight * height];
area[l.start.X * width, l.start.Y * height] = 1;
movement = new int[,] {
{0, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 0}
};
}
public void Update() {
area = dilateArray(area, movement);
}
public int[,] dilateArray(int[,] a, int[,] b) {
int[,] d = new int[a.GetLength(0), a.GetLength(1)];
int[,] aPad = padArray(a, b.GetLength(0) / 2, b.GetLength(1) / 2);
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++) {
d[i, j] = logicalAND(selectArrayPart(aPad, i, j, b.GetLength(0), b.GetLength(1)), b);
}
}
return d;
}
public int[,] padArray(int[,] a, int paddingX, int paddingY) {
int[,] b = new int[a.GetLength(0) + paddingX * 2, a.GetLength(1) + paddingY * 2];
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++) {
b[i + paddingX, j + paddingY] = a[i, j];
}
}
return b;
}
public int logicalAND(int[,] a, int[,] b) {
if (a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1)) {
//ERROR
Console.WriteLine("Error");
return 0;
}
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < a.GetLength(1); j++) {
int c = a[i, j] == 1 && b[i, j] == 1 ? 1 : 0;
if (c == 1) {
return 1;
}
}
}
return 0;
}
public int[,] selectArrayPart(int[,] a, int x, int y, int width, int height) {
int[,] b = new int[width, height];
for (int i = x; i < x + width; i++) {
for (int j = y; j < y + height; j++) {
b[i - x, j - y] = a[i, j];
}
}
return b;
}
public Texture2D createTexture(GraphicsDevice g, int width, int height) {
SpriteBatch s = new SpriteBatch(g);
RenderTarget2D renderTarget = new RenderTarget2D(g, area.GetLength(0), area.GetLength(1));
g.SetRenderTarget(renderTarget);
g.Clear(Color.Transparent);
s.Begin();
for (int i = 0; i < area.GetLength(0); i++) {
for (int j = 0; j < area.GetLength(1); j++) {
if (area[i, j] == 0) {
s.FillRectangle(new RectangleF(i, j, 1, 1), Color.Black * 0.5f);
} else {
s.FillRectangle(new RectangleF(i, j, 1, 1), Color.White);
}
}
}
s.End();
g.SetRenderTarget(null);
return renderTarget;
}
}
Edit: The in game size for a single tile is 100px (pretty much). A lot of levels are around 1400x600px.
Post has been edited 1 time(s), last time on May 9 2017, 6:42 pm by Apos.