Thread: Bouncing off surfaces

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Bouncing off surfaces

    I'm looking to make a simple game/demonstration of a ball infinitely bouncing around in a box. I'm unsure of what formulas I should be using to get an acurate representation of a ball bouncing with a constant speed off of surfaces. If anybody can direct me to any resources I'd appreciate it.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You say box, so I assume you want the ball to bounce off a plane. A plane has a normal direction N (usually unit length, but we don't care for this discussion). You can write the velocity of the ball v as v1 + v2, where v1 is the velocity "with" the normal, or against the plane (which you get by doing a projection) and v2 is the velocity "with" the plane itself, or along the wall (the rest of the velocity). After the bounce, v2 will be the same (the ball will "slide" along the wall in this direction), but v1 will be replaced by -v1 (bouncing straight off the wall).

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    ..
    if (ball.x < box.left || ball.x > box.right)
    {
       ball.vx = -ball.vx;
    }
    
    if (ball.y < box.top || ball.y > box.bottom)
    {
      ball.vy = -ball.vy;
    }
    ...
    ...
    ball.x += ball.vx * speed * frameDelta;
    ball.y += ball.vy * speed * frameDelta;
    ...
    This is a simple non-physics based collision.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Only for perpendicular surfaces though.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm looking to make a simple game/demonstration of a ball infinitely bouncing around in a box.
    A box is composed of surfaces that are perpendicular.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a bouncing ball?
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2008, 03:41 AM
  2. bouncing bullets
    By dydoko in forum Game Programming
    Replies: 8
    Last Post: 09-18-2003, 08:31 PM
  3. Simple bouncing ball program
    By funkydude9 in forum Game Programming
    Replies: 3
    Last Post: 08-18-2003, 10:00 PM
  4. Ordered releasing of surfaces?
    By Hunter2 in forum Game Programming
    Replies: 8
    Last Post: 12-27-2002, 11:16 PM
  5. Bouncing ball - help ??
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 04-13-2002, 02:34 PM