Jump to content
  • Advertisement
Sign in to follow this  
newn

Ping Pong - ball movement. Completely random, smooth, not 'locked' movement. How to?

This topic is 3033 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

Hi everyone. I took different library, which is much more advanced, and completely in C++ - SFML now, so i'm remaking Ping Pong game. Now i would like to do a smooth, nice movement, random movement...

I used to write the movement like that:

if (dir == 1 && y > 0)
{
for(velocity_x = 0;velocity_x > -3; velocity_x --)
{
for(velocity_y = 0;velocity_y > -3; velocity_y --)
{
velocity_x --;
velocity_y --;
}
}
x -= vel_x - velocity_x;
y -= vel_y - velocity_y;
}

Which 'lags', and is not very smooth, looks crappy. I think about something like that, but i'm unable to make it work:

Before the hit to the wall or the stick, leave it constant. On hit generate a random number, for the direction, to which the ball would move. But how to make the direction work - i don't know. x++ and y-- ain't gonna work here.

Share this post


Link to post
Share on other sites
Advertisement
you need to get the elapsed time between frames and calculate the movement based on that

it will look something like this

x += elapsed_time * speed;

Share this post


Link to post
Share on other sites
Well, i done that direction stuff, it works great, although, i didn't understood part of code, that i used. I mean what it's supposed to do. :/

Share this post


Link to post
Share on other sites
Hey newn try this interactive tutorial on basic 2D game physics:

http://www.rodedev.com/tutorials/gamephysics/

It should give you a better idea of what you need to do.

Share this post


Link to post
Share on other sites
Do I understand in correctly that you want to make the ball bounce of walls and the stick randomly? That's not the right behaviour for a pong game. The angle of reflection should be defined and dependent on the angle of impact and velocity of the stick at the moment of reflection. In fact the velocity of the stick wouldn't have such an effect in reality, but in pong-like games it's common.
And the most realistic behaviour would be to compute also rotation of the ball, that would affect greatly the angle of reflection also on static walls.

About the movement in general: such stuff is easily solved with vectors. If you're using some library that includes vector calculations already, like for example DirectX and it's D3DXVector2 it's really easy.
You'll have a 2D vector for position and 2D vector for velocity.
Velocity vector will be changed by you on every impact (you'll reflect the vector). Position vector will change "automatically" every frame by this very simple code:

vecPos += vecVel * dt

provided the vector library has overloaded + and * operators for 2D vectors ;)

dt is time between frames.

Share this post


Link to post
Share on other sites
@ EJH, thanks, that tutorial is real nice! Explained a lot of things. Especially those in the end of the tutorial. ;)

@ Tom KQT, Well, i'm currently working on a menu pretty much. But i still need the images, which should be created by someone, so i've a time gap. Did sounds today, and some other minor stuff.
That, what you named, those inpacts, reflections, movement angle, sides, etc, yea, i bet they are being used in pong games. I haven't played many pong games though... I remember playing one, when i was about 10 or 11... That was a long time ago, but yea, i other than that - nop. And that wasn't really a pong game, it was Arkanoid style game. So those things you named started coming back to my mind, hehe... Nostalgia, lol.

