|
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)
CurrentDirection = BallDirection.Right;
if ((x + w) >= ScreenW)
CurrentDirection = BallDirection.Top;
if ((x) <= 0)
CurrentDirection = BallDirection.Bottom;
if ((y) <= 0)
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();
}
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
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 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 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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Experts,
I have created one windows application and deployed it in some 500 machines in my company. The problem is, the applicaiton is updated weekely atlest. I cannot reinstall the .exe once again in all the 500 machines. Any solution for this.........
|
|
|
|
|
For that you need to develop ClickOnce application for automatic
update of your installed application.
Following is very nice articles series for the same.
Adding automatic updates to your program - Part 1[^]
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Hi
I am making the setup of my application using wix.
I want to Write application version no and installation path to registry so that it write to the path I define in wix code
Please help
Thanks
|
|
|
|
|
Your solution is here[^].
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Thanks
But How can I capture the application installation path and store it to registry
as user can install it to c drive or any other drive or location.
|
|
|
|
|
|
in my c# application i made a webrequest to download a file , it works well in xp, but in win 7 it gives a webexception saying timeout, can any one help me with this.
|
|
|
|
|
Do you get the same exception after switching off the firewall?
This statement is false.
|
|
|
|
|
drawn an rectangle on the image using picture box.
quires
1. when tried to zoom image the drawn rectangles needs to be placed on the drawn area.
2. Also draw another rectangle (in zoomed mode).
3. when i zommed out the rectangle needs to resized.
How can i achieve this.
I achieved this by using graphics.drawrectangle but i don't want to draw rectangle on the image because it's stay permanently on the image.
Please help on this......
public void ZoomIn()
{
if (sz1.Width >= 800 * 6)
MessageBox.Show("Max ZoomIn");
else
{
sz1.Width += 100;
sz1.Height += 100;
pictureBox1.Size = sz1;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (mybitmap == null)
{
mybitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
mybitmap.SetResolution(300, 300);
}
rect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mybitmap == null)
{
return;
}
using (Pen pen = new Pen(Color.Green, 2))
{
foreach (Rectangle r in rectangles)
{
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawString(lab[c].ToString(), new Font(lab[c].ToString(), 8F), new SolidBrush(label1.ForeColor), r);
}
}
}
modified on Tuesday, July 13, 2010 4:15 AM
|
|
|
|
|
You can't seriously expect anyone to wade through all of this code. Focus in on a specific issue and show a code fragment: if anyone wants to help and wants to see more they'll ask but you'll not get an answer whilst your question remains like this.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
Sorry i thought that full code will use to analyze the issue. Now i have edited and pasted the code. please advice me....
I have drawn rectangle. Now i tried to zoom the image and succeed on that. my problem is when i zoom the image the rectangle drawn stay in same size, but rectangle also needs to be increase along with image.
Please help on this....
|
|
|
|
|
It looks like you are not resizing the rectangle.
Just racking up the postings
|
|
|
|