## AS: Ball inertia

• 3,092 Views
• 28 Replies
pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
AS: Ball inertia 2007-03-25 20:09:52

AS: main

INTRO: First off, I got this from the topic "swingy thingy". Thanks to sk8more, this can be done.

WHAT: Well, this concept is like a rubber band. When you move your mouse the ball slings itself at the direction to move it to. The strind is connected to the ball, and the string is the thing that acts like a rubberband.

WHY: You can use this in multible ways. I'm using this to make a game. It'll come out soon.

Example: here

Tutorial:

First, make a ball. This ball can be anything you want it to be, because this is what is being "bungied". Press F8. Make it a movieclip and name it "ball". Now, go to the properties and give it an instance name of "ball". No quotes.

Press F9 and copy this code in:

inertia =.9;
ki = 0.1;
x1 =33
y1 =33
}
onClipEvent (enterFrame) {
x = -_x+_root._xmouse;
y = -_y+_root._ymouse;
x1 = x1*inertia+x*ki;
y1 = y1*inertia+y*ki;
_x += x1;
_y += y1;

}

This code is simply giving it it's physical properties. The mouse moves, the ball moves at a faster pace. When it's at it's max length, it slows down. Then, comes back. This gives it a rubber band characteristic.

Now on the very frame where you have that ball, put this code in: (hint: highlight the frame)

onEnterFrame=function () {
with (createEmptyMovieClip("line", 1)) {
lineStyle(10)
moveTo(_root._xmouse,_root._ymouse)
lineTo(ball._x,ball._y)
}
}

This is the rubber band. This code says that where ever the bale ball is that side stays with the ball. And the other side follows the mouse.

I hoped this helped you. I tried to explain what the codes briefly. You get the idea.

-pivot (AKA: msmstudios)

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

EvanHayes
EvanHayes
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Ball inertia 2007-03-25 20:25:41

hahah nice pivot,woo im in an as main topic :)
ill bookmark it even though...
well it has my name in it,but good tut

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-25 20:35:18

I hope this gets added to As: main...

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

Pyromaniac
Pyromaniac
• Member since: Jan. 14, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Ball inertia 2007-03-25 20:48:51

You should EXPLAIN the code instead of saying copy and paste, thats what the As:Main topics are for.

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-25 20:54:07

At 3/25/07 08:48 PM, Pyromaniac wrote: You should EXPLAIN the code instead of saying copy and paste, thats what the As:Main topics are for.

I did. I explained what it does and why.

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

Nuggs
Nuggs
• Member since: Dec. 17, 2005
• Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Ball inertia 2007-03-25 20:59:33

How did you decide to set the variables at such seemingly random numbers?
inertia =.9;
ki = 0.1;
x1 =33
y1 =33

Games|1|2|3| Movies|1|2|

Pyromaniac
Pyromaniac
• Member since: Jan. 14, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:14:19

At 3/25/07 08:54 PM, pivot11 wrote:
At 3/25/07 08:48 PM, Pyromaniac wrote: You should EXPLAIN the code instead of saying copy and paste, thats what the As:Main topics are for.
I did. I explained what it does and why.

Line by line buddy. Hell if you want I'll explain if you don't understand what it does.

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:14:26

At 3/25/07 08:59 PM, Nuggs wrote: How did you decide to set the variables at such seemingly random numbers?
inertia =.9;
ki = 0.1;
x1 =33
y1 =33

Random, and then he tested to see if it was good or not...

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

EvanHayes
EvanHayes
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:23:19

ok,well inertia is how long the ball moves and kinetik(k) its how fast or how much kenetic energy there is the higher the number k is the faster and same with enertia but it increases distance

p1x and p1y are random set them to whatever,you just need a variable to be defined as a number

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Kart-Man
Kart-Man
• Member since: Jan. 7, 2007
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:33:05

At 3/25/07 09:23 PM, sk8more wrote: explanation

Oh whatever; regardless, it's an interesting effect.

postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:39:48

Go to the topic swingy thingy.

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

Pyromaniac
Pyromaniac
• Member since: Jan. 14, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:40:01

What you should do then is
//
var p1:Number;
var p2:Number;
//

EvanHayes
EvanHayes
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Ball inertia 2007-03-25 21:44:47

yea,that is more efficient,but for most people that way works just as well,just a bit more laggy if you have like 300 of them but for now its fine

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
Response to AS: Ball inertia 2007-03-25 22:04:12

this isnt a tutorial. its a code snippet. they wont learn anything

This blog I made | This game sucks

suppi
suppi
• Member since: Jun. 29, 2003
• Offline.
Forum Stats
Member
Level 07
Audiophile
Response to AS: Ball inertia 2007-03-25 22:14:32

who cares yo most people that need these tutorials dont have the mental capacity to learn these codes - like me!

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
Response to AS: Ball inertia 2007-03-25 22:16:44

At 3/25/07 10:14 PM, suppi wrote: who cares yo most people that need these tutorials dont have the mental capacity to learn these codes - like me!

oh that makes total sense

im outta here

This blog I made | This game sucks

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-25 22:19:42

lol, ok...

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-26 16:42:46

wow, no replies?

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

Cybex
Cybex
• Member since: Mar. 4, 2005
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Ball inertia 2007-03-26 17:47:55

At 3/25/07 08:54 PM, pivot11 wrote: I did. I explained what it does and why.

Not really, you even suggested they should just copy and paste the code. You explained wehat the code does breifly, but you didn't say why it does that and exactl how. You want to explain it so that someone with AS knowledge could reproduce it with only the explanation.

EvanHayes
EvanHayes
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Ball inertia 2007-03-26 18:10:19

do you think i should remake this tut?
considering its my code.

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-03-26 18:22:59

Awwww, but I always tried to make an AS thing but all the topics were taken.

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

ToasterDemon
ToasterDemon
• Member since: May. 15, 2005
• Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to AS: Ball inertia 2007-04-05 20:29:23

Any idea of how to add maximum string length?

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2007-04-05 20:30:56

How did this get out!!! And yes, the intervals. mess with those. Or just ask sk8more

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame

Ansel
Ansel
• Member since: Sep. 2, 2006
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Ball inertia 2007-04-05 21:33:32

hmmm. how can i modify this so that the ball rotates to face the mouse at all times?

LesPaulPlayer
LesPaulPlayer
• Member since: May. 18, 2006
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Ball inertia 2007-04-05 21:55:06

At 4/5/07 09:33 PM, JunkyCow wrote: hmmm. how can i modify this so that the ball rotates to face the mouse at all times?

wow. I dont even know how you don't know this. Thats like some of the most important AS ever.

EvanHayes
EvanHayes
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Ball inertia 2007-04-05 21:56:35

At 4/5/07 09:33 PM, JunkyCow wrote: hmmm. how can i modify this so that the ball rotates to face the mouse at all times?

ok,junky you have to use a atan2 function
x=_x-_root._xmouse
y=_y-_root._ymouse
_rotation=Math.atan2(y,x)/(Math.PI/180)

in an onClipEvent handler and you got yourself some rotation

woot,go my code :)

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

ToasterDemon
ToasterDemon
• Member since: May. 15, 2005
• Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to AS: Ball inertia 2007-04-06 09:50:31

So nobody has any idea of the code that would give a max string length?

izzy032294
izzy032294
• Member since: Feb. 2, 2008
• Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Ball inertia 2008-02-02 17:02:30

ok I'll try to figure this out

inertia =.9; //these are the variables
ki = 0.1;
x1 =33;
y1 =33;
}

onClipEvent (enterFrame) { //basically means at all times

x = -_x+_root._xmouse; //the x is the balls x coordinates plus the mouse's x coordinates
y = -_y+_root._ymouse; // the y is the balls y coordinates plus the mouse's y coordinates

x1 = x1*inertia+x*ki; //the x1 is itself times inertia(making itself less and less)+x(we explained above what x was) times ki making it less and less
y1 = y1*inertia+y*ki; //the y1 is itself times inertia(making itself less and less)+y(we explained above what y was) times ki (making it less and less)

the part that makes everything work is coming up

_x += x1; // which means the ball x position is x1 which always gets less and less until it hits zero I didn't say that above because what is .9(inertia) times 0?
_y += y1; // which means the ball y position is y1 which always gets less and less until it hits zero.
once they both hit zero the ball stops until you move the mouse then the ball takes it's x and y position subtracted from the mouse's x and y and multiplied by inertia

I hope I helped and didn't just confuse people more

pivot11
pivot11
• Member since: Jun. 16, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Ball inertia 2008-03-26 10:35:21

At 2/2/08 05:02 PM, izzy032294 wrote: ok I'll try to figure this out

inertia =.9; //these are the variables
ki = 0.1;
x1 =33;
y1 =33;
}

onClipEvent (enterFrame) { //basically means at all times

x = -_x+_root._xmouse; //the x is the balls x coordinates plus the mouse's x coordinates
y = -_y+_root._ymouse; // the y is the balls y coordinates plus the mouse's y coordinates

x1 = x1*inertia+x*ki; //the x1 is itself times inertia(making itself less and less)+x(we explained above what x was) times ki making it less and less
y1 = y1*inertia+y*ki; //the y1 is itself times inertia(making itself less and less)+y(we explained above what y was) times ki (making it less and less)

the part that makes everything work is coming up

_x += x1; // which means the ball x position is x1 which always gets less and less until it hits zero I didn't say that above because what is .9(inertia) times 0?
_y += y1; // which means the ball y position is y1 which always gets less and less until it hits zero.
once they both hit zero the ball stops until you move the mouse then the ball takes it's x and y position subtracted from the mouse's x and y and multiplied by inertia

I hope I helped and didn't just confuse people more

Oh, inertia is how stretchy it can go. If you set it to 1 itll never come back to the ball. .99 is the most elastic, and .00001 (etc to infinity) is the least. I set it to .9 as it was comfortable to me. If you want it to stratch more put .95. Less, put like .70

Blok' Party, Orbital Khaos, site, MSMstudios, Phrozen Phlame