글쎄 나는 이것에 대해 많은 것을 수색 했어.하지만 내가 알 수있는 것은 pi * direction과 같은 말이다. 방향은 공이 들어오는 각도 다. 하지만 제 문제는 볼을 처음부터 들어올 각도를 어떻게 얻을 수 있을지 모르겠습니다. 그래서 저는 이것을 할 수 없습니다. 아무도 설명 할 수있는 방법은 볼이 외륜 모자를 쳤다 각도를 계산할 것입니다, 속도가 공을 리바운드 후 부여해야합니다 각도와 다음에 의해 증가해야 그게 끝내 줄 겁니다.Pong 패들 충돌 속도와 리바운드 각도
모든 응답을 보내 주셔서 감사합니다.
/* 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;
}
}
UPDATE 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
공이 풀린 첫 번째 각도를 모르십니까? 거기에서 공이 패들과 그 이후의 각도에 부딪 힐 각도를 계산할 수 있습니다. Mitch와 동의하지만, 이것은 프로그래밍보다는 수학입니다. 좋은 퍼즐 비록 –