1

I'm currently working on a simple ball collision program in C#, where an indeterminate amount of balls bounce around the screen, each being added to the program on a button click. So far I've gotten everything working, and I have a basic collision system. Now, I'm trying to add more realistic physics to the balls.

The thing is, I have little understanding of physics. I've been reading a bit about vector physics and I get the gist of it, but I'm not quite making the connection between what I've read and how I'm supposed to implement these concepts programmatically. Could anybody provide me some resources on how one is actually supposed to implement vector physics to something like a simple ball collision program like I've written?

Here's what I have right now. It's basically just two booleans determining whether my ball goes in one of four directions. When two balls collide, they just pass their directions to each other.

public void newball(PaintEventArgs e, int panelWidth, int panelHeight)
{
    //Detects walls of form. if it detects a wall or another ball, it bounces
    if (X >= panelWidth)
    {
        bounceX = true;
    }
    else if (X <= 25)
    {
        bounceX = false;
    }

    if (Y >= panelHeight )
    {
        bounceY = true;
    }
    else if (Y <= 25)
    {
        bounceY = false;
    }

    //balls only go in four directions right now have to implement vector physics.*/ 
    if (bounceX == false)
    {
        X = X+2;
    }
    else if (bounceX == true)
    {
        X = X -2;
    }
    if (bounceY == false)
    {
        Y = Y+2;
    }
    else if (bounceY == true)
    {
        Y = Y-2;
    }

    //this draws the ball on the panel.
    Pen clsPen = Pens.Black;
    Color color = Color.Black;
    SolidBrush brush = new SolidBrush(color);
    e.Graphics.DrawEllipse(clsPen, X - 25, Y -25, 25, 25);
    e.Graphics.FillEllipse(brush, X-25, Y-25, 25, 25);
}

And here are some of the things I've read:

2D Collisions, Vectors

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
AllyOmega
  • 45
  • 1
  • 5
  • 1
    possible duplicate of [Ball to Ball Collision - Detection and Handling](http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling) – Saro Taşciyan Feb 10 '14 at 01:00
  • 2
    If you want realistic motion, you should implement realistic 1-ball motion before you worry about collisions. Do you need help with vectors? – Beta Feb 10 '14 at 01:37
  • Yeah, vectors are what I'm struggling with. I understand them in an abstract way, but I'm having trouble how exactly I'm supposed to implement them programmatically. I'm just not sure how I'd add velocity, magnitude, etc. to a ball and use that to make the ball go in a certain direction. The best thing for me would probably be to see example code, but I haven't been able to find anything I've found readable. – AllyOmega Feb 10 '14 at 01:42

0 Answers0