2011-05-30 71 views
1

好的,所以這是我的問題。在XNA中向上彎曲的弧形移動2D精靈?

我一直試圖在XNA中創建一種視覺日/夜週期,其中我有一個基礎類更新並保存時間,並且Sky類根據類更新的時間輸出背景。

但我不能弄清楚的是,如何讓月球/太陽在彎曲的向上弧線上移動,該弧線基於一天中的什麼時間跨越屏幕。最有問題的部分是讓Y軸變爲曲線,而X軸隨着時間的推移而移動。

任何人都可以幫助我嗎?

編輯: 好吧,看起來像安德魯魯塞爾的例子,幫助我做我需要做的。 雖然我不得不精力一點,但我終於找到了合適的解決方案: float Time =(float)Main.inGameTime.Seconds /(InGameTime.MaxGameHours * 60 * 60/2);

this.Position.X = Time * (Main.Viewport.X + Texture.Width * 2) - Texture.Width; 
this.Position.Y = Main.Viewport.Y - (Main.Viewport.Y * (float)Math.Sin(Time * MathHelper.Pi)/2) - (Main.Viewport.Y/2) + 50; 

回答

2

試試看Math.SinMath.Cos的功能。這些是您要查找的trigonometric functions

像這樣的東西(給人一種位置SpriteBatch):

float width = GraphicsDevice.Viewport.Width; 
float height = GraphicsDevice.Viewport.Height; 
float time = 0.5f; // assuming 0 to 1 is one day 
Vector2 sunPosition = new Vector2(time * width, 
     height - height * (float)Math.Sin(time * width/MathHelper.TwoPi)); 

(聲明:我沒有測試此代碼。)

+0

非常感謝你,不得不爲我的視口和所有這些修改它,但現在它就像一個魅力。查看我的問題解答。 – SpikensbroR 2011-05-30 16:26:53

+0

如果您想包含夜間循環,則可以使用Math.Sin和Math.Cos來創建完整的圓周運動 – tweetypi 2011-06-01 01:00:21