Professional Documents
Culture Documents
B 2 Circle Shape
B 2 Circle Shape
package Box2D.Collision.Shapes{
import Box2D.Common.Math.*;
import Box2D.Common.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Common.b2internal;
use namespace b2internal;
/**
* A circle shape.
* @see b2CircleDef
*/
public class b2CircleShape extends b2Shape
{
override public function Copy():b2Shape
{
var s:b2Shape = new b2CircleShape();
s.Set(this);
return s;
}
/**
* @inheritDoc
*/
public override function TestPoint(transform:b2Transform, p:b2Vec2) :
Boolean{
//b2Vec2 center = transform.position + b2Mul(transform.R, m_p);
var tMat:b2Mat22 = transform.R;
var dX:Number = transform.position.x + (tMat.col1.x * m_p.x +
tMat.col2.x * m_p.y);
var dY:Number = transform.position.y + (tMat.col1.y * m_p.x +
tMat.col2.y * m_p.y);
//b2Vec2 d = p - center;
dX = p.x - dX;
dY = p.y - dY;
//return b2Dot(d, d) <= m_radius * m_radius;
return (dX*dX + dY*dY) <= m_radius * m_radius;
}
/**
* @inheritDoc
*/
public override function RayCast(output:b2RayCastOutput,
input:b2RayCastInput, transform:b2Transform):Boolean
{
//b2Vec2 position = transform.position + b2Mul(transform.R, m_p);
var tMat:b2Mat22 = transform.R;
var positionX:Number = transform.position.x + (tMat.col1.x * m_p.x +
tMat.col2.x * m_p.y);
var positionY:Number = transform.position.y + (tMat.col1.y * m_p.x +
tMat.col2.y * m_p.y);
return false;
}
/**
* @inheritDoc
*/
public override function ComputeAABB(aabb:b2AABB, transform:b2Transform) :
void{
//b2Vec2 p = transform.position + b2Mul(transform.R, m_p);
var tMat:b2Mat22 = transform.R;
var pX:Number = transform.position.x + (tMat.col1.x * m_p.x +
tMat.col2.x * m_p.y);
var pY:Number = transform.position.y + (tMat.col1.y * m_p.x +
tMat.col2.y * m_p.y);
aabb.lowerBound.Set(pX - m_radius, pY - m_radius);
aabb.upperBound.Set(pX + m_radius, pY + m_radius);
}
/**
* @inheritDoc
*/
public override function ComputeMass(massData:b2MassData, density:Number) :
void{
massData.mass = density * b2Settings.b2_pi * m_radius * m_radius;
massData.center.SetV(m_p);
/**
* @inheritDoc
*/
public override function ComputeSubmergedArea(
normal:b2Vec2,
offset:Number,
xf:b2Transform,
c:b2Vec2):Number
{
var p:b2Vec2 = b2Math.MulX(xf, m_p);
var l:Number = -(b2Math.Dot(normal, p) - offset);
if (l < -m_radius + Number.MIN_VALUE)
{
//Completely dry
return 0;
}
if (l > m_radius)
{
//Completely wet
c.SetV(p);
return Math.PI * m_radius * m_radius;
}
//Magic
var r2:Number = m_radius * m_radius;
var l2:Number = l * l;
var area:Number = r2 *( Math.asin(l / m_radius) + Math.PI / 2) + l *
Math.sqrt( r2 - l2 );
var com:Number = -2 / 3 * Math.pow(r2 - l2, 1.5) / area;
return area;
}
/**
* Get the local position of this circle in its parent body.
*/
public function GetLocalPosition() : b2Vec2{
return m_p;
}
/**
* Set the local position of this circle in its parent body.
*/
public function SetLocalPosition(position:b2Vec2):void {
m_p.SetV(position);
}
/**
* Get the radius of the circle
*/
public function GetRadius():Number
{
return m_radius;
}
/**
* Set the radius of the circle
*/
public function SetRadius(radius:Number):void
{
m_radius = radius;
}
};