Sphere CollisionDownload Example
// Class Main.as
package
{
import flare.basic.*
import flare.collisions.*
import flare.core.*
import flare.system.*
import flare.utils.*
import flash.display.*
import flash.events.*
import flash.geom.*
public class Main extends Sprite
{
private var scene:Scene3D
private var level:Pivot3D
private var player:Pivot3D
private var collisions:SphereCollision
public function Main()
{
stage.quality = "medium"
// creates the scene.
scene = new Scene3D( this )
// define complete event.
scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent )
// loads 3d objects.
level = scene.addChildFromFile( "level.f3d" )
player = scene.addChildFromFile( "walker.f3d" )
}
private function completeEvent(e:Event):void
{
// Resets 3d object scales and rotations. This method must not be used with objects that include animations.
// Animated object/s may derive unexpected results.
// The resetXForm method can be used to correct the collision calculation in those cases with scaled objects which may cause difficulties.
Pivot3DUtils.resetXForm( level )
// create and define the collision.
collisions = new SphereCollision( player, 20, new Vector3D( 0, 20, 0 ) )
// test the collisions with entire level.
collisions.addCollisionWith( level )
// update the scene.
scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent )
}
private function updateEvent(e:Event):void
{
// rotate and translate the player.
if ( Input3D.keyDown( Input3D.RIGHT ) ) { player.rotateY( 5 ); player.frameSpeed = 1 }
if ( Input3D.keyDown( Input3D.LEFT ) ) { player.rotateY( -5 ); player.frameSpeed = 1 }
if ( Input3D.keyDown( Input3D.UP ) ) { player.translateZ( 4 ); player.frameSpeed = 1 }
if ( Input3D.keyDown( Input3D.DOWN ) ) { player.translateZ( -4 ); player.frameSpeed = -1 }
// if some key is pressed, play the animation.
if ( Input3D.keyDown( Input3D.UP ) ||
Input3D.keyDown( Input3D.DOWN ) ||
Input3D.keyDown( Input3D.LEFT ) ||
Input3D.keyDown( Input3D.RIGHT ) )
player.play()
else
player.stop()
// like gravity.
player.y -= 3
// once we moved the player, we test for collisions
collisions.slider()
// the time taken to perform the last check in milliseconds.
info_txt.text = "Collision time:" + collisions.collisionTime
/**** simple camera managment ****/
// set the camera position relative to the player.
Pivot3DUtils.setPositionWithReference( scene.camera, 0, 600, -600, player, 0.1 )
// orientate the camera to player position.
Pivot3DUtils.lookAtWithReference( scene.camera, 0, 0, 0, player )
}
}
}

English