Handmade Hero » Forums » Code » Day 75: Has anyone had problems with the stairs?
Huntress77
7 posts
#13871 Day 75: Has anyone had problems with the stairs?
3 weeks, 1 day ago

I've been coding along with the archives, and I must have done something wrong because my avatar is on the wrong Z-plane when it emerges from the top of the stairs. Instead of going up 3 meters, it goes up 3 meters + the 1.5 meter offset passed to the stairs.

That was leading to no collisions with the "upper floor" walls because I wasn't checking the correct Z-plane for collisions. I was able to "fix" that problem by adding 1.5 meters to the Z in my collision code, but I'd much rather fix the stairs than leave it that way.

In case you're still vague on what I'm talking about, here's what my Z coordinate collision check looks like now:
1
if ((Rel.Z + (0.5 * World->TileDepthInMeters) >= MinCorner.Z)
1
&& (Rel.Z + (0.5 * World->TileDepthInMeters) < MaxCorner.Z))


That's where my avatar goes when I climb the stairs; Rel.Z plus half of the tile depth in meters. It's absolutely driving me batty. When I step through, I can see the little guy suddenly jump up by the offset, then move the rest of the way in the expected increments, but I can't pinpoint what's causing him to do that.

I'll paste in all the code I think might be relevant beneath this, but let me know if you need something I haven't included.
Huntress77
7 posts
#13872 Day 75: Has anyone had problems with the stairs?
3 weeks, 1 day ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
internal add_low_entity_result
AddStair(game_state *GameState, uint32 AbsTileX, uint32 AbsTileY, uint32 AbsTileZ)
{
	v3 Dim = { GameState->World->TileSideInMeters,
				2.0f * GameState->World->TileSideInMeters,
				GameState->World->TileDepthInMeters };
	world_position P = ChunkPositionFromTilePosition(GameState->World, AbsTileX, AbsTileY, AbsTileZ);
	add_low_entity_result Entity = AddGroundedEntity(GameState, EntityType_Stairwell, P, Dim);	

	AddFlags(&Entity.Low->Sim, EntityFlag_Collides);

	return(Entity);
}


1
2
3
4
5
6
7
8
9
internal add_low_entity_result
AddGroundedEntity(game_state *GameState, entity_type Type, world_position P, v3 Dim)
{
	world_position OffsetP = MapIntoChunkSpace(GameState->World, P, V3(0, 0, 0.5f * Dim.Z));
	add_low_entity_result Entity = AddLowEntity(GameState, Type, OffsetP);
	Entity.Low->Sim.Dim = Dim;

	return(Entity);
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
internal void
HandleOverlap(game_state *GameState, sim_entity *Mover, sim_entity *Region, real32 dt, real32 *Ground)
{
 if (Region->Type == EntityType_Stairwell)
	{
		rectangle3 RegionRect = RectCenterDim(Region->P, Region->Dim);
		v3 Bary = Clamp01(GetBarycentric(RegionRect, Mover->P));

		*Ground = Lerp(RegionRect.Min.Z, Bary.Y, RegionRect.Max.Z);
	}
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
real32 Ground = 0.0f;
		
	{
		rectangle3 EntityRect = RectCenterDim(Entity->P, Entity->Dim);
		for (uint32 TestHighEntityIndex = 0;
			TestHighEntityIndex < SimRegion->EntityCount;
			++TestHighEntityIndex)
		{
			sim_entity *TestEntity = SimRegion->Entities + TestHighEntityIndex;
			if (CanOverlap(GameState, Entity, TestEntity))
			{
				rectangle3 TestEntityRect = RectCenterDim(TestEntity->P, TestEntity->Dim);
				if (RectanglesIntersect(EntityRect, TestEntityRect))
				{
					HandleOverlap(GameState, Entity, TestEntity, dt, &Ground);
				}
			}

		}
	}
Pacifist
5 posts
#13873 Day 75: Has anyone had problems with the stairs?
3 weeks, 1 day ago

What I've often found helpful when debugging code I've written following along with HMH (and I truly cannot find the problem myself after several hours of trying) is to compile a copy of that day's HMH source code and see if that contains the same problem. If yes, then you can rest assured that Casey will most likely solve that bug in a later episode.

If it doesn't have the same bug, that its helpful to have your code and Casey's code side by side as you step through. You could even comment out your own code and transplant Casey's in its place the see if that solves the issue, then work backwards to figure out what you did wrong.

I find this really helps narrow down where the problem area is and sometimes (when Casey's transplanted code doesn't fix the bug) I discover that it was my conception of what the bug was that was holding me back and the problem is really somewhere else in the codebase that I wasn't looking in.