Skip to main content
GameDev.net gamedev.net
🔒 Locked

Objects jumping on height mesh

Started by theZapper Jan 14, 2005 at 11:00 AM 2 replies 700+ views
Original Post
theZapper
theZapper
I recently posted about finding the height at a particular point on a triangle mesh landscape. Thanks to Roos I put the plane equation to work and it does work most of the time. However, my objects will move over the landscape following the surface closly but every now and then they jump up in the y direction for no apparent reason. What's more annoying is that they only jump a small distance, so it's not a divide by zero problem which would be easier to find. I know the objects are on the correct triangle and I know that I am getting the correct surrounding vertices and am creating the vectors correctly to define the plane, so maybe somthing wrong with the plane maths? this is how i rearrainged it Ax + By + Cz - d = 0 Ax + By + Cz = d By = d - Ax - Cz y = (d - Ax - Cz) / B and here's the code I use: d = point * norm; // * is dot product, point is a corner of the tri newy = (d - (norm.x * x) - (norm.z * z)) / norm.y; // is this right? Please help! Thanks
---When I'm in command, every mission's a suicide mission!
WebsiteWill
WebsiteWill
Not sure if this would help or not but, if you track your character's absolute position, each frame you could check his most current two positions to see the range of normal movement. Then you put a breakpoint in your code (or a message box) that will fire if your character moves more then that average amount in a single frame.

So in a normal frame, y coordinate changes by ~1, if this frame the change in y is > y by some arbritraty amount, fire a messagebox.

This way you can run around till it happens and when it does you can then evaluate the state of the game to see why it happened.

Not sure it will help or not as I havne't actually implemented anything like this but it would be my first idea if I was in your position.

Webby
ajas95
ajas95
Your math looks fine. The 2 things I can think of: first is it a height-field type mesh -- where you essentially have quads that you divide along a diagonal to get 2 tris? It could be that as the character walks across the diagonal, you select vertexes of the new triangle such that the diagonal switches... this is a not uncommon typo.

And 2) (assuming heightfield again), if you don't switch triangles at all when he is on a quad... just use v0, v1, v2 to compute the plane and ignore v3, for example, that will leave you with jumps at the edges if the 4 pts are non-coplanar.

If you are using an arbitrary mesh, then you should make sure that your triangle detection code is working 100%.
roos
roos
You too, huh? I had a lot of trouble getting those "jumps" out of my last game. However, I'm pretty sure the problems I had were not related to the plane math code that I pasted in your other post. Although of course you should double-check if you think that code might be the suspect, but I'm *fairly* sure it works fine because (a) it's used in my last game and that game works perfectly, and (b) it's a pretty short section of code that I more or less ripped from a math book.

If the plane math code seems correct, here are some other things to consider:

-If you are making a game where the camera follows the terrain based on its slope, such as a racing game, then the problem might not be the objects jumping up and down, but rather the camera changing angle in a discontinuous way. In this case, a cheap way to solve it (which I used for my game) is simply to take a bunch of interpolated height samples along a line extending in the direction of movement and average them...

-Are you 100% sure you're creating the triangles correctly? It can be a bit tricky, I've been there myself, but I'd definitely check it...

Those are pretty much the 2 things I can think of...

If you might happen to have a copy of "3D Engine Design" by David Eberly, then check out page 397, it has some info on height calculations...

roos


EDIT: I don't know if this will help, but anyways here's my code that gets the height of a point on a terrain that you might be able to compare yours to. Sorry if it's a bit long and messy :)


//-------------------------------------------------------------------------------------------------//  GetHeightAtPosition: with the given position in world coordinates in the XZ plane,//                       find the height of the terrain there//-------------------------------------------------------------------------------------------------_float P_Terrain::GetHeightAtPosition( _float posX, _float posZ ){	// convert world coordinates into a percent relative to terrain bounds	_float pctX = .5f + (posX - m_Origin.x) / m_XSize;	_float pctY = .5f + (posZ - m_Origin.z) / m_ZSize;	// the point is outside the terrain!	if( pctX > 1.0f || pctX < 0.0f || pctY > 1.0f || pctY < 0.0f )		return 0.0f;	// get indices into the height map values of the quad this point is inside	_long x1 = (_long) (pctX * (m_MapWidth-1));	_long x2 = x1 + 1;	_long y1 = (_long) (pctY * (m_MapHeight-1));	_long y2 = y1 + 1;	_long w = m_MapWidth;	_long h = m_MapHeight;	// get the heights at four corners of the quad	_float heightUL = (_float) m_pHeightMap[x1 + y2 * w];  // note, bigger "y" index into heightmap corresponds	_float heightUR = (_float) m_pHeightMap[x2 + y2 * w];  // to higher values of Z on the terrain	_float heightDL = (_float) m_pHeightMap[x1 + y1 * w];	_float heightDR = (_float) m_pHeightMap[x2 + y1 * w];	// fractional offsets relative to the terrain tile we're on	_float dx = (pctX * (m_MapWidth-1)) - x1;	_float dy = (pctY * (m_MapHeight-1)) - y1;	// to get the height of a point, first we have to know which triangle that point lies in	_long triangle;   // keep track of which triangle we are dealing with        // use equation of the diagonal, x + y = 1 to determine which tri we're in	if( dx + dy > 1.0f )      // point is in "upper-right" triangle		triangle = 0;	else                      // point is in "lower-left" triangle		triangle = 1;	// find the coordinates of the tile we're on (X,Y) - (Xpp,Ypp)	_float X, Y, Xpp, Ypp;	X = m_Origin.x + ((_float)x1 / (m_MapWidth-1) - .5f) * m_XSize;	Y = m_Origin.z + ((_float)y1 / (m_MapHeight-1) - .5f) * m_ZSize;	Xpp = m_Origin.x + ((_float)x2 / (m_MapWidth-1) - .5f) * m_XSize;	Ypp = m_Origin.z + ((_float)y2 / (m_MapHeight-1) - .5f) * m_ZSize;	// find the 4 points of this terrain quad	P_Vector3 A,B,C,D;	A = P_Vector3( X, heightUL, Ypp );	B = P_Vector3( Xpp, heightUR, Ypp );	C = P_Vector3( X, heightDL, Y );	D = P_Vector3( Xpp, heightDR, Y );	_float y;	if( triangle == 0 )  // upper right triangle	{		P_Triangle3D tri( A,B,D );		y = tri.GetHeightAtPoint( P_Point2Df( posX, posZ ) );	}	else                 // lower left triangle	{		P_Triangle3D tri( A,C,D );		y = tri.GetHeightAtPoint( P_Point2Df( posX, posZ ) );	}	return y;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.