Quick Mass On Spring Tutorial

Learn how to make certain types of games and use gameEditor.

Quick Mass On Spring Tutorial

Postby jazz_e_bob » 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!
Controlling complexity is the essence of computer programming.
User avatar
jazz_e_bob
 
Posts: 742
Joined: Tue Jul 01, 2003 9:38 pm
Location: Bloke from Cockatoo Creek Australia
Score: 14 Give a positive score

Postby makslane » Mon Nov 08, 2004 5:18 pm

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
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Re: Quick Mass On Spring Tutorial

Postby akhad » Sun Oct 05, 2008 8:04 am

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.
User avatar
akhad
 
Posts: 43
Joined: Tue Jan 16, 2007 1:59 pm
Location: U.K
Score: 0 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest