Upload
bradyn-mottram
View
220
Download
0
Embed Size (px)
Citation preview
Is this thing on?
Ten Interview Questions for Game Programmers
Marc LeBlanc
April 2004
#1: 2D Point-in-Triangle
Given: struct Point
{
float x,y;
};
Write: bool IsPointInTriangle(Point p, Point v1,
Point v2, Point v3)
P
V1
V2V3
#1: 2D Point-in-Triangle
Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3)
V1
V2V3
#1: 2D Point-in-Triangle
Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3)
{
return (v3 – v1) (v2 – v1) > 0;}
V1
V2V3
#2: Sphere vs. Sphere
Given: bool LineIntersectsSphere(Point3D p0,
Point3D p1,
Point3D C,
float r);
C
P1
P0
R
#2: Sphere vs. Sphere
Write: bool SphereCollidesWithSphere(Point3D a0, Point3D a1,
float ar,
Point3D b0, Point3D b1,
float rb);
B0Rb
A0
Ra
B1
A1
Step 1: One Moving Sphere
B0Rb
A0
Ra
B1
Step 1: One Moving Sphere
B0
A0
Ra + Rb
B1
Step 2: Two Moving Spheres
B0Rb
A0
Ra
B1
A1
Step 2: Two Moving Spheres
B0Rb
A0
Ra
B1
#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 StreamGiven: class ElementStream{
bool GetNextElement(Element* pElementOut); };
Write:Element SelectRandomElement(ElementStream* pStream);
• Each element is equally likely.• Use constant space.
#4: Select from StreamElement 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?
Notational Conventions
• In the spaceship’s frame of reference, z is forward.
• M is the body-to-world transform.
M =
… … … 0
… … … 0
fx fy fz 0
tx ty tz 1
Solution
M[row 3] += 10 * METER * M[row 2];
M =
… … … 0
… … … 0
fx fy fz 0
tx ty tz 1
#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
1. Transform objects to screen space bounding rects, test against mouse.
• Doesn’t deal with occlusion well.
2. Transform mouse to ray, test ray vs. boxes.• Could make good use of culling database
3. 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.
Fin