Handmade Hero » Forums » Code » Elastic Collision recommendations
itzjac
33 posts
#17391 Elastic Collision recommendations
1 month ago Edited by itzjac on Feb. 11, 2019, 12:45 a.m. Reason: Initial post

Hi everyone,

I was looking for a reference to implement a simple elastic collision system in 2D. I found this thread, which points to a really awesome material regarding Real Time Collision

Recommendations for 2D collision physics references

That will be great for later, but at this point I just want to follow up on the simple and traditional formulas Elastic Collision, if you scroll down to the bottom you will find the 2D solution of the problem.

They mention two possible solutions, one using angles and other using dot product(which in the end uses another angle anyways).
Any other recommendation you would suggest me?

Feel free to extend the discussion to 3D as well, I might use that too later, at this point I just need the 2D version.

Appreciate any help.

Have a nice one!
mrmixer
Simon Anciaux
565 posts
#17394 Elastic Collision recommendations
1 month ago

I don't know what you're looking for exactly but you can search the forums for GJK (collision detection algorithm) and you'll find some resources.

Rigid body dynamics by Chris Hecker should be good (but I never read them).
itzjac
33 posts
#17396 Elastic Collision recommendations
1 month ago Edited by itzjac on March 13, 2019, 12:42 a.m.

Will take it from there. Thank you
OliverMarsh
Oliver
120 posts / 2 projects

A budding game developer and programmer

#17397 Elastic Collision recommendations
1 month ago Edited by Oliver on Feb. 13, 2019, 4:55 a.m.

Chris Hecker's rigid body dynamics is good, he derives the formula for impulse resolution, which has a coefficient of restitution, which when one gives the elastic rebound effect (i.e. no energy is lost in the collision.) The equation derived is also in Ian Parberry's 3D math primer for graphics & game development, which I found a bit easier to follow.

Chris Hecker's article assumes you have some way of detecting when there is a collision, and requires you have two bits of information: 1.the normal of the surface you hit and 2. the point of impact (only required when shapes can have angular momentum).

I think the hardest part is the actual detection of collision and returning the information needed for the resolution. The best solution I've fount is Erin Catto's GJK implementation . The source code is on the box2d site under downloads. And I think it is the method used in Box2D.

Note: this is different to Casey's use of GJK. Casey's method gives you a yes/no answer whether something is overlapping, whereas Erin's gives you the information you need to solve the collision resolution equations (point of collision, and surface normal).

NOTE: You can use Casey's method with the EPA algorithm to resolve a collision (i.e. not overlap anymore) and find the surface normal, but from what I know, you can't get the Point of Collision from it.
itzjac
33 posts
#20504 Elastic Collision recommendations
1 month ago Edited by itzjac on Feb. 16, 2019, 4:07 p.m.

Thanks Oliver.

After looking at the documents I choose to start with the Box2D examples, they seem to be the most complete.

But jeez!!! Talking about distribution, getting the Box2D Testbed working is a huge pain.

I am not sure if I was too stupid to simply compile the Box2D out of the box, as is from github.
The amount of dependencies is ridiculous: freglut, glew, glfw, glui and yet you have to add manually the asset to run the executable independently. :( :(
The CMake files contain some errors and std::min, std::max functions have to be undef to work along with Windows.h.
Sad and done!

I am sharing you the example compiled for windows in case you would like to try it out. A good chance to earn community's trust, you will get to know is not a surprise:

Box2D Testbed

This is a great start, enjoy the samples they are worth just watching :)
Tiles

PS: Is it possible to add images to this forum and share links to the uploaded images? What is it used the img tag then?
mmozeiko
Mārtiņš Možeiko
1901 posts / 1 project
#20505 Elastic Collision recommendations
1 month ago Edited by Mārtiņš Možeiko on Feb. 16, 2019, 10:01 p.m.

Not sure what kind of Box2D you were compiling, but code on GitHub is built with premake, not cmake. It includes all the dependencies in source. And I don't know where you are seeing freeglut. It uses only glfw to create window.

Also reminder for people to be careful when running random binaries found on internet.
itzjac
33 posts
#20506 Elastic Collision recommendations
1 month ago Edited by itzjac on Feb. 16, 2019, 11:27 p.m.

We are in the same page GitHUB Box 2D, but with different branches.

I downloaded from the release page Box2D releases, which I expected to be the most reliable and stable version(s), those indeed contain CMake and dependencies, but have several errors in the CMakeLists files.

I guess for the next I better live with the newest and use premake, (not sure really :S).


mmozeiko
Mārtiņš Možeiko
1901 posts / 1 project
#20507 Elastic Collision recommendations
1 month ago Edited by Mārtiņš Možeiko on Feb. 17, 2019, midnight

Well... last "release" was in on Apr 5, 2014. Not sure what you expected there :) I guess if you would use same tools, same versions (cmake, VS, etc...) as in 2014 then everything would compile without errors.

Usually git master will be most stable and reliable code, as most bugs are fixed there and code updated to work with latest OS/tools and not in old releases.
itzjac
33 posts
#20508 Elastic Collision recommendations
4 weeks, 1 day ago

Thanks mmozeiko.

I must admit this was due to biased opinions, the last projects we usually have master with the latest but not stable.

It is way easier with premake, just a min to get all running.
It still had an error, complaining with the default SDK, but switched to the one I have installed and no errors and, one of the previously mentioned dependencies, a data folder to execute the game stand alone, is still in latest :(

Now finally, time to study the physic's code am interested.