java - Recursion virgin getting stack overflow error with collision detection -


i have ball class has position, velocity, , acceleration vectors attributes. program instantiates 2 ball objects , moves them around according gravitational force between them.

i have working great, trying implement collision detection feature makes balls bounce off of 1 another.

here method updates velocity:

    public chaney2dvector collisionvelocity(ball ball2) {     float x = velocity.x + ((ball2.mass / mass) * (ball2.velocity.x -              ball2.collisionvelocity(this).x));     float y = velocity.y + ((ball2.mass / mass) * (ball2.velocity.y -              ball2.collisionvelocity(this).y));     chaney2dvector updatedvelocity = new chaney2dvector(x,y);     velocity = updatedvelocity;     return velocity;  } 

i've used method called recursion, haven't i? first time trying this, apologies if i've done wrong.

when program runs, have collision condition active in update method:

    if (this.getdistance(ball2) < (this.radius + ball2.radius))     {         velocity = this.setvelocity(collisionvelocity(ball2).x,                 collisionvelocity(ball2).y);     } 

i want program assign new collision velocity velocity variable in each ball. when run program, balls move toward each other fine. however, when balls collide don't bounce... stick in stationary position , stack overflow error. read elsewhere may have termination condition, have no idea means. ideas??

thanks lot help!

i wrote simple example of recursion without terminating condition. example not find end. java call method, causes java call method again. neverending story:

public class snippet {      public static void main(string[] args) {         recursionwithoutterminatingcondition();     }      private static void recursionwithoutterminatingcondition() {         recursionwithoutterminatingcondition();     }  } 

this other example better. java start calling method on , on, reduces parameter value each call. lead ending:

public class snippet {      public static void main(string[] args) {         recursionwithterminatingcondition(20);     }      private static void recursionwithterminatingcondition(int numberoftimes) {         if (numberoftimes > 0)             recursionwithterminatingcondition(numberoftimes - 1);     }  } 

to specific case:

  • if declare, collision velocity of 1 ball depends on collision velocity of other one, java cannot find solution.

some common ways of building workarounds kind of problems:

  • if declare, collision velocity of 1 ball depends on other ball's collision velocity of last time interval (so stored value of last calculation), java can calculate values.
  • alternatively let java calculate collision velocity of first ball, assumpting other collision velocity 0. , use calculated collision velocity calculate other ball's collision velocity, , on. declare, java can stop recalculating values, each recalculation resulting in change of less 0.01 (or similar).
  • or maybe there other formula, not depending on other balls collision velocity.

sadly physic knowledge short formulas :(


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -