- Thread starter if (user.brain == NULL)
- Start date

With the help of the Stack Overflow community I've written a pretty basic-but fun physics simulator. You click and drag the mouse to launch a ball. It will bounce around and eventually stop on the "

stackoverflow.com

I'm thinking something like this would happen:

- The stationary ball would get part of the moving ball's momentum
- This only is affected by the projection of the inertia along the collision vector
- Thus, you'd get the projection speed by getting the dot product of the movement vector (hspeed,vspeed) and the unit vector in the angle of collision (lengthdir_x( point_direction(x, y, otherBall.x, otherBall.y)), lengthdir_y(point_direction(x, y, otherBall.x, otherBall.y)))
- compute the speed and direction, then use motion_add to add that speed to the stationary ball. Add the same value but to angle + 180 for the moving ball, creating the equal-but-opposite force as per Newton's laws.
- the speeds also gets affected by the balls' "bounciness constants" which are just arbitrary numbers you tweak until they feel right. Larger balls have a lower bounciness since you need more force to move them
- you might also want a friction multiplier which is < 1, but close to 1. Using that, some speed is always lost on a collision.
- If both balls are moving, you'd compute all the inertia transfer first, and then apply it to both balls at once. (This will have the same effect as the code outlined above if one of the balls is stationary, since the inertia it transfers is zero if its speed is zero, don't worry.)

Thank you for the link, I went to the site and found another link to an article on 2d ball collisions. I am slowly making my way through the paper as I am only in algebra 1 and the article is using complicated vector math. Wish me luck!

## Ball to Ball Collision - Detection and Handling

With the help of the Stack Overflow community I've written a pretty basic-but fun physics simulator. You click and drag the mouse to launch a ball. It will bounce around and eventually stop on the "stackoverflow.com

Stick to it, this kind of vector math is in reality much simpler than trigonometry (sin/cos/lengthdir_x...).using complicated vector math

And it much more powerful too, angles are a code smell...

If you are writing a golf game some simple vector maths will go a very long way, simple things like vector adition, scaling, and slightly more complex operations like dot products. You don't need much more than that.

The way I remember dot products is they tell you how similar two angles are.

In more mathematical terms, they give you the cosine of the angle between the two vectors, multiplied by the vector length.

But with vector addition, scaling, length, and dot products you have absolutely all the tools to do golf game physics.