The classic bouncing ball logic!!
-
Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:
if ((y + h) >= ScreenH)//hit bottom CurrentDirection = BallDirection.Right; if ((x + w) >= ScreenW)//hit right CurrentDirection = BallDirection.Top; if ((x) <= 0)//hit left CurrentDirection = BallDirection.Bottom; if ((y) <= 0)//hit top CurrentDirection = BallDirection.Left; switch(CurrentDirection) { case BallDirection.Bottom: x += step; y += step; break; case BallDirection.Top: x -= step; y -= step+7; break; case BallDirection.Left: x -= step; y += step; break; case BallDirection.Right: x += step; y -= step; break; } pb.Left = x; pb.Top = y; Invalidate(); }
-
Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:
if ((y + h) >= ScreenH)//hit bottom CurrentDirection = BallDirection.Right; if ((x + w) >= ScreenW)//hit right CurrentDirection = BallDirection.Top; if ((x) <= 0)//hit left CurrentDirection = BallDirection.Bottom; if ((y) <= 0)//hit top CurrentDirection = BallDirection.Left; switch(CurrentDirection) { case BallDirection.Bottom: x += step; y += step; break; case BallDirection.Top: x -= step; y -= step+7; break; case BallDirection.Left: x -= step; y += step; break; case BallDirection.Right: x += step; y -= step; break; } pb.Left = x; pb.Top = y; Invalidate(); }
-
We got an article on Gravity and Collision Simulation in C#[^] that might help :)
I are Troll :suss:
-
Thanks Eddy, unfortunately it doesn't help me!! I need a free bouncing behavior of one ball against walls.. Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't Thanks Ed.
I have found very useful links. http://www.vb-helper.com/howto_net_bouncing_ball.html[^] http://www.krissteele.net/blogdetails.aspx?id=87[^] HTH
Jinal Desai - LIVE Experience is mother of sage....
-
Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:
if ((y + h) >= ScreenH)//hit bottom CurrentDirection = BallDirection.Right; if ((x + w) >= ScreenW)//hit right CurrentDirection = BallDirection.Top; if ((x) <= 0)//hit left CurrentDirection = BallDirection.Bottom; if ((y) <= 0)//hit top CurrentDirection = BallDirection.Left; switch(CurrentDirection) { case BallDirection.Bottom: x += step; y += step; break; case BallDirection.Top: x -= step; y -= step+7; break; case BallDirection.Left: x -= step; y += step; break; case BallDirection.Right: x += step; y -= step; break; } pb.Left = x; pb.Top = y; Invalidate(); }
You're doing it the hard way, why not just store a speed vector? (and don't create a struct/class for it, just store two ints or floats somewhere) Speed can be negative, depending on the direction. It will also make it much easier if you decide to do "other funny physics things", like gravity or other accelerations; you could just add the acceleration vector to the speed vector every update. (or scaled by time, if your steps are not fixed) To bounce that way, you can do something like
until no change
{
if (ball outside of horizontal range)
mirror speed horizontally and mirror ball around the edge it hits
if (ball outside of vertical range)
mirror speed vertically and mirror ball around the edge it hits
}Muammar© wrote:
y -= step+7;
That is a little odd.
-
Thanks Eddy, unfortunately it doesn't help me!! I need a free bouncing behavior of one ball against walls.. Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't Thanks Ed.
Muammar© wrote:
unfortunately it doesn't help me!!
I was guessing that you were stuck on the gravity-part, adjusting speed when you hit a wall. What are you exactly stuck with, and how can I help?
Muammar© wrote:
Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't
I did honey, and you stated that it didn't help.
I are Troll :suss:
-
Muammar© wrote:
unfortunately it doesn't help me!!
I was guessing that you were stuck on the gravity-part, adjusting speed when you hit a wall. What are you exactly stuck with, and how can I help?
Muammar© wrote:
Would you be a dear and look at my code?? I'm sure someone like you can figure out something I didn't
I did honey, and you stated that it didn't help.
I are Troll :suss:
-
Hey everyone, I'm sorry I'm asking this kind of question but I completely lost my logic today and need your help on this.. You see, it's a simple bouncing ball application, how can I make the ball bounce randomly and freely in the screen and still look natural.. Many thanks guys, here's what I'm doing now:
if ((y + h) >= ScreenH)//hit bottom CurrentDirection = BallDirection.Right; if ((x + w) >= ScreenW)//hit right CurrentDirection = BallDirection.Top; if ((x) <= 0)//hit left CurrentDirection = BallDirection.Bottom; if ((y) <= 0)//hit top CurrentDirection = BallDirection.Left; switch(CurrentDirection) { case BallDirection.Bottom: x += step; y += step; break; case BallDirection.Top: x -= step; y -= step+7; break; case BallDirection.Left: x -= step; y += step; break; case BallDirection.Right: x += step; y -= step; break; } pb.Left = x; pb.Top = y; Invalidate(); }
That is all wrong. You have a position p (with x and y components), a speed v (with x and y components), and an acceleration/gravity a (possibly with x and y components). For small time steps, you could evolve like this (apply to each component individually):
p+=v;
v+=a;where a probably is just a gravitational constant (which happens to be positive since the y-axis is pointing downward in GDI+). And collisions would invert the relevant speed component, so floor/ceiling would do v.y=-v.y; and left/right walls would cause v.x=-v.x; ADDED optionally you could also reduce the speed by some factor (say v*=0.9) on each collision to mimic non-elasticity. /ADDED :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Tuesday, July 13, 2010 7:42 AM
-
Thanks Ed. I just wanted the angle of the movement to change randomly each time I hit the walls.. and I'm not a honey :-\ BTW: I felt sorry yesterday for your world cup loss, you did wonderful nevertheless :)
Muammar© wrote:
I just wanted the angle of the movement to change randomly each time I hit the walls..
That would give an unnatural effect, a bounce needs to be the opposite of the approaching direction and speed. Then there's acceleration, unless you want the ball to bounce at a constant speed. To translate this to your code; the current speed of your picturebox is determined by the
Step
variable?Muammar© wrote:
and I'm not a honey
Hehe, point taken.
Muammar© wrote:
I felt sorry yesterday for your world cup loss, you did wonderful nevertheless
Thanks for the sentiment - I don't understand even the basic rules of the game, so I can't have an opinion really. The only part that I saw in the reruns this morning is a karate-kick in the chest of a soccer-player.
I are Troll :suss:
-
That is all wrong. You have a position p (with x and y components), a speed v (with x and y components), and an acceleration/gravity a (possibly with x and y components). For small time steps, you could evolve like this (apply to each component individually):
p+=v;
v+=a;where a probably is just a gravitational constant (which happens to be positive since the y-axis is pointing downward in GDI+). And collisions would invert the relevant speed component, so floor/ceiling would do v.y=-v.y; and left/right walls would cause v.x=-v.x; ADDED optionally you could also reduce the speed by some factor (say v*=0.9) on each collision to mimic non-elasticity. /ADDED :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Tuesday, July 13, 2010 7:42 AM
Luc Pattyn wrote:
a speed v (with x and y components)
Was I the only one paying attention in school physics classes? You there at the back, boy: what is the difference between speed and velocity? No cheating by looking it up in the text book.
Luc Pattyn wrote:
where a probably is just a gravitational constant
"A" gravitational constant? When I was at school, there was only one. I suppose it does make the simulation more interesting if you can adjust the fundamental nature of the universe. http://en.wikipedia.org/wiki/Gravitational_constant[^]
-
Luc Pattyn wrote:
a speed v (with x and y components)
Was I the only one paying attention in school physics classes? You there at the back, boy: what is the difference between speed and velocity? No cheating by looking it up in the text book.
Luc Pattyn wrote:
where a probably is just a gravitational constant
"A" gravitational constant? When I was at school, there was only one. I suppose it does make the simulation more interesting if you can adjust the fundamental nature of the universe. http://en.wikipedia.org/wiki/Gravitational_constant[^]
David Skelly wrote:
the difference between speed and velocity?
speed, velocity, derivative of position against time, it is all the same vector. If you want to play word games, be my guest.
David Skelly wrote:
"A" gravitational constant?
Here "a" stands for acceleration, i.e. force per unit of mass (remember Newton?); there could be many forces, from what the OP tells us gravity could be one of them. There may be other forces such as friction in a fluid, springs, you name it. The minimum to satisfy the OP seems to be a constant due to gravity, hence "a gravitational constant" (which, if you really payed attention isn't really constant, as it depends on distance and homogeneity of the masses involved, but from where he started out, that would be good enough an improvement already). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
David Skelly wrote:
the difference between speed and velocity?
speed, velocity, derivative of position against time, it is all the same vector. If you want to play word games, be my guest.
David Skelly wrote:
"A" gravitational constant?
Here "a" stands for acceleration, i.e. force per unit of mass (remember Newton?); there could be many forces, from what the OP tells us gravity could be one of them. There may be other forces such as friction in a fluid, springs, you name it. The minimum to satisfy the OP seems to be a constant due to gravity, hence "a gravitational constant" (which, if you really payed attention isn't really constant, as it depends on distance and homogeneity of the masses involved, but from where he started out, that would be good enough an improvement already). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Luc Pattyn wrote:
speed, velocity, derivative of position against time, it is all the same vector
But speed isn't a vector. It's a scalar. Or to put it another way, it is the derivative of distance against time, not position against time.
Luc Pattyn wrote:
acceleration, i.e. force per unit of mass
No, acceleration is the rate of change of velocity versus time.
Luc Pattyn wrote:
if you really payed attention
Well, that was always my problem in physics classes. It was just so dull.
Luc Pattyn wrote:
it depends on distance and homogeneity of the masses involved,
No, I'm pretty sure the gravitational constant is the same everywhere throughout the universe, which is why it's called a constant. ;)
-
Luc Pattyn wrote:
speed, velocity, derivative of position against time, it is all the same vector
But speed isn't a vector. It's a scalar. Or to put it another way, it is the derivative of distance against time, not position against time.
Luc Pattyn wrote:
acceleration, i.e. force per unit of mass
No, acceleration is the rate of change of velocity versus time.
Luc Pattyn wrote:
if you really payed attention
Well, that was always my problem in physics classes. It was just so dull.
Luc Pattyn wrote:
it depends on distance and homogeneity of the masses involved,
No, I'm pretty sure the gravitational constant is the same everywhere throughout the universe, which is why it's called a constant. ;)
Here on Gallifrey, gravity is whatever we want it to be. ;)
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.