Back to Content

3D Collision with Actionscript 3

This example shows how to test a collision between the mouse cursor and a 3d object in a Coppercube scene using Actionscript 3. This example shows how to move a 3d object in a Coppercube scene using Actionscript 3. This feature only works with the Flash (.swf) target of CopperCube. For generic scripting which works on all targets, take a look at the scripting overview for JavaScript scripting.

To start, do the following:

The scene for this example.

Use the following code (saved as '' here, if you are using it in another way you will have to rename the constructor function):
    import flash.display.*
    public class main extends Sprite 
        private var fileToLoad:String = "test.swf"; 
        private var loader:Loader; 
        private var coppercubeSprite:DisplayObject;
        public function main()
            // load the coppercube scene from the other swf
            loader = new Loader();    

            loader.load(new URLRequest(fileToLoad)); 
            loader.contentLoaderInfo.addEventListener(Event.INIT, onCoppercubeLoaded); 
            addEventListener(Event.ENTER_FRAME, onGraphicsFrame);

        // called when the coppercube .swf file has been loaded and initialized
        private function onCoppercubeLoaded(e:Event):void 
            // get access to the simple API object:
            coppercubeSprite = loader.content as DisplayObject;
        // called every frame
        public function onGraphicsFrame(event:Event):void
            if (coppercubeSprite == null)
            var simpleAPI:Object = coppercubeSprite['simpleAPI'];
            if (simpleAPI && simpleAPI.isLoaded())
                var cubeMesh1:Object = simpleAPI.getSceneNodeByName("cubeMesh1");
                var cubeMesh2:Object = simpleAPI.getSceneNodeByName("cubeMesh2");
                var pos3DMouse:Object = simpleAPI.get3DPositionFrom2DPosition(mouseX, mouseY);
                var pos3DCamera:Object = simpleAPI.getSceneNodePosition(simpleAPI.getActiveCamera());
                if (pos3DCamera && pos3DMouse)
                    // test with cube mesh 1
                    var cube1Hovered:Boolean = 
                        simpleAPI.doesLineCollideWithBoundingBoxOfSceneNode(cubeMesh1, pos3DCamera.x, pos3DCamera.y, pos3DCamera.z,
                                                                            pos3DMouse.x, pos3DMouse.y, pos3DMouse.z);
                    var cube2Hovered:Boolean = 
                        simpleAPI.doesLineCollideWithBoundingBoxOfSceneNode(cubeMesh2, pos3DCamera.x, pos3DCamera.y, pos3DCamera.z,
                                                                            pos3DMouse.x, pos3DMouse.y, pos3DMouse.z);
                    simpleAPI.setMaterialLighting(cubeMesh1, 0, cube1Hovered);
                    simpleAPI.setMaterialLighting(cubeMesh2, 0, cube2Hovered);
This will load the just created file test.swf and turn on the dynamic light for the cube where the mouse pointer is over, resulting in a dark cube because we did not add any light to the scene. The result should look like this:

The 3d object where the mouse cursor is over gets dark.

If you don't see anything in your flash movie, check if the test.swf file could be loaded correctly. Note that you need to target Flash player 11 or, when using the 'Use Old Flash player rendering' setting at least Flash player 10 (when using Flex, use at least Flex SDK 3.3 with the compiler flag -target-player 10).
You might also need to compile your flash file with the compiler setting '-use-network=false'. An alternative is to change your local flash player security settings or to run the flash movie from a (local) webserver.

This example works by accessing the simpleAPI object exposed by the CopperCube SWF file. You can manipulate all aspects of the 3D scene with this API: textures, materials, positions, objects etc. For a full reference to all functions, see the AS3 Scripting reference.