290 likes | 398 Views
Is this thing on?. Ten Interview Questions for Game Programmers. Marc LeBlanc April 2004. #1: 2D Point-in-Triangle. V 1. Given: struct Point { float x,y; }; Write: bool IsPointInTriangle(Point p, Point v1, Point v2, Point v3). P. V 3. V 2. #1: 2D Point-in-Triangle.
E N D
Ten Interview Questions for Game Programmers Marc LeBlanc April 2004
#1: 2D Point-in-Triangle V1 Given: struct Point { float x,y; }; Write: bool IsPointInTriangle(Point p, Point v1, Point v2, Point v3) P V3 V2
#1: 2D Point-in-Triangle Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3) V1 V3 V2
#1: 2D Point-in-Triangle Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3) { return (v3 – v1) (v2 – v1) > 0; } V1 V3 V2
#2: Sphere vs. Sphere Given: bool LineIntersectsSphere(Point3D p0, Point3D p1, Point3D C, float r); P1 P0 R C
#2: Sphere vs. Sphere Write: bool SphereCollidesWithSphere(Point3D a0, Point3D a1, float ar, Point3D b0, Point3D b1, float rb); Ra A0 B1 B0 A1 Rb
Step 1: One Moving Sphere Ra A0 B1 B0 Rb
Step 1: One Moving Sphere Ra + Rb A0 B1 B0
Step 2: Two Moving Spheres Ra A0 B1 B0 A1 Rb
Step 2: Two Moving Spheres B1 Ra A0 B0 Rb
#3: Shuffle a Deck Given: int rand(); Write: void ShuffleDeckInPlace(Card* deck, int nCards); • What are the requirements?
#3: Shuffle a Deck Given: int rand(); Write: void ShuffleDeckInPlace(Card* deck, int nCards) { for (int i = nCards; i > 0; i--) Swap(nCards[i-1],nCards[rand()%i]); }
#4: Select from Stream Given: class ElementStream { bool GetNextElement(Element* pElementOut); }; Write: Element SelectRandomElement(ElementStream* pStream); • Each element is equally likely. • Use constant space.
#4: Select from Stream Element SelectRandomElement(ElementStream* pStream) { Element winner = Element(); Element next; int nSeen = 0; while (pStream->GetNextElement(&next)) { nSeen++; if (rand() % nSeen == 0) winner = next; } return winner; }
#5: Memory Pool Write a “heap” that manages the memory for objects of fixed size. • Cope with the console environment • No operating system • No heap • Solution • Active and free lists • Sentinels
#6: The Cows Come Home Given a tilemap with cows, barns & obstacles:
#6: The Cows Come Home • Cows get teleported out to pasture. • They come home to the nearest barn. • They come home a lot. • Sometimes barns & obstacles get created or destroyed, but not very often. Implement pathfinding for cows.
#6: The Cows Come Home • Precompute a pathing database. • Flood-fill each tile with: • Pointer to next tile on path, or • Scalar value for gradient descent
#5: Silver Spaceship Let M be a 4x4 matrix that describes the position and orientation of a spaceship. What is the fastest way to mutate M to move the spaceship 10 meters forward?
… … … 0 … … … 0 M = fx fy fz 0 tx ty tz 1 Notational Conventions • In the spaceship’s frame of reference, z is forward. • M is the body-to-world transform.
… … … 0 … … … 0 M = fx fy fz 0 tx ty tz 1 Solution M[row 3] += 10 * METER * M[row 2];
#8: Pong AI How would you implement an AI to stand in place of a human player in a 2-player pong game? • Do you understand the requirements? • Smart is easy. • Fun is hard. • Do you know the difference?
#9: 3D Click In a 3D scene…
#9: 3D Click What object did the user click on?
#9: 3D Click Givens: • Transforms for view and camera • For each object: • Body-space dimensions • World-to-body transform
Three Strategies • Transform objects to screen space bounding rects, test against mouse. • Doesn’t deal with occlusion well. • Transform mouse to ray, test ray vs. boxes. • Could make good use of culling database • Render an off-screen “paint-by-numbers” image and search. • Old school, hard to use hardware.
#10: Extra Credit Write Spherical Linear Interpolate for Quaternions. Quaternion Slerp(Quaternion q1, Quaternion q2, float t); • You should know it. • You probably won’t need to on an interview.