Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Physics and Math (not advanced)

  1. #11

    Physics and Math (not advanced)

    Quote Originally Posted by arthurprs
    Quote Originally Posted by Robert Kosek
    Hello,

    Your problem is in procedure Tform1.colision; because you loop through the balls twice. You might have more than one problem, but this is the one I really see.

    You only have two balls, and you know this, so why do you check the collision of the balls twice in nested for-loops? If ball 1 collided with ball 2, obviously ball 2 collided with 1! Yet, you loop through and check again. This is where the strange behavior comes in. To use a for loop with this sort of thing, you'll have to "register" collisions to avoid, uh, collision collisions.... How to make that less wacky and more understandable? Ah, prevent duplicate collisions. Sorry, for a second there I got stuck between cars in my train of thought.
    ...

    Thx for the help! ;]

    i will try what u say, but just one thing, i think the loop is necessary because the original source have 4 balls (you can see the dynamic array), isn't it ?


    *from brazil so sorry my english
    i have finnaly find the problem but don't know how solve it

    the balls get same V after the colision, so with same V balls continues coliding and getting more and more V

    A -> with 0.5v
    B <- with -0.9v

    result in

    A <- with -0.4v
    B <- with -0.4v

    =/

    [pascal]
    procedure Tform1.colision;
    var
    n, i: Integer;
    dist: double;
    begin

    for n := 0 to Length(ball) - 1 do
    begin // n = currentball

    ball[n].v2x := 0;
    ball[n].v2y := 0;
    ball[n].colided := False;

    // wall hit
    //x
    if (ball[n].x <= 0)
    or (ball[n].x2 >= Form1.ClientWidth) then
    begin
    ball[n].vx := -ball[n].vx;
    // todo: atrito 10%
    end;
    //y
    if (ball[n].y <= 0)
    or (ball[n].y2 >= Form1.ClientHeight) then
    begin
    ball[n].vy := -ball[n].vy;
    // todo: atrito 10%
    end;

    // other balls hit
    for i := 0 to Length(ball) - 1 do // i = other ball
    if (i <> n) then
    begin
    dist := distance(ball[i].cx, ball[i].cy, ball[n].cx, ball[n].cy);
    if dist < (ball[i].raio + ball[n].raio) then
    begin
    ball[n].colided := True;
    form1.Caption := 'colidiu bola : ' + IntToStr(n) + ' , ' + IntToStr(i) + ' na distancia de ' + FloatToStr(dist) + ' ' + FloatToStr(ball[n].vx) + ' ' + FloatToStr(ball[n].vy) + ' ' + FloatToStr(ball[i].vx) + ' ' + FloatToStr(ball[i].vy);
    ball[n].v2x := ball[n].v2x + ball[i].vx;
    ball[n].v2y := ball[n].v2y + ball[i].vy;
    end;
    end;
    end;

    for n := 0 to Length(ball) - 1 do
    begin
    if ball[n].colided then
    begin
    ball[n].vx := ball[n].vx + ball[n].v2x;
    ball[n].vy := ball[n].vy + ball[n].v2y;
    end;
    ball[n].Refresh;
    end;
    end;
    [/pascal]

    EDITED
    From brazil (:

    Pascal pownz!

  2. #12

    Physics and Math (not advanced)

    arthurprs, the board mangled your code. Please uncheck the HTML option in the post and clean it back up so we can read it .

  3. #13

    Physics and Math (not advanced)

    Quote Originally Posted by jdarling
    arthurprs, the board mangled your code. Please uncheck the HTML option in the post and clean it back up so we can read it .
    edited
    From brazil (:

    Pascal pownz!

  4. #14

    Physics and Math (not advanced)

    Gah! No pastebin, please! It is horrid for cut and paste so I can't excerpt and point out things in your code here. There's a checkbox over the "submit" button that says "Disable HTML in this post"; check that and leave the code in there please.

    It looks like the collision math itself is flawed. To me it seems you're just adding together the velocities of the collision balls withough any further computation. Are you using a specific formula?

  5. #15

    Physics and Math (not advanced)

    Quote Originally Posted by Robert Kosek
    Gah! No pastebin, please! It is horrid for cut and paste so I can't excerpt and point out things in your code here. There's a checkbox over the "submit" button that says "Disable HTML in this post"; check that and leave the code in there please.

    It looks like the collision math itself is flawed. To me it seems you're just adding together the velocities of the collision balls withough any further computation. Are you using a specific formula?
    no formula just ading the other ball V =/

    what is the correct way ?
    From brazil (:

    Pascal pownz!

  6. #16

    Physics and Math (not advanced)

    Ah! Therein lies the problem.

    If two balls collide in direct impact (head on) they don't come to a complete stop like your math would indicate. They bounce off each other in opposite directions. With your code two balls doing this will bounce in such a way that one stops and the other goes crazy.

    This page might help: http://www.physicsforums.com/archive...hp/t-8650.html

  7. #17

    Physics and Math (not advanced)

    Quote Originally Posted by Robert Kosek
    Ah! Therein lies the problem.

    If two balls collide in direct impact (head on) they don't come to a complete stop like your math would indicate. They bounce off each other in opposite directions. With your code two balls doing this will bounce in such a way that one stops and the other goes crazy.

    This page might help: http://www.physicsforums.com/archive...hp/t-8650.html
    :!: Vryyyy Complicated,

    when the code ready i'll post results
    From brazil (:

    Pascal pownz!

  8. #18

    Physics and Math (not advanced)

    Real code, real problem comes

    http://www.mediafire.com/?fdwtze33wwt
    Source + Demo exe


    [pascal]
    procedure Tform1.colision;
    var
    n, i: Integer;
    dist, dt: Double;
    dx, dy: Double;
    ax, ay: Double;
    vp_n, vp_i: Double;
    begin

    for n := 0 to Length(ball) - 1 do
    begin // n = currentball

    // wall hit
    //x
    if (ball[n].x <= 0)
    or (ball[n].x2 >= Form1.ClientWidth) then
    begin
    ball[n].vx := -ball[n].vx {* 0.9};
    end;
    //y
    if (ball[n].y <= 0)
    or (ball[n].y2 >= Form1.ClientHeight) then
    begin
    ball[n].vy := -ball[n].vy {* 0.9};
    end;

    // other balls hit
    for i := 0 to Length(ball) - 1 do // i = other ball
    if (i <> n) and not (ball[i].colided) and not (ball[n].colided) then
    begin
    *dx := ball[n].cx - ball[i].cx;
    *dy := ball[n].cy - ball[i].cy;
    *dist := Sqrt(Power(dx, 2) + Power(dy, 2));
    if dist <= (ball[i].raio + ball[n].raio) then
    begin
    *ax := dx / dist;
    *ay := dy / dist;
    *vp_n := ball[n].vx * ax + ball[n].vy * ay;
    *vp_i := ball[i].vx * ax + ball[i].vy * ay;
    *dt := (ball[n].raio + ball[i].raio - dist) / (vp_n + vp_i);
    *ball[n].x := ball[n].x - (ball[n].vx * dt);
    *ball[n].y := ball[n].y - (ball[n].vy * dt);
    *ball[i].x := ball[i].x - (ball[i].vx * dt);
    *ball[i].y := ball[i].y - (ball[i].vy * dt);
    ball[n].colided := True;
    ball[i].colided := True;
    form1.Caption := 'colidiu bola : ' + IntToStr(n) + ' , ' + IntToStr(i) + ' na distancia de ' + FloatToStr(dist) + ' ' + FloatToStr(ball[n].vx) + ' ' + FloatToStr(ball[n].vy) + ' ' + FloatToStr(ball[i].vx) + ' ' + FloatToStr(ball[i].vy);
    ball[n].v2x := ball[n].v2x + ball[i].vx; // this will change, but need solve the oter problem first
    ball[n].v2y := ball[n].v2y + ball[i].vy;
    ball[i].v2x := ball[i].v2x + ball[n].vx;
    ball[i].v2y := ball[i].v2y + ball[n].vy;
    end;
    end;
    end;

    for n := 0 to Length(ball) - 1 do
    begin
    if ball[n].colided then
    begin
    ball[n].vx := ball[n].vx + ball[n].v2x;
    ball[n].vy := ball[n].vy + ball[n].v2y;
    ball[n].v2x := 0;
    ball[n].v2y := 0;
    ball[n].colided := False;
    end;
    ball[n].Refresh;
    end;
    end;
    [/pascal]

    Based on http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0 i write some code to make distance of balls be ball1Ray + ball2Ray
    i marked the lines in the code with a *

    i think i writed it correctly, but it don't work, the code moves the ball to a wrong place =/ and not when the dist = ball1Ray + ball2Ray

    :?
    From brazil (:

    Pascal pownz!

  9. #19
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Physics and Math (not advanced)

    Ok... it would really help if you explained in plain english, instead of trying to explain it in code. For all we know, you are new to programming in general and your problem could range anywhere from physics, implementation or standard coding practices.

    We understand that your english is weak [size=9px](thats ok a LARGE amount of visitors here don't have English as their first language too)[/size] but the best way is to get the problem across in people language first. We also can spot your mistakes quicker.

    Like this I'm afraid I can't tell if you whole code is 'out to lunch' [size=9px](English expression for totally wrong)[/size] or not.


    What I can offer from what I see; Make sure that you are not moving everything before you know where it all is going to go first. In other words, If you have 2 balls that are to bump into each other and react on their own then try this:

    1) move ALL balls

    2) detect ALL collisions

    3) change velocity on ALL balls with a collision

    4) go back and start at 1...

    Now you can combine 2 and 3, but do NOT combine 1 and 3 or 1 and 2. Imagine a whole table full of pool balls bouncing around and moving... try to determine where they are all going... confusing huh? Sort of the same deal here.

    Hope this little bit of theory helps.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  10. #20

    Physics and Math (not advanced)

    Quote Originally Posted by WILL
    Ok... it would really help if you explained in plain english, instead of trying to explain it in code. For all we know, you are new to programming in general and your problem could range anywhere from physics, implementation or standard coding practices.

    We understand that your english is weak [size=9px](thats ok a LARGE amount of visitors here don't have English as their first language too)[/size] but the best way is to get the problem across in people language first. We also can spot your mistakes quicker.

    Like this I'm afraid I can't tell if you whole code is 'out to lunch' [size=9px](English expression for totally wrong)[/size] or not.


    What I can offer from what I see; Make sure that you are not moving everything before you know where it all is going to go first. In other words, If you have 2 balls that are to bump into each other and react on their own then try this:

    1) move ALL balls

    2) detect ALL collisions

    3) change velocity on ALL balls with a collision

    4) go back and start at 1...

    Now you can combine 2 and 3, but do NOT combine 1 and 3 or 1 and 2. Imagine a whole table full of pool balls bouncing around and moving... try to determine where they are all going... confusing huh? Sort of the same deal here.

    Hope this little bit of theory helps.
    edited the post, and 99% sure that the error are in the new code ops:
    cuz i tried only to "-V" and it worked perfecly
    From brazil (:

    Pascal pownz!

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •