Jump to content
  • Advertisement
Sign in to follow this  
Oisin

Pong Angles

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!