Fast ray-heightmap intersection problem

Started by
4 comments, last by ferrous 10 years, 3 months ago

Can't get intersection when starting position is over the map and it crashes when it's out.



/*
Given a point that is on a ray, move it until it is within a given heightmap's borders.
*/
void MoveIntoMap(Vec3f& point, Vec3f ray, Heightmap* hmap)
{
	// Already within map?
	if(point.x >= 1 
		&& point.x < hmap->m_widthx * TILE_SIZE - 1
		&& point.z >= 1
		&& point.z < hmap->m_widthz * TILE_SIZE - 1)
		return;

	// Get x distance off the map.

	float xdif = 0;

	if(point.x < 0)	// If start x is behind the map
		xdif = point.x - 5;	// Add padding to make sure we're within the map
	else if(point.x > hmap->m_widthx * TILE_SIZE)	// If start x is in front of the map
		xdif = hmap->m_widthx * TILE_SIZE - point.x + 5;	// Add padding to make sure we're within the map

	// Ray is of unit length, so this gives us how much we travel along the ray to get  x to within the map border.
	float x0moveratio = -xdif / ray.x;
	point = point + ray * x0moveratio;
	
	// Get z distance off the map.

	float zdif = 0;

	if(point.z < 0)	// If start z is behind the map
		zdif = point.z - 5;	// Add padding to make sure we're within the map
	else if(point.z > hmap->m_widthz * TILE_SIZE)	// If start z is in front of the map
		zdif = hmap->m_widthz * TILE_SIZE - point.z + 5;	// Add padding to make sure we're within the map
	
	// Ray is of unit length, so this gives us how much we travel along the ray to get z to within the map border.
	float z0moveratio = -zdif / ray.z;
	point = point + ray * z0moveratio;
}

bool TileIntersect(Heightmap* hmap, Vec3f* line, int x, int z, Vec3f* intersection)
{
	Vec3f tri[3];
	const int wx = hmap->m_widthx;
	const int wz = hmap->m_widthz;
	Vec3f* v = hmap->m_vertices;

	tri[0] = v[ (z * wx + x) * 3 * 2 + 0 ];
	tri[1] = v[ (z * wx + x) * 3 * 2 + 1 ];
	tri[2] = v[ (z * wx + x) * 3 * 2 + 2 ];

	if(IntersectedPolygon(tri, line, 3, intersection))
		return true;

	tri[0] = v[ (z * wx + x) * 3 * 2 + 3 ];
	tri[1] = v[ (z * wx + x) * 3 * 2 + 4 ];
	tri[2] = v[ (z * wx + x) * 3 * 2 + 5 ];

	if(IntersectedPolygon(tri, line, 3, intersection))
		return true;

	return false;
}

bool FastMapIntersect(Heightmap* hmap, Vec3f line[2], Vec3f* intersection)
{
	// If both start and end are on one side of the map, we can't get an intersection, so return false.

	if(line[0].x < 0 && line[1].x < 0)
		return false;

	if(line[0].x >= hmap->m_widthx * TILE_SIZE && line[1].x >= hmap->m_widthx * TILE_SIZE)
		return false;

	if(line[0].z < 0 && line[1].z < 0)
		return false;
	
	if(line[0].z >= hmap->m_widthz * TILE_SIZE && line[1].z >= hmap->m_widthz * TILE_SIZE)
		return false;
	
	Vec3f ray = Normalize( line[1] - line[0] );
	float lengthsqrd = Magnitude2( line[1] - line[0] );

	MoveIntoMap(line[0], ray, hmap);	// Move the start to within the map if it isn't already.
	MoveIntoMap(line[1], ray, hmap);	// Move the end to within the map if it isn't already.

	float lengthdone = 0;

	Vec3f currpoint = line[0];

	int currtilex = currpoint.x / TILE_SIZE;
	int currtilez = currpoint.z / TILE_SIZE;

	int nexttilex = currtilex;
	int nexttilez = currtilez;
	
	// The directions in which we will move to the next tile on the x and z axis
	int tiledx = 0;
	int tiledz = 0;

	if(ray.x > 0)
		tiledx = 1;
	else if(ray.x < 0)
		tiledx = -1;

	if(ray.z > 0)
		tiledz = 1;
	else if(ray.z < 0)
		tiledz = -1;

	// Move from tile to tile along the line until,
	// testing each tile's triangles for intersection.
	while(lengthdone*lengthdone < lengthsqrd)
	{
		float xdif = fabs( currtilex * TILE_SIZE - currpoint.x );
		float zdif = fabs( currtilez * TILE_SIZE - currpoint.z );

		float xmoveratio = xdif / ray.x + 1;	// Add padding to make sure we get into the next tile
		float zmoveratio = zdif / ray.z + 1;	// Add padding to make sure we get into the next tile

		float moveratio = 0;

		// Move the smallest distance to the next tile
		if(xmoveratio < zmoveratio)
		{
			moveratio = xmoveratio;
		}
		else
		{
			moveratio = zmoveratio;
		}

		currpoint = currpoint + ray * moveratio; 

		if(TileIntersect(hmap, line, currtilex, currtilez, intersection))
			return true;

		lengthdone += moveratio;

		// Move the smallest distance to the next tile
		if(xmoveratio < zmoveratio)
		{
			currtilex = nexttilex;
			nexttilex += tiledx;
		}
		else
		{
			currtilez = nexttilez;
			nexttilez += tiledz;
		}
	}

	return false;
}

