Packageflare.physics
Classpublic class Physics
InheritancePhysics Inheritance Object

Creates a new physics manager. By default one is created with each Scene3D, but you can create and manage your own instances for specific pourposes.

View the examples

See also

Scene3D physics


Public Properties
 PropertyDefined By
  allowOverlaping : Number = 0.2
[static] Defines the amount of overlaping allowed between objects.
Physics
  axis : int
Gets or sets the default axis vector for the broad phase.
Physics
  colliders : Vector.<Collider>
The collection of colliders.
Physics
  contacts : Vector.<Contact>
[read-only] Gets a reference to the contacts vector resulted in the last test() or rayCast() call.
Physics
  gravity : Vector3D
The default gravity vector.
Physics
  numContacts : int
[read-only] Gets the number of contacts resulted in the last step() call.
Physics
  sleepingOverlap : Number = 0.02
[static] A value which defines a tolerance for sleeping objects to overlap before wake up.
Physics
Public Methods
 MethodDefined By
  
Physics(broad:IBroadPhase = null)
Creates a new physics manager.
Physics
  
Adds a new collider to the simulation.
Physics
  
awake(position:Vector3D, radius:Number):void
Awakes objects around a certain point, useful for big impacts.
Physics
  
debug(aabb:Boolean = true, contacts:Boolean = true, count:int = -1):void
This method allows for some visual debuggung tools when working with physics.
Physics
  
rayCast(pos:Vector3D, dir:Vector3D, target:Collider, maxDistance:Number = 1000):Contact
Test a ray against a specific collider.
Physics
  
removeCollider(collider:Collider):void
Remvoes a collider from the physics engine.
Physics
  
reset():void
Physics
  
step(iterations:int = 3, timeStep:Number):void
Performs one step of the physics simulation.
Physics
  
test(c0:Collider, c1:Collider, contactCount:int = 0):int
Test collisions between two colliders.
Physics
Public Constants
 ConstantDefined By
  MAX_COLLIDERS : uint = 65536
[static]
Physics
  MAX_CONTACTS : uint = 65536
[static]
Physics
Property Detail
allowOverlapingproperty
public static var allowOverlaping:Number = 0.2

Defines the amount of overlaping allowed between objects. Greater values results in more stable simulations, but causes the objects to overlap more. This value is completelly dependent of the scale units your are working. Adjust it if necessary.

axisproperty 
axis:int

Gets or sets the default axis vector for the broad phase. A value of -1 means the axis will be automatically adjusted, while this could cause performance issue when changing the axis. A value o 0 corresponds to the X-Axis, 1 to Y-Axis, and 2 to Z-Axis. Set this property to uase the longest axis in yout scene, for example Y-Axis for a vertical game or X-Axis for a side scroller game.


Implementation
    public function get axis():int
    public function set axis(value:int):void
collidersproperty 
public var colliders:Vector.<Collider>

The collection of colliders. this is only for reference, do not change this vector.

contactsproperty 
contacts:Vector.<Contact>  [read-only]

Gets a reference to the contacts vector resulted in the last test() or rayCast() call. Because contacts are getting reused from call to call, the length of the vector could be anything from 0 to MAX_CONTACTS. To get the correct number of contacts use the returned count value from the test() or rayCast() method.


Implementation
    public function get contacts():Vector.<Contact>

See also

gravityproperty 
public var gravity:Vector3D

The default gravity vector. Colliders receive this refernce when added to the physics manager. You can set a different gravity vector for each collider if needed.

See also

numContactsproperty 
numContacts:int  [read-only]

Gets the number of contacts resulted in the last step() call.


Implementation
    public function get numContacts():int

See also

sleepingOverlapproperty 
public static var sleepingOverlap:Number = 0.02

A value which defines a tolerance for sleeping objects to overlap before wake up. Small values works fine in most of the cases. When dealing with a lot of objects stacked randomly together, try increasing this value to get them to enter in sleeping mode more easily.

Constructor Detail
Physics()Constructor
public function Physics(broad:IBroadPhase = null)

Creates a new physics manager. By default one is created with each Scene3D, but you can create and manage your own instances for specific pourposes.

Parameters
broad:IBroadPhase (default = null)

See also

Method Detail
addCollider()method
public function addCollider(collider:Collider):Collider

Adds a new collider to the simulation. If the collider is set to a Pivot3D, there is no need to add the collider manually to the physics engine.

