Game colisao

7 respostas
java
Pantrol

alguém pode me ajudar com esse meu projeto, criai um joguinho estava indo tudo de boas, só que ae deu erro quando fiz o código de colisão na parede.
meu projeto ta no google drive foi feito em java 15…
https://drive.google.com/file/d/1y8J_IxYH0YyQaQy0orgBUazyTPDxwCA9/view?usp=sharing
e o código esta aqui…

public static boolean isFree(int xnext, int ynext) {
		int x1 = xnext / TILE_SIZE;
		int y1 = ynext / TILE_SIZE;
		
		int x2 = (xnext + TILE_SIZE - 1) / TILE_SIZE;
		int y2 = ynext / TILE_SIZE;
		
		int x3 = xnext / TILE_SIZE;
		int y3 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		
		int x4 = (xnext + TILE_SIZE - 1)/ TILE_SIZE;
		int y4 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		// ou seja ele esta perguntando se e uma instancia de waal taile 
		// se for e true, se nao e false 
		return (tiles[x1 +(y1*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x2 +(y2*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x3 +(y3*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x4 +(y4*Word.WIDTH)] instanceof WallTile);

		
	}

	public void render(Graphics g) {
		// 30 por que e o tamanho de cada taile!
		int xstart = (Camera.x / 30);
		int ystart = (Camera.y /30);
		// 
		int xfinal = xstart + (Game.WIDTH /30);
		int yfinal = ystart + (Game.HEIGHT /30);

		for (int xx = xstart; xx <= xfinal; xx++) {
			for (int yy = ystart; yy <= yfinal; yy++) {

				// vamos usar um if caso der valores negativos em algumas das direçoes
				if (xx < 0 || yy < 0 || xx >= WIDTH || yy >= HEIGHT) {
					continue;
				}

				Taile taile = tiles[xx + (yy * WIDTH)];
				taile.render(g);

			}
		}
=====================================================================

public void tick() {

muved = false;

// aqui ele vai verificar se ele pode avançar antes mesmo de avança!

// assim da para saber se ele vai colidir ou nao antes de avançar!

if (right && Word.isFree ((int)(x+speed), this.getY())) {

muved = true;

dir = right_dir;

x += speed;
} else if (left && Word.isFree((int)(x-speed), this.getY())) {

			muved = true;
			dir = left_dir;
			x -= speed;

		}

		else if (up && Word.isFree(this.getX() -(int) speed, this.getY())) {
			muved = true;
			dir = up_dir;
			y -= speed;

		} else if (down && Word.isFree(this.getX() +(int) speed, this.getY())) {

			muved = true;
			dir = down_dir;
			y += speed;

		}

7 Respostas

RoinujNosde

Qual erro?

Pantrol

tipo não mostra erro o jogo abre mas o play não anda mais, antes do código de colisão ele andava, (acho q o código ta funcionando mas devo ter erado em algum calculo e ele ta considerando tudo ao redor como obstáculo por isso não se move)

RoinujNosde

Não deveria retornar false aqui caso seja WallTile?

Pantrol

Não. achei um erro faltou o "! "de return !(tiles[x1… ainda sim ele ta colidindo nos locais errados.

RoinujNosde

Mostra aí como ficou o !

Pantrol
corigi algumas coisas ma smasmo assim ta colidindo  em poucos pontos e mesmo assim errado  era para colodir nas paredes do mapa

public static boolean isFree(int xnext, int ynext) {

int x1 = xnext / TILE_SIZE;

int y1 = ynext / TILE_SIZE;
int x2 = (xnext + TILE_SIZE - 1) / TILE_SIZE;
		int y2 = ynext / TILE_SIZE;
		
		int x3 = xnext / TILE_SIZE;
		int y3 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		
		int x4 = (xnext + TILE_SIZE - 1)/ TILE_SIZE;
		int y4 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		// ou seja ele esta perguntando se e uma instancia de waal taile 
		// se for e true, se nao e false 
		return !(tiles[x1 +(y1*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x2 +(y2*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x3 +(y3*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x4 +(y4*Word.WIDTH)] instanceof WallTile);

		
	}

	public void render(Graphics g) {
		// 30 por que e o tamanho de cada taile!
		int xstart = Camera.x >> 4;
		int ystart = Camera.y >> 4;
		// 
		int xfinal = xstart + (Game.WIDTH >> 4);
		int yfinal = ystart + (Game.HEIGHT >> 4);

		for (int xx = xstart; xx <= xfinal; xx++) {
			for (int yy = ystart; yy <= yfinal; yy++) {

				// vamos usar um if caso der valores negativos em algumas das direçoes
				if (xx < 0 || yy < 0 || xx >= WIDTH || yy >= HEIGHT) {
					continue;
				}

				Taile taile = tiles[xx + (yy * WIDTH)];
				taile.render(g);

			}
===========================================================

public static boolean isFree(int xnext, int ynext) {

int x1 = xnext / TILE_SIZE;

int y1 = ynext / TILE_SIZE;
int x2 = (xnext + TILE_SIZE - 1) / TILE_SIZE;
		int y2 = ynext / TILE_SIZE;
		
		int x3 = xnext / TILE_SIZE;
		int y3 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		
		int x4 = (xnext + TILE_SIZE - 1)/ TILE_SIZE;
		int y4 = (ynext + TILE_SIZE - 1) / TILE_SIZE;
		// ou seja ele esta perguntando se e uma instancia de waal taile 
		// se for e true, se nao e false 
		return !(tiles[x1 +(y1*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x2 +(y2*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x3 +(y3*Word.WIDTH)] instanceof WallTile) ||
				(tiles[x4 +(y4*Word.WIDTH)] instanceof WallTile);

		
	}

	public void render(Graphics g) {
		// 30 por que e o tamanho de cada taile!
		int xstart = Camera.x >> 4;
		int ystart = Camera.y >> 4;
		// 
		int xfinal = xstart + (Game.WIDTH >> 4);
		int yfinal = ystart + (Game.HEIGHT >> 4);

		for (int xx = xstart; xx <= xfinal; xx++) {
			for (int yy = ystart; yy <= yfinal; yy++) {

				// vamos usar um if caso der valores negativos em algumas das direçoes
				if (xx < 0 || yy < 0 || xx >= WIDTH || yy >= HEIGHT) {
					continue;
				}

				Taile taile = tiles[xx + (yy * WIDTH)];
				taile.render(g);

			}
RoinujNosde
return !((tiles[x1 +(y1 *Word.WIDTH)] instanceof WallTile) || (tiles[x2 +(y2* Word.WIDTH)] instanceof WallTile) || (tiles[x3 +(y3 *Word.WIDTH)] instanceof WallTile) || (tiles[x4 +(y4* Word.WIDTH)] instanceof WallTile));
Criado 12 de abril de 2021
Ultima resposta 14 de abr. de 2021
Respostas 7
Participantes 2