コード例 #1
0
void PhysicsActor::InitPhysics(float density, float friction, float restitution, eShapeType shapeType, bool isSensor, int groupIndex, int collisionFlags, bool fixedRotation)
{
	if (!theWorld.IsPhysicsSetUp())
	{
		std::cout << "ERROR: World physics must be initialized before Actor's." << std::endl;
		return;
	}

	b2CircleDef circle;
	b2PolygonDef box;
	b2ShapeDef* shape;
	if (shapeType == SHAPETYPE_BOX)
	{
		// The extents is just a vector of the box's half widths. 
		// Box2D is tuned for meters, kilograms, and seconds. (Unless you've changed its units.)
		box.SetAsBox(0.5f*_size.X, 0.5f*_size.Y);
		shape = &box;
	}
	else if (shapeType == SHAPETYPE_CIRCLE)
	{
		// TODO: handle ellipse?
		circle.radius = 0.5f*_size.X;
		shape = &circle;
	}
	else
	{
		std::cout << "ERROR: Invalid shape type given." << std::endl;
		return;
	}

	shape->density = density;
	shape->friction = friction;
	shape->restitution = restitution;
	shape->groupIndex = groupIndex;
	shape->isSensor = isSensor;
	if( collisionFlags != -1 )
	{
		shape->maskBits = (short)collisionFlags;
		shape->categoryBits = (short)collisionFlags;
	}

	InitShape( shape );

	b2BodyDef bd;
	bd.userData = this;
	bd.position.Set(_position.X, _position.Y);
	bd.angle = MathUtil::ToRadians(_rotation);
	bd.fixedRotation = fixedRotation;
	if (MathUtil::FuzzyEquals(density, 0.0))
	{
		_physBody = theWorld.GetPhysicsWorld().CreateStaticBody(&bd);
	}
	else
	{
		_physBody = theWorld.GetPhysicsWorld().CreateDynamicBody(&bd);
	}
	_physBody->CreateShape(shape);
	_physBody->SetMassFromShapes();
	CustomInitPhysics();
}
コード例 #2
0
ファイル: PhysicsActor.cpp プロジェクト: CmPons/angel2d
void PhysicsActor::InitPhysics()
{
	if (!theWorld.IsPhysicsSetUp())
	{
		sysLog.Log("ERROR: World physics must be initialized before Actor's.");
		return;
	}
	
	b2CircleShape circle;
	b2PolygonShape box;
	b2Shape* shape = NULL;
	if (_shapeType == SHAPETYPE_BOX)
	{
		// The extents is just a vector of the box's half widths. 
		// Box2D is tuned for meters, kilograms, and seconds. (Unless you've changed its units. [You probably shouldn't.])
		box.SetAsBox(0.5f*_size.X, 0.5f*_size.Y);
		shape = &box;
	}
	else if (_shapeType == SHAPETYPE_CIRCLE)
	{
		circle.m_radius = 0.5f*_size.X;
		shape = &circle;
	}
	else
	{
		sysLog.Log("ERROR: Invalid shape type given.");
		return;
	}
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = shape;
	fixtureDef.density = _density;
	fixtureDef.friction = _friction;
	fixtureDef.restitution = _restitution;
	
	fixtureDef.filter.groupIndex = _groupIndex;
	fixtureDef.isSensor = _isSensor;
	
	InitShape( shape );
	
	b2BodyDef bd;
	bd.userData = this;
	bd.position.Set(_position.X, _position.Y);
	bd.angle = MathUtil::ToRadians(_rotation);
	bd.fixedRotation = _fixedRotation;
	if (MathUtil::FuzzyEquals(_density, 0.0f))
	{
		bd.type = b2_staticBody;
	}
	else 
	{
		bd.type = b2_dynamicBody;
	}
	
	_physBody = theWorld.GetPhysicsWorld().CreateBody(&bd);
	_physBody->CreateFixture(&fixtureDef);
	_physBody->SetUserData(this);
	CustomInitPhysics();
}