Anyway, about realism... (Yes, i'm using SFML, it have some Vector stuff) It sounded quite hard to me, lol. All that stuff, and angles... But when i think about it, well, it's pretty much a few if statements, and that's all. But yea, i'll need to figure out those... Calculation things. Thinking about something now, but since i won't be able to program today, or at least for about 4-5 hours, but probably whole day, so i shouldn't think about that yet.

Oh, and by the way, haven't mentioned that yet: i've made the ball to bounce pretty much randomly, but at least not in the same side. Seems pretty much okay, looks pretty good, but sometimes bounces too straith, need to tweak those calculations a bit... So now what's left about physics, is probably the stuff, what Tom has just said - make it more realistic and cool.

EDIT: Hmmm, thought of some idea about the physics:
What if, i would divide the pong size, by let's say 10. Then do something about the velocity. Like make velocity dependant on the place where it lands on the pong... And dependant on the speed it moves up/down. But have no idea how to do it yet...

Hm. And Vector is something like calculation, the value of the velocity, like VelVec is equal to the velocity? Or i'm wrong about vectors? Eh, damn my knowledge.

[Edited by - newn on August 25, 2010 8:39:49 AM]

Share this post


Link to post
Share on other sites
Vector in a 2D space (your case) is just two values tied together (like an array of two values).
Vector of position consists of two coordinates of position, x and y. You are storing them to separate variables, with vector you would store them both to one vector. The vector still has those two values, you can still access them like vecPos.x and vecPos.y, but above this you can make vector operations on it.
Vector of velocity is a bit more complicated. Vector of velocity defines both the direction in which the ball is moving and also how fast it is moving. And this is stored also in two values which could be separately described as velocity along the x axis and along the y axis.

The basic relation between the change of position and velocity in physics is:

v = ds/dt

which uses differentials, but can be replaced just with changes over a small period of time.
You want to calculate position (s), so from the equation you get:

ds = v * dt

and as I said we can go from differentials (derivativies) to changes, it can be formulated also as:

change_of_position = velocity * change_of_time

If you use just single values (scalars) in the equation, you get a linear movement (1D), for example:

change_of_position = 12 * 0.1

which means that and object with velocity 12 units/second will move during 0.1 seconds by 1.2 units.


The good point is that this works also with vectors, both change_of_position and velocity can be vectors (change_of_time is just a single value). And by using vectors, you calculate 2D movement (or 3D is using 3-element vectors) with a single simple equation!

And finaly - what to do with the calculated change_of_position? Simply, use it to change the position :D

position = position + change_of_position
or
position = position + velocity * change_of_time
or
position += velocity * change_of_time

You really type it like this in your code (of course with your variable (vector) names), this is not just some theoretical mathematic representation which would need to be programmed on 20 lines of code ;)

(change_of_time is a time increment, usually named dt, between two steps of the calculation, in game programming usually equals the time between two frames)

Share this post


Link to post
Share on other sites
Thanks for the long and nice explanation, explained me a lot of things, especially about vectors. And made me laugh in the end, how simple that was, lol.

Anyway, i've this code for my ball's movement (although, it's pretty much random, works well though, but i'd like to make it more realistic. :P I'm a guy, who likes to make perfect things, even though they are hard. That i learned, when i was making few websites for myself.):

const float PI = 3.14159f;
float BallSpeed = 400.f;
float BallAngle;
do
{
BallAngle = sf::Randomizer::Random(0.f, 2 * PI);
}
while(std::abs(std::cos(BallAngle)) < 0.5f);

/* collision checking and other boring stuff */

BallAngle = PI - BallAngle;
SpriteBall.SetX(SpritePong1.GetPosition().x + SpritePong1.GetSize().x + 0.1f);

P.S. i should make the game working, by the end of the day, with datafiles and stuff, which means, i should release some... version to the public on my blog. Not many readers, but still, maybe someone'll download and try it, lol.
I will be working on more realistic physics and a few other things, which will be described on my site too, so i that doesn't mean i'll drop the pong making, until it'll be a nice game, which MAYBE will be interesting to play for someone. Such games ain't very interesting, but still, some people do play 'em, i believe. ;) So anyway, if you'd like to check it out with current physics - it's free, hehe (link's in my profile, not sure, if i'm allowed to post links on the posts and signature).

So anyway, today is the day, when my brains ain't working (i'm having days, when i'm smart and where i'm dumb, lol. Sounds funny, but well.), so i couldn't think of something smart about this thing, but anyway, i'm trying to make it to be different from the place, where it hits the paddle. Should be some simple mathematics action, like dividing the paddle to 10 parts, and the in which part the ball hit, make the speed and the angle a little bit different.

And again - thanks for help!

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!