嗯,我已經爲此找了很多東西,但我能找到的所有人都是這麼說的,就像做pi *方向一樣,方向就是我假設球進入的角度。但是我的問題是,我不知道如何獲得球首先進入的角度,所以我不能做到這一點。 如果任何人都可以解釋我將如何計算球擊中球拍帽的角度,反彈後球的速度大小以及應該增加的角度,那樣會很棒。Pong Paddle碰撞速度和反彈角度
謝謝任何和所有的答覆!
我的代碼工作如下(這樣你就可以得到我想要做的是怎麼一個想法):
/* General Paddle Properties */
double PaddleLength = 80; //Down-wards length of the paddle
double PaddleWidth = 8; //How thick the paddle is
/* Positioning of user control paddle */
double UserPaddleTop = 0; //How far away from the top of the screen the paddle is
double UserPaddleLeft = 10; //How far left from the side of the client rectangle it is
/* Positioning of ai controled paddle */
double AIPaddleTop = 0;
double AIPaddleLeft = 10;
/* Ball properties and position */
double BallSize = 5;
double BallTop = 0;
double BallLeft = 0;
double BallSpeedY = -0.01, BallSpeedX = -0.03;
方法:
private void UpdateBall()
{
if (((int)(UserPaddleLeft + PaddleWidth) == (int)BallLeft) && !((int)UserPaddleTop > (int)BallTop) && !((int)(UserPaddleTop + PaddleLength) < BallTop)
|| ((int)(AIPaddleLeft - PaddleWidth) == (int)BallLeft) && !((int)AIPaddleTop > (int)BallTop) && !((int)(AIPaddleTop + PaddleLength) < BallTop)) //Collided
{
BallSpeedX = -BallSpeedX; //The height is 800 the balltop is 300
BallSpeedY = Math.Cos(BallSpeedX
}
if ((int)BallTop == 0 || (int)BallTop == ClientRectangle.Height) //Hit the top
{
BallSpeedY = -BallSpeedY;
}
if ((int)BallLeft == 0)
{
System.Diagnostics.Debug.WriteLine("AI gets one point!");
BallSpeedX = -0.03; //Goes towards the user AI has scored
Scores[0]++;
this.Title = "Pong Testing - Scores: " + Scores[0] + "|" + Scores[1];
ResetAll();
}
else if ((int)BallLeft == ClientRectangle.Width)
{
System.Diagnostics.Debug.WriteLine("User gets one point!");
BallSpeedX = 0.03; //Goes towards the AI user has scored
Scores[1]++;
this.Title = "Pong Testing - Scores: " + Scores[0] + "|" + Scores[1];
ResetAll();
}
BallLeft = (BallLeft + BallSpeedX);
BallTop = (BallTop + BallSpeedY);
}
private void UpdateAI()
{
if(!((int)(BallTop + PaddleLength) == 0) && !((int)(BallTop + PaddleLength) >= ClientRectangle.Height)) //Make sure updating it pos won't make it go out of bounds
AIPaddleTop = BallTop; //Change to real ai by using offset
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if ((int)UserPaddleTop != 0 && Keyboard[Key.Up])
{
UserPaddleTop = UserPaddleTop - MoveSpeed;
}
else if (Keyboard[Key.Down] && (int)(UserPaddleTop + PaddleLength) != ClientRectangle.Height)
{
UserPaddleTop = UserPaddleTop + MoveSpeed;
}
}
更新1:
感謝所有人的幫助,我已經能夠爲它提供一些基本的代碼,但是現在這個代碼只是把球飛得太快,以至於無法得到它。任何人都可以幫忙嗎?
代碼:
double AngleNormal = Math.Atan2(BallSpeedX,BallSpeedY);
double AngleBallMovement = Math.Sqrt((BallSpeedX * BallSpeedX) + (BallSpeedY * BallSpeedY));
double ReflectionAngle = AngleNormal - (AngleBallMovement - AngleNormal);
BallSpeedY = Math.Sin(ReflectionAngle);
BallSpeedX = Math.Cos(ReflectionAngle);
這是一個數學問題,與編程無關。 –
我會說,因爲我正在編程計算價值,但我不是在這裏爭論。對不起,如果它實際上只是數學。 – user1763295
難道你不知道球被釋放的第一個角度嗎?從那裏你可以計算出球將撞擊槳葉的角度和之後的角度。儘管我同意米奇的觀點,但這比編程更重要。不錯的拼圖雖然 –