public void addNewSpriteAtPosition(CCPoint p) { //CCLog.Log("Add sprite #{2} : {0} x {1}", p.X, p.Y, _batch.ChildrenCount + 1); //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just randomly picking one of the images int idx = (CCRandom.Float_0_1() > .5 ? 0 : 1); int idy = (CCRandom.Float_0_1() > .5 ? 0 : 1); var sprite = new CCPhysicsSprite(spriteTexture, new CCRect(32 * idx, 32 * idy, 32, 32)); AddChild(sprite, 0, kTagForPhysicsSprite); sprite.Position = new CCPoint(p.X, p.Y); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef def = new b2BodyDef(); def.position = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO); def.type = b2BodyType.b2_dynamicBody; b2Body body = _world.CreateBody(def); // Define another box shape for our dynamic body. var dynamicBox = new b2PolygonShape(); dynamicBox.SetAsBox(.5f, .5f); //These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fd = new b2FixtureDef(); fd.shape = dynamicBox; fd.density = 1f; fd.friction = 0.3f; b2Fixture fixture = body.CreateFixture(fd); sprite.PhysicsBody = body; //_world.SetContactListener(new Myb2Listener()); // _world.Dump(); }
public void addNewSpriteAtPosition(CCPoint p) { CCLog.Log("Add sprite #{2} : {0} x {1}", p.X, p.Y, _batch.ChildrenCount+1); //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just randomly picking one of the images int idx = (Random.Float_0_1() > .5 ? 0 : 1); int idy = (Random.Float_0_1() > .5 ? 0 : 1); var sprite = new CCPhysicsSprite(); sprite.InitWithTexture(m_pSpriteTexture, new CCRect(32 * idx, 32 * idy, 32, 32)); _batch.AddChild(sprite, 0, kTagForPhysicsSprite); sprite.Position = new CCPoint(p.X, p.Y); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef def = b2BodyDef.Create(); def.position = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO); def.type = b2BodyType.b2_dynamicBody; b2Body body = _world.CreateBody(def); //body.SetActive(true); // Define another box shape for our dynamic body. var dynamicBox = new b2PolygonShape(); dynamicBox.Radius = 32f / PTM_RATIO; dynamicBox.SetAsBox(dynamicBox.Radius / 2, dynamicBox.Radius/2); //These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fd = b2FixtureDef.Create(); fd.shape = dynamicBox; fd.friction = CCMacros.CCRandomBetween0And1(); fd.density = 10f * CCMacros.CCRandomBetween0And1(); b2Fixture fixture = body.CreateFixture(fd); sprite.PhysicsBody = body; // _world.Dump(); }