0
\$\begingroup\$

I know how to scroll a background using spritebatches + 2D background images, but how would I do it with a 3D model?

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

You will first need to ensure the model actual tiles seamlessly in the direction you want it to scroll. Then, a simple solution is to draw the model twice.

Consider the viewport of your game, represented by this ASCII box:

+------------+
|            |
|            | 
|            |
+------------+

When you draw your 3D background model, represented by the ASCII M, it normally ends up looking something like this:

   MMMMMMMM
   MMMMMMMM
+------------+
|  MMMMMMMM  |
|  MMMMMMMM  | 
|  MMMMMMMM  |
+------------+

That is, some portion of the model is visible and some portion is outside the viewing area. To "loop" this background model, you detect when the edge of the model is about to come into view (this is a simple rectangle-rectangle containment test, because you can project the bounding volume of your model onto your viewing plane), and the frame just before the edge would be visible, you draw a second copy of the model above the first (represented below by the ASCII 'm'):

   mmmmmmmm
   mmmmmmmm
   MMMMMMMM
+------------+
|  MMMMMMMM  |
|  MMMMMMMM  | 
|  MMMMMMMM  |
+------------+
   MMMMMMMM
   MMMMMMMM

As the game continues to scroll, both models are onscreen, giving the illusion of a seamlessly tiling world:

   mmmmmmmm
   mmmmmmmm
   mmmmmmmm
+------------+
|  mmmmmmmm  |
|  mmmmmmmm  | 
|  MMMMMMMM  |
+------------+
   MMMMMMMM
   MMMMMMMM
   MMMMMMMM

Once the first model has scrolled entirely off the screen you can stop drawing it, so at most you are only rendering the model twice.

If the model is particularly huge, you may want to break it into smaller chunks simply to avoid all the extra geometry processing to determine that large portions of the model are offscreen.

\$\endgroup\$

You must log in to answer this question.