• 12
• 27
• 9
• 9
• 20

# Find ball speed given the x and y trajectory of a ball (pong)

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

## Recommended Posts

If the x trajectory of a ball in pong is 1 and the y trajectory is 0, the ball moves straight to the right. However, since the x trajectory is 1, it is increasing its x location by 1 every time it moves. Because of this, if the x trajectory is 0.5 and the y trajectory is 0, it is moving 0.5 pixels every time it moves. Now, the ball is moving 0.5 pixels every time it moves which is half as much as last time. Because of this, it is moving half as fast and the speed should always be the same. To solve this problem, you would have a timer that would regulate the ball speed. To get the speed of the timer is something i have been trying to figure out but failing at. It seems like such a thing to regulate the speed of something given the x and y trajectory would exist as a function in the computer programming world. Does it? If not, could someone help me write my own? This is what I currently have but it is faulting.  private const float legOfComparison = 1.4142135623730950488016887242097f; // square root of 2 public static float GetNewBallSpeed(double leg1, double leg2) { return (float)Math.Sqrt(Math.Pow(leg1, 2) + Math.Pow(leg2, 2)) / legOfComparison; }  Basically, the leg1 is the x trajectory and the leg2 is the y trajectory. However, the x and y trajectory are floats and there may be some implicit conversions from double to float and vice versa going on so there may be some precision loss. However, my current thinking on this is: The first leg of the triangle (x trajectory) squared + The second leg of the triangle (y trajectory) squared = some number Find the square root of some number = next number next number / the square root of 2 = the new speed of the ball However, when doing that, the speed is still not regulated and I often, while playing the game, notice the ball slowing down and speeding up between paddle bounces, which is when I assign the new speed of the ball. Thanks for any help given =)

##### Share on other sites
This stuff is usually easier if you think in terms of vectors.

In simple terms, a vector is simply the 'x' and 'y' you refer to, grouped together, e.g.:
struct Vector {    float x,y;};
You can then implement some common (and useful) functions, such as:
float length(const Vector& v) {    return std::sqrt(v.x*v.x + v.y*v.y);}
Which you should recognize from your existing code.

You then assign the ball a velocity, which is a vector representing both direction and speed (the speed is the length of the vector).

Typically you would update the position of the ball like this:
ball.position += ball.velocity * elapsedTime;
Where elapsedTime is the amount of time that's passed since the last update (typically counted in seconds).

Once you've adapted your code to work with vectors directly, whatever problem you're trying to solve (which, to be honest, I'm not quite clear on) should become quite a bit less daunting.

Understanding simple vector math is a must (more or less) for writing any sort of non-trivial game application (at least one that involves geometry and motion, and that includes Pong), so if you're unfamiliar with the topic I'd suggest looking for some good books and/or online resources and reading up a little.

##### Share on other sites

A lot of math still confuses me because I'm only a sophomore in high school..

However, my problem still persists of non-constant speed =(

// Update is called as much as possible. Many, many times per millisecond.

/// <summary>
/// </summary>
public void Update()
{
if (gameTimer.ElapsedMilliseconds >= msBallSpeed)
{
Move();

gameTimer.Reset();
gameTimer.Start();
}
}

// Move calls continue ;)

/// <summary>
/// Continues the balls trajectory.
/// </summary>
private void Continue()
{
location.X += trajectory.X * (gameTimer.ElapsedMilliseconds / 1);
location.Y += trajectory.Y * (gameTimer.ElapsedMilliseconds / 1);
}

I noticed how you showed me the length method but never told me when, if, or how to use it. Should I not be using it? I thought I was supposed to set my msBallSpeed variable to that of what your length method returns each time the ball hits the paddle and changes velocity, no?

Also, at the end of the length method, shouldn't you divide by the square root of 2?

##### Share on other sites
Quote:
 Original post by Fromethiusprivate const float legOfComparison = 1.4142135623730950488016887242097f;

This long number gives a false sense of accuracy. You can't get more accurate than 1.4142135f with float. If you don't believe me, try this:

System.out.println(1.4142135f == 1.4142135623730950488016887242097f);

This will output true.

##### Share on other sites
I probably can't answer all your questions (just for lack of time), but I'll try to give you a few more tips, just to get you pointed in the right direction.
Quote:
 // Update is called as much as possible. Many, many times per millisecond.
Really? That's several thousand frames per second :-
Quote:
 I noticed how you showed me the length method but never told me when, if, or how to use it.
Well, actually, if you handle your updates the 'correct' way, you won't need to :) I probably should have been more clear about that.
Quote:
 Also, at the end of the length method, shouldn't you divide by the square root of 2?
IMO, you're handling the updating of your objects (and time-based motion in general) the wrong way. I'd probably have to re-read your original post more carefully to understand what you mean to accomplish by dividing by sqrt(2), but no, that's not typically how you would do things.

Here again is a brief summary:
// Somewhere, assign your ball an initial velocity, say, straight across the screen:ball.velocity = vector(1, 0) * speed;// Where 'speed' is however fast you want the ball to move in game units per// second.// Then, in the update function:ball.position += ball.velocity * deltaTime;// Note that this all assumes you are using a floating-point representation for// your variables.
From there, all changes in velocity would be the result of vector math, most likely as the result of bouncing along with perhaps some simple friction (so that the paddle imparts some of its velocity to the ball).

It may be that you'll want to restrict the ball to a given speed regardless of changes in velocity, in which case you can do something like this:
ball.velocity /= length(ball.velocity);ball.velocity *= speed;
And you finally get to use the length() function :) (Actually, the first line performs a vector normalization, which you'd probably want to make a separate function for.)

##### Share on other sites
Thanks so much for all your help.

Really appreciated. =)

Resolved.