1

In my XNA game, I'm using Viewport.Project to get 2d screen coordinates of a sun's 3D coordinates (mainly because it looks nice). I got this working, however, when I look in opposite directions of the sun, I see 2 of them (technically, one). I have no idea what's happening here..

Anyone know what's going on? And how can I prevent this?

To help visualize this, here's a image of what I'm talking about: Hyperlinked image, don't have 10+ rep

This is the code:

        Rectangle clientBounds = Window.ClientBounds;
        Texture2D sun = Content.Load<Texture2D>(@"Image\Skybox_Sun");
        Vector3 coords = new Vector3(1000, 0, 1000);
        Vector3 coords1 = graphics.GraphicsDevice.Viewport.Project(coords, camera.Projection, camera.View, Matrix.Identity);
        suncoords = coords1;
        spriteBatch.Begin();
        spriteBatch.Draw(sun, new Vector2(coords1.X, coords1.Y), null, Color.White, 0, new Vector2(cursor.Width / 2, cursor.Height / 2), q, SpriteEffects.None, 0);
        spriteBatch.End();
user1306322
  • 8,561
  • 18
  • 61
  • 122
Jared
  • 129
  • 3
  • 13

1 Answers1

2

Have you tried checking coords1.Z? I imagine that the sun behind you corresponds to a negative Z, in which case you could try adding an if condition before your draw like this:

if(coords1.Z > 0)
  spriteBatch.Draw(sun, new Vector2(coords1.X, coords1.Y), null, Color.White, 0, new Vector2(cursor.Width / 2, cursor.Height / 2), q, SpriteEffects.None, 0);
LiquidAsh
  • 271
  • 3
  • 4
  • In the image, "Sun Coords" is the sun's screen coordinates. The only thing that varies between those two is that the Z decreases by a VERY, very small value. – Jared Mar 19 '12 at 02:56
  • Anyways, I solved it by using view frustum culling. Thanks for the suggestion anyways. – Jared Mar 19 '12 at 02:57
  • 1
    Ah yes, Now I see the position that you are printing to the screens... It just needs to be between 0 and 1 (not <0 or in your case >1). Glad you were able to get this working. – LiquidAsh Mar 19 '12 at 03:25