• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

ball collision angle

I am making a golf game and I need to find the angle and speed at which 2 balls would move if they collided. I know I can get the angle of the collision by using point_direction(x, y, otherBall.x, otherBall.y) but I don't know what to do from there.
 

Yal

šŸ§ *penguin noises*
GMC Elder
I am making a golf game and I need to find the angle and speed at which 2 balls would move if they collided. I know I can get the angle of the collision by using point_direction(x, y, otherBall.x, otherBall.y) but I don't know what to do from there.
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.)
 
if you want something very realistic, you can have a look at articles like this one :
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!
 

GMWolf

aka fel666
using complicated vector math
Stick to it, this kind of vector math is in reality much simpler than trigonometry (sin/cos/lengthdir_x...).
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.
 
Top