Thursday, March 20, 2008

Gravity Fields

To use the force you must learn the force. Gravity is Newton's force.
Simulation is a great way to learn about gravity. So is dancing.
This is a simple physics engine for simulating a particle in a central force field. The code is in the comments section. Here is an example of what you can do with it.

1 comment:

Tony said...

max v2;
#N vpatcher 251 72 1326 670;
#P origin -25 0;
#P window setfont "Sans Serif" 20.;
#P comment 635 404 122 196628 SPACEBAR TOGGLES TRAILS;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P message 9 117 31 196617 0.01;
#P flonum 10 146 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 340 160 940 560;
#P origin 0 -6;
#P window setfont "Sans Serif" 9.;
#P window linecount 0;
#P newex 533 152 43 196617 s space;
#P newex 190 94 54 196617 pack 1. 2.;
#P newex 259 51 27 196617 f;
#P window linecount 1;
#P newex 190 71 50 196617 poltocar;
#B color 5;
#P window linecount 0;
#P newex 211 48 29 196617 t b f;
#P newex 174 25 47 196617 cartopol;
#P inlet 279 -3 15 0;
#P newex 472 123 98 196617 sel 30 31 28 29 32;
#P number 426 123 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 447 85 40 196617 key;
#P outlet 211 346 15 0;
#P window linecount 1;
#P message 50 171 132 196617 0.003849 -0.00923;
#P newex 50 149 62 196617 prepend set;
#P newex 283 126 61 196617 vexpr -$f1;
#P message 284 169 118 196617 -0.003849 0.00923;
#P newex 284 147 62 196617 prepend set;
#P message 224 258 118 196617 -0.00923 -0.003849;
#P newex 221 243 62 196617 prepend set;
#P message 179 307 118 196617 0.00923 0.003849;
#P newex 152 282 62 196617 prepend set;
#P flonum 276 27 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 221 222 61 196617 vexpr -$f1;
#P newex 179 192 54 196617 pack 1. 2.;
#P newex 196 132 50 196617 expr $f1;
#P newex 179 156 55 196617 expr -$f2;
#P newex 167 123 27 196617 t l l;
#P newex 174 3 85 196617 vexpr ($f1-$f2);
#P newex 212 -25 27 196617 t l l;
#P inlet 212 -45 15 0;
#P inlet 174 -17 15 0;
#P connect 28 0 17 0;
#P connect 17 0 18 0;
#P connect 22 0 18 0;
#P connect 7 0 10 0;
#P connect 28 0 4 0;
#P fasten 2 1 3 0 234 1 179 1;
#P connect 0 0 3 0;
#P connect 3 0 24 0;
#P connect 4 0 5 0;
#P connect 5 0 7 0;
#P connect 10 0 11 0;
#P connect 22 3 11 0;
#P connect 27 0 26 0;
#P connect 26 0 28 0;
#P connect 4 1 6 0;
#P connect 24 1 25 0;
#P connect 15 0 19 0;
#P connect 13 0 19 0;
#P connect 11 0 19 0;
#P connect 18 0 19 0;
#P connect 1 0 2 0;
#P fasten 7 0 8 0 184 213 226 213;
#P connect 8 0 12 0;
#P fasten 6 0 7 1 201 180 228 180;
#P connect 12 0 13 0;
#P connect 22 2 13 0;
#P connect 25 1 26 1;
#P connect 26 1 28 1;
#P fasten 2 0 3 1 217 -4 254 -4;
#P connect 25 0 27 0;
#P connect 23 0 9 0;
#P connect 9 0 27 1;
#P connect 28 0 16 0;
#P connect 16 0 14 0;
#P connect 14 0 15 0;
#P connect 22 1 15 0;
#P connect 20 0 21 0;
#P connect 20 0 22 0;
#P connect 22 4 29 0;
#P pop;
#P newobj 13 198 38 196617 p;
#N vpatcher 139 281 918 695;
#P window setfont "Sans Serif" 9.;
#P message 367 268 142 196617 setpixel 254 127 255 255 0;
#P newex 340 211 20 196617 t b;
#P message 362 229 186 196617 linesegment 250 127 254 129 0 255 0;
#P message 380 192 174 196617 linesegment 253 \$2 254 \$1 0 0 255;
#P newex 473 147 48 196617 pack 1 2;
#P newex 511 112 27 196617 int;
#P newex 463 82 58 196617 t i i b;
#P newex 463 55 104 196617 scale -0.1 0.1 128 0;
#P inlet 463 35 15 0;
#P message 178 190 174 196617 linesegment 253 \$2 254 \$1 0 255 0;
#P newex 271 145 48 196617 pack 1 2;
#P newex 309 110 27 196617 int;
#P newex 261 80 58 196617 t i i b;
#P newex 261 53 104 196617 scale -0.1 0.1 128 0;
#P inlet 261 33 15 0;
#P newex 91 146 48 196617 pack 1 2;
#P newex 129 111 27 196617 int;
#P newex 81 81 58 196617 t i i b;
#P newex 81 54 104 196617 scale -0.1 0.1 128 0;
#P message 91 170 309 196617 scrollrect 1 0 255 255 -1 0 \, linesegment 253 \$2 254 \$1 255 0 0;
#P inlet 81 34 15 0;
#P outlet 91 202 15 0;
#P connect 1 0 3 0;
#P connect 3 0 4 0;
#P fasten 4 0 6 0 86 134 96 134;
#P connect 6 0 2 0;
#P connect 18 0 0 0;
#P connect 21 0 0 0;
#P connect 2 0 0 0;
#P connect 12 0 0 0;
#P connect 4 2 5 0;
#P fasten 5 0 6 1 134 134 134 134;
#P fasten 4 1 5 1 110 104 151 104;
#P connect 11 0 12 0;
#P connect 7 0 8 0;
#P connect 8 0 9 0;
#P fasten 9 0 11 0 266 133 276 133;
#P connect 9 2 10 0;
#P fasten 10 0 11 1 314 133 314 133;
#P fasten 9 1 10 1 290 103 331 103;
#P connect 18 0 20 0;
#P connect 20 0 19 0;
#P connect 1 0 21 0;
#P connect 17 0 18 0;
#P connect 13 0 14 0;
#P connect 14 0 15 0;
#P fasten 15 0 17 0 468 135 478 135;
#P connect 15 2 16 0;
#P fasten 16 0 17 1 516 135 516 135;
#P fasten 15 1 16 1 492 105 533 105;
#P pop;
#P newobj 436 28 55 196617 p draw;
#P user lcd 543 41 273 122 1 0 1 0 0;
#P comment 397 66 100 196617 gravity constant;
#P comment 238 41 100 196617 intial conditions;
#P comment 580 209 100 196617 total energy;
#P comment 364 231 100 196617 potential energy;
#P newex 206 278 22 196617 r K;
#P flonum 286 231 69 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 450 182 22 196617 r K;
#P message 365 66 30 196617 -0.1;
#P newex 365 87 22 196617 s K;
#P hidden newex 0 231 26 196617 r go;
#P newex 160 21 26 196617 r go;
#P newex 328 86 26 196617 s go;
#P newex 277 86 30 196617 s x0;
#P message 232 65 37 196617 0. 0.1;
#P message 277 64 48 196617 10. -0.1;
#P newex 160 128 30 196617 r x0;
#P newex 92 42 30 196617 r v0;
#P newex 232 88 30 196617 s v0;
#P newex 59 394 49 196617 delay 10;
#P newex 81 418 36 196617 zl reg;
#P newex 92 369 40 196617 t b l l;
#P button 328 27 27 0;
#P message 160 43 31 196617 0. 0.;
#P newex 92 300 125 196617 vexpr $f1*$f1*$f2*$f3;
#P newex 92 255 27 196617 t f f;
#P newex 92 211 96 196617 t l l;
#P hidden message 0 258 70 196617 scalarmode 1;
#P newex 109 278 79 196617 vexpr $f1*$f2;
#P flonum 505 207 72 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 505 182 27 196617 + 1.;
#P flonum 288 153 68 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 288 128 141 196617 expr ($f1*$f1+$f2*$f2)/2.;
#P newex 286 207 175 196617 expr $f3/(sqrt($f1*$f1+$f2*$f2));
#N vpatcher 395 73 1173 587;
#P origin -159 0;
#P window setfont "Sans Serif" 9.;
#P newex 449 182 43 196617 r space;
#P toggle 422 185 15 0;
#P newex 416 215 29 196617 gate;
#P newex 426 137 26 196617 r go;
#P newex 570 407 48 196617 pack 1 2;
#P newex 518 408 48 196617 pack 1 2;
#P newex 466 407 48 196617 pack 1 2;
#P newex 414 408 48 196617 pack 1 2;
#P newex 354 406 48 196617 pack 1 2;
#P newex 300 408 48 196617 pack 1 2;
#P newex 246 407 48 196617 pack 1 2;
#P newex 192 411 48 196617 pack 1 2;
#P newex 142 413 48 196617 pack 1 2;
#P newex 90 414 48 196617 pack 1 2;
#P newex 33 410 48 196617 pack 1 2;
#P message 183 466 115 196617 setpixel \$1 \$2 255 0 0;
#P newex 313 33 64 196617 unpack 1. 2.;
#P message 366 134 34 196617 reset;
#P newex 266 189 27 196617 t l b;
#P newex 73 335 376 196617 unpack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26;
#P newex 184 298 27 196617 t l l;
#P newex 81 286 366 196617 pack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26;
#P number 169 113 65 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 496 129 65 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 421 108 27 196617 - 1;
#P newex 373 108 27 196617 + 1;
#P newex 289 104 27 196617 - 1;
#P newex 241 104 27 196617 + 1;
#P newex 375 70 104 196617 scale -50. 50. 0 255;
#P newex 250 146 78 196617 pack 1. 2. 3. 4.;
#P newex 244 70 104 196617 scale -50. 50. 0 255;
#P message 250 170 140 196617 frameoval 126 126 128 128;
#P inlet 312 12 15 0;
#P outlet 299 228 15 0;
#P connect 14 2 19 0;
#P connect 14 3 19 1;
#P connect 13 1 14 0;
#P connect 11 0 12 0;
#P connect 14 4 20 0;
#P connect 10 0 12 1;
#P connect 14 0 12 2;
#P connect 14 1 12 3;
#P connect 14 5 20 1;
#P connect 14 2 12 4;
#P connect 14 6 21 0;
#P connect 14 3 12 5;
#P connect 14 4 12 6;
#P connect 3 0 11 0;
#P connect 14 5 12 7;
#P connect 14 7 21 1;
#P connect 27 0 18 0;
#P connect 28 0 18 0;
#P connect 29 0 18 0;
#P connect 26 0 18 0;
#P connect 25 0 18 0;
#P connect 24 0 18 0;
#P connect 23 0 18 0;
#P connect 22 0 18 0;
#P connect 21 0 18 0;
#P connect 20 0 18 0;
#P connect 19 0 18 0;
#P fasten 12 0 13 0 86 396 135 396 135 265 189 265;
#P connect 14 8 22 0;
#P connect 14 6 12 8;
#P connect 14 7 12 9;
#P connect 14 8 12 10;
#P connect 14 9 22 1;
#P connect 14 9 12 11;
#P connect 3 0 6 0;
#P connect 17 0 3 0;
#P connect 14 10 23 0;
#P connect 14 10 12 12;
#P connect 6 0 4 0;
#P connect 4 0 2 0;
#P connect 14 11 12 13;
#P connect 2 0 15 0;
#P connect 8 0 4 1;
#P connect 14 12 12 14;
#P connect 14 11 23 1;
#P connect 3 0 7 0;
#P connect 14 13 12 15;
#P connect 7 0 4 2;
#P connect 18 0 0 0;
#P connect 16 0 0 0;
#P connect 15 0 0 0;
#P connect 14 12 24 0;
#P connect 14 14 12 16;
#P connect 1 0 17 0;
#P connect 9 0 4 3;
#P connect 14 15 12 17;
#P connect 14 16 12 18;
#P connect 14 13 24 1;
#P connect 14 17 12 19;
#P connect 14 14 25 0;
#P connect 14 18 12 20;
#P connect 30 0 16 0;
#P connect 31 0 16 0;
#P connect 5 0 8 0;
#P connect 17 1 5 0;
#P connect 14 19 12 21;
#P connect 14 20 12 22;
#P connect 14 15 25 1;
#P connect 14 21 12 23;
#P connect 14 16 26 0;
#P connect 32 0 31 0;
#P connect 14 22 12 24;
#P connect 5 0 9 0;
#P connect 33 0 32 0;
#P connect 14 23 12 25;
#P connect 15 1 31 1;
#P connect 14 17 26 1;
#P connect 14 18 27 0;
#P fasten 5 0 10 0 501 84;
#P connect 14 19 27 1;
#P connect 14 20 28 0;
#P connect 14 21 28 1;
#P connect 14 22 29 0;
#P connect 14 23 29 1;
#P pop;
#P newobj 263 258 55 196617 p draw;
#P newex 92 233 160 196617 expr 1./sqrt($f1*$f1+$f2*$f2);
#P newex 92 175 36 196617 zl reg;
#P newex 92 152 78 196617 vexpr $f1+$f2;
#P newex 92 100 36 196617 zl reg;
#P newex 92 77 78 196617 vexpr $f1+$f2;
#P comment 360 154 100 196617 kinetic energy;
#P user panel 357 148 79 25;
#X brgb 200 128 128;
#X frgb 255 0 0;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 358 225 89 25;
#X brgb 128 200 128;
#X frgb 0 255 0;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 580 203 62 25;
#X brgb 128 128 200;
#X frgb 0 0 255;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P user lcd 263 283 250 250 1 0 1 0 0;
#P window setfont "Sans Serif" 20.;
#P window linecount 3;
#P comment 620 286 120 196628 ARROW KEYS STEER SHIP;
#P hidden connect 35 0 18 0;
#P connect 35 0 49 0;
#P connect 49 0 48 0;
#P connect 18 0 47 0;
#P connect 9 0 47 1;
#P connect 48 0 47 2;
#P fasten 24 0 26 0 97 391 64 391;
#P fasten 26 0 25 0 64 414 86 414;
#P fasten 25 0 6 0 86 450 50 450 50 73 97 73;
#P connect 28 0 6 0;
#P connect 47 0 6 0;
#P connect 6 0 7 0;
#P connect 7 0 8 0;
#P connect 8 0 9 0;
#P connect 9 0 19 0;
#P connect 19 0 10 0;
#P connect 10 0 20 0;
#P hidden connect 18 0 21 0;
#P connect 20 0 21 0;
#P connect 21 0 24 0;
#P connect 24 1 25 1;
#P connect 20 1 17 0;
#P hidden connect 18 0 17 0;
#P fasten 17 0 21 1 114 297 154 297;
#P connect 34 0 22 0;
#P fasten 7 0 6 1 97 124 184 124 184 70 165 70;
#P connect 22 0 6 1;
#P fasten 9 0 8 1 97 199 184 199 184 149 165 149;
#P connect 29 0 8 1;
#P fasten 19 1 17 1 183 258 183 258;
#P connect 40 0 21 2;
#P fasten 23 0 31 0 333 60 237 60;
#P connect 31 0 27 0;
#P fasten 9 0 11 0 97 199 268 199;
#P connect 11 0 1 0;
#P fasten 23 0 30 0 333 60 282 60;
#P connect 30 0 32 0;
#P fasten 9 0 12 0 97 199 291 199;
#P connect 12 0 39 0;
#P fasten 7 0 13 0 97 124 293 124;
#P connect 13 0 14 0;
#P connect 23 0 33 0;
#P fasten 23 0 37 0 333 60 370 60;
#P connect 37 0 36 0;
#P connect 14 0 46 0;
#P connect 38 0 12 2;
#P connect 39 0 46 1;
#P connect 16 0 46 2;
#P fasten 14 0 15 0 293 175 510 175;
#P connect 15 0 16 0;
#P fasten 39 0 15 1 291 251 643 251 643 174 527 174;
#P fasten 46 0 45 0 441 50 531 50 531 29 548 29;
#P pop;