Game Development Community

FxFoliageReplicator Problem

by Joe Melton · in Torque Game Engine · 09/18/2004 (1:00 pm) · 16 replies

I have a "blank" mission that I use when I want to make new missions. Basically, it's a bare-bones mission, with nothing but terrain, sky, and a spawn sphere. The terrain is flat, at zero elevation, and has a single texture applied.

Anyway, I ran into an issue with the fxFoliageReplicator. I added it to the scene, configured it, and clicked Apply. Nothing happened. The placement area "circle" was visible, but no foliage was visible. I tried it on one of the default missions, and it worked.

Today I came back to it and decided to see what the problem was. I went into the settings and started changing a couple things here and there. When I checked the box for "AllowOnInteriors" and clicked Apply, the foliage appeared. I thought this was strange, because there are no interiors in the scene. I then added an interior to the scene. I unchecked the "AllowOnInteriors" box and clicked Apply. All of the foliage disappeared. I expected it to remain on the terrain, but it did not.

I decided to do some further testing. I added some elevation to a small section of the terrain. I then clicked Apply on the fxFoliageReplicator again. The foliage appeared. I set the terrain elevation back to zero and again applied the fxFoliageReplicator settings. The foliage disappeared. I checked the box for "AllowOnInteriors" and the foliage reappeared.

I think the fxFoliageReplicator doesn't like totally flat terrain at zero elevation for some reason. I don't know if anybody else has run into this, but I just thought I'd comment on it.

I'm using HEAD from September 13.

#1
09/19/2004 (2:03 am)
Joe,

Below is the snippet of code that does the actual positioning of the foliage on the client. I won't be able to look at this for a few days but if anyone else can see a reason why it would exhibit this problem then please jump in. The only placed for a potential problem that I can see initially is that the rayCast is only within the -2000~+2000 Z and that the system ignores terrain angles above a certain threshold, neither of which should cause the problem you're seeing.

- Melv.

// Find it a home ...
		do
		{
			// Get the fxFoliageReplicator Position.
			FoliagePosition = getPosition();

			// Calculate a random offset
			HypX	= RandomGen.randF(mFieldData.mInnerRadiusX, mFieldData.mOuterRadiusX);
			HypY	= RandomGen.randF(mFieldData.mInnerRadiusY, mFieldData.mOuterRadiusY);
			Angle	= RandomGen.randF(0, M_2PI);

			// Calcualte the new position.
			FoliagePosition.x += HypX * mCos(Angle);
			FoliagePosition.y += HypY * mSin(Angle);

			// Initialise RayCast Search Start/End Positions.
			FoliageStart = FoliageEnd = FoliagePosition;
			FoliageStart.z = 2000.f;
			FoliageEnd.z= -2000.f;

			// Perform Ray Cast Collision on Client.
			CollisionResult = gClientContainer.castRay(	FoliageStart, FoliageEnd, FXFOLIAGEREPLICATOR_COLLISION_MASK, &RayEvent);

			// Did we hit anything?
			if (CollisionResult)
			{
				// For now, let's pretend we didn't get a collision.
				CollisionResult = false;

				// Yes, so get it's type.
				U32 CollisionType = RayEvent.object->getTypeMask();

				// Check Illegal Placements, fail if we hit a disallowed type.
				if (((CollisionType & TerrainObjectType) && !mFieldData.mAllowOnTerrain)	||
					((CollisionType & InteriorObjectType) && !mFieldData.mAllowOnInteriors)	||
					((CollisionType & StaticTSObjectType) && !mFieldData.mAllowStatics)	||
					((CollisionType & WaterObjectType) && !mFieldData.mAllowOnWater) ) continue;

				// If we collided with water and are not allowing on the water surface then let's find the
				// terrain underneath and pass this on as the original collision else fail.
				if ((CollisionType & WaterObjectType) && !mFieldData.mAllowWaterSurface &&
					!gClientContainer.castRay( FoliageStart, FoliageEnd, FXFOLIAGEREPLICATOR_NOWATER_COLLISION_MASK, &RayEvent)) continue;

				// We passed with flying colour so carry on.
				CollisionResult = true;
			}

			// Invalidate if we are below Allowed Terrain Angle.
			if (RayEvent.normal.z < mSin(mDegToRad(90.0f-mFieldData.mAllowedTerrainSlope))) CollisionResult = false;

		// Wait until we get a collision.
		} while(!CollisionResult && --RelocationRetry);
#2
09/19/2004 (3:03 am)
I've been getting similar problems, but not with flat terrain. I've had the issues with the interiors thing he mentions. Its very odd.

I've been forced to just keep playing around until it magically works ;)
#3
09/19/2004 (10:40 am)
Thanks Melv. I'll try to see if I can figure it out. If not, I'll wait anxiously for your update. :)

Looks like mine is a bit different than yours:

