setting new Y axis in as3 - actionscript-3

Ok lets say i have image and i want to rotate it along Y axis. Problem is i need to move my Y axis so rotation would be proper. I do my work in pure as3 code so this trick needs to be done in pure as3 code, so no flash drawing tools available :( .
here some images that may help you
LATEST EDIT: problem is solved with ez then i found that as3 actually also have 4x4 matrices that called Matrix3D in documentation , so now it possible to do all 3d rotations.

Answering your question without a code example is very hard. As I can see, your local position of the figure is somewhat different from what you intend it to be to rotate as desired.
Generally spoken, there are two potential issues to solve:
You may adjust your image's x and y values so that the image is always drawn as desired. So you need to add/remove an offsetX and offsetY variable to all your drawings of your image to fine-adjust it's local position.
If you use a rotation function with a matrix you should beware of the correct sequence. Rotation applies differently either when you apply translation before or after it.
Hope this may help you. If you want a more specific answer, you need to provide some code exampple.

Related

as3 3D global transform / rotation

I have a little problem. I have 2D image of roulette wheel on the stage and used 3D rotation tool to get a little 3D effect (Image 1). Now I want to rotate it on it's Z axis so that I can get spining effect of wheel. I tried to use rotationZ += ... but it doesn't rotate it the way I want. I saw one tool in flash toolbar called "Global transform" (Image 2). When I click it I see transformation lines for image (Image 3), that I need for rotation, the blue line is one I need (Z axis). But I don't know how to use that kind of transformation via code. I don't even know if that is local or global rotation. I hope you understand me, maybe pictures will help you! Thanks!
HERE IS DROPBOX LINK FOR IMAGES --> Dropbox Images
What you are trying to do is simply an issue of nesting elements and applying effects on them in certain ways.
To accomplish the rotation as you want, you'll want to create your roulette wheel image in 2D as if you were looking at from a birds eye view. I highly recommend creating this graphic in vector format. Import this graphic into Flash and turn it into a movieclip. For arguments sake, I'm going to give this movieclip an instance name of wheel so it is easy for us to follow along with these instructions. Now, within the wheel instance, you'll want to apply a rotation to the roulette wheel. You can accomplish this either by using a Motion tween on the timeline, but the best way would probably be to use a programmatic approach so you can randomize the rotation in some way.
Now, place the 'wheel' instance on the stage and use the 3D Rotation Tool and rotate the wheel on it's X axis, this will make the wheel look like one in your image.
Now if you have performed the rotation within the wheel instance properly, you will have your spinning wheel.
I've created a sample of what you want with the following files using Flash CC 2014 that you can download here:
FLA File
Sample compiled SWF FILE
There is actually some math involved in understanding 3D transformations - including rotations - and how they combine with each other.
If your are interested in further explanations, and have a bit of math background, have a look at the theory
otherwise, the first thing you should remember is that rotation/scaling/translations may be composed (combined) but that the order is very important. rotationX, rotationY, rotationZ are sort of "coordinates" that enable to describe any orientation in the vector space, like x,y,z describe any position. But they are not sufficient to allow to do any rotation around any axis from any current orientation.
In your case, what you want is to combine a rotation around it's own local z (blue) axis, which means before the initial rotation you have already applied. ex:
roulette.transform.matrix3D.prependRotation(10, new Vector3D(0,0,1));
where 10 would be the angle you chose for the rotation, and the pivot of your object would be the pivot of the rotation (ohterwise, specify your own pivot by passing an additional Vector3D
To complete the picture, if you use appendRotation() instead of prependRotation(), you will rotate your object in the z axis of the world (global) coordinates (after it's current trnasformation).
You may also as already suggested create a Symbol to use as a container, and rotate you object inside it's container, but messing with Matrix3D will give you more control in the end, often with just one line of code like in this case.

Rendering a Skybox without 3d libraries

So... I have some sets of 6 pictures, like these http://www.humus.name/index.php?page=Textures , and I basically want to render them on an html5 canvas like this: http://www.allforthecode.co.uk/aftc/forum/user/modules/forum/article.php?index=5&subindex=4&aid=303
But I'd rather not use any 3d library such as webgl or three.js, since that's the only 3d-related feature I need and I want the whole thing to be as lightweight as possible.
I thought, "c'mon, it's just a rotating cube, can't be that hard!"
WRONG.
My original planwas to keep the camera position fixed, ant then to keep track of the x and y offset (in radians) of each vertex, and then to project them on my canvas and to deform the context accordingly to render each face of the cube.
That approach doesn't seem to work, tho, so... can someone give me a pseudocode algorithm?
I think a good way to tackle this problem is to use CSS3 3d transformations. There's quite a few turorials to be found on the web giving details on how to build a 3d cube with CSS. Instead of using <div>s to build the cube's sides, you could use <img> or even <canvas> elements. By playing around with perspective attributes you should be able to place the 'camera' inside the cube looking out.

Perspective image transformation in Flex

What the best way to make this kind of transformation: I have 4 pairs of coordinates, and I need to put the corners of my image exactly to them?
Like in photoshop, when you in free transform mode and moving corners of the image while holding ctrl.
Something like that.
If you can use the Flash 10 drawTriangles() method, then this link will provide you with a great explanation of what you are trying to do, with some well documented functioning code. The code is very similar to the solution that romamik provided, however, you will need to calculate the correct t value of the uvtData to get it to render correctly:
To summarize from that link:
Calculate the center point of the rectangle you are trying to draw
Get the distance between each point and the center
Calculate the ratio between the diagonals
use the formula (1/point_distance)*ratio to calculate perspective distortion for each point -- project into a faux 3d space (aka "magic" as the author of the post calls it)
That should produce the effect you are looking for.
If you can't use drawTriangles() then this link provides an alternative, however it wont produce as good of results as the drawTriangles() method.
You can use graphics.drawTriangles.
http://help.adobe.com/en_US/as3/dev/WS84753F1C-5ABE-40b1-A2E4-07D7349976C4.html
This code should do what you need (untested)
graphics.beginBitmapFill(bitmapData);
graphics.drawTriangles(
Vector.<Number>([10,10, 100,10, 10,100, 100,100]),
Vector.<int>([0,1,2, 1,3,2]),
Vector.<Number>([0,0, 1,0, 0,1, 1,1])
);
You can use the DistortImage class ported from the old 3D engine Sandy. A cleaner way would be the make use of rotateX and rotateY but it would require some mathematical pain!

Orthographic projection - What is the process converting 3d point to 2d

I'm implementing a simple penalty shootout game using actionscript 3.0. The view of the game is similar to view of the old "Sensible World of Soccer". I want to use 3d game logic by using dimension z as I think that it could help me in order to achieve better collision detection - response results. However, I would like to keep the graphics style and view equivalent to old 2d soccers'. Hence, I assume that orthographic projection is suitable for this implementation. Although there is plenty of information in the internet regarding orthographic projection, I'm a little bit confused about how someone can apply it in his/her code.
So my questions are:
Which is the procedure step by step in order for someone to convert a 3d (x, y, z) point to 2d (x', y') point in orthographic projection?
Can we avoid using matrices? If yes, what are the equations that associate coordinates x', y' with x, y, z?
Do we have to define a camera position and angle before applying the conversion? In my case, camera will be in a fixed position and angle.
DisplayObjects and their descendants (ie MovieClip and Sprite) have a z property you can use to do this without the headaches - they also have rotationX/Y/Z and scaleX/Y/Z properties too!
Using 'z' will adjust the position and scale of an object accordingly (though it will convert vectors to bitmaps), there's no depth sorting, so it will stay on top of objects even if its z co-ord suggests it should be behind them, but for the project you have in mind I can't see this being a problem - it's pretty easy to fix anyway, have an array of objects in the scene, sort it according to z-position and reset the depth index of each/re-add to stage in sorted order.
You can use the perspectiveProjection member of a clip to adjust the FOV, origin etc -
Perspective Tutorial
..but you don't need to get any more sophisticated than that. Certainly you don't need to dabble with matrices with a fixed camera view, even if you wanted to calculate this manually as an experiment.
Hope this helps

Making a draggable, resizable image on canvas

I am reading this tutorial about making draggable and resizable rectangles
http://simonsarris.com/blog/225-canvas-selecting-resizing-shape
It is a good one and I want to create a similar one using images and this is what I had done
http://jsfiddle.net/LUhsK/2/
all i had done is to change the script that instead of drawing a square I draw an image given the image source, initial X, initial Y, width and height but as you can see it doesn't work. I really can't find what seems to be the problem
If I clearly understood what you are searching for I guess it can be easily resolved with
droppedElement = ui.helper.clone();
droppedElement.resizable();