Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Breakout

OceanJeffOceanJeff Member Posts: 15
Here is a fairly simple problem, but I am working on a solution:

You have a ball that moves around the screen at velocity from 0 to 10 in 360 degrees. There is a large block in the center of the screen, rectangle shaped.

The question is this: What is the best way to bounce the ball off the rectangle in the center of the screen?? I have already got the IFs down to bounce the ball off the sides of the screen, but that's easy: IF the ball goes out of bounds in one axis, you multiply the velocity by NEGATIVE ONE.

Let me know if you have any ideas,

Jeff Cummings

P.S. This is in 2D.

Comments

  • CroWCroW Member Posts: 348
    [b][red]This message was edited by CroW at 2004-3-31 13:36:14[/red][/b][hr]
    hmm,just multplying with -1 results in a 45 degree-bounce:

    [edited: acii-picture removed,coz it doesnt display correctly :(]

    the mathematical correct way should use two vectors:

    V1 = direction of the ball
    V2 = the 'wall' (or a normal of it)

    now calculate the angle between both vectors:

    cos alpha = V1 * V2 <- scalar-product of V1 and V2!!

    now that you know the angle between the incoming-vector of the ball and your wall you could easily calculate your reflection-angle and then rotate your incoming vector to that angle.Its a good idea to have a math-book by the hand :)
  • HEPHEP Member Posts: 113
    The IntersectRect function may be useful:
    [code]
    RECT ball;
    RECT block;
    RECT col;

    // ...

    if(IntersectRect(&col, &ball, &block))
    {
    if(col.right - col.left > col.bottom - col.top)
    SetVelY(-GetVelY()); //ball hits block at bottom or top
    else
    SetVelX(-GetVelX()); //ball hits block at left or right
    }
  • OceanJeffOceanJeff Member Posts: 15
    Thanks for the replying guys, but I figured it out! I've got a complete breakout game now, and I've upgraded to Direct X 9.0b (not c)

    I modified a collision detection subroutine that I found, and it bounces perfectly in 360 degrees.

    Anyways, I'm working on Assembly Language version only with DEBUG of all things, and I may create just a DOS TEXT version of this program...there goes my direct x experience.

    later,

    jeff
    jeffwebgamer@hotmail.com
Sign In or Register to comment.