Note that there are some explanatory texts on larger screens.

plurals
  1. POPong Paddle collision velocity and rebound angle
    primarykey
    data
    text
    <p>Well I've searched a lot for this but all I can find is people saying like do pi * direction, direction being the angle that the ball is coming in at I assume. But my problem is, I have no idea how I get the angle the ball is coming in at in the first place so I can't do these. If anyone could <strong>explain</strong> how I would calculate the angle that the ball has hit the paddle hat, the amount of velocity that the ball should be given after rebound and the angle it should be incremented by then that would be awesome.</p> <p>Thankyou for any and all responses!</p> <p>My code works as follows (So you can get an idea of how I'd like to do it):</p> <pre><code>/* 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; </code></pre> <p>Methods:</p> <pre><code>private void UpdateBall() { if (((int)(UserPaddleLeft + PaddleWidth) == (int)BallLeft) &amp;&amp; !((int)UserPaddleTop &gt; (int)BallTop) &amp;&amp; !((int)(UserPaddleTop + PaddleLength) &lt; BallTop) || ((int)(AIPaddleLeft - PaddleWidth) == (int)BallLeft) &amp;&amp; !((int)AIPaddleTop &gt; (int)BallTop) &amp;&amp; !((int)(AIPaddleTop + PaddleLength) &lt; 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) &amp;&amp; !( (int)(BallTop + PaddleLength) &gt;= 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 &amp;&amp; Keyboard[Key.Up]) { UserPaddleTop = UserPaddleTop - MoveSpeed; } else if (Keyboard[Key.Down] &amp;&amp; (int)(UserPaddleTop + PaddleLength) != ClientRectangle.Height) { UserPaddleTop = UserPaddleTop + MoveSpeed; } } </code></pre> <p>UPDATE 1:</p> <p>Thanks to the help of everyone I have been able to come up with some basic code for it but now this code just sends the ball flying so fast that it's impossible to get it. Could anyone help please?</p> <p>Code:</p> <pre><code> 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); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload