• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
polyfrag

Fast ray-heightmap intersection problem

5 posts in this topic

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.

Edited by polyfrag
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

Edited by polyfrag
0

Share this post


Link to post
Share on other sites

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;
}
Edited by polyfrag
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0