home *** CD-ROM | disk | FTP | other *** search
- package Box2D.Dynamics
- {
- import Box2D.Collision.Shapes.b2MassData;
- import Box2D.Collision.Shapes.b2Shape;
- import Box2D.Collision.Shapes.b2ShapeDef;
- import Box2D.Common.Math.b2Mat22;
- import Box2D.Common.Math.b2Math;
- import Box2D.Common.Math.b2Sweep;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Common.Math.b2XForm;
- import Box2D.Dynamics.Contacts.b2ContactEdge;
- import Box2D.Dynamics.Joints.b2JointEdge;
-
- public class b2Body
- {
-
- public static var e_fixedRotationFlag:uint = 64;
-
- public static var e_frozenFlag:uint = 2;
-
- public static var e_maxTypes:uint = 3;
-
- public static var e_sleepFlag:uint = 8;
-
- private static var s_massData:b2MassData = new b2MassData();
-
- public static var e_bulletFlag:uint = 32;
-
- public static var e_staticType:uint = 1;
-
- public static var e_islandFlag:uint = 4;
-
- public static var e_allowSleepFlag:uint = 16;
-
- private static var s_xf1:b2XForm = new b2XForm();
-
- public static var e_dynamicType:uint = 2;
-
-
- public var m_next:b2Body;
-
- public var m_xf:b2XForm;
-
- public var m_contactList:b2ContactEdge;
-
- public var m_angularVelocity:Number;
-
- public var m_shapeList:b2Shape;
-
- public var m_force:b2Vec2;
-
- public var m_mass:Number;
-
- public var m_sweep:b2Sweep;
-
- public var m_torque:Number;
-
- public var m_userData:*;
-
- public var m_flags:uint;
-
- public var m_world:b2World;
-
- public var m_prev:b2Body;
-
- public var m_invMass:Number;
-
- public var m_type:int;
-
- public var m_linearDamping:Number;
-
- public var m_shapeCount:int;
-
- public var m_angularDamping:Number;
-
- public var m_invI:Number;
-
- public var m_linearVelocity:b2Vec2;
-
- public var m_sleepTime:Number;
-
- public var m_jointList:b2JointEdge;
-
- public var m_I:Number;
-
- public function b2Body(bd:b2BodyDef, world:b2World)
- {
- m_xf = new b2XForm();
- m_sweep = new b2Sweep();
- m_linearVelocity = new b2Vec2();
- m_force = new b2Vec2();
- super();
- m_flags = 0;
- if(bd.isBullet)
- {
- m_flags |= e_bulletFlag;
- }
- if(bd.fixedRotation)
- {
- m_flags |= e_fixedRotationFlag;
- }
- if(bd.allowSleep)
- {
- m_flags |= e_allowSleepFlag;
- }
- if(bd.isSleeping)
- {
- m_flags |= e_sleepFlag;
- }
- m_world = world;
- m_xf.position.SetV(bd.position);
- m_xf.R.Set(bd.angle);
- m_sweep.localCenter.SetV(bd.massData.center);
- m_sweep.t0 = 1;
- m_sweep.a0 = m_sweep.a = bd.angle;
- var tMat:b2Mat22 = m_xf.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- m_sweep.c.x += m_xf.position.x;
- m_sweep.c.y += m_xf.position.y;
- m_sweep.c0.SetV(m_sweep.c);
- m_jointList = null;
- m_contactList = null;
- m_prev = null;
- m_next = null;
- m_linearDamping = bd.linearDamping;
- m_angularDamping = bd.angularDamping;
- m_force.Set(0,0);
- m_torque = 0;
- m_linearVelocity.SetZero();
- m_angularVelocity = 0;
- m_sleepTime = 0;
- m_invMass = 0;
- m_I = 0;
- m_invI = 0;
- m_mass = bd.massData.mass;
- if(m_mass > 0)
- {
- m_invMass = 1 / m_mass;
- }
- if((m_flags & b2Body.e_fixedRotationFlag) == 0)
- {
- m_I = bd.massData.I;
- }
- if(m_I > 0)
- {
- m_invI = 1 / m_I;
- }
- if(m_invMass == 0 && m_invI == 0)
- {
- m_type = e_staticType;
- }
- else
- {
- m_type = e_dynamicType;
- }
- m_userData = bd.userData;
- m_shapeList = null;
- m_shapeCount = 0;
- }
-
- public function GetLinearVelocityFromWorldPoint(worldPoint:b2Vec2) : b2Vec2
- {
- return new b2Vec2(m_linearVelocity.x + m_angularVelocity * (worldPoint.y - m_sweep.c.y),m_linearVelocity.x - m_angularVelocity * (worldPoint.x - m_sweep.c.x));
- }
-
- public function SetLinearVelocity(v:b2Vec2) : void
- {
- m_linearVelocity.SetV(v);
- }
-
- public function WakeUp() : void
- {
- m_flags &= ~e_sleepFlag;
- m_sleepTime = 0;
- }
-
- public function GetLocalCenter() : b2Vec2
- {
- return m_sweep.localCenter;
- }
-
- public function ApplyTorque(torque:Number) : void
- {
- if(IsSleeping())
- {
- WakeUp();
- }
- m_torque += torque;
- }
-
- public function IsFrozen() : Boolean
- {
- return (m_flags & e_frozenFlag) == e_frozenFlag;
- }
-
- public function IsDynamic() : Boolean
- {
- return m_type == e_dynamicType;
- }
-
- public function GetLinearVelocity() : b2Vec2
- {
- return m_linearVelocity;
- }
-
- public function SynchronizeTransform() : void
- {
- m_xf.R.Set(m_sweep.a);
- var tMat:b2Mat22 = m_xf.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- m_xf.position.x = m_sweep.c.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- m_xf.position.y = m_sweep.c.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- }
-
- public function GetInertia() : Number
- {
- return m_I;
- }
-
- public function IsSleeping() : Boolean
- {
- return (m_flags & e_sleepFlag) == e_sleepFlag;
- }
-
- public function SetMassFromShapes() : void
- {
- var s:b2Shape = null;
- if(m_world.m_lock == true)
- {
- return;
- }
- m_mass = 0;
- m_invMass = 0;
- m_I = 0;
- m_invI = 0;
- var centerX:Number = 0;
- var centerY:Number = 0;
- var massData:b2MassData = s_massData;
- s = m_shapeList;
- while(s)
- {
- s.ComputeMass(massData);
- m_mass += massData.mass;
- centerX += massData.mass * massData.center.x;
- centerY += massData.mass * massData.center.y;
- m_I += massData.I;
- s = s.m_next;
- }
- if(m_mass > 0)
- {
- m_invMass = 1 / m_mass;
- centerX *= m_invMass;
- centerY *= m_invMass;
- }
- if(m_I > 0 && (m_flags & e_fixedRotationFlag) == 0)
- {
- m_I -= m_mass * (centerX * centerX + centerY * centerY);
- m_invI = 1 / m_I;
- }
- else
- {
- m_I = 0;
- m_invI = 0;
- }
- m_sweep.localCenter.Set(centerX,centerY);
- var tMat:b2Mat22 = m_xf.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- m_sweep.c.x += m_xf.position.x;
- m_sweep.c.y += m_xf.position.y;
- m_sweep.c0.SetV(m_sweep.c);
- s = m_shapeList;
- while(s)
- {
- s.UpdateSweepRadius(m_sweep.localCenter);
- s = s.m_next;
- }
- var oldType:int = m_type;
- if(m_invMass == 0 && m_invI == 0)
- {
- m_type = e_staticType;
- }
- else
- {
- m_type = e_dynamicType;
- }
- if(oldType != m_type)
- {
- s = m_shapeList;
- while(s)
- {
- s.RefilterProxy(m_world.m_broadPhase,m_xf);
- s = s.m_next;
- }
- }
- }
-
- public function PutToSleep() : void
- {
- m_flags |= e_sleepFlag;
- m_sleepTime = 0;
- m_linearVelocity.SetZero();
- m_angularVelocity = 0;
- m_force.SetZero();
- m_torque = 0;
- }
-
- public function GetJointList() : b2JointEdge
- {
- return m_jointList;
- }
-
- public function SetXForm(position:b2Vec2, angle:Number) : Boolean
- {
- var s:b2Shape = null;
- var inRange:Boolean = false;
- if(m_world.m_lock == true)
- {
- return true;
- }
- if(IsFrozen())
- {
- return false;
- }
- m_xf.R.Set(angle);
- m_xf.position.SetV(position);
- var tMat:b2Mat22 = m_xf.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- m_sweep.c.x += m_xf.position.x;
- m_sweep.c.y += m_xf.position.y;
- m_sweep.c0.SetV(m_sweep.c);
- m_sweep.a0 = m_sweep.a = angle;
- var freeze:Boolean = false;
- s = m_shapeList;
- while(s)
- {
- inRange = s.Synchronize(m_world.m_broadPhase,m_xf,m_xf);
- if(inRange == false)
- {
- freeze = true;
- break;
- }
- s = s.m_next;
- }
- if(freeze == true)
- {
- m_flags |= e_frozenFlag;
- m_linearVelocity.SetZero();
- m_angularVelocity = 0;
- s = m_shapeList;
- while(s)
- {
- s.DestroyProxy(m_world.m_broadPhase);
- s = s.m_next;
- }
- return false;
- }
- m_world.m_broadPhase.Commit();
- return true;
- }
-
- public function GetLocalPoint(worldPoint:b2Vec2) : b2Vec2
- {
- return b2Math.b2MulXT(m_xf,worldPoint);
- }
-
- public function ApplyForce(force:b2Vec2, point:b2Vec2) : void
- {
- if(IsSleeping())
- {
- WakeUp();
- }
- m_force.x += force.x;
- m_force.y += force.y;
- m_torque += (point.x - m_sweep.c.x) * force.y - (point.y - m_sweep.c.y) * force.x;
- }
-
- public function SynchronizeShapes() : Boolean
- {
- var s:b2Shape = null;
- var xf1:b2XForm = s_xf1;
- xf1.R.Set(m_sweep.a0);
- var tMat:b2Mat22 = xf1.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- xf1.position.x = m_sweep.c0.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
- xf1.position.y = m_sweep.c0.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
- var inRange:Boolean = true;
- s = m_shapeList;
- while(s)
- {
- inRange = s.Synchronize(m_world.m_broadPhase,xf1,m_xf);
- if(inRange == false)
- {
- break;
- }
- s = s.m_next;
- }
- if(inRange == false)
- {
- m_flags |= e_frozenFlag;
- m_linearVelocity.SetZero();
- m_angularVelocity = 0;
- s = m_shapeList;
- while(s)
- {
- s.DestroyProxy(m_world.m_broadPhase);
- s = s.m_next;
- }
- return false;
- }
- return true;
- }
-
- public function GetAngle() : Number
- {
- return m_sweep.a;
- }
-
- public function GetXForm() : b2XForm
- {
- return m_xf;
- }
-
- public function GetLinearVelocityFromLocalPoint(localPoint:b2Vec2) : b2Vec2
- {
- var A:b2Mat22 = m_xf.R;
- var worldPoint:b2Vec2 = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y,A.col1.y * localPoint.x + A.col2.y * localPoint.y);
- worldPoint.x += m_xf.position.x;
- worldPoint.y += m_xf.position.y;
- return new b2Vec2(m_linearVelocity.x + m_angularVelocity * (worldPoint.y - m_sweep.c.y),m_linearVelocity.x - m_angularVelocity * (worldPoint.x - m_sweep.c.x));
- }
-
- public function GetNext() : b2Body
- {
- return m_next;
- }
-
- public function GetMass() : Number
- {
- return m_mass;
- }
-
- public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2) : void
- {
- if(IsSleeping())
- {
- WakeUp();
- }
- m_linearVelocity.x += m_invMass * impulse.x;
- m_linearVelocity.y += m_invMass * impulse.y;
- m_angularVelocity += m_invI * ((point.x - m_sweep.c.x) * impulse.y - (point.y - m_sweep.c.y) * impulse.x);
- }
-
- public function GetAngularVelocity() : Number
- {
- return m_angularVelocity;
- }
-
- public function SetAngularVelocity(omega:Number) : void
- {
- m_angularVelocity = omega;
- }
-
- public function SetMass(massData:b2MassData) : void
- {
- var s:b2Shape = null;
- if(m_world.m_lock == true)
- {
- return;
- }
- m_invMass = 0;
- m_I = 0;
- m_invI = 0;
- m_mass = massData.mass;
- if(m_mass > 0)
- {
- m_invMass = 1 / m_mass;
- }
- if((m_flags & b2Body.e_fixedRotationFlag) == 0)
- {
- m_I = massData.I;
- }
- if(m_I > 0)
- {
- m_invI = 1 / m_I;
- }
- m_sweep.localCenter.SetV(massData.center);
- var tMat:b2Mat22 = m_xf.R;
- var tVec:b2Vec2 = m_sweep.localCenter;
- m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
- m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
- m_sweep.c.x += m_xf.position.x;
- m_sweep.c.y += m_xf.position.y;
- m_sweep.c0.SetV(m_sweep.c);
- s = m_shapeList;
- while(s)
- {
- s.UpdateSweepRadius(m_sweep.localCenter);
- s = s.m_next;
- }
- var oldType:int = m_type;
- if(m_invMass == 0 && m_invI == 0)
- {
- m_type = e_staticType;
- }
- else
- {
- m_type = e_dynamicType;
- }
- if(oldType != m_type)
- {
- s = m_shapeList;
- while(s)
- {
- s.RefilterProxy(m_world.m_broadPhase,m_xf);
- s = s.m_next;
- }
- }
- }
-
- public function IsStatic() : Boolean
- {
- return m_type == e_staticType;
- }
-
- public function GetWorldVector(localVector:b2Vec2) : b2Vec2
- {
- return b2Math.b2MulMV(m_xf.R,localVector);
- }
-
- public function GetShapeList() : b2Shape
- {
- return m_shapeList;
- }
-
- public function Advance(t:Number) : void
- {
- m_sweep.Advance(t);
- m_sweep.c.SetV(m_sweep.c0);
- m_sweep.a = m_sweep.a0;
- SynchronizeTransform();
- }
-
- public function SetBullet(flag:Boolean) : void
- {
- if(flag)
- {
- m_flags |= e_bulletFlag;
- }
- else
- {
- m_flags &= ~e_bulletFlag;
- }
- }
-
- public function CreateShape(def:b2ShapeDef) : b2Shape
- {
- var s:b2Shape = null;
- if(m_world.m_lock == true)
- {
- return null;
- }
- s = b2Shape.Create(def,m_world.m_blockAllocator);
- s.m_next = m_shapeList;
- m_shapeList = s;
- ++m_shapeCount;
- s.m_body = this;
- s.CreateProxy(m_world.m_broadPhase,m_xf);
- s.UpdateSweepRadius(m_sweep.localCenter);
- return s;
- }
-
- public function IsConnected(other:b2Body) : Boolean
- {
- var jn:b2JointEdge = m_jointList;
- while(jn)
- {
- if(jn.other == other)
- {
- return jn.joint.m_collideConnected == false;
- }
- jn = jn.next;
- }
- return false;
- }
-
- public function DestroyShape(s:b2Shape) : void
- {
- if(m_world.m_lock == true)
- {
- return;
- }
- s.DestroyProxy(m_world.m_broadPhase);
- var node:b2Shape = m_shapeList;
- var ppS:b2Shape = null;
- var found:Boolean = false;
- while(node != null)
- {
- if(node == s)
- {
- if(ppS)
- {
- ppS.m_next = s.m_next;
- }
- else
- {
- m_shapeList = s.m_next;
- }
- found = true;
- break;
- }
- ppS = node;
- node = node.m_next;
- }
- s.m_body = null;
- s.m_next = null;
- --m_shapeCount;
- b2Shape.Destroy(s,m_world.m_blockAllocator);
- }
-
- public function GetUserData() : *
- {
- return m_userData;
- }
-
- public function IsBullet() : Boolean
- {
- return (m_flags & e_bulletFlag) == e_bulletFlag;
- }
-
- public function GetWorldCenter() : b2Vec2
- {
- return m_sweep.c;
- }
-
- public function AllowSleeping(flag:Boolean) : void
- {
- if(flag)
- {
- m_flags |= e_allowSleepFlag;
- }
- else
- {
- m_flags &= ~e_allowSleepFlag;
- WakeUp();
- }
- }
-
- public function SetUserData(data:*) : void
- {
- m_userData = data;
- }
-
- public function GetLocalVector(worldVector:b2Vec2) : b2Vec2
- {
- return b2Math.b2MulTMV(m_xf.R,worldVector);
- }
-
- public function GetWorldPoint(localPoint:b2Vec2) : b2Vec2
- {
- var A:b2Mat22 = m_xf.R;
- var u:b2Vec2 = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y,A.col1.y * localPoint.x + A.col2.y * localPoint.y);
- u.x += m_xf.position.x;
- u.y += m_xf.position.y;
- return u;
- }
-
- public function GetWorld() : b2World
- {
- return m_world;
- }
-
- public function GetPosition() : b2Vec2
- {
- return m_xf.position;
- }
- }
- }
-