所以我目前正在嘗試讓一個類的遊戲Asteroids。問題是,自從我上一次上課以來,我一年四分之三沒做過任何編碼,並且幾乎忘記了我學到的所有東西。我需要使用推力/加速度來移動船舶,但也需要加壓,並且有摩擦力,以便當推力停止時,船隻減速而不是立即停止。我有下面的基本數學來旋轉和加速船。我很清楚,編程將問題分解成簡單的步驟,問題來了,我不知道下一步該怎麼去。任何幫助將非常感激。在C中製作小行星遊戲#
// Ship's starting position
static double positionX = 500.0;
static double positionY = 500.0;
// Calculate ship heading vectors based on current orientation in Radians
static double orientationInRadians;
static double xVector = Math.Sin(orientationInRadians);
static double yVector = Math.Cos(orientationInRadians);
/*Use Left and Right arrows to rotate
Once vector is found,
calculate position of ship 10 units away from current position along heading vector
scale vector to a unit (length of 1) vector*/
static double magnitude = Math.Sqrt(xVector * xVector + yVector * yVector);
static double unitVectorX = xVector/magnitude;
static double unitVectorY = yVector/magnitude;
/*Now that the vector is one unit long
but still points in the ships current orientation
move ship with positionX and positionY as its current coordinates*/
static double distanceToTravel = 10.0;
double newPositionX = positionX + unitVectorX * distanceToTravel;
double newPositionY = positionY + unitVectorY * distanceToTravel;
/*Remember to track the ship's current position with a double or float
and make distanceToTravel non-constant for acceleration instead of "jumps"*/
編輯:我忘了提及,這是我唯一的代碼。所以我基本上被一個引擎移動的東西不動。
::發牢騷::在小行星船*不*放慢我們你關閉引擎。獲得物理學*正確*是它的魅力之一。然後下車我的草坪! – dmckee 2010-09-11 14:55:42
順便說一句 - 這個問題可能會被視爲「不真實」。或多或少,因爲你沒有**問題,而是整個項目,並不知道從哪裏開始。你應該設定一些小目標,並對它們進行研究。然後當你遇到困難時,在這裏詢問*特定的問題。可能的目標:+在屏幕上打開一個窗口+在船上繪製「船」,並在窗口中居中+在任意位置和方向繪製船舶+清除船舶並在更新位置和方向上重繪+使船跟蹤控件。 .. – dmckee 2010-09-11 15:00:09