How To Rotate A Cube In 3D With Matrix
April 4th, 2011
Another simple trick to avoid locks when rotating objects in 3D.
Goal: rotate a cube in all axes and avoid axes to switch.
See the problem in a video:
1. Consider following scenario (WRONG):
protected function init():void{ box = new Box(200,200,200,2,2,2); box.setMaterialToAllFaces(new FillMaterial(0xFF0000,1,1));; } protected function onMouseMove(event:MouseEvent):void{ if(!isDragging) return; var deltaX:int = lastX - event.stageX; var deltaY:int = lastY - event.stageY; lastX = event.stageX; lastY = event.stageY; // IMPORTANT PART box.rotationZ += deltaX*Math.PI/180; box.rotationY += deltaY*Math.PI/180; camera.render(); }
When you use rotationZ and rotationY together you will find that by rotating the cube you switch axes and at the end you rotate different axes than you wanted at the beginning.
2. Solution: rotate with Matrix
protected function init():void{ box = new Box(200,200,200,2,2,2); box.setMaterialToAllFaces(new FillMaterial(0xFF0000,1,1));; } protected function onMouseMove(event:MouseEvent):void{ if(!isDragging) return; var deltaX:int = lastX - event.stageX; var deltaY:int = lastY - event.stageY; lastX = event.stageX; lastY = event.stageY; // IMPORTANT PART var matrix:Matrix3D = box.matrix; matrix.appendRotation(deltaX,new Vector3D(0,0,1)); matrix.appendRotation(-deltaY,new Vector3D(1,0,0)); box.matrix = matrix; camera.render(); }
See correct version - rotating a cube with:
Also if you have a different solution - don’t hesitate to share it. I am pretty sure, there are more ways to do this.
Local Flash Peer-to-Peer Communication over LAN (without Cirrus/Stratus)
August 27th, 2010
Some of you guys were probably wondering how to establish P2P connections in the local network (LAN) without Adobe Cirrus. Yes. It’s possible. Using native IP-only multicast. Let’s build a simple chat with Posting. Adding Multicast broadcast is just a simple next step which I am sure you can accomplish by yourself with few hints from my article about Multicast
Open an IP Multicast connection. This can be done by specifying connection string as “rtmpf:”. Note, that this technique cannot be used for one-to-one communication. So no DIRECT_CONNECTIONS with NetStream, but you can do all RTMFP Group operations.
netConnection.connect("rtmfp:");
Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
July 28th, 2010
In the previous tutorial File Sharing over P2P in Flash Player 10.1 with Object Replication we went through the Object Replication basics. And you can see that the Receiver is requesting packets one by one. That’s not suitable for the real world app, but it’s good for testing on a LAN to see the progress. In the real world app, you can immediately request all packets using NetGroup.addWantObjects(beginIndex, endIndex);.
Transferring VoD video over P2P
Let’s get something real with Object Replication. The use-case I like most is a Realtime P2P Distributed System for Video-on-Demand.
Read the rest of this entry »