Topic: Isometric projection in Flash. |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: f(x) |
posted 09-12-2007 13:43
Ok, I found a tutorial on isometric projection in Flash but now there's a part I just can't figure out. The tutorial mentions that 2D environments cannot be transformed to 3D environments. True, but that's only because the value of the third dimension is unknown in a 2D world. If we give this third dimension a value, for example, zero, we can then transform the 2D environment into a 3D environment. code: // transforms x,y,z coordinates into Flash x coordinate xFla = function (x, y, z) { // cartesian coordinates xCart = (x-z)*Math.cos(0.46365); // flash coordinates xI = xCart+xOrigin; return (xI); }; // transforms x,y,z coordinates into Flash y coordinate yFla = function (x, y, z) { // cartesian coordinates yCart = y+(x+z)*Math.sin(0.46365); // flash coordinates yI = -yCart+yOrigin; return (yI); };
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 09-12-2007 16:20
My brain says you're looking for a perspective transformation (possibly with fixed parameters). |
Paranoid (IV) Inmate From: f(x) |
posted 09-13-2007 22:02
Sorry, that's not what I'm looking for. Perhaps this may clarify: code: // Slightly altered versions of the previous functions: // transforms x,y,z coordinates into Flash x coordinate xFla = function (x, y, z) { // cartesian coordinates xCart = (x-z)*Math.cos(0.46365); // flash coordinates xI = xCart; return (xI); }; // transforms x,y,z coordinates into Flash y coordinate yFla = function (x, y, z) { // cartesian coordinates yCart = y+(x+z)*Math.sin(0.46365); // flash coordinates yI = -yCart; return (yI); }; // Let's say function blah() is here... // Usage: blah ( point_X , point_Y , altitude ) ix = 12; iy = 5; iz = 23; fx = xFla(ix,iy,iz); fy = yFla(ix,iy,iz); trace("fx = "+fx+" | fy = "+fy); // output: fx = -9.83868733881014 | fy = -20.652550692568 n = blah(fx,fy,iy); trace("x = "+n[0]+" | y = "+n[1]+" | z = "+n[2]); // output: x = 12 | y = 5 | z = 23
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 09-13-2007 22:11
code: z = 23; //constant... xBlah = function (x, y) { x = (xCart + z * Math.cos(0.46365) ) / Math.cos(0.46365) return (x); }; // transforms x,y,z coordinates into Flash y coordinate yBlah = function (xCart, yCart) { // cartesian coordinates y = yCart-(xCart+z)*Math.sin(0.46365); return (-y); };
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 09-13-2007 22:12
just to clarify: |
Paranoid (IV) Inmate From: f(x) |
posted 09-13-2007 22:18 |
Paranoid (IV) Inmate From: f(x) |
posted 09-14-2007 11:48
Since the function didn't work out as expected and I'm not the best at mathematics, I reached the author of the tutorial via Skype. I told him what I wanted to do and he came up with the right functions. So, apparently he's very good with the math. So, I have it figured out now. Thanks for your effort anyway. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 09-15-2007 10:07
mind to post the improvements for further reference? |
Paranoid (IV) Inmate From: f(x) |
posted 09-15-2007 16:08
Sure. code: // --- Functions --- // Convert isometric coordinates to Flash X coordinate xFla = function (x, y, z) { return ((x-z)*isoCos); }; // Convert isometric coordinates to Flash Y coordinate yFla = function (x, y, z) { return (-(y+(x+z)*isoSin)); }; // Convert Flash X and Y and altitude coordinates to isometric X coordinate xIso = function (xF, yF, y) { return ((xF)/isoCos-(yF+y)/isoSin)/2; }; // Convert Flash X and Y and altitude coordinates to isometric Y coordinate (actually Z, but I don't give a hoot) yIso = function (xF, yF, y) { return (-((xF)/isoCos+(yF+y)/isoSin))/2; }; // --- Variables --- // isoCos and isoSin are defined in variables so they don't have to be calculated every time the functions are executed isoCos = Math.cos(0.46365); isoSin = Math.sin(0.46365); // --- Example Use --- // This example shows mostly why I wanted the new functions isoWorld_mc.onMouseUp = function() { // Let's put _xmouse and _ymouse into isometric coordinates isoMouseX = xIso(_xmouse, _ymouse, 0); isoMouseY = yIso(_xmouse, _ymouse, 0); // Let's round it to the nearest 100 isoMouseSnapX = Math.round(isoMouseX/100)*100; isoMouseSnapY = Math.round(isoMousey/100)*100; // Let's convert them back to Flash coordinates flaMouseX = xFla(isoMouseSnapX, 0, isoMouseSnapY); flaMouseY = yFla(isoMouseSnapX, 0, isoMouseSnapY); // Now, let's change the position of this sucker this._x -= flaMouseX; this._y -= flaMouseY; };
|