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 #5

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


FiqStudios

Throwing Engine.

Posted by FiqStudios Jan. 24, 2008 @ 6:57 PM EST

A simple throwing engine meant to mimic something like ball physics.
NOTE: I based this engine off of Keith Peter's Engine. However, his engine wasn't nearly as dynamic as my revised version.

Throwing Engine.

Source.

____________________

Script:

Placed inside the ball MovieClip. Frame actions.

var dragging = false;
var left:Number = 0;
var right:Number = Stage.width;
var bottom:Number = Stage.height;
var thickness:Number = (_height+_width)/4;
var vx:Number = 0;
var vy:Number = 0;
var vt:Number = 0;
var maxSpeed:Number = 10;
var throwSpeed:Number = .9;
var friction:Number = .999;
var bounce:Number = -.9;
var gravity:Number = .4;
function onPress():Void {
dragging = true;
this.startDrag();
}
function onRelease():Void {
dragging = false;
this.stopDrag();
}
function onReleaseOutside():Void {
dragging = false;
this.stopDrag();
}
function onEnterFrame():Void {
if (dragging) {
vx = (_x-oldX)*throwSpeed;
vy = (_y-oldY)*throwSpeed;
oldX = _x;
oldY = _y;
} else {
_rotation += vx*360/(thickness*2*Math.PI);
vy += gravity;
vx *= friction;
vy *= friction;
vt = Math.sqrt(vx*vx+vy*vy);
if (vt>maxSpeed) {
vx = vx*maxSpeed/vt;
vy = vy*maxSpeed/vt;
}
_x += vx;
_y += vy;
if (_y+thickness>bottom) {
_y = bottom-thickness;
vy *= bounce;
}
if (_x+thickness>right) {
_x = right-thickness;
vx *= bounce;
}
if (_x-thickness<left) {
_x = thickness;
vx *= bounce;
}
}
}

____________________

Setting up the .fla:

Create your ball. Make sure it's completely round and even, or else it will look odd. Turn it into a symbol, and add the script to the MovieClip's frame actions. You may adjust the variable, I tried hard to make it very dynamic.

____________________

Explanation:

var dragging = false;

Initiative....

var left:Number = 0;
var right:Number = Stage.width;
var bottom:Number = Stage.height;

Sets the "walls" where the ball will bounce from.

var thickness:Number = (_height+_width)/4;

Sets the thickness of the ball. I could've just used "_height/2" or "_width/2", but I wanted it to be perfect.

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

Moar initiatives. (I should really just group these together.)

var maxSpeed:Number = 10;
var throwSpeed:Number = .9;

maxSpeed sets how fast the ball is allowed to go. If you don't want a limited speed just remove the "if(vt>maxSpeed)" part and this variable; or you could just be lazy and set it to something high like 999999.

var friction:Number = .999;
var bounce:Number = -.9;
var gravity:Number = .4;

Amount of air friction, bounciness, and force of gravity, respectively.

function onPress():Void {
dragging = true;
this.startDrag();
}
function onRelease():Void {
dragging = false;
this.stopDrag();
}
function onReleaseOutside():Void {
dragging = false;
this.stopDrag();
}

Determines if you are pressing the ball, starts and stops dragging, and uses "dragging" as a flag.

function onEnterFrame():Void {
if (dragging) {
vx = (_x-oldX)*throwSpeed;
vy = (_y-oldY)*throwSpeed;
oldX = _x;
oldY = _y;

Updates vx and vy for when you release.

} else {
_rotation += vx*360/(thickness*2*Math.PI);

Dynamic rolling based on the size of the ball. I know, I'm awesome.

vy += gravity;
vx *= friction;
vy *= friction;

Applies gravity and air friction

vt = Math.sqrt(vx*vx+vy*vy);
if (vt>maxSpeed) {
vx = vx*maxSpeed/vt;
vy = vy*maxSpeed/vt;
}

Determines the total velocity and applies limits.

_x += vx;
_y += vy;

Moves the ball...

if (_y+thickness>bottom) {
_y = bottom-thickness;
vy *= bounce;
}
if (_x+thickness>right) {
_x = right-thickness;
vx *= bounce;
}
if (_x-thickness<left) {
_x = thickness;
vx *= bounce;
}

Applies the walls and bounce.

}
}

Copyright 2008 Ryan Keathley.

Updated: 01/31/08 10:52 PM Log in to comment! | Share this!

The People Have Spoken

1 Comment

Jan. 26, 2008 | 1:49 AM 125Maniac says:

very clever...

Jan. 26, 2008 | 1:57 AM FiqStudios responds:

Thanks, I worked hard to make it dynamic and optimize it.

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