r/Python • u/mbecks • Jul 21 '19
Physics simulator with rotation and collision I made with Python. If you dropped your phone, and it just kept bouncing!
189
u/mausthekat Jul 21 '19
Nice! Now you just need to add in a coefficient of restitution so the poor thing can stop bouncing!
21
u/mbecks Jul 21 '19
Definitely! I wanted to get this stable bounce first though. The conditionals for the collision become more difficult as the block gets closer and closer to being at rest on the surface, often resulting in sinking/jittering
9
u/deflatedfruit f-string fanboy Jul 21 '19
I've made a 2D physics engine and have the same issue with sinking. If you do get a solution working, can you let me know please?
10
u/mbecks Jul 21 '19
I've been thinking about an 'on_surface' bool attribute to the node object that can flip to true and cancel out gravity with a normal force.
5
u/SV-97 Jul 21 '19
Couldn't you just add a decay/timestep to the velocity if the distance of any point to the surface is below a given treshold? That's how I modeled decaying orbits (well with a decay every timestep, without distance to a surface :D ) and it worked great.
3
u/mbecks Jul 22 '19 edited Jul 22 '19
Solved it pretty much like this. I find the energy conservation delta v of collision then take away some fixed proportion of it, so basically:
delt_v=get_cons_energy_delt_v(inputs)
delt_v=0.8*delt_v2
u/SV-97 Jul 22 '19
So basically the delta v of "shit going down hitting the bottom and going up afterwards"?
3
0
Jul 21 '19
[deleted]
6
Jul 22 '19
Plus you could throw it super hard at that magic surface and it wouldn't affect the phone at all, just how bouncy it is. Fun!
12
u/fatbob42 Jul 21 '19
How do you do the graphics?
18
u/mbecks Jul 21 '19
7
Jul 21 '19
I found matplotlib animation way too unwieldy and now use moviepy to create plot animations
1
u/mbecks Jul 21 '19
I'll definitely look into that. How is the speed?
1
Jul 21 '19
Decent enough
1
Jul 21 '19
better than matplotlib??
3
Jul 21 '19
To be clear I still use matplotlib to draw the frames just use moviepy to put them together
1
u/Astrokiwi Jul 22 '19
I just dump the pngs and use ffmpeg. Then I don't need to worry about memory usage. And if it crashes part way through some fairly heavy rendering, I still have some output to look at.
1
2
2
Jul 21 '19
any chance youll release the code? I'm working on something similar for generating these geometries but have nothing for running any analysis on them besides commercial codes
2
u/mbecks Jul 21 '19
Done. Can the tool create mass distributions in various geometries? I would also be interested in getting the code for that
2
Jul 21 '19
mine takes a 3D binary or grey scale numpy array and makes it into a set of spheres and does labelling for non continuous arrays
16
u/EleLasoo Jul 21 '19
Wow mate thats extremly cool! I want more info and the source code!!!!! Which libraries did you use? What physics did you use??
6
u/Fixxan Jul 21 '19
This is why phone cases are not made out of Super bouncy ball material.
Great demonstration!
2
u/omsalta Jul 21 '19
It seems something is missing.. Maybe conservation of linear momentum as the object doesn't translate in x or y.
2
u/mbecks Jul 21 '19
The collision assumes the delta v from the collision is in the the z direction, normal to the floor. This does not conserve linear momentum as the floor does not move, it basically has infinite mass so momentum conservation of the system becomes meaningless. The blocks x and momentum stay and remain 0, as the momentums of the points about the blocks center of mass all cancel out.
2
u/omsalta Jul 22 '19
My point still stands.. If your collision assumes delta v in the z direction only, then your object wouldn't translate it in x or y. I was trying to point out that this makes it look unrealistic unless you want it this way for a specific reason.
2
u/mbecks Jul 22 '19
I created it this way because the conservation of energy equation where delta v is arbitrary 3d vector is complicated and underdetermined (one eq 3 variables). Assuming delta v purely in z direction collapses the equation into something solvable without any other assumptions (one eq one variable). I would like to add in friction and get some horizontal component to delta v eventually, but one step at a time.
2
u/Le_stormwolf Jul 21 '19
The idea of my phone falling and bouncing forever is giving me a mild anxiety attack.
Nice simulation, but please, don't make such a simulation ever again.
I reckon Satan just added this simulation to his tool belt of torture devices. Yeah, for sure.
1
4
1
u/BoppreH Jul 21 '19
That's pretty cool! Is it me or is it gaining energy and bouncing higher? This is a common problem when using a naive integrator like Euler.
3
u/mbecks Jul 21 '19
Energy goes into rotation, when it's rotating more, it goes less high, and vice versa. I've been analyzing the energy throughout and it is stable in the same range. This uses rk4 for rotation and a simple fast corrected method for moving the center of mass position. I am honestly still figuring out how energy is moving around.
1
u/BoppreH Jul 21 '19
If you are already tracking total energy, then it's probably my intuition that's wrong.
1
2
2
2
2
2
u/GickRick Jul 22 '19
Wow i only solved live charts with matplotlib, man it's awesome to find out that you can actually animate 3D objects.... Awesome work💪💯, when i get free time i can help with refactoring the code
2
u/tripex Jul 22 '19
Where is that license bot i asked for a while ago? Cool project, please pick a license.
2
u/Addena_9 Jul 22 '19
Great. Isn't it not enough to just have one corner of my screen broken. Now all of them are. Thanks!
1
1
1
1
u/Fernando3161 Jul 21 '19
Very cool! Can you input the rebound coefficient as a variable and see what happens?
1
u/danemacmillan Jul 21 '19
This is very inaccurate. My phone has only ever fallen flat on its face, no bounce, for maximum screen destruction.
1
1
u/thekalmanfilter Jul 22 '19
Wow you can do a moving animation with code?? How does that work? I can do a line chart but how do you keep it moving???
1
1
1
u/lucidmath Jul 22 '19
This is awesome! I don't know much about physics, what are the principles behind this?
1
u/StarkillerX42 Jul 22 '19
Remove the axis, flip the colors to a black background, and voila you have a new screensaver
0
Jul 21 '19
[deleted]
11
u/mbecks Jul 21 '19
conservation of angular momentum in free space. for the collisions, conservation of kinetic energy, including rotational energy, before and after collision.
0
Jul 22 '19
[deleted]
1
u/RemindMeBot Jul 22 '19
I will be messaging you on 2019-07-26 12:17:11 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
3
46
u/mbecks Jul 21 '19 edited Jul 22 '19
here is the code! link to github
if you are interested in running it, all the files must be in the same folder, and you must have ffmpeg installed to write the mp4.
In main.py, you can change block.animate2(....) to block.animate(....) to create just the bouncing block, or just run animate2 to see in addition the how values for kinetic (top) and potential (bottom) energy change as the block bounces. You can also mess with the initial conditions, although changing Elc to anything other than 1 usually causes errors.
edit: took some files down to upload some changes and having trouble uploading them... should be up soon