Public Group

# Pong Angles

This topic is 2692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble with, what I believe is, the rounding of angles in my C++ Pong clone.

I implemented a Ball with a velocity and an angle. The velocity is its current speed, and the angle is theta of the circle. The problem is when I test if I hit a barrier:

 int x = location->getX() + cos(angle * PI / 180) * velocity; int y = location->getY() + sin(angle * PI / 180) * velocity; if (x < 0 || x > 1024) { angle -= 180; } if (y < 0 || y > 768) { angle -= 180; } location->setX(x); location->setY(y);

When the ball first starts moving (angle = 0, velocity = 1) it hits the right side of the screen and then has its angle = -180. The problem is that after it sets its angle to -180, the Y values start to decrease (which shouldn't happen since sin(-180) = 0).

##### Share on other sites
I am not a math wiz, but I think you are having problems because you are mixing degrees with radians, and are not paying attention to operator precedence in C++.

Forgive me, but Please Excuse My Dear Aunt Sally. She's a real bitch sometimes.

##### Share on other sites
I don't think so:

I convert all degrees to radians.
Operator precedence for relations is higher than for logical OR.

##### Share on other sites
What is your visual result? Is there a gradual drop off or is there a sudden, one time drop on the y axis?

When I evaluate [font="Courier New"]sin(-180 * 3.14159 / 180)[/font] I get [font="Courier New"]-0.054803618904513401978590813552063[/font].

##### Share on other sites
By definition, a velocity has a direction and a speed by itself. Sticking just with velocity, you don't need angles at all.
 int x = location->getX() + velocityX * deltaTime; int y = location->getY() + velocityY * deltaTime; if (x < 0 || x > 1024) { velocityX *= -1; } else { location->setX(x); } if (y < 0 || y > 768) { velocityY *= -1; } else { location->setY(y); }

##### Share on other sites
Using just global x,y values (rather than using "location"), I can't reproduce your problem. What data types are your variables (e.g., x,y in your location object, angle and velocity), integers or floats?

For debugging purposes, you might calculate each part of the equation separately, step through your code and see where the problem lies. I.e., something like:
 int yL = location->GetY(); float angleT = angle*PI/180; inr yD = sin( angleT); int y = yL + yD * velocity; 

##### Share on other sites

I'm having trouble with, what I believe is, the rounding of angles in my C++ Pong clone.

As Hodgman said, you don't need angles for this (it can all be done with vector math).

##### Share on other sites
Hodgman is right, this seems over complicated.

Say you have a ball moving at a constant x axis speed of 5 and a constant y axis speed of 5.
 int yvel = 5; int xvel = 5; ball_y_coord += yvel; ball_x_coord +=xvel; 
Then, it would collide with the bottom of the screen:
: collision_function(); { if(ball_y_coord + ball_width > SCREEN_HEIGHT) { yvel *= -1; } //other collisions (paddles, other walls) } 

That should work successfully and simply.

##### Share on other sites
Thank you all, changing to two velocity values has worked correctly.

1. 1
2. 2
3. 3
Rutin
21
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007206

×