public CCPhysicsSprite addGrossiniAtPosition(CCPoint location) { int posx, posy; posx = CCRandom.Next() * 200; posy = CCRandom.Next() * 200; posx = (Math.Abs(posx) % 4) * 85; posy = (Math.Abs(posy) % 3) * 121; CCPhysicsSprite sp = new CCPhysicsSprite(spriteTexture.Texture, new CCRect(posx, posy, 85, 121)); cpBB verts = new cpBB(-24, -54, 24, 54); var body = new cpBody(1, cp.MomentForBox2(1, new cpBB(-24, -54, 24, 54))); //); body.SetPosition(new cpVect(posx, posy)); space.AddBody(body); var shape = cpPolyShape.BoxShape2(body, verts, 0); shape.e = .5f; shape.u = .5f; space.AddShape(shape); sp.Body = body; AddChild(sp); sp.Position = location; return(sp); }
private void AddBall() { //Ball float ballDiameter = _gameWidth * BALL_RADIUS_PERCENT; float ballCenterX = _gameWidth * BALL_X_PERCENT; float ballCenterY = ballDiameter / 2 + BALL_Y_MARGIN; var def = new b2BodyDef(); def.position = new b2Vec2(ballCenterX / App.PTM_RATIO, ballCenterY / App.PTM_RATIO); def.type = b2BodyType.b2_dynamicBody; b2Body physBody = _world.CreateBody(def); //Circle Physics Shape var shape = new b2CircleShape(); shape.Radius = ballDiameter / 2 / App.PTM_RATIO; var fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1f; fd.restitution = BALL_RECOIL; fd.friction = BALL_FRICTION; physBody.CreateFixture(fd); _contactListener.Ball = physBody; CCTexture2D ballTex = null; if (string.IsNullOrWhiteSpace(_ballFilename)) { ballTex = new CCTexture2D("soccer"); } else { FileStream fs = new FileStream(_ballFilename, FileMode.Open); ballTex = new CCTexture2D(fs); } _ballN = new CCPhysicsSprite(ballTex, physBody) { PositionX = ballCenterX, PositionY = ballCenterY, ContentSize = new CCSize(ballDiameter, ballDiameter) }; AddChild(_ballN); }
void AddNewSpriteAt(PointF pos) { int posx, posy; var parent = GetChild(parentnode) as CCSpriteBatchNode; posx = (int)(rnd.NextDouble() * 200.0f); posy = (int)(rnd.NextDouble() * 200.0f); posx = (posx % 4) * 85; posy = (posy % 3) * 121; var sprite = new CCPhysicsSprite(parent.Texture, new RectangleF(posx, posy, 85, 121)); //this used to work, but crashes now, as it sets the Body's position and the Body isn't set yet :/ //sprite.Position = pos; parent.Add(sprite); PointF[] verts = { new PointF(-24, -54), new PointF(-24, 54), new PointF(24, 54), new PointF(24, -54), }; var body = new Body(1, Helper.MomentForPolygon(1f, verts, PointF.Empty)) { Position = pos }; space.Add(body); var shape = new PolygonShape(body, verts, PointF.Empty) { Elasticity = .5f, Friction = .5f }; space.Add(shape); sprite.Body = body; }
private void AddShape(b2World world, CCPoint position) { b2Vec2 positionVec = new b2Vec2(position.X, position.Y); var box = new CCPhysicsSprite("hd/images/cloud", IntroLayer.PTM_RATIO); box.Position = position; var def = new b2BodyDef(); def.position = new b2Vec2(positionVec.x / IntroLayer.PTM_RATIO, positionVec.y / IntroLayer.PTM_RATIO); def.linearVelocity = new b2Vec2(0.0f, -1.0f); def.type = b2BodyType.b2_dynamicBody; b2Body body = world.CreateBody(def); // Polygon Shape //var shape = new b2PolygonShape(); //shape.SetAsBox(50f / IntroLayer.PTM_RATIO, 50f / IntroLayer.PTM_RATIO); // Circle Shape var shape = new b2CircleShape(); shape.Radius = 50f / IntroLayer.PTM_RATIO; var fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1f; fd.restitution = 0f; fd.friction = 0.2f; body.CreateFixture(fd); box.PhysicsBody = body; sprites.Add(box); AddChild(box); }
public MachineLayer() { space = new Space() { Gravity = new PointF(0, -10), }; Add(new CCSprite("bg.jpg") { Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(512, 384) : new PointF(240, 160), }); //motorblock, no body, no shape var motorblock = new CCSprite("motor_block.png") { Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(160, 460) : new PointF(80, 230), }; Add(motorblock); //motorwheel var motorwheel = new CCPhysicsSprite("motor_wheel.png") { Body = new Body(1, Helper.MomentForCircle(1, 20, 20, PointF.Empty)), Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(160, 460) : new PointF(80, 230), }; Add(motorwheel); space.Add(motorwheel.Body); space.Add(new CircleShape(motorwheel.Body, UIDevice.CurrentDevice.IsIPad() ? 20 : 10, PointF.Empty) { Group = 1 }); space.Add((Constraint) new PivotJoint(space.StaticBody, motorwheel.Body, UIDevice.CurrentDevice.IsIPad() ? new PointF(160, 460) : new PointF(80, 230))); space.Add((Constraint) new SimpleMotor(space.StaticBody, motorwheel.Body, 10f)); //wheel var wheel = new CCPhysicsSprite("wheel.png") { Body = new Body(25, Helper.MomentForCircle(25, 140, 140, PointF.Empty)), Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(160, 300) : new PointF(80, 150), }; space.Add(wheel.Body); space.Add(new CircleShape(wheel.Body, UIDevice.CurrentDevice.IsIPad() ? 140 : 70, PointF.Empty) { Group = 1 }); Add(wheel); space.Add((Constraint) new PivotJoint(space.StaticBody, wheel.Body, UIDevice.CurrentDevice.IsIPad() ? new PointF(160, 300) : new PointF(80, 150))); space.Add((Constraint) new GearJoint(motorwheel.Body, wheel.Body, 0f, -7f)); //cylinder. no physics body. only a shape Add(new CCSprite("cylinder.png") { Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(570, 300) : new PointF(285, 150), }); //space.Add (new PolygonShape (space.StaticBody, new [] { // new PointF (350, 160), // new PointF (350, 200), // new PointF (750, 200), // new PointF (750, 160), //}, PointF.Empty)); // //space.Add (new PolygonShape (space.StaticBody, new [] { // new PointF (350, 400), // new PointF (350, 440), // new PointF (750, 440), // new PointF (750, 400), //}, PointF.Empty)); //piston var piston = new CCPhysicsSprite("piston.png") { Body = new Body(8, float.PositiveInfinity), //never rotates Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(370, 300) : new PointF(185, 150), }; space.Add(piston.Body); space.Add(new PolygonShape(piston.Body, UIDevice.CurrentDevice.IsIPad() ? 100 : 50, UIDevice.CurrentDevice.IsIPad() ? 200 : 50) { Group = 1 }); Add(piston); //conrod var conrod = new CCPhysicsSprite("conrod.png") { Body = new Body(4, Helper.MomentForPolygon(4, new[] { new PointF(-160, -20), new PointF(-160, 20), new PointF(160, 20), new PointF(160, -20), }, PointF.Empty)), Position = UIDevice.CurrentDevice.IsIPad() ? new PointF(190, 300) : new PointF(95, 150) }; space.Add(conrod.Body); space.Add(new PolygonShape(conrod.Body, UIDevice.CurrentDevice.IsIPad() ? 320 : 160, UIDevice.CurrentDevice.IsIPad() ? 40 : 20) { Group = 1 }); Add(conrod); space.Add((Constraint) new PivotJoint(wheel.Body, conrod.Body, UIDevice.CurrentDevice.IsIPad() ? new PointF(40, 300) : new PointF(20, 150))); space.Add((Constraint) new PivotJoint(conrod.Body, piston.Body, UIDevice.CurrentDevice.IsIPad() ? new PointF(340, 300) : new PointF(170, 150))); space.Add((Constraint) new GrooveJoint(space.StaticBody, piston.Body, UIDevice.CurrentDevice.IsIPad() ? new PointF(0, 300) : new PointF(0, 150), UIDevice.CurrentDevice.IsIPad() ? new PointF(1024, 300) : new PointF(480, 150), UIDevice.CurrentDevice.IsIPad() ? new PointF(0, 0): new PointF(0, 0))); }