简而言之,我的问题是如何连接复杂的2个多边形形状并对其进行纹理化。
这是关于我的问题的信息:
我有b2WeldJointDef的问题,附加2 b2body的但是当我开始调试e_jointBit打开。
这是我的代码,我附上了图片
![b2BodyDef bodyDef;
bodyDef.position.Set((screenSize.width/2.0)/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
//CCLOGWARN("position:pX:%f pY:%f",pX,pY);
bodyDef.type=b2_dynamicBody;
bodyDef.userData = sprite;
bodyDef.linearDamping = 1.0;
bodyDef.angularDamping = 1.0;
b2FixtureDef* fixtureDef=new b2FixtureDef();
fixtureDef->density=3.0;
fixtureDef->restitution=0.0;
fixtureDef->friction=1.0;
fixtureDef->filter.groupIndex=-1;
b2Body* car_body = world->CreateBody(&bodyDef);
// here i build the custom polygons to form complex body
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2\[10\];
// setting the vertices all working great :
...
...
...
polygonShape->Set(vertices,10);
fixtureDef->shape = polygonShape;
car_body->CreateFixture(fd);
// -------- trying to attach the axle to the main car buy
b2BodyDef axlecontainerBodyDef;
axlecontainerBodyDef.userData = axlecontainerSprite;
axlecontainerBodyDef.type=b2_dynamicBody;
axlecontainerBodyDef.position.Set(((screenSize.width/2.0))/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
b2Body* axlecontainerBody = world->CreateBody(&axlecontainerBodyDef);
b2FixtureDef * axleContainerFixture = new b2FixtureDef();
axleContainerFixture->density=3.0;
axleContainerFixture->friction=0.0;
axleContainerFixture->restitution=1.0;
axleContainerFixture->filter.groupIndex=-1;
//LEFT AXLE CONTAINER
// here i build the custom polygons to form complex new body to be attached to the main car body
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2\[10\];
// setting the vertices all working great :
...
...
...
polygonShape->Set(vertices,10);
axleContainerFixture->shape = polygonShape;
axlecontainerBody->CreateFixture(fd);
b2WeldJointDef *weldJointDef = new b2WeldJointDef();
weldJointDef->bodyA=axlecontainerBody;
weldJointDef->bodyB=body;
float x = body->GetWorldCenter().x/PTM_RATIO;
float y = body->GetWorldCenter().y/PTM_RATIO;
weldJointDef->localAnchorA.Set(-0.5, -0.3);
weldJointDef->localAnchorB.Set(-0.5, -0.3);
weldJointDef->referenceAngle = 0* M_PI /3;
//weldJointDef->collideConnected = false;
world->CreateJoint(weldJointDef);][2]
现在你可以在附图中看到焊点不在正确的位置, 我究竟做错了什么 ?或者你对这个使用焊接接头有什么看法? 这是正确的方法吗?
更新
在一遍又一遍地阅读和更改代码后,我仍然可以使事情发挥作用
我需要jonits仍然错过了:
这是我的固定代码,因为你可以看到它从车身上取下车身的中心:
b2BodyDef bodyDef;
bodyDef.position.Set((screenSize.width/2.0)/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
bodyDef.type=b2_dynamicBody;
bodyDef.userData = sprite;
bodyDef.linearDamping = 1.0;
bodyDef.angularDamping = 1.0;
b2FixtureDef* fixtureDef=new b2FixtureDef();
fixtureDef->density=3.0;
fixtureDef->restitution=0.0;
fixtureDef->friction=1.0;
fixtureDef->filter.groupIndex=-1;
b2Body* car_body = world->CreateBody(&bodyDef);
// here i build the custom polygons to form complex body
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2 [10 ];
// setting the vertices all working great :
...
...
...
polygonShape->Set(vertices,10);
fixtureDef->shape = polygonShape;
car_body->CreateFixture(fd);
// -------- trying to attach the axle to the main car buy
b2BodyDef axlecontainerBodyDef;
axlecontainerBodyDef.userData = axlecontainerSprite;
axlecontainerBodyDef.type=b2_dynamicBody;
axlecontainerBodyDef.position.Set(((screenSize.width/2.0))/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
b2Body* axlecontainerBody = world->CreateBody(&axlecontainerBodyDef);
b2FixtureDef * axleContainerFixture = new b2FixtureDef();
axleContainerFixture->density=1.0;
axleContainerFixture->friction=1.0;
//axleContainerFixture->restitution=1.0;
axleContainerFixture->isSensor = false;
//LEFT AXLE CONTAINER
// here i build the custom polygons to form complex new body to be attached to the main car body
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2\[10\];
// setting the vertices all working great :
...
...
...
polygonShape->Set(vertices,10);
axleContainerFixture->shape = polygonShape;
axlecontainerBody->CreateFixture(fd);
b2WeldJointDef *weldJointDef = new b2WeldJointDef();
weldJointDef->bodyA=body;
weldJointDef->bodyB=axlecontainerBody;
float x = axlecontainerBody->GetWorldCenter().x/PTM_RATIO;
float y = axlecontainerBody->GetWorldCenter().y/PTM_RATIO;
weldJointDef->Initialize(body,axlecontainerBody,body->GetWorldCenter());
world->CreateJoint(weldJointDef);
答案 0 :(得分:1)
您的代码的这一部分看起来有点可疑:
weldJointDef->localAnchorA.Set(-0.5, -0.3);
weldJointDef->localAnchorB.Set(-0.5, -0.3);
相对于两者焊接锚点的主体的局部空间位置似乎很奇怪(-0.5,-0.3)。这是对的吗?
上次我必须创建焊接接头时,我使用了关节的Initialize(...)函数这样做了。:
void CreateIdleJoint()
{
// Create a weld joint between the
// body and the thing it is moving on.
b2WeldJointDef jointDef;
jointDef.Initialize(_bodyMoving, _bodyMovingOn, _bodyMovingOn->GetWorldCenter());
jointDef.dampingRatio = 0.5;
jointDef.frequencyHz = 2.0;
jointDef.collideConnected = true;
_bodyMoving->GetWorld()->CreateJoint(&jointDef);
}
作为参考,这是Initialize函数的内部结构:
void b2WeldJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
{
bodyA = bA;
bodyB = bB;
localAnchorA = bodyA->GetLocalPoint(anchor);
localAnchorB = bodyB->GetLocalPoint(anchor);
referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
}