BramVandenbon
Oct 9th, 2004, 10:59 AM
Please take a look at the JPEG file of the attachement for a clearer view of what I am going to explain... Because this thread will make a great challenge.:)
I am currently making a supermario game and I am having some problems with the collision detection. This is part of a level class but that is (not really important) I have tried to put all keywords in bold making it easy to read through my thread rapidly if you dont feel like reading everything ;).
My level is a 2D array. It is loaded from a file(not important) and is filled up with numbers. Those numbers tell us what kind of object is placed in that position. Ofcourse in my application I have about 20 different kind of objects but here I would like to keep things simple.
//my 2D array
int board[][];
//constants
int solid_block = 1;
int air = 2;
I also have an array that holds pictures. Using threading my window is repeanted every 20ms according to the values inside the 2Darray. All blocks have the same size in height and width (a int unit) and their positions are calculated using the x and y from the array. It looks a bit like this:
int unit = 30; //constant for calculating the position
Image img;
for x to ...{
for y to ...{
if board[x][y] == solid_block{ // board[x][y] == 1 ,
//there is a block on this position
img = imageblock,
}else{
img = imageair;
}
g.drawImage(img, x*unit, y*unit, unit,unit, this);
}
}
Inside my game I have SelfmadeObjects and those are moving accross the board. These images are moving on top of the level. For example SuperMario himself.
Now we arrive to the problem:
For collisiondetection I use a function
// int[][] Board, int unit, int solid_block, int air are globally defined
public Point collision (Rectangle2D A, Point B){
return new Point(x,y);
}
Rectangle2D is SuperMario. It has a height a width and a x and y position. (Note that this class has properties like getCentralX and getCentralY, maybe it comes in handy.)
Point B is actually used as a geneometric vector. It has a deltaX and a deltaY.[B] These are the distances that [b]SuperMario would like to move. These can be negative if mario wants to move to the left or to the top.
The return value of this function should be the distance X and distance Y that SuperMario can move in that direction without having a collision with one of the solid blocks. Ofcourse I return this value as a Point (because you can not return 2 values.)
I have tried to program this before... But I am having all kind of bugs and problems. I would appreciate if somebody else would give it a try?
(Codelistings are welcome, stuff like "Your program is all wrong and you should start over again", is not welcome)
If so, thank you very much in advance. :)
I am currently making a supermario game and I am having some problems with the collision detection. This is part of a level class but that is (not really important) I have tried to put all keywords in bold making it easy to read through my thread rapidly if you dont feel like reading everything ;).
My level is a 2D array. It is loaded from a file(not important) and is filled up with numbers. Those numbers tell us what kind of object is placed in that position. Ofcourse in my application I have about 20 different kind of objects but here I would like to keep things simple.
//my 2D array
int board[][];
//constants
int solid_block = 1;
int air = 2;
I also have an array that holds pictures. Using threading my window is repeanted every 20ms according to the values inside the 2Darray. All blocks have the same size in height and width (a int unit) and their positions are calculated using the x and y from the array. It looks a bit like this:
int unit = 30; //constant for calculating the position
Image img;
for x to ...{
for y to ...{
if board[x][y] == solid_block{ // board[x][y] == 1 ,
//there is a block on this position
img = imageblock,
}else{
img = imageair;
}
g.drawImage(img, x*unit, y*unit, unit,unit, this);
}
}
Inside my game I have SelfmadeObjects and those are moving accross the board. These images are moving on top of the level. For example SuperMario himself.
Now we arrive to the problem:
For collisiondetection I use a function
// int[][] Board, int unit, int solid_block, int air are globally defined
public Point collision (Rectangle2D A, Point B){
return new Point(x,y);
}
Rectangle2D is SuperMario. It has a height a width and a x and y position. (Note that this class has properties like getCentralX and getCentralY, maybe it comes in handy.)
Point B is actually used as a geneometric vector. It has a deltaX and a deltaY.[B] These are the distances that [b]SuperMario would like to move. These can be negative if mario wants to move to the left or to the top.
The return value of this function should be the distance X and distance Y that SuperMario can move in that direction without having a collision with one of the solid blocks. Ofcourse I return this value as a Point (because you can not return 2 values.)
I have tried to program this before... But I am having all kind of bugs and problems. I would appreciate if somebody else would give it a try?
(Codelistings are welcome, stuff like "Your program is all wrong and you should start over again", is not welcome)
If so, thank you very much in advance. :)