Wednesday, March 20, 2013
added to my list of people to stalk
IK Solver in AS3:
http://vimeo.com/23191399
http://www-scf.usc.edu/~shopkins/contact.html
also
http://www.adobe.com/devnet/flash/articles/spring_tool.html
Trig rounding errors in AS3
Maybe I just don't know my trig...
But if I do...
The difference between 1.888 and 1.253 is a HUGE rounding error.
var m:Number = Math.asin( Math.sin(1.888) );
//m = 1.253
http://answers.yahoo.com/question/index?qid=20080227172624AAwIQZA
Thursday, March 14, 2013
Iterate through quadrants
This is ridiculous:
var xo:int; //x offset.
var yo:int; //y offset.
var i2:int;
var z0:int;
var z1:int;
var zeroMaybe:int;
for (var i:int = 0; i < 4; i++)
{
zeroMaybe = (i < 2) ? 0 : 1;
i2 = i + 1;
z0 = (i & 1);
z1 = (i2 & 1);
xo = (z0 + (-1 * z1)) * zeroMaybe;
yo = (z1 + ( -1 * z0)) * (1-zeroMaybe);
trace("debug");
}
Guess I should just put the pairs into a vector.
Trying to iterate over checking 4 positions relative to a tile:
UP,DOWN,LEFT,RIGHT.
By outputting the correct offset values each iteration:
[0,1] [0,-1], [1,0], [-1,0] in no specific order.
Actually... This isn't bad for building a lookup table that is a linked-list.
Same thing said another way:
for (var i:uint = 0; i < 4; i++)
{
var and3:uint = i & 3; //OUTPUT: 0,1,2,3 0,1,2,3
//OUTPUT in BINARY: 00, 01, 10, 11
//If we SUBTRACT the bits, we can get 0,-1,1,0. Just what we need.
var a3 :uint = (i + 2) & 3; //same as and3, but input is shifted over two.
//When and3 and a3 are converted to -1,1,or zero, we will have the following outputs in this order:
// [0,1] , [-1,0], [1,0], [0,-1] ALL THE KITTY KORNERS.
var npX:int = ((and3 & 2)>>1) - (and3 & 1); //convert number 0-3 to: 0,1,-1
var npY:int = ((a3 & 2)>>1) - (a3 & 1); //convert number 0-3 to: 0,1,-1
//Building linked-list table of offset quadrant values:
cur.x = npX;
cur.y = npY;
if (i != 3)
{
cur.hasNext = true;
cur.next = new LinkedXYPair();
cur = cur.next;
}
trace("debug: [" + npX + " : " + npY + "]");
}
Sunday, March 10, 2013
Shiny Little Plasma Flares
Wednesday, March 6, 2013
When is SetPixel32 Faster than CopyPixels?
package
{
//Common knowledge is SetPixel32 is slower that CopyPixels.
//What I want to know however is... What is the equivalency ratio?
//Meaning:
//Does using setPixel32 100 times take the same amount of time as CopyPixels for a (100x100) (10,000 pixel area)? Or something like that?
//If you are going to use particles that use the "setPixel32" command... We need to know.
//Compare setting pixel32 to usingCopyPixels on ONE pixel and see the speed difference.
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Timer;
import flash.utils.getTimer;
/**
* ...
* @author JMIM
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
for (var i:int = 1; i < 100; i++)
{
pixelTest(i);
}
}
public function pixelTest(inRecSize:int):void
{
trace("rec size == " + inRecSize + "--------::");
var t1:Number;
var t2:Number;
var tt:Number;
var counter:int;
var counterMax:int = 98765;
var theBm:BitmapData = new BitmapData(500, 500, true, 0x00);
var copyFromBm:BitmapData = new BitmapData(500, 500, true, 0x00);
var ZZ:Point = new Point(0, 0);
var rec1x1:Rectangle = new Rectangle(0, 0, inRecSize, inRecSize);
var setPixelCount:int = 0;
var setPixelTimes:int = (inRecSize * inRecSize);
t1 = getTimer();
counter = 0;
do
{
counter++;
if (counter > counterMax) { break; }
setPixelCount = 0;
do {
setPixelCount++;
if (setPixelCount > setPixelTimes) { break;}
theBm.setPixel32(10, 10, 0x88FF);
}while (true);
}while (true);
t2 = getTimer();
tt = t2 - t1;
trace("setPixel32 time:: tt==" + tt);
t1 = getTimer();
counter = 0;
do
{
counter++;
if (counter > counterMax) { break;}
theBm.copyPixels(copyFromBm, rec1x1, ZZ, null, null, true);
}while (true);
t2 = getTimer();
tt = t2 - t1;
trace("copyPixels time:: tt==" + tt);
}
}//class
}//package
//OUTPUT: rec size == 1--------:: setPixel32 time:: tt==50 copyPixels time:: tt==160 rec size == 2--------:: setPixel32 time:: tt==130 copyPixels time:: tt==160 rec size == 3--------:: setPixel32 time:: tt==270 copyPixels time:: tt==170 rec size == 4--------:: setPixel32 time:: tt==470 copyPixels time:: tt==170 rec size == 5--------:: setPixel32 time:: tt==680 copyPixels time:: tt==170 rec size == 6--------:: setPixel32 time:: tt==980 copyPixels time:: tt==180 rec size == 7--------:: setPixel32 time:: tt==1320 copyPixels time:: tt==170 rec size == 8--------:: setPixel32 time:: tt==1730 copyPixels time:: tt==180 rec size == 9--------:: setPixel32 time:: tt==2150 copyPixels time:: tt==190 rec size == 10--------:: setPixel32 time:: tt==2690 copyPixels time:: tt==190 rec size == 11--------:: setPixel32 time:: tt==3220 copyPixels time:: tt==190 rec size == 12--------:: .... //Personally interested in this one, because my tilemap uses 16x16 tiles: rec size == 16--------:: setPixel32 time:: tt==6830 copyPixels time:: tt==210
Particle Bit Packing Math Experiment
Was wondering if I could make a more efficient particle system by packing the position, velocity, and acceleration values of the particle into a SINGLE signed integer.
The thought was that access would be faster since it is all in the same variable.
So far... I don't see any support for this in my research. Here is my math stuff.
Note: was also packing color information.
A bit more looking, looks like you may be able to get a marginal speed increase.
But so marginal that, you would have to pack and unpack in a flawless manner to get that speed increase.
And the obfuscation would make the system too hard to work with and edit.
//Packs position, velocity, acceleration, and color into a SIGNED(+-) integer.
public function putPVAC(p2047:int, v127:int, a7:int, c3A:int, c3R:int, c3G:int, c3B:int):int
{
//SIZES: HEX BITS UINT INT BIN
//pos: FFF 12 4095 +-2047 1111 1111 1111
//vel: FF 8 255 +-127 1111 1111
//acc: F 4 15 +-7 1111
//clr: FF 8 255 N/A 1111 1111
// aa: 3 2 3 3 11
// rr: 3 2 3 3 11
// gg: 3 2 3 3 11
// bb: 3 2 3 3 11
//pack in position:
var packed:int = p2047;
packed = packed << 8; //shifting to make room for VELOCITY.
//pack in velocity.
if (v127 < 0) { v127 = 127 + (0 - v127);} //handle negative numbers.
packed = (packed | (v127 & 0xFF)) << 4; //shifting to make room for ACCELERATION.
//pack in acceleration:
if (a7 < 0) { a7 = 7 + (0 - a7); } //handle negative numbers.
packed = (packed | (a7 & 0xF)) << 2; //shift to make room for first color component.
// 42 127 212
//0 ----- 85 ----- 170 ----- 255
//pack in ARGB:
if (c3A > 212) { packed = (packed | (0x3)); } else
if (c3A > 127) { packed = (packed | (0x2)); } else
if (c3A > 42 ) { packed = (packed | (0x1)); };
//other wise it is zero and nothing needs to be set.
packed = (packed << 2); //shift to make room for SECOND color component.
// 42 127 212
//0 ----- 85 ----- 170 ----- 255
//pack in ARGB:
if (c3R > 212) { packed = (packed | (0x3)); } else
if (c3R > 127) { packed = (packed | (0x2)); } else
if (c3R > 42 ) { packed = (packed | (0x1)); };
//other wise it is zero and nothing needs to be set.
packed = (packed << 2); //shift to make room for THIRD color component. (G)
// 42 127 212
//0 ----- 85 ----- 170 ----- 255
//pack in ARGB:
if (c3G > 212) { packed = (packed | (0x3)); } else
if (c3G > 127) { packed = (packed | (0x2)); } else
if (c3G > 42 ) { packed = (packed | (0x1)); };
//other wise it is zero and nothing needs to be set.
packed = (packed << 2); //shift to make room for FOURTH color component. (B)
// 42 127 212
//0 ----- 85 ----- 170 ----- 255
//pack in ARGB:
if (c3B > 212) { packed = (packed | (0x3)); } else
if (c3B > 127) { packed = (packed | (0x2)); } else
if (c3B > 42 ) { packed = (packed | (0x1)); };
//other wise it is zero and nothing needs to be set.
return packed;
}
public function getPVAC(com:int):void
{
var acc:int;
var vel:int;
var pos:int;
var clr:int;
var color:uint;
// AA RR GG BB
// 11 11 11 11
//extract color: // 0xC0 | 0x30 | 0xC | 0x3
clr = com & 0xFF; //ARGB color channels.
color = ( ((clr & 0xC0)>>6) * 85) << 24 |
( ((clr & 0x30)>>4) * 85) << 16 |
( ((clr & 0xC )>>2) * 85) << 8 |
( ((clr & 0x3 )>>0) * 85) << 0;
//extract acceleration.
com = com >> 8;
acc = com & 0xF;
if (acc > 7) { acc = 7 - acc; }
//extract velocity.
com = com >> 4; // ==com >> 12 when looking at cumulatively.
vel = com & 0xFF;
if (vel > 127) { vel = 127 - vel; }
//extract position:
//This is only ONE line because the signed bit on the com integer affects this.
pos = com >> 8; // ==com >> 20 when looking at cumulatively.
trace("color==" + color);
trace("pos ==" + pos);
trace("vel ==" + vel);
trace("acc ==" + acc);
//make sure color is what you think it is:
var rgb:RGB = RGB.make(color);
trace("a ==" + rgb.Alpha);
trace("r ==" + rgb.Red);
trace("g ==" + rgb.Green);
trace("b ==" + rgb.Blue);
}//getPVAC
Tuesday, March 5, 2013
int(2147483648)
int(2147483648) marginally faster than int.minValue in AS3
t1 = getTimer();
counter = 0;
do
{
counter++;
if (counter > counterMax) { break;}
someValue = int(2147483648);
}while (true);
t2 = getTimer();
tt = t2 - t1;
trace("tt==" + tt);
t1 = getTimer();
counter = 0;
do
{
counter++;
if (counter > counterMax) { break;}
someValue = int.MIN_VALUE;
}while (true);
t2 = getTimer();
tt = t2 - t1;
trace("tt==" + tt);
Friday, March 1, 2013
Subscribe to:
Comments (Atom)