Parameters

collider:Collider

Returns
Collider — The added collider or null if the collider was already added.

See also

awake()method 
public function awake(position:Vector3D, radius:Number):void

Awakes objects around a certain point, useful for big impacts.

Parameters

position:Vector3D — The origin of the impact.
 
radius:Number — The radius around the origin.

debug()method 
public function debug(aabb:Boolean = true, contacts:Boolean = true, count:int = -1):void

This method allows for some visual debuggung tools when working with physics. It should be called usually inside render or postRender event.

Parameters

aabb:Boolean (default = true) — true to show axis aligned bounding boxes.
 
contacts:Boolean (default = true) — true to show contact points. The scale of these is directly related to allowOverlaping property, which means when more overlapping is allowed, contact points will appear bigger.
 
count:int (default = -1) — A number of maximum contacts to draw.

rayCast()method 
public function rayCast(pos:Vector3D, dir:Vector3D, target:Collider, maxDistance:Number = 1000):Contact

Test a ray against a specific collider. This method ignores possible states like sleeping, triggers, or disabled colliders.

Parameters

pos:Vector3D — The source position of thr ray.
 
dir:Vector3D — The normalized direction of the ray.
 
target:Collider — The target collider to test against.
 
maxDistance:Number (default = 1000) — The ray maximum distance.

Returns
Contact — The result contact if any,

See also

removeCollider()method 
public function removeCollider(collider:Collider):void

Remvoes a collider from the physics engine. If the collider is removed from a Pivot3D, there is no need to remove the collider manually from the physics engine.

Parameters

collider:Collider — The target collider to remove.

See also

reset()method 
public function reset():void

step()method 
public function step(iterations:int = 3, timeStep:Number):void

Performs one step of the physics simulation. This method should be called inside the update event of the scene or enterFrame event to update the physics simulation.

Parameters

iterations:int (default = 3) — The number of iterations for the solver to perform. Higher values results in more aacurate and stable simulations but are more CPU expensives.

A value of 0, results in a detection phase only and no response to collisions is generated.

 
timeStep:Number (default = NaN) — The inverse delta time for each step. Higher values results slower but more stable simulations. By default is set to 1 / 30.

test()method 
public function test(c0:Collider, c1:Collider, contactCount:int = 0):int

Test collisions between two colliders. This method checks for collisions even if the colliders are not included in the same collision group and ignoring other possible states like sleeping, triggers, or disabled colliders. The result contacts are stored in the contacts property.

Parameters

c0:Collider — First collider.
 
c1:Collider — Second collider.
 
contactCount:int (default = 0) — The index in the contacs vector where the result contacts will be placed.

Returns
int — The last contact index if any.

See also


Example
Testing for collisions and contacts.
var count:int = 0;
count = physics.test( colliderA, colliderB, count );
count = physics.test( colliderA, colliderC, count );
count = physics.test( colliderA, colliderD, count );
if ( count > 0 ) {
    for ( var i:int = 0; i < count; i++ ) {
        var contact:Contact = physics.testContacts[i];
    }
}
         
Constant Detail
MAX_COLLIDERSConstant
public static const MAX_COLLIDERS:uint = 65536

MAX_CONTACTSConstant 
public static const MAX_CONTACTS:uint = 65536

Examples
Basic physics usage.
package  
{
    import flare.basic.*;
    import flare.physics.colliders.*;
    import flare.primitives.*;
    import flash.display.*;
    import flash.events.*;
    
    public class PhysicsTest extends Sprite 
    {
        private var scene:Scene3D;
        
        public function PhysicsTest() 
        {
            scene = new Viewer3D( this, "", 0.2 );
            scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
            scene.camera.setPosition( 20, 40, -60 );
            scene.camera.lookAt( 0, 0, 0 );
            
            var floor:Cube = new Cube( "floor", 100, 5, 100 );
            floor.collider = new BoxCollider( 100, 5, 100 );
            floor.collider.isStatic = true;
            
            var cube:Cube = new Cube( "cube" );
            cube.collider = new BoxCollider( 10, 10, 10 );
            cube.collider.isRigidBody = true;
            cube.setPosition( 0, 30, 0 );
            cube.rotateZ( 23 );
            
            scene.addChild( floor );
            scene.addChild( cube );
        }
        
        private function updateEvent(e:Event):void 
        {
            scene.physics.step();
        }
    }
}