Pong Angles

Started by
7 comments, last by Oisin 12 years, 11 months ago
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).
Advertisement
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.

I don't think so:

I convert all degrees to radians.
Operator precedence for relations is higher than for logical OR.
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].

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);
}
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;

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


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).
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.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of [size=2]Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast
Thank you all, changing to two velocity values has worked correctly.

This topic is closed to new replies.

Advertisement