0

After some time searching, I have revised my question.

I have found numerous examples of ball to ball collisions, but the only ones that seem to work use Vector2d or Vector2D.

This is a problem, because I am only allowed to use the regular java library, so my main question is: How do I convert the examples (which I will post below) to use what I can use?

I have several variables, both balls have the same mass, the velocities are broken into different variables, x and y. Also I have access to their x and y pos.

This is the ONLY problem left in my application.

I am at a total loss on how to convert the below example.

// get the mtd
Vector2d delta = (position.subtract(ball.position));
float d = delta.getLength();
// minimum translation distance to push balls apart after intersecting
Vector2d mtd = delta.multiply(((getRadius() + ball.getRadius())-d)/d); 


// resolve intersection --
// inverse mass quantities
float im1 = 1 / getMass(); 
float im2 = 1 / ball.getMass();

// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1 / (im1 + im2)));
ball.position = ball.position.subtract(mtd.multiply(im2 / (im1 + im2)));

// impact speed
Vector2d v = (this.velocity.subtract(ball.velocity));
float vn = v.dot(mtd.normalize());

// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;

// collision impulse
float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2);
Vector2d impulse = mtd.multiply(i);

// change in momentum
this.velocity = this.velocity.add(impulse.multiply(im1));
ball.velocity = ball.velocity.subtract(impulse.multiply(im2));

Here is the URL for the question:

http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling

And I have taken a look at his source code.

Thank you for taking the time to read this issue.

SUCCESS!

I have found how to use Vector2d, and it works PERFECTLY! Will edit later with answer!

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
DaCoder
  • 83
  • 1
  • 8

1 Answers1

0

I'm implementing my own 3d engine in c# based on a really basic 3d open-source engine in JavaScript called a3. I don't know If I have 100% understand you but It sounds like you can only find examples with Vector2d but you are not allowed to use that class?

I that is the case, as you can imagine javascript does not have native Vector2d types so someone had to implement. Don't be afraid of giving it a try, is just a few high school maths functions, you should be able to implement your own Vector2d class in just a few minutes

The following link contain implementations if vector2d, vector3d, vector4d, matrix3, and matrix4 in javascript: https://github.com/paullewis/a3/tree/master/src/js/core/math hope it helps :)

Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
  • I can distribute a .class that I made with the jar file, But I cannot find how to import it! – DaCoder May 15 '13 at 00:58
  • Do you mean sharing your code here? You can try to share it using gist on github https://gist.github.com/ and then share the link here, or edit your question above and add the code to it. – Remo H. Jansen May 15 '13 at 08:14