Quick Mass On Spring Tutorial
Posted: Sat Nov 06, 2004 10:27 am
Add actor mouseFollower
On Create mouseFollower
FollowMouse("Event Actor", BOTH_AXIS);
Add actor massOnSpring
On Create massOnSpring
// inertia relates to the quantity of energy that
// the spring will carry
// inertia = 1 would mean that the spring doesn't
// lose any energy, and that it will oscillate
// forever
myInertia = 0.9 ;
// k relates to the spring, and how "hard" it will be.
// The higher k the faster the mass will come back.
mySpringHardness = 0.1 ;
On Draw massOnSpring
// We calculate the distance to the mouse
double mouseDistanceX = -x + mouseFollower.x ;
double mouseDistanceY = -y + mouseFollower.y ;
//We calculate the amount by which the mass will to move
double xp = xp * myInertia + mouseDistanceX * mySpringHardness ;
double yp = yp * myInertia + mouseDistanceY * mySpringHardness ;
//We move it
x += xp ;
y += yp ;
Have fun playing with inertia and spring hardness!
Boing!
On Create mouseFollower
FollowMouse("Event Actor", BOTH_AXIS);
Add actor massOnSpring
On Create massOnSpring
// inertia relates to the quantity of energy that
// the spring will carry
// inertia = 1 would mean that the spring doesn't
// lose any energy, and that it will oscillate
// forever
myInertia = 0.9 ;
// k relates to the spring, and how "hard" it will be.
// The higher k the faster the mass will come back.
mySpringHardness = 0.1 ;
On Draw massOnSpring
// We calculate the distance to the mouse
double mouseDistanceX = -x + mouseFollower.x ;
double mouseDistanceY = -y + mouseFollower.y ;
//We calculate the amount by which the mass will to move
double xp = xp * myInertia + mouseDistanceX * mySpringHardness ;
double yp = yp * myInertia + mouseDistanceY * mySpringHardness ;
//We move it
x += xp ;
y += yp ;
Have fun playing with inertia and spring hardness!
Boing!