这里我在box2d做一个小游戏。因为我想要一个像Lift一样自动上下移动的身体。 我尝试使用此代码的代码可以在我触摸时移动。但我希望移动y位置0到480并再次将480反转为0.
enter code here
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
//[self addNewSpriteWithCoords: location];
b2Vec2 force = b2Vec2(0, 20);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
}
答案 0 :(得分:0)
请参阅此处的关节部分:
http://www.box2d.org/manual.html
棱柱关节可能对此有好处。如果它应该是电梯/电梯,你可能还想考虑一个运动机构,但实际上这取决于你希望实现的目标。绝对检查一下body部分中运动学和动态(和静态)主体之间的区别。