When I test each tile for intersection it works, but that's too slow, so I'm trying to get this to work.

Advertisement

Would you mind going over the algorithm in words first, in addition to a code dump. It sounds sort of like you're possibly doing a form of bresenham or wu's line algorithm?

We have a ray and we check what distance we have to travel along that ray to get to the next tile.

We don't know if the next tile is +1/-1 on the x axis or the z, all we know is whether we go +1 or -1 on the x and z axis from the signedness of the ray on those axises.

We check the distance needed to travel along the ray to get to the next tile on the x axis and the next tile on the z axis. To do that we get the world distance to the next tile on the x and z axis. (World distance = tile number * tile size). We divide the distance to the next tile on the x and z axis by the same component of the ray (normalized) to get the distance we travel along the ray. We advance to whichever tile has the lesser value.

We save the point for advancing to the next next tile.

We then divide the point x and z component (world coordinates) by tile size to get the tile coordinates and check that tile for intersection against the original line/ray.

We check the distance that we have covered so far, squared (faster), to see if we've reached the end of the line or not.

What's wu's algorithm by the way. Need the fastest algorithm.

http://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm

It's a method for drawing antialiased lines that don't start at the center of the pixels. If you think of the pixels as your tiles, it can be adapted to grids.

Fixed

[edit] Another fix


/*
Given a point that is on a ray, move it until it is within a given heightmap's borders.
Returns success (true) or failure (false).
*/
bool MoveIntoMap(Vec3f& point, Vec3f ray, Heightmap* hmap)
{
	// Already within map?
	if(point.x >= 1 
		&& point.x < (hmap->m_widthx-1) * TILE_SIZE - 1
		&& point.z >= 1
		&& point.z < (hmap->m_widthz-1) * TILE_SIZE - 1)
		return true;

	// Get x distance off the map.

	float xdif = 0;

	if(point.x < 0)	// If start x is behind the map
		xdif = point.x - 5;	// Add padding to make sure we're within the map
	else if(point.x >= (hmap->m_widthx-1) * TILE_SIZE)	// If start x is in front of the map
		xdif = (hmap->m_widthx-1) * TILE_SIZE - point.x + 5;	// Add padding to make sure we're within the map

	// Ray is of unit length, so this gives us how much we travel along the ray to get  x to within the map border.
	float x0moveratio = -xdif / ray.x;
	point = point + ray * x0moveratio;
	
	// Get z distance off the map.

	float zdif = 0;

	if(point.z < 0)	// If start z is behind the map
		zdif = point.z - 5;	// Add padding to make sure we're within the map
	else if(point.z >= (hmap->m_widthz-1) * TILE_SIZE)	// If start z is in front of the map
		zdif = (hmap->m_widthz-1) * TILE_SIZE - point.z + 5;	// Add padding to make sure we're within the map
	
	// Ray is of unit length, so this gives us how much we travel along the ray to get z to within the map border.
	float z0moveratio = -zdif / ray.z;
	point = point + ray * z0moveratio;
	
	// If we still couldn't get the point within the map 
	// (maybe the ray is outside the map, beside a corner)
	// then return false.
	if(point.x < 1 
		|| point.x >= (hmap->m_widthx-1) * TILE_SIZE - 1
		|| point.z < 1
		|| point.z >= (hmap->m_widthz-1) * TILE_SIZE - 1)
		return false;

	return true;
}

bool TileIntersect(Heightmap* hmap, Vec3f* line, int x, int z, Vec3f* intersection)
{
	Vec3f tri[3];
	const int wx = hmap->m_widthx;
	const int wz = hmap->m_widthz;
	Vec3f* v = hmap->m_vertices;

	tri[0] = v[ (z * wx + x) * 3 * 2 + 0 ];
	tri[1] = v[ (z * wx + x) * 3 * 2 + 1 ];
	tri[2] = v[ (z * wx + x) * 3 * 2 + 2 ];

	if(IntersectedPolygon(tri, line, 3, intersection))
		return true;

	tri[0] = v[ (z * wx + x) * 3 * 2 + 3 ];
	tri[1] = v[ (z * wx + x) * 3 * 2 + 4 ];
	tri[2] = v[ (z * wx + x) * 3 * 2 + 5 ];

	if(IntersectedPolygon(tri, line, 3, intersection))
		return true;

	return false;
}

bool FastMapIntersect(Heightmap* hmap, Vec3f line[2], Vec3f* intersection)
{
	// If both start and end are on one side of the map, we can't get an intersection, so return false.

	if(line[0].x < 0 && line[1].x < 0)
		return false;

	if(line[0].x >= hmap->m_widthx * TILE_SIZE && line[1].x >= hmap->m_widthx * TILE_SIZE)
		return false;

	if(line[0].z < 0 && line[1].z < 0)
		return false;
	
	if(line[0].z >= hmap->m_widthz * TILE_SIZE && line[1].z >= hmap->m_widthz * TILE_SIZE)
		return false;
	
	Vec3f ray = Normalize( line[1] - line[0] );
	float lengthsqrd = Magnitude2( line[1] - line[0] );	//length squared

	// Move the start to within the map if it isn't already.
	if(!MoveIntoMap(line[0], ray, hmap))
		return false;

	// Move the end to within the map if it isn't already.
	if(!MoveIntoMap(line[1], ray, hmap))
		return false;

	float lengthdone = 0;

	Vec3f currpoint = line[0];

	int currtilex = currpoint.x / TILE_SIZE;
	int currtilez = currpoint.z / TILE_SIZE;
	
	// The directions in which we will move to the next tile on the x and z axis
	int tiledx = 0;
	int tiledz = 0;

	if(ray.x > 0)
		tiledx = 1;
	else if(ray.x < 0)
		tiledx = -1;

	if(ray.z > 0)
		tiledz = 1;
	else if(ray.z < 0)
		tiledz = -1;

	if(tiledx == 0 && tiledz == 0)
		return false;
	
	int nexttilex = currtilex + tiledx;
	int nexttilez = currtilez + tiledz;

	// Move from tile to tile along the line until,
	// testing each tile's triangles for intersection.
	while(lengthdone*lengthdone < lengthsqrd)
	{
		float xdif = currtilex * TILE_SIZE - currpoint.x;
		float zdif = currtilez * TILE_SIZE - currpoint.z;
		
		float xmoveratio = xdif / ray.x;
		float zmoveratio = zdif / ray.z;

		// Advance to the next tile margin on the x axis
		while(xmoveratio < 0)
		{
			currtilex = nexttilex;
			nexttilex += tiledx;
			xdif = currtilex * TILE_SIZE - currpoint.x;
			xmoveratio = xdif / ray.x;
		}
		
		// Advance to the next tile margin on the zaxis
		while(zmoveratio < 0)
		{
			currtilez = nexttilez;
			nexttilez += tiledz;
			zdif = currtilez * TILE_SIZE - currpoint.z;
			zmoveratio = zdif / ray.z;
		}

		float moveratio = 0;

		// Move the smallest distance to the next tile
		if(xmoveratio < zmoveratio && xmoveratio > 0)
		{
			moveratio = xmoveratio;
		}
		else if(zmoveratio < xmoveratio && zmoveratio > 0)
		{
			moveratio = zmoveratio;
		}
		else if(xmoveratio == zmoveratio && xmoveratio > 0)
		{
			moveratio = xmoveratio;
		}
		else
		{
			return false;
		}

		//The currpoint's don't exactly follow the original ray/line,
		//so we have to check nearby tiles for intersections.
		int mintilex = currtilex;
		int mintilez = currtilez;
		int maxtilex = currtilex;
		int maxtilez = currtilez;

		//Are we close to the previous tile on the x axis?
		if( max(0, currpoint.x/TILE_SIZE-0.1f) < mintilex)
			mintilex--;

		//Are we close to the previous tile on the z axis?
		if( max(0, currpoint.z/TILE_SIZE-0.1f) < mintilez)
			mintilez--;

		//Are we close to the next tile on the x axis?
		if( min(hmap->m_widthx-1, currpoint.x/TILE_SIZE+0.1f) > maxtilex)
			maxtilex++;

		//Are we close to the next tile on the z axis?
		if( min(hmap->m_widthz-1, currpoint.z/TILE_SIZE+0.1f) > maxtilez)
			maxtilez++;

		for(int itertilex = mintilex; itertilex <= maxtilex; itertilex ++)
			for(int itertilez = mintilez; itertilez <= maxtilez; itertilez ++)
				if(TileIntersect(hmap, line, itertilex, itertilez, intersection))
					return true;

		currpoint = currpoint + ray * moveratio; 

		lengthdone += moveratio;

		// Move the smallest distance to the next tile
		if(xmoveratio < zmoveratio && xmoveratio > 0)
		{
			currtilex = nexttilex;
			nexttilex += tiledx;
		}
		else if(zmoveratio < xmoveratio && zmoveratio > 0)
		{
			currtilez = nexttilez;
			nexttilez += tiledz;
		}
		else if(xmoveratio == zmoveratio)
		{
			currtilex = nexttilex;
			currtilez = nexttilez;
			nexttilex += tiledx;
			nexttilez += tiledz;
		}
	}

	return false;
}

Heh, if anyone wants to actually see what he did to fix his issues, here is an online code differ:

http://www.tareeinternet.com/scripts/comparison-tool/#diff

This topic is closed to new replies.

Advertisement