Hello,
ok, I am having a bit of a problem getting the "intersects" method working the way I need it to.
I am making a game that is using an overview looking straight down. The map is 1600x1600 the viewing area for the game is 800x800.
I have set the movement up so that the player is centered on the screen and when the viewable area gets to the edge of the map, the map stops moving and the player moves until he reaches the edge, then once he go in the opposite direction the player moves until he is back at center then the map starts to move again.
The player has been placed in a rectangle for collision detection, as well as the objects I want to be able to detect (trees) as you will see in the code i lined up a row of trees on the left edge.
the problem that I am having is that even though the trees only extend down to around the 800 mark of pixels of the map, when I do the "intersects" the guy stops as if there are trees lining the entire left side.
I did some testing in the move left (as you will see in the code) and what seems to be the problem is the intersects is taking the on screen position of the player (400 x 400) but using the actual pixel position of the objects.. which goes from y=0 to y=780 (approx)..
so my question is, is there a way to do an if check and add x and/or y value to the position of the player to get an accurate intersects? or is there some other kind of work around??
when i run this the coordinates of the rectangles are as such:
and the py stays at 400 until i get to the bottom of the map, then it goes up.. which is why i am getting a false positive on the intersects.
please note that there is alot of garbage laying around in my code, please dont lambase me for poor coding habits, i will clean them up once i get things figured out, for now i learn by adding in and removing things to see what happens.
this is how I achieved this:
Thanks for any help...
ok, I am having a bit of a problem getting the "intersects" method working the way I need it to.
I am making a game that is using an overview looking straight down. The map is 1600x1600 the viewing area for the game is 800x800.
I have set the movement up so that the player is centered on the screen and when the viewable area gets to the edge of the map, the map stops moving and the player moves until he reaches the edge, then once he go in the opposite direction the player moves until he is back at center then the map starts to move again.
The player has been placed in a rectangle for collision detection, as well as the objects I want to be able to detect (trees) as you will see in the code i lined up a row of trees on the left edge.
the problem that I am having is that even though the trees only extend down to around the 800 mark of pixels of the map, when I do the "intersects" the guy stops as if there are trees lining the entire left side.
I did some testing in the move left (as you will see in the code) and what seems to be the problem is the intersects is taking the on screen position of the player (400 x 400) but using the actual pixel position of the objects.. which goes from y=0 to y=780 (approx)..
so my question is, is there a way to do an if check and add x and/or y value to the position of the player to get an accurate intersects? or is there some other kind of work around??
when i run this the coordinates of the rectangles are as such:
Px: 74.0 Py 400.0 Ox 0.0 Oy 420.0
and the py stays at 400 until i get to the bottom of the map, then it goes up.. which is why i am getting a false positive on the intersects.
please note that there is alot of garbage laying around in my code, please dont lambase me for poor coding habits, i will clean them up once i get things figured out, for now i learn by adding in and removing things to see what happens.
this is how I achieved this:
package javagame;
import java.awt.Rectangle;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
int state;
Image grid, man, tree1;
Rectangle[] mapList;
Image[] mapImage;
float xloc = 400, yloc = 400, manx = 400, many = 400;
boolean hasImage = false;
Background bg;
Rectangle r,p;
public Play(int in) throws SlickException{
state = in;
}
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
int tileX = 32, tileY = 32;
bg = new Background(tileX, tileY);
grid = new Image("res/grass.png");
man = new Image("res/enemy.png");
tree1 = new Image("res/GrassKnoll.png");
mapList = new Rectangle[12];
mapImage = new Image[12];
mapList[0] = new Rectangle(0, 0, tree1.getWidth(), tree1.getHeight()); mapImage[0] = tree1;
mapList[1] = new Rectangle(0, tree1.getHeight() , tree1.getWidth(), tree1.getHeight()); mapImage[1] = tree1;
mapList[2] = new Rectangle(0 ,tree1.getHeight() * 2, tree1.getWidth(), tree1.getHeight()); mapImage[2] = tree1;
mapList[3] = new Rectangle(0, tree1.getHeight() * 3, tree1.getWidth(), tree1.getHeight()); mapImage[3] = tree1;
mapList[4] = new Rectangle(0, tree1.getHeight() * 4, tree1.getWidth(), tree1.getHeight()); mapImage[4] = tree1;
mapList[5] = new Rectangle(0, tree1.getHeight() * 5, tree1.getWidth(), tree1.getHeight()); mapImage[5] = tree1;
mapList[6] = new Rectangle(0, tree1.getHeight() * 6, tree1.getWidth(), tree1.getHeight()); mapImage[6] = tree1;
mapList[7] = new Rectangle(0, tree1.getHeight() * 7, tree1.getWidth(), tree1.getHeight()); mapImage[7] = tree1;
mapList[8] = new Rectangle(0, tree1.getHeight() * 8, tree1.getWidth(), tree1.getHeight()); mapImage[8] = tree1;
mapList[9] = new Rectangle(0, tree1.getHeight() * 9, tree1.getWidth(), tree1.getHeight()); mapImage[9] = tree1;
mapList[10] = new Rectangle(0 ,tree1.getHeight() * 10, tree1.getWidth(), tree1.getHeight()); mapImage[10] = tree1;
mapList[11] = new Rectangle(0, tree1.getHeight() * 11, tree1.getWidth(), tree1.getHeight()); mapImage[11] = tree1;
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(grid, 0 - xloc, 0 - yloc);
g.drawImage(grid, 0 - xloc, 395 - yloc);
g.drawImage(grid, 0 - xloc, 795 - yloc);
g.drawImage(grid, 0 - xloc, 1195 - yloc);
g.drawImage(grid, 395 - xloc, 0 - yloc);
g.drawImage(grid, 395 - xloc, 395 - yloc);
g.drawImage(grid, 395 - xloc, 795 - yloc);
g.drawImage(grid, 395 - xloc, 1195 - yloc);
g.drawImage(grid, 795 - xloc, 0 - yloc);
g.drawImage(grid, 795 - xloc, 395 - yloc);
g.drawImage(grid, 795 - xloc, 795 - yloc);
g.drawImage(grid, 795 - xloc, 1195 - yloc);
g.drawImage(grid, 1195 - xloc, 0 - yloc);
g.drawImage(grid, 1195 - xloc, 395 - yloc);
g.drawImage(grid, 1195 - xloc, 795 - yloc);
g.drawImage(grid, 1195 - xloc, 1195 - yloc);
p = new Rectangle(Math.round(manx), Math.round(many), man.getWidth(), man.getHeight());
for(int i=0;i<mapList.length;i++) {
g.fillRect((mapList[i].x - xloc), (mapList[i].y - yloc), mapList[i].width, mapList[i].height,tree1,0,0);
}
g.fillRect(p.x, p.y, p.width, p.height, man, 0, 0);
g.drawString("X:" + xloc + " Y:" + yloc, 600, 100);
g.drawString("MX:" + manx + " MY:" + many, 600, 200);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_UP)) {
if(yloc < 0) {
if (many > 0) {
many -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(p.intersects(mapList[i])) { many += 2 * .5f; break;} }
}
} else {
if(many > 400) {
many -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(p.intersects(mapList[i])) { many += 2 * .5f; } }
} else {
yloc -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(p.intersects(mapList[i])) { yloc += 2 * .5f; } }
}
}
}
if(input.isKeyDown(Input.KEY_DOWN)) {
if(yloc > 800) {
if(many < (800 - man.getHeight())) {
many += 1 * .5f;
}
} else {
if(many < 400) {
many += 1 * .5f;
} else {
yloc += 1 * .5f;
}
}
}
if(input.isKeyDown(Input.KEY_LEFT)) {
if(xloc < 0) {
if (manx > 0) {
manx -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(mapList[i].intersects(p)) { manx += 2 * .5f; System.out.println("Px: " + p.getX() + " Py "+p.getY()+" Ox "+mapList[i].getX() + " Oy "+mapList[i].getY());} }
}
} else {
if(manx > 400) {
manx -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(p.intersects(mapList[i])) { manx += 2 * .5f; System.out.println("got 2");} }
} else {
xloc -= 1 * .5f;
for(int i=0;i<mapList.length;i++) { if(p.intersects(mapList[i])) { xloc += 2 * .5f; System.out.println("got 3");} }
}
}
}
if(input.isKeyDown(Input.KEY_RIGHT)) {
if(xloc > 800) {
if(manx < (800 - man.getWidth())) {
manx += 1 * .5f;
}
} else {
if(manx < 400) {
manx += 1 * .5f;
} else {
xloc += 1 * .5f;
}
}
}
}
@Override
public int getID() {
return state;
}
}
Thanks for any help...