0

Ok,

So I'm amking a game and need walls so you can't walk through walls (This is a top down view game, screenshot here), although, my calculation for seeing if you are going to hit a wall slows down the game with the 3 walls I have (I stopped making walls to figure out the calculation). 12 hours later, no result, still lags. So here I am, begging for help.

Here is where the wall is actually made:

Vector<Wall> walls = new Vector<Wall>(); 
walls.add(new Wall(new Point(137*factor,112*factor), new Dimension(163*factor,26*factor), Color.WHITE, factor));
walls.add(new Wall(new Point(137*factor,112*factor), new Dimension(13*factor,150*factor), Color.RED, factor));
walls.add(new Wall(new Point(149*factor,237*factor), new Dimension(39*factor,26*factor), Color.BLUE, factor));

The Wall constructor:

public Wall(Point pos, Dimension dims, Color col, int factor){
	this.pos = pos;
	this.dims = dims;
	this.color = col;
	g.setColor(this.color);
	g2.setColor(this.color);
	g.drawRect(this.pos.x, this.pos.y, this.dims.width, this.dims.height);
	g2.fillRect(this.pos.x/factor, this.pos.y/factor, this.dims.width/factor, this.dims.height/factor);
	wall = new Rectangle(this.pos, this.dims);
}

The calculation to determine if a wall is about to be hit:

//Motion: UP
boolean wall = false;
for(int i=0;i<walls.size();i++){
	player.pos.y = player.pos.y - 10; //Test for calc
	player.updateRectangle();
	if(walls.get(i).isAtWall(player.player)){
		wall = true;
	}
	player.pos.y = player.pos.y + 10; //Reset
	player.updateRectangle();
}

//Motion: DOWN
boolean wall = false;
for(int i=0;i<walls.size();i++){
	player.pos.y = player.pos.y + 10; //Test for calc
	player.updateRectangle();
	if(walls.get(i).isAtWall(player.player)){
		wall = true;
	}
	player.pos.y = player.pos.y - 10; //Reset
	player.updateRectangle();
}

//Motion: LEFT
boolean wall = false;
for(int i=0;i<walls.size();i++){
	player.pos.x = player.pos.x - 10; //Test for calc
	player.updateRectangle();
	if(walls.get(i).isAtWall(player.player)){
		wall = true;
	}
	player.pos.x = player.pos.x + 10; //Reset
	player.updateRectangle();
}

//Motion: RIGHT
boolean wall = false;
for(int i=0;i<walls.size();i++){
	player.pos.x = player.pos.x + 10; //Test for calc
	player.updateRectangle();
	if(walls.get(i).isAtWall(player.player)){
		wall = true;
	}
	player.pos.x = player.pos.x - 10; //Reset
	player.updateRectangle();
}

The check wall method:

public boolean isAtWall(Rectangle player){
	Point test1 = new Point(player.x, player.y); //Top Left Corner
	Point test2 = new Point(player.x, player.y+player.width); //Top Right Corner
	Point test3 = new Point(player.x+player.height, player.y); //Bottom Left Corner
	Point test4 = new Point(player.x+player.height, player.y+player.width); //Bottom Right Corner
	if(wall.contains(test1)
			|| wall.contains(test2)
			|| wall.contains(test3)
			|| wall.contains(test4)){
		return true;
	}
	return false;
}

I hope I provided enough information, if not, I will answer any and all questions.

I REALLY appreciate any help I can get... Like I've said, 12 hours straight and I can't think of anything.

(Oh, and yes the code stops the player from jumping through the wall)

-- Turt2Live

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by turt2live
0

EDIT: I was actually causing lag by layering BufferedImages on top of eachother...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.