Demo of Simple 2D Gravitational Physics
Blitz3D Forums/Blitz3D Beginners Area/Demo of Simple 2D Gravitational Physics
| ||
I'm no beginner but I just did this for fun and it's actually so simple yet so interesting (to me anyway) that I thought I would post it in the beginners area as it covers using functions types and some simple physics and Psuedo- realtime functions. By playing with the settings you can create some very complex patterns. I didn't code in a nice interface because that would defeat the object of posting the code. Play with the code :) Things to play with - Satelite starting position and dx, dy values and its mass gravity well strength and position Try with just one well if you like just comment out the lines that draw and resolve forces on w2 Try adding another well to make 3 of them. Type satelite Field x#;statelite position x and y coord Field y# Field dx#;speed of the satelite in x and y dir Field dy# Field fx#;forces acting on the satelite in x and y directions Field fy# Field mass; mass of the satelite End Type Type well Field gravity#; gravitational force of the well Field x#; wells xy coords Field y# End Type Graphics 800,600,32,2 Global period# this_time=MilliSecs() Delay 10 ;make a satelite and set it with some initial values s.satelite =New satelite s\x = 400 s\y = 250 s\dx=1.5 s\dy=0 s\mass = 5 ;create a gravity well w.well= New well w\x=400 w\y=300 w\gravity=70000 ;and another for good measure w2.well= New well w2\x=300 w2\y=300 w2\gravity=70000 SetBuffer BackBuffer() drawWell(w) drawWell(w2) While Not KeyHit(1) period#=0.01 s\fx=0 s\fy=0 resolveForces(s,w) resolveForces(s,w2) updateSatelite(s,w) drawSatelite(s) Flip ;note there is no CLS in this because we want to see the satelites path ;and not just its current position Delay 10 Wend Function resolveForces( s.satelite, w.well) ;the force on the satelite always works towards the well ;the force on the satelite is proportional to 1/r^2 ;where r is the distance between the satelite and the center of gravity ;find the wells position relative to the satelite Local dx#=w\x-s\x Local dy#=w\y-s\y ;calc r^2 Local r2#=((dx)^2+(dy)^2) ;calc r Local r#=Sqr(r2) ;calc mag of force at the satelites position Local f#= w\gravity * r2^-1 ;resolve in the x and y s\fy=s\fy+f*(dy/r) s\fx=s\fx+f*(dx/r) End Function Function updateSatelite(s.satelite, w.well) ;apply newtons laws in x and y directions dx#=s\dx ;newtons law f=ma re-arranged to be a=f/m ax#=s\fx/s\mass ;newtons law v=u+at s\dx=dx+ax*period dy#=s\dy ay#=s\fy/s\mass s\dy=dy+ay*period s\x=s\x+s\dx s\y=s\y+s\dy End Function Function drawSatelite(s.satelite) ;just a dot will do Plot s\x,s\y End Function Function drawWell(w.well) ;a nice + to mark the center of gravitation Line w\x-5,w\y,w\x+5,w\y Line w\x,w\y-5,w\x,w\y+5 End Function |
| ||
When I say pseudo realtime what I mean is the functions which calculate the position of the satelite do so "incrementaly" meaning that they take the last position and increment it based on the x and y velocities proportional to the time that has passed in the last program loop (the period). Its pseudo realtime because I forced the period to be 10 milliseconds. See if you can calculate the time that passed in the last loop each time. Then the code will be proper realtime. This is the first program I have ever coded that worked as intended first time with no compile errors and I been programming for over 10 years, It does happen sometimes, just not very often :D |