29
Is this thing on?

Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Embed Size (px)

Citation preview

Page 1: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Is this thing on?

Page 2: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Ten Interview Questions for Game Programmers

Marc LeBlanc

April 2004

Page 3: 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

Page 4: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#1: 2D Point-in-Triangle

Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3)

V1

V2V3

Page 5: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#1: 2D Point-in-Triangle

Hint: bool IsTriangleClockwise(Point v1, Point v2, Point v3)

{

return (v3 – v1) (v2 – v1) > 0;}

V1

V2V3

Page 6: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#2: Sphere vs. Sphere

Given: bool LineIntersectsSphere(Point3D p0,

Point3D p1,

Point3D C,

float r);

C

P1

P0

R

Page 7: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#2: Sphere vs. Sphere

Write: bool SphereCollidesWithSphere(Point3D a0, Point3D a1,

float ar,

Point3D b0, Point3D b1,

float rb);

B0Rb

A0

Ra

B1

A1

Page 8: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Step 1: One Moving Sphere

B0Rb

A0

Ra

B1

Page 9: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Step 1: One Moving Sphere

B0

A0

Ra + Rb

B1

Page 10: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Step 2: Two Moving Spheres

B0Rb

A0

Ra

B1

A1

Page 11: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Step 2: Two Moving Spheres

B0Rb

A0

Ra

B1

Page 12: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#3: Shuffle a Deck

Given: int rand();

Write: void ShuffleDeckInPlace(Card* deck, int nCards);

• What are the requirements?

Page 13: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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]);

}

Page 14: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#4: Select from StreamGiven: class ElementStream{

bool GetNextElement(Element* pElementOut); };

Write:Element SelectRandomElement(ElementStream* pStream);

• Each element is equally likely.• Use constant space.

Page 15: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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;}

Page 16: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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

Page 17: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#6: The Cows Come Home

Given a tilemap with cows, barns & obstacles:

Page 18: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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.

Page 19: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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

Page 20: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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?

Page 21: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

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

Page 22: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Solution

M[row 3] += 10 * METER * M[row 2];

M =

… … … 0

… … … 0

fx fy fz 0

tx ty tz 1

Page 23: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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?

Page 24: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#9: 3D Click

In a 3D scene…

Page 25: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#9: 3D Click

What object did the user click on?

Page 26: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#9: 3D Click

Givens:

• Transforms for view and camera

• For each object:– Body-space dimensions– World-to-body transform

Page 27: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

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.

Page 28: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

#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.

Page 29: Is this thing on?. Ten Interview Questions for Game Programmers Marc LeBlanc April 2004

Fin