Page 1 of 1

Quick Mass On Spring Tutorial

PostPosted: Sat Nov 06, 2004 10:27 am
by jazz_e_bob
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!

PostPosted: Mon Nov 08, 2004 5:18 pm
by makslane
And... add the string:

Add a mass_string actor (select Canvas on Add Actor panel)
Stretch this actor to fit the view

On Create Actor event, add a Script Editor action:
setpen(255, 255, 255, 0, 2); //(255, 255, 255) = blank, 0 = no transparency, 2 = line thick

On Draw Actor event, add a Script Editor action:

int x1 = mouseFollower.xscreen, y1 = mouseFollower.yscreen,
x2 = massOnSpring.xscreen, y2 = massOnSpring.yscreen;

//Put the screen coords in actor coords
screen_to_actor(&x1, &y1);
screen_to_actor(&x2, &y2);

//Erase canvas
erase(0, 0, 0, 1.0);

//Draw the line
moveto(x1, y1);
lineto(x2, y2);

Download at: http://game-editor.com/examples/mass_on_spring.ged

Re: Quick Mass On Spring Tutorial

PostPosted: Sun Oct 05, 2008 8:04 am
by akhad
Ok, this is an old post but I'm having fun messing with the variables and learning to code inertia. If anyone could help me with some example code I'd be grateful.
How could I have the 2nd actor (not the mousefollower) orbit around the mousefollower actor, but still respond to any movement with some inertia, but always staying at a certain distance/orbit from the mousefollower actor.
Then with a simple key press make it simply fall back to the initial inertia mode, so it sticks to the mousefollower.