// Calculate a random offset
			HypX	= RandomGen.randF(mFieldData.mInnerRadiusX, mFieldData.mOuterRadiusX);
			HypY	= RandomGen.randF(mFieldData.mInnerRadiusY, mFieldData.mOuterRadiusY);
			Angle	= RandomGen.randF(0, M_2PI);

			// Calculate the new position.
			Point3F randomShapePosLocal;
			randomShapePosLocal.x = HypX * mCos(Angle);
			randomShapePosLocal.y = HypY * mSin(Angle);

			// Transform into world space coordinates
			Point3F shapePosWorld;
			MatrixF objToWorld = getRenderTransform();
			objToWorld.mulP(randomShapePosLocal, &shapePosWorld);
			FoliagePosition = shapePosWorld;

			// Initialise RayCast Search Start/End Positions.
			FoliageStart = FoliageEnd = FoliagePosition;
			FoliageStart.z = 2000.f;
			FoliageEnd.z= -2000.f;
#4
09/19/2004 (11:53 am)
Well that made no difference.

So I tried some other things. Turning off culling makes it work like it should. If I turn on culling, all of the foliage disappears.

My culling settings are:

UseCulling = ON
CullResolution = 64
ViewDistance = 150
ViewClosest = 1
FadeInRegion = 10
FadeOutRegion = 1
AlphaCutoff = 0.2
GroundAlpha = 1

I have 5000 foliage objects, inner X,Y radius of 0, outer X,Y radius of 128.

So here's the next test:

I added some elevation to a small section of terrain (because it was all flat at elevation zero). I turned culling back on. The foliage reappeared.

I then set the terrain height back to zero. I re-applied the fxFoliageReplicator and the foliage disappeared.

I turned culling off, and the foliage reappeared.

So basically, it appears that culling doesn't work if the terrain is at a set height of zero. I'll try it for other set heights.

Next, I set the height to 100, but just in the area covered by the fxFoliageReplicator. I turned on culling and hit apply. No foliage. Turned off culling, and the foliage appeared.

I added a bit of elevation inside the area, turned on culling, and hit apply. Foliage appeared.

My conclusion is that the fxFoliageReplicator will not display foliage if culling is turned on and the area covered by the fxFoliageReplicator is completely flat. When using culling and completely flat terrain, even the quads disappear if UseDebugInfo is checked.

Hope that helps some. I wouldn't know the first part about how to fix it, although I'll probably dig through the code now...
#5
09/19/2004 (1:32 pm)
Joe,

As I say, I haven't really got much time to look at this so perhaps you could try something for me?

Set the object-box (size) for the replicator to something really big (normally this box is not used); specifically, one that extends way above/below the flat-terrain you're trying to get the replicator to place stuff on. I had a quick look and it seems that there may be an issue with the quadrant checking in the culling routine.

If setting the size of the replicator to something big e.g. something that intersects the region you're placing on solves the issue then I know exactly what it is else I'll still have to look into in later in the week.

- Melv.
#6
09/19/2004 (2:08 pm)
I'll check that right now.

Yeah, that worked. As I scaled the box on the z-axis, the foliage gradually appeared. It began appearing when the z-scale was approximately 1.5. As I scaled it higher, more foliage appeared.
#7
09/20/2004 (1:31 am)
Joe,

I cannot replicate this problem (sorry for the pun)! To ensure that it's fixed correctly, I'd like to duplicate your problem.

Perhaps you should send me your mission/terrain file so that I can check it out for you?

- Melv.
#8
09/21/2004 (9:03 am)
Joe,

Got your email this evening and spent a little time looking at the problem.

The good news is that I've fixed it so if you download the "fxFoliageReplicator.cc" (Rev 2.3) from the CVS [HEAD], it should resolve your issue.

Please let me know whether it does in fact solve it for all configurations.

All the best,

- Melv.
#9
09/21/2004 (10:10 am)
Yea, I've had some experience with this. I managed to characterize the situation quite well. Essentially, for foliage to appear in any given area of your circle/elipse the 'z' elevation must be above your terrain AND below your terrain's maximum elevation point. Also, if you have sloping terrain and move your area to intersect with it, you will notice that the trees will not go in any location where the top of your tree is above the z elevation of the circle, or in other words, if the tree won't fit between the terrain and the bottom of your circle plane, it won't draw. Simple solution is to place the elevation of your circle as high as possible without going above the highest terrain point. You will not be able to place trees at the topmost sections of your terrain because of this limitation, however. And the flatter your terrain, the worse off you are.

For the longest time I thought this was a culling problem, but once I moved the circle up high the problem was hacked. I have not taken time to pour through the code myself but I'm sure the solution is fairly simple.
#10
09/21/2004 (10:12 am)
Shannon,

See my post above!

;)

- Melv.
#11
09/21/2004 (10:16 am)
Thanks, will give it a whirl.
#12
09/21/2004 (10:27 am)
Cool; let me know how you get on. :)

- Melv.
#13
09/21/2004 (11:01 am)
Great work Melv, thanks.
#14
09/21/2004 (1:42 pm)
Tis no problemo. 8)

- Melv.
#15
09/21/2004 (6:11 pm)
Thanks Melv. I'll download it now.
#16
09/21/2004 (6:29 pm)
Looks like that did the trick. Thanks a lot Melv!