[Alternativa3D] HitTest Method
/**
* Copyright clockmaker ( http://wonderfl.net/user/clockmaker )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7YDv
*/
package
{
import alternativ5.engine3d.controllers.*;
import alternativ5.engine3d.core.*;
import alternativ5.engine3d.display.*;
import alternativ5.engine3d.materials.*;
import alternativ5.engine3d.primitives.*;
import alternativ5.types.*;
import alternativ5.utils.*;
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
[SWF(width="465", height="465", frameRate="24", backgroundColor="0x0")]
public class Main extends Sprite
{
private var scene:Scene3D;
private var view:View;
private var camera:Camera3D;
private var cameraController:CameraController;
private var sphere1:GeoSphere;
private var sphere2:GeoSphere;
private var sphere1MaxDistance:Number = 200;
private var sphere2MaxDistance:Number = 200;
public function Main()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// Creating scene
scene = new Scene3D();
scene.root = new Object3D();
// creeate sphere
sphere1 = new GeoSphere(sphere1MaxDistance, 2)
sphere1.cloneMaterialToAllSurfaces(new DevMaterial());
scene.root.addChild(sphere1);
sphere2 = new GeoSphere(sphere2MaxDistance, 2)
sphere2.cloneMaterialToAllSurfaces(new DevMaterial());
scene.root.addChild(sphere2);
// Adding camera and view
camera = new Camera3D();
camera.x = 0;
camera.y = 500;
camera.z = 1000;
scene.root.addChild(camera);
view = new View(null, stage.stageWidth, stage.stageHeight);
addChild(view);
view.camera = camera;
// Connecting camera controller
cameraController = new CameraController(stage);
cameraController.camera = camera;
cameraController.lookAt(new Point3D(0,0,0));
// FPS display launch
FPS.init(stage);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
// Scene calculating
scene.calculate();
// motion
sphere2.x = 1000 * Math.sin(getTimer() / 1000);
// hitCheck
var color:uint = (hitTestObject3D(sphere1, sphere2)) ? 0xFF0000 : 0x0000FF;
graphics.clear();
graphics.beginFill(color);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
}
/**
* HitTest Object3D
* logic from Papervision3D
* @param obj1 Object3D
* @param obj2 Object3D
* @param multiplier
* @return isHit
*/
private function hitTestObject3D( obj1:Object3D, obj2:Object3D, multiplier:Number=1 ):Boolean
{
var dx :Number = obj1.x - obj2.x;
var dy :Number = obj1.y - obj2.y;
var dz :Number = obj1.z - obj2.z;
var d2 :Number = Math.sqrt(dx*dx + dy*dy + dz*dz);
var sA :Number = sphere1MaxDistance;
var sB :Number = sphere2MaxDistance;
sA = sA * multiplier;
return sA + sB > d2;
}
}
}