This is a simulation of a cart connected to a spring. You can drag both the cart and the wheel, and you can release them at a particular velocity.
Roughly speaking, this solves the differential equation mx''+kx=f, where f is the driving function. In this case, f is some sine wave, represented by the wheel.
The differential equation is solved using Euler's method, and it is fairly straightforward:
this.spring_drive_angle += 2 * Math.PI * this.spring_drive_freq * dt; this.spring_x = this.spring_beginning_x + this.spring_drive_ampl * Math.sin(this.spring_drive_angle); // Fspring = k(rest_pos - x) var force = this.spring_constant * (this.spring_x + this.spring_rest_length - this.obj_x); // Ffriction = - friction * mass * (+1 if velocity positive, -1 if negative, 0 if 0) force -= this.friction * this.obj_mass * Math.sign(this.obj_vx); // F = ma var obj_ax = force / this.obj_mass; // Euler's method step this.obj_x += dt * this.obj_vx; this.obj_vx += dt * obj_ax;
The complete source code for the simulation is this Javascript code. Most of it is just for drawing a representation of the cart and wheel and for handling mouse input.