Snow...

Hi,

Struggling with checking when my snow flakes hit the ground. Snow flakes are in screen space, thus 0,1024 is top left, 1280,1024 top right, 0,0 bottom left, 1280,0 bottom right.

My tiles are in world space.

I’ve tried:


Vector3 rainPos = new Vector3(this.x, this.y,0);
		
		camera.unproject(rainPos);
		int squareX = (int) rainPos.x;
		int squareY = (int) rainPos.y;
		squareX -= squareX % TILEWIDTH;
		squareY -= squareY % TILEHEIGHT;

		if(!this.worldMap.checkCollision(new Vector3(squareX,squareY,0))) // collision
		{
			this.y = 1024; this.x = r.nextInt(Gdx.graphics.getWidth());
			this.velocity = r.nextInt(15)+4;
		}
		else
			this.y -= velocity;
		
		render(batch, sprite);

and


public boolean checkCollision(Vector3 position) {
		Vector3 screenSpace = toScreenSpace(position);
		int x = Math.round(screenSpace.x);
		int y = Math.round(screenSpace.y);
		BlankEntity entity = getBlock(x, y);
		if (entity instanceof CaveTopEntity || entity instanceof CaveLeftEntity || entity instanceof CaveRightEntity )
			return true;
		return entity == null /* || entity.toString().equals(".") */
				|| entity instanceof CaveEntity
				|| entity instanceof WaterEntity
				|| entity instanceof LavaEntity;

	}

	/*
	 * getBlock get tile from map
	 */
	public BlankEntity getBlock(int x, int y) {
		if (x <= WorldMap.w && x >= 0 && y <= WorldMap.h && y >= 0)
			return worldMap[x][y];
		else
			return null;
	}

	private Vector3 toScreenSpace(Vector3 position) {
		Vector3 v = new Vector3(0, 0, 0);
		v.x = position.x / TILEWIDTH;
		v.y = position.y / TILEHEIGHT;
		return v;
	}


But collision not working as I feel it is not the correct coordinates as the collision does work for player/enemies etc, seems to be looking in incorrect place…

Any help is appreciated in converting local coordinates into camera space…

Thanks

I have now fixed this and put a video up showing the outcome in the WIP section, please take a look, think it looks quite nice, wind to be added later, now to get
on with adding snow of which I’m hoping to make this build up on the landscape…

This is the code now that fixed my issue:


public void move(SpriteBatch batch, Texture sprite) {
		
		float height = Gdx.graphics.getHeight();                           // Need to get height of screen as OpenGL starts at bottom 0,0 :-)
		Vector3 rainPos = new Vector3(this.x, height-this.y,0);
		
		camera.unproject(rainPos);
		int squareX = (int) rainPos.x;
		int squareY = (int) rainPos.y;
		squareX -= squareX % 16;
		squareY -= squareY % 16;

		System.out.println(squareX + "," + squareY);
		if(!this.worldMap.checkCollision(new Vector3(squareX,squareY,0))) // collision
		{
			this.y = height; this.x = r.nextInt(Gdx.graphics.getWidth());
			this.velocity = r.nextInt(15)+4;
		}
		else
			this.y -= velocity;
		
		render(batch, sprite);
		
	}