topical media & game development
graphic-processing-algorithm-Ch06-p145-MySpring.pde / pde
class MySpring{
float M = 0.8; // Mass
float K = 0.2; // Spring constant
float D = 0.92; // Damping
float Rx = 100; // Rest position
float Ry = 100; // Rest position
float xpos; // Position x
float ypos; // Position y
float vx = 0.0; // Velocity x
float vy = 0.0; // Velocity y
float a = 0; // Acceleration
float f = 0; // Force
boolean released = false;
void move(){
f = -K * (xpos - Rx); // f=-ky
a = f / M; // Set the acceleration, f=ma == a=f/m
vx = D * (vx + a); // Set the velocity
xpos += vx; // Updated x position
f = -K * (ypos - Ry); // f=-ky
a = f / M; // Set the acceleration, f=ma == a=f/m
vy = D * (vy + a); // Set the velocity
ypos += vy; // Updated y position
if(abs(vx)<0.01 && abs(vy) < 0.01) {
vx = 0.0;
vy = 0.0;
released = false;
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.