Problem
Making virtual characters look believable in real time is never one algorithm. A character that walks smoothly still clips through walls without collision detection; a character that avoids obstacles still looks robotic if its legs skate across the floor; a crowd that follows shortest paths still jams at doorways without physical and psychological spacing; and an interactive scene still feels lifeless if agents cannot coordinate roles, react to a human player, and reach for objects in the room.
Over one graduate semester in Rutgers CS-523, our team of three (Dhaivat Patel, Chaitanya Vallabhaneni, and Kavit Shah) built this stack from the ground up in Unity and C# across four progressive repositories: B2-Collision, B3-Character-Animation, B4-SocialForces, and B5_Behavior_Trees. Instead of treating them as four disconnected coursework folders, this Deep dive brings the full progression together into one simulation pipeline.
Architecture
Every stage runs on Unity’s C# runtime (Rigidbody, NavMesh, Animator/Mecanim, and FinalIK with TreeSharpPlus in the final stage) and adds one higher layer of physical or behavioral agency:
- Collision (
01, B2). A depth-5 spatialQuadTreepartitions the XZ plane into cells so only prisms sharing a leaf become candidate pairs, deduplicated with a single integer key (bigger * prismCount + smaller). Each candidate pair runs through a GJK simplex test in Minkowski difference space; disjoint pairs exit early without resolution, while overlapping prisms run an Expanding Polytope Algorithm (EPA) loop to compute the minimum translation vector (penetrationDepthVectorAB) and separate cleanly. - Animation and navigation (
02, B3). Unity’sNavMeshAgentcomputes steering paths, whileLocomotionSimpleAgentdisables direct transform updates (agt.updatePosition = false) and projects the path delta into local forward/right velocity parameters (velx,vely) for a Mecanim 2D blend tree. WhenSamplePathPositionhits an off-mesh jump area (h.mask & 4), it branches intoJump4Blendautomatically while aLookAtcomponent steers head gaze towardsteeringTarget. - Social forces (
03, B4). Crowd agents combine NavMesh waypoints (recomputed every 25 frames) with Helbing’s social-forces physics onRigidbodybodies (80 kgmass,3 mperception sphere). Each step sums goal attraction, exponential neighbor repulsion, physical compression, tangential sliding friction, wall repulsion, and optional leader-following, clamped tomaxSpeed. Once an agent clears its final waypoint, it deactivates and leaves the simulation. - Behavior trees (
04, B5).ProjBehaviorTreeorchestrates three autonomous NPCs and a player-controlled character usingTreeSharpPlusandFinalIK. Parallel sequences bring the three NPCs together, a customSelectorShuffleBiasednode assigns roles (assigner, light switcher, TV switcher), and FinalIK effectors drive hand-switch reaches and sofa seating. ADecoratorLoopwithLeafAssertwatchesbulb.enabled: if the human player toggles the light more than three times, the loop branches into an angry ending.
Approach
Collision
Checking every pair of convex prisms against every other prism scales quadratically. In B2-Collision, I built a two-phase pipeline inside PrismManager.cs and QuadTree.cs. Each update rebuilds a depth-5 QuadTree over the spawn region, registers every regular and irregular prism’s XZ bounding box, and emits deduplicated PrismCollision pairs. For each pair, CheckCollision iteratively queries MinkowskiSupport to build a simplex around the origin (GJK). If the dot product against the search direction is non-positive, the prisms are guaranteed disjoint. Otherwise, EPA expands the closest edge to the origin until the distance improvement drops below 0.5, producing the exact separation vector.
Animation and navigation
Coupling a NavMeshAgent directly to a humanoid character causes foot sliding because the agent slides at constant speed regardless of stride length. In B3-Character-Animation, I separated pathfinding from body placement by turning off agt.updatePosition and feeding the smoothed local delta into the Mecanim Animator, letting OnAnimatorMove reconcile the transform with agt.nextPosition:
Vector3 wdpos = agt.nextPosition - transform.position;
float dypos = Vector3.Dot(transform.forward, wdpos);
float dxpos = Vector3.Dot(transform.right, wdpos);
Vector2 dpos = new Vector2(dxpos, dypos);
float s = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
sdpos = Vector2.Lerp(sdpos, dpos, s);
if (Time.deltaTime > 1e-5f) vel = sdpos / Time.deltaTime;
bool smove = vel.magnitude > 0.5f && agt.remainingDistance > agt.radius;
anim.SetBool("move", smove);
anim.SetFloat("velx", vel.x);
anim.SetFloat("vely", vel.y <= 0.5f ? vel.y : (Input.GetKey(KeyCode.LeftShift) ? 1.5f : 1f));
On top of single-character WASD locomotion (Part 1) and click-to-move with automatic obstacle jumping and head look-at (Part 2), I extended the director and ClickToMove scripts to a 10-agent room evacuation scene (Part 3) where any individual agent can be clicked and redirected mid-evacuation.
Social forces
NavMesh paths alone tell agents where to go, not how to squeeze through a doorway without overlapping. In B4-SocialForces, we modeled each agent as an 80 kg Rigidbody with a trigger sphere (perceptionRadius = 3) tracking nearby neighbors in a HashSet<GameObject>. Teammate Kavit Shah led the core Agent.cs force integration and write-up while I worked on wall setup, UI controls, and the Part 2 behaviors (spiral, wall-following, and WASD leader-following). Every tick combines psychological repulsion (A * exp(-d / B)), body compression (k * overlap), tangential sliding friction (Kappa), and wall contact impulses so crowds naturally form lanes and arc around congestion.
Behavior trees
The final stage, B5_Behavior_Trees, turns steering and animation into an interactive living-room story. Using TreeSharpPlus and FinalIK, three NPCs walk to a meeting spot in parallel, randomly elect an assigner via our custom SelectorShuffleBiased node (40% bias toward the first child), point with speech bubbles, and split up to flip the light switch and TV switch using hand IK effectors before sitting on the sofa with hip IK effectors. I built the PlayerController, speech-bubble UI, and initial ProjBehaviorTree setup, while teammate Chaitanya Vallabhaneni contributed scene interactions and additional tree updates. Walking near NPCs triggers greetings; flipping the light switch forces the assigned NPC off the sofa to turn it back off, and interrupting them more than three times switches the story into an angry ending.
Results
Across the four repositories, every stage runs interactively in real time in Unity:
- Collision (B2) detects and separates overlapping regular and irregular prisms using a depth-5 QuadTree, GJK, and EPA without relying on Unity’s built-in collider solver.
- Animation and navigation (B3) shipped as three playable WebGL builds on itch.io: Locomotion (Part 1), NavMesh Navigation (Part 2), and 10-Agent Crowd (Part 3), linked out from the meta strip above rather than embedded so the portfolio stays fast on mobile.
- Social forces (B4) simulates multi-agent crowd navigation, wall repulsion, sliding friction, and interactive leader-following across maze scenes.
- Behavior trees (B5) delivers two distinct interactive endings (friendly greetings vs. the angry light-switcher after more than three player interruptions) driven by live IK affordances and a continuous
DecoratorLoop.
What I learned
- Broad phase and narrow phase solve different bottlenecks. Building the QuadTree alongside GJK/EPA made it concrete why game engines split spatial partitioning from exact geometry tests: pruning pairs cheaply matters more than micro-optimizing a simplex loop.
- Decoupling simulation state from animation prevents skating. Letting
NavMeshAgentown the planned path whileAnimatorowns the stride, bridged by a smoothed local-space velocity vector, was the single cleanest architectural seam of the semester. - Force tuning is sensitive to scale and clamping. In social-force crowds, exponential repulsion (
A = 2000,B = 0.08) can launch agents across the scene if two bodies overlap deeply; clamping net force tomaxSpeedand amortizing NavMesh replanning every 25 frames kept the simulation stable. - Behavior trees shine when worlds are interruptible. Hard-coding a cutscene breaks the moment a human player flips a light switch; wrapping
LeafAssert(bulb.enabled)inside aDecoratorLooplet the NPC react naturally, leave the sofa, and escalate after three interruptions without a tangle of boolean flags.
