Sunday, February 18, 2007

2d, 3d Geometry Information

Game Programmers Encylopedia
2D math principles
3D math principles

Distance Between Two Points (3d)

GEN_get3DDistance = function (ax, ay, az, bx, by, bz) {
dx = ax-bx;
dy = ay-by;
dz = az-bz;
theDistance = Math.sqrt(dx*dx+dy*dy+dz*dz);
return theDistance;
};
trace(GEN_get3DDistance(5,0,0,10,0,0))

Distance Between Two Points (2d)

GEN_get2DDistance = function (ax, ay, bx, by) {
dx = ax-bx;
dy = ay-by;
theDistance = Math.sqrt(dx*dx+dy*dy);
return theDistance;
};
this.onEnterFrame = function() {
trace(GEN_get2DDistance(thing._x, thing._y, _xmouse, _ymouse));
};

Optimizing Frame Rate

http://mxdj.sys-con.com/read/142694_p.htm

Shared Object


//HIGH SCORE SHARED OBJECT =======================================
// create a new shared object named "healthfirstdental"
my_so = SharedObject.getLocal("healthfirstdental");
// set a new variable (alreadyvisited) to 0
if (my_so.data.alreadyvisited == undefined) {
my_so.data.alreadyvisited = 1;
// this saves the shared object immediately as opposed to when the swf shuts down
my_so.flush;
} else {
gotoAndStop("skipped");
}

//zeroalreadyvisited.onRelease = function() {
//my_so.clear();
//};

Parallax Function


GEN_positionInParallax = function (thisMovieClip, theBoundsMovieClip, theLeaderMovieClip) {
//
// X...
//get offset % by comparing theLeaderMovieClip with theBoundsMovieClip
extraXSpace = (theLeaderMovieClip._width)-(theBoundsMovieClip._width);
westGap = (theBoundsMovieClip._x)-(theLeaderMovieClip._x);
leaderWestGapXPercentage = westGap/extraXSpace;
//set position
followerExtraXSpace = (thisMovieClip._width)-(theBoundsMovieClip._width);
thisMovieClip._x = (-(followerExtraXSpace*leaderWestGapXPercentage))+theBoundsMovieClip._x;
//
// Y...
//get offset % by comparing theLeaderMovieClip with theBoundsMovieClip
extraYSpace = (theLeaderMovieClip._height)-(theBoundsMovieClip._height);
northGap = (theBoundsMovieClip._y)-(theLeaderMovieClip._y);
leaderWestGapYPercentage = northGap/extraYSpace;
//set position
followerExtraYSpace = (thisMovieClip._height)-(theBoundsMovieClip._height);
thisMovieClip._y = (-(followerExtraYSpace*leaderWestGapYPercentage))+theBoundsMovieClip._y;
};

Saturday, February 17, 2007

Keyboard Control


if (Key.isDown(39)) {
//LEFT;
}
if (Key.isDown(37)) {
//RIGHT;
}
if (Key.isDown(40)) {
//UP;
}
if (Key.isDown(38)) {
//DOWN);
}
if (Key.isDown(65)) {
//A;
}
if (Key.isDown(83)) {
//S;
}

Circular Plotting Function


GEN_PlotItemsOnCircle = function (numberOfItems, theRadius, startDegree) {
sliceSize = (360/numberOfItems);
theCoordinates = [];
for (i=0; i<(numberOfItems); i++) {
angle = startDegree*(Math.PI/180);
xPlot = theRadius*Math.cos(angle);
yPlot = theRadius*Math.sin(angle);
startDegree += sliceSize;
theCoordinates[i] = [xPlot, yPlot];
}
return theCoordinates;
};

Flash 3D Function


GEN_putObjectIn3dScene = function (theClip, cameraDistanceFromTracerPlane, x, y, z, vanishingPointX, vanishingPointY) {
if (z<0) {
theClip._alpha = 0;
} else {
theClip._alpha = 100;
}
var zfactor = cameraDistanceFromTracerPlane/z;
theClip._x = x*zfactor+vanishingPointX;
theClip._y = y*zfactor+vanishingPointY;
theClip._xscale = 100*zfactor;
theClip._yscale = 100*zfactor;
theClip.cacheAsBitmap();
theClip.swapDepths(Math.floor(100000-z));
};

Fuse Kit

Fuse Kit is a very nice set of classes, that when added to Flash, add a way to easily tween movieclips in tons of ways. The description below is the beginners "simpleSetup" mode only. There are a number of ways to use Fuse Kit. This is the easiest-to-understand way.

You need to first install Fuse Kit. Installing this MXP installs new classes into Flash.

Next use this code to import the classes into your code:


import com.mosesSupposes.fuse.*;
ZigoEngine.simpleSetup(Shortcuts, PennerEasing, FuseFMP);



To set a simple property for a movieClip without tweening:


my_mc.Blur_blur=20;



NOTE: To see what these cool new properties are, go to Flash Help FuseKit2.2/ mosessupposes.fuse/ Shortcuts

To set a simple property for a movieClip with tweening:


my_mc.slideTo(300, 300, 2);
my_mc.rotateTo(180, 2);
my_mc.colorTo("#ffffff", 10)
my_mc.scaleTo(10, 4)
my_mc.Blur_blurXTo(50, 1, "easeInOutElastic", 2);


Fuse Kit 2.1 also installs a cool tweening pallette into Flash. Go to Window>Other Panels>customEasingTool2 to see the tweens and even make up your own.

If you put a number in quotes, it will add or subtract the number. For example, if a movieclip._alpha = 90, using "10" in the Fuse command will tween it up from 90 to 100. Using 10 will tween the alpha to 10. See "Know your relatives" in the PDF below for a better explanation.

Here is a PDF Beginners Guide that explains the various components of Fuse.