Archived

This topic is now archived and is closed to further replies.

ms291052

Terrain Intersections

Recommended Posts

I have built a BMP-based height map terrain program, and am working on an engine. Right now I''m trying to combine the two, to allow my mesh-based worlds in my engine to include terrains. So far what I''ve done is exported the terrain as a .x files, then, I''m setting the terrain as part of my 3D world, located in a separte .x file (which I''d like to combine, so how, if possible). But my main problem is, in my engine, I can''t use a pretty GetHeightAtPoint(float x, float z) function, because not everything''s convex, as I can have multiple floors, or platforms at the same x and z, but different y coordinates. I can''t seem to figure out, however, how to make the player go over the terrain without my nice GetHeightAtPoint. So far here''s what I have:
        D3DXVECTOR3 pos;
	DWORD count = 0;
	float vmag = MagnitudeSq(g_Player.GetVelocity());
	D3DXVECTOR3 pos;
	if(vmag == 0)
		pos = g_Player.GetPosition();
	else
		pos = (g_Player.GetPosition() + (g_Player.GetVelocity()/sqrtf(vmag))*g_Player.radius);

	if(!PointInRoom(pos))
	{
		if(PointInRoom(pos + D3DXVECTOR3(0.0f, g_Player.radius/2.0f, 0.0f)))
		{
			D3DXVECTOR3 newpos = pos + D3DXVECTOR3(0.0f, g_Player.radius/2.0f, 0.0f);
			float fDist;
			D3DXIntersect(g_Room.Mesh.m_pMesh, &newpos, &D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0, 0, 0, 0, &fDist, 0, 0);
			if(fDist < g_Player.radius)
				goto Bypass;
			else
				newpos += D3DXVECTOR3(0.0f, fDist - g_Player.radius, 0.0f);
			g_Player.SetPosition(newpos);
			g_Player.SetVelocity(Origin);
			return;
		}
Bypass:
		D3DXVECTOR3 temp = g_Player.GetVelocity();
		float fDist;
		D3DXIntersect(g_Room.Mesh.m_pMesh, &pos, &g_Player.GetVelocity(), 0, 0, 0, 0, &fDist, 0, 0);
		if(fDist > g_Player.radius)
			g_Player.SetPosition(g_Player.GetPosition() + (Normalize(g_Player.GetVelocity())*(fDist-g_Player.radius)));
		g_Player.SetVelocity(Origin);
	}
}
assuming that g_Player is my player object, g_Room, is the world loaded (with Mesh being a subobject of my mesh wrapper), MagnitudeSq returns the magnitude squared of a vector, and that PointInRoom is a working function that returns a bool. I''d love to hear from anyone who has any ideas.

Share this post


Link to post
Share on other sites
Some sort of flagging system controlled by point activations.
For instance, say you are running around on your outside terrain. During this time, player->WhichTerrain() would return TERRAIN. Then your player goes inside a castle and in doing so passes a trigger object that sets his player->WhichTerrain() to CastleFloor001.

Now you can use which terrain he is on to figure out his y dimension.

Or, if you are using some sort of precomputer quad/oct tree like structure then you can assign certain values to certain nodes then
player->GetNodeType()
if(NodeType == TERRAIN)
getYfromTerrainMap
else if(NodeType == Castle001)
getYfromCastle001Mesh


Seems like it might work but others may have better ideas.
Webby

Share this post


Link to post
Share on other sites