FiqStudios's Banner

FiqStudios

Reviews News Main
FiqStudios

Age/Gender: n/a, Male
Location: New Mexico
Job: Student

Rusty Air.

Contact Info

Newgrounds Stats

Sign-Up Date:
12/23/07

Level: 8
Aura: Dark

Rank: Scout
Blams: 56
Saves: 154
Rank #: 36,853

Whistle Status: Garbage

Exp. Points: 590 / 710
Exp. Rank #: 64,712
Voting Pow.: 5.00 votes

BBS Posts: 671 (0.92 per day)
Flash Reviews: 2
Music Reviews: 0
Trophies: 0
Stickers: 0

Entry #4

Jump to Entry: [ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ]


FiqStudios

Trampoline Physics

Posted by FiqStudios Jan. 21, 2008 @ 3:49 AM EST

For this project I decided to retun to my olden days of Kinematics and Physics. I decided to try to replicate a trampoline, and the project was a dashing success. On we go shall we?

"Trampoline Physics"

Source.

____________________

Setting up the flash:

Make a small dot (for aesthetic purposes, although it can be anything.) and turn it into MovieClip. Remove the MovieClip from the stage so that it exists only in the library. Open up the MovieClip's properties window from the library and give it an linkage name of "dot". Once again, change the frame rate to something practical (25-45).

____________________

Script:

In the main frame panel:

var spring_loc:Number = 300;
var pressing:Boolean = false;
var dot:MovieClip = attachMovie("dot", "dot", getNextHighestDepth());
dot._x = Stage.width/2;
dot._y = 50;
function onMouseDown():Void {
pressing = true;
}
function onMouseUp():Void {
pressing = false;
}

In the frame actions (not ClipEvents) of the dot MovieClip. *Note: This may be accessed by double clicking the MovieClip in the library, and opening the Actios panel*

function mvx() {
x2 = _root._xmouse;
xvel = x2-x1;
x1 = x2;
return xvel;
}
function mvy() {
y2 = _root._ymouse;
yvel = y2-y1;
y1 = y2;
return yvel;
}
var vx:Number = 0;
var vy:Number = 0;
var gf:Number = .22;
var fr:Number = .99;
var bn:Number = -.8;
function onEnterFrame():Void {
var mouse_vx:Number = mvx()/80;
var mouse_vy:Number = mvy()/80;
vy += gf;
vx *= fr;
vy *= fr;
if (_y>_root.spring_loc) {
vy -= Math.sqrt(_y-_root.spring_loc)/13;
}
if (_root.pressing == true) {
vx += mouse_vx;
vy += mouse_vy;
}
if (_x<0) {
_x = 0;
vx *= bn;
}
if (_x>Stage.width) {
_x = Stage.width;
vx *= bn;
}
_x += vx;
_y += vy;
}

____________________

Explanation:

var spring_loc:Number = 300;

This sets the y distance at which the forces of elasticity will begin to act on the dot.

var pressing:Boolean = false;

Just more meaningless, initiative variables....

var dot:MovieClip = attachMovie("dot", "dot", getNextHighestDepth());
dot._x = Stage.width/2;
dot._y = 50;

Adds the dot MovieClip to the stage, gives it an instance name of "dot" and sets it's position.I like to use attachMovie() for this because it doesn't give the MovieClip a negetive depth, which is something that can be very limiting.

function onMouseDown():Void {
pressing = true;
}
function onMouseUp():Void {
pressing = false;
}

Simple enough. If you're clicking it sets "pressing" to true, otherwise it sets it to false.

Now for the dot's actions; and all the physics.

function mvx() {
x2 = _root._xmouse;
xvel = x2-x1;
x1 = x2;
return xvel;
}
function mvy() {
y2 = _root._ymouse;
yvel = y2-y1;
y1 = y2;
return yvel;
}

Simple function pair that returns the mouse's "velocity".

var vx:Number = 0;
var vy:Number = 0;

Initiative variables.

var gf:Number = .22;

Sets the force of gravity.

var fr:Number = .99;

Sets amount of air friction.

var bn:Number = -.8;

Sets how much the dot will bounce off walls.

function onEnterFrame():Void {

Every frame....

var mouse_vx:Number = mvx()/80;
var mouse_vy:Number = mvy()/80;

For some reason I needed variables for this.

vy += gf;
vx *= fr;
vy *= fr;

Applies gravity and air friction.

if (_y>_root.spring_loc) {
vy -= Math.sqrt(_y-_root.spring_loc)/13;
}

If the dot is past the point where the elasticity is supposed to take hold, keep subtacting from vy until it's forced back up again. Imagine it's landing on an invisible rubber band, and the farther it falls into it the stronger the forces become. Notice the Math.sqrt( , without it the forces became too rigid and the dot would barely sink before being abruptly thrown back up.

if (_root.pressing == true) {
vx += mouse_vx;
vy += mouse_vy;
}

This checks the variable created in the main frame and sees if the mouse is down so it can begin the throwing part.

if (_x<0) {
_x = 0;
vx *= bn;
}
if (_x>Stage.width) {
_x = Stage.width;
vx *= bn;
}

The walls and bouncing. Multiplies vx by a negetive number less than one to get it moving with less speed in the opposite direction. Just like real bouncing >:U.

_x += vx;
_y += vy;

Moves the dot according to the previously set variables...

____________________

If you are going to use this in a project or game you must give me credit and have my permission in advance. Copyright 2008 Ryan Keathley.

Updated: 01/21/08 4:29 AM Log in to comment! | Share this!

Jump to Entry: [ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ]