赞
踩
静态刚体不会移动,也不应该移动- 因为物理引擎可以依赖静态刚体不会互相碰撞这个特性来做出一些优化。 例如:游戏中的箱子
静态刚体的密度可以被设为0.
动态刚体,它们会相互碰撞,而且也会和静态刚体碰撞。动态刚体除了拥有位置
- // 生成旋转关节
- b2RevoluteJointDef jointDef;
- jointDef.Initialize(bodyA, bodyB, bodyB->GetWorldCenter());
- bodyA->GetWorld()->CreateJoint(&jointDef);
- b2PrismaticJointDef jointDef;
- b2Vec2 worldAxis(0.0f, 1.0f);
- jointDef.Initialize(bodyA, bodyB, body->GetWorldCenter(), w orldAxis);
- jointDef.lowerTranslation = 0.0f;
- jointDef.upperTranslation = 0.75f;
- jointDef.enableLimit = true;
- jointDef.maxMoto rForce = 60.0f;
- jointDef.motorSpeed = 20.0f;
- jointDef.enableMotor = false;
- joint = (b2PrismaticJoint*)body- >GetWorld()->CreateJoint(&jointDef);
- fixtureDef.friction = 0.99f;
- fixtureDef.restitution = 0.01f;
<pre name="code" class="cpp">
- b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
- bool allowBodiesToSleep = true;
- world = new b2World(gravity, allowBodiesToSleep);
- b2BodyDef containerBodyDef;
- b2Body* containerBody = world->CreateBody(&containerBodyDef);
- CGSize screenSize = [CCDirector sharedDirector].winSize;
- float widthInMeters = screenSize.width / PTM_RATIO;
- float heightInMeters = screenSize.height / PTM_RATIO;
- //四个角的坐标, 以米为单位:
- b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
- b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
- b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
- b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters) ;
- -(b2Vec2) toMeters:(CGPoint)point
- {
- return b2Vec2(point.x / PTM_R ATIO, point.y / PTM_RATIO);
- }
- -(CGPoint) toPixels:(b2Vec2)vec
- {
- return ccpMult(CGPointMake (vec.x, vec.y), PTM_RATIO);
- }
- -(void) addNewSpriteAt:(CGPoint)pos
- {
- CCSpriteBatchNode* batch = (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode];
- int idx = CCRANDOM_0_1() * TILESET_COLUMNS;
- int idy = CCRANDOM_0_1 () * TILESET_ROWS;
- CGRect tileRect = CGRectMake(TILESIZE * idx, TILESIZE * idy, TILESIZE, TILESIZE);
- CCSprite* sprite = [CCSprite sp riteWithBatchNod e:batch rect:tileRect];
- sprite.position = pos;
- [batch addChild:sprite];
-
- //创建一个刚体的定义,并将其设置为动态刚体
- b2BodyDef bodyDef;
- bodyDef.type = b2_dynamicBody;
- bodyDef.position = [self toMeters:pos];
- bodyDef.userData = sprite;
- b2Body* body = world->CreateBody(&bodyDef);
-
- // 定义一个盒子形状,并将其复制给body fixture
- b2PolygonShape dynamicBox;
- float tileInMeters = TILESIZE / PTM_RATIO;
- dynamicBox.SetAsBox(tileInMeters * 0.5f, tileInMe ters * 0.5f);
-
- b2FixtureDef fixtureDef;
- fixtureDef.shape = &dynamicBox;
- fixtureDef.density = 0.3f;
- fixtureDef.friction = 0.5f;
- fixtureDef.restitution = 0.6f;
- body->CreateFixture(&fixtureDef);
- }
- -(void) update:(ccTime)delta
- {
- //使用固定的时间间隔将物理模拟向前推进一步
- float timeStep = 0.03f;
- int32 velocityIterations = 8;
- int32 positionIterations = 1;
- world->Step(timeStep, velocityIterati ons, positionIterations);
-
- for (b2Body* body = world->GetBodyList(); body != nil; body = body->GetNext())
- {
- CCSprite* sprite = (CCSprite*)body->GetUserData();
- if (sprite!= NULL)
- {
- sprite.position = [self toPixels:body->GetPosition()];
- float angle = body ->GetAngle();
- sprite.rotation = CC_RADIAN S_TO_DEGREES(angle) * -1;
- }
- }
- }
http://www.box2d.org/manual.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。