Angles
-
I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.
double a = 135;
double angle = (Math::PI / 180) * a;
Ball_List[b]->xVel = speed*Math::Cos(angle);
Ball_List[b]->yVel = -speed*Math::Sin(angle);Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.
-
I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.
double a = 135;
double angle = (Math::PI / 180) * a;
Ball_List[b]->xVel = speed*Math::Cos(angle);
Ball_List[b]->yVel = -speed*Math::Sin(angle);Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.
Cyclone_S wrote:
I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle
That is pretty vague, and probably not according to normal physics. Maybe what you want is: the outgoing angle equals the supplement of the incoming angle plus some delta, which is zero in the center and grows when the hit point is away from the center; so maybe calculate that distance and use it to add to or multiply the outgoing angle.
Cyclone_S wrote:
going in a circle
the equations for a circle in two dimensional space are:
(x - xc)^2 + (y - yc)^2 = r^2
or
x = xc + r * cos(a)
y = yc + r * sin(a)where (x,y) is a point on the circle, (xc,yc) is the center, r the radius, a an angle in radians. I cannot believe you would not know that. Look at the equations, they say the point(x,y) is at a fixed distance r from a fixed point (xc,yc). :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3modified on Tuesday, May 31, 2011 9:17 PM
-
Cyclone_S wrote:
I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle
That is pretty vague, and probably not according to normal physics. Maybe what you want is: the outgoing angle equals the supplement of the incoming angle plus some delta, which is zero in the center and grows when the hit point is away from the center; so maybe calculate that distance and use it to add to or multiply the outgoing angle.
Cyclone_S wrote:
going in a circle
the equations for a circle in two dimensional space are:
(x - xc)^2 + (y - yc)^2 = r^2
or
x = xc + r * cos(a)
y = yc + r * sin(a)where (x,y) is a point on the circle, (xc,yc) is the center, r the radius, a an angle in radians. I cannot believe you would not know that. Look at the equations, they say the point(x,y) is at a fixed distance r from a fixed point (xc,yc). :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3modified on Tuesday, May 31, 2011 9:17 PM
Luc Pattyn wrote:
I cannot believe you would not know that.
Just out of curiosity, why would you assume he would know?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Luc Pattyn wrote:
I cannot believe you would not know that.
Just out of curiosity, why would you assume he would know?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
Assuming he is adolescent or older, he would have learned this and much more at school, both as formula's for describing a circle, and as a geometric illustration for explaining what a sine and cosine actually are. He already was using angles, sine and cosine, in his post, so it puzzles me how he would not come up with the equations if he had ever seen and understood them. And then there are books, and Google, and Wolfram, etc. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
I got two questions. First I'm making a brick game. I want the ball to bounce off the paddle at different angles depending on where the ball hits the paddle. If the angle variable is specified directly it bounces off in the correct angle but I'm not sure how to calculate the angle depending on where the ball hits the paddle.
double a = 135;
double angle = (Math::PI / 180) * a;
Ball_List[b]->xVel = speed*Math::Cos(angle);
Ball_List[b]->yVel = -speed*Math::Sin(angle);Second. I want to animate a box going in a circle but I'm Not sure on the math. Thanks in advance.
You might want to check out this site[Physics engines for dummies] for an easy introduction into the basics of vector algebra used to emulate quasi-physical simulations. You will find that in stead of angles and sinus/cosinus it explains the calculations needed for reflections with the help of vector algebra. Although the code is not C/C++ it should be easy enough to translate the relevant code for your problems.
-
Assuming he is adolescent or older, he would have learned this and much more at school, both as formula's for describing a circle, and as a geometric illustration for explaining what a sine and cosine actually are. He already was using angles, sine and cosine, in his post, so it puzzles me how he would not come up with the equations if he had ever seen and understood them. And then there are books, and Google, and Wolfram, etc. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
Thanks for the replies. Both problems are mostly solved. I have the paddle/ball equation figured out but I'm having a problem where any value less then 1 is ignored... any ideas? I need finer precision. Thanks.
Cyclone_S wrote:
I need finer precision
then scale it all up in integers; or use floating-point arithmetic throughout. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
Cyclone_S wrote:
I need finer precision
then scale it all up in integers; or use floating-point arithmetic throughout. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3I am using doubles in the function. The problem is I can't move the panel(ball) anything less then 1 pixel. Maybe move the ball 1pixel over a couple of frames? I'm not sure how to do that. Here is the code.
int speed=Ball\_List\[b\]->speed; // Paddle center. int paddle\_center = Player->Location.X + Player->Width / 2; // Ball center. int ball\_center = Ball\_List\[b\]->gameBall->Location.X + Ball\_List\[b\]->gameBall->Width / 2; // Find the location on the paddle that the ball hit int paddle\_location = ball\_center - paddle\_center; // Increase X speed according to distance from center of paddle. double a=(90-paddle\_location); double angle = (Math::PI / 180) \* a; // angle in radians. Ball\_List\[b\]->xVel = speed\*Math::Cos(angle); Ball\_List\[b\]->yVel = -speed\*Math::Sin(angle); Ball\_List\[b\]->gameBall->Top = Player->Location.Y - 16;
Thanks
-
I am using doubles in the function. The problem is I can't move the panel(ball) anything less then 1 pixel. Maybe move the ball 1pixel over a couple of frames? I'm not sure how to do that. Here is the code.
int speed=Ball\_List\[b\]->speed; // Paddle center. int paddle\_center = Player->Location.X + Player->Width / 2; // Ball center. int ball\_center = Ball\_List\[b\]->gameBall->Location.X + Ball\_List\[b\]->gameBall->Width / 2; // Find the location on the paddle that the ball hit int paddle\_location = ball\_center - paddle\_center; // Increase X speed according to distance from center of paddle. double a=(90-paddle\_location); double angle = (Math::PI / 180) \* a; // angle in radians. Ball\_List\[b\]->xVel = speed\*Math::Cos(angle); Ball\_List\[b\]->yVel = -speed\*Math::Sin(angle); Ball\_List\[b\]->gameBall->Top = Player->Location.Y - 16;
Thanks
All of that could have been floats or doubles; it is only when painting that it eventually needs to be rounded to pixels, hence integers. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4 -
All of that could have been floats or doubles; it is only when painting that it eventually needs to be rounded to pixels, hence integers. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4