public override bool init() { if (!base.init()) return false; if (!base.init()) { return false; } CCSize winSize = CCDirector.sharedDirector().getWinSize(); title = CCLabelTTF.labelWithString("FootBall", "Arial", 24); title.position = new CCPoint(winSize.width / 2, winSize.height - 50); this.addChild(title, 1); ball = CCSprite.spriteWithFile(@"images/ball"); ball.position = new CCPoint(100, 300); this.addChild(ball); Vector2 gravity = new Vector2(0.0f, -30.0f); bool doSleep = true; world = new World(gravity, doSleep); ///////////////////////// BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position = new Vector2(0, 0); Body groundBody = world.CreateBody(groundBodyDef); PolygonShape groundBox = new PolygonShape(); FixtureDef boxShapeDef = new FixtureDef(); boxShapeDef.shape = groundBox; groundBox.SetAsEdge(new Vector2(0, 0), new Vector2((float)(winSize.width / PTM_RATIO), 0)); groundBody.CreateFixture(boxShapeDef); groundBox.SetAsEdge(new Vector2(0, 0), new Vector2(0, (float)(winSize.height / PTM_RATIO))); groundBody.CreateFixture(boxShapeDef); groundBox.SetAsEdge(new Vector2(0, (float)(winSize.height / PTM_RATIO)), new Vector2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO))); groundBody.CreateFixture(boxShapeDef); groundBox.SetAsEdge(new Vector2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO)), new Vector2((float)(winSize.width / PTM_RATIO), 0)); groundBody.CreateFixture(boxShapeDef); BodyDef ballBodyDef = new BodyDef(); ballBodyDef.type = BodyType.Dynamic; ballBodyDef.position = new Vector2( (float)(100 / PTM_RATIO), (float)(300 / PTM_RATIO)); ballBodyDef.userData = ball; body = world.CreateBody(ballBodyDef); CircleShape circle = new CircleShape(); circle._radius = (float)(26.0 / PTM_RATIO); FixtureDef ballShapeDef = new FixtureDef(); ballShapeDef.shape = circle; ballShapeDef.density = 1.0f; ballShapeDef.friction = 0.0f; ballShapeDef.restitution = 1.0f; body.CreateFixture(ballShapeDef); this.schedule(tick); return true; }
public LevelComponent(GameContent gameContent, World world) { this.gameContent = gameContent; this.world = world; levelData = gameContent.content.Load<LevelData>("Levels/level" + gameContent.levelIndex); MaxAtoms = levelData.MaxAtoms; int totalProbability = 0; for (int i = 0; i < levelData.AtomProbability.Length; i++) totalProbability += levelData.AtomProbability[i]; if (totalProbability != 100) throw new Exception("must be 100"); entryPoint = levelData.Entry; bonusType = levelData.BonusType; BodyDef bd = new BodyDef(); Body ground = world.CreateBody(bd); PolygonShape ps = new PolygonShape(); List<Vector2> v = levelData.ContinuousBoundry; for (int i = 0; i < v.Count - 1; i++) { if (v[i + 1].X == -1 && v[i + 1].Y == -1) { i++; continue; } ps.SetAsEdge(v[i] / gameContent.b2Scale, v[i + 1] / gameContent.b2Scale); ground.CreateFixture(ps, 0); } for (int i = 0; i < levelData.EquipmentDetails.Count; i++) { Equipment eq = new Equipment(levelData.EquipmentDetails[i].EquipmentName, gameContent, world); eq.body.Position = levelData.EquipmentDetails[i].Position / gameContent.b2Scale; eq.body.Rotation = levelData.EquipmentDetails[i].RotationInDeg / 180 * (float)MathHelper.Pi; eq.isClamped = levelData.EquipmentDetails[i].IsClamped; eq.body.SetType(BodyType.Static); equipments.Add(eq); if (eq.equipName == EquipmentName.thermometer) thermometer = eq; if (eq.equipName == EquipmentName.pHscale) pHscale = eq; } }
public Level(ScreenManager screenManager, int levelIndex) { this.gameContent = screenManager.GameContent; this.levelIndex = levelIndex; camera = new Camera2D(); groundBody = world.CreateBody(new BodyDef()); LoadTiles(levelIndex); UpdateCameraChaseTarget(new GameTime()); Vector2[] v = {Vector2.Zero, new Vector2(tiles.GetLength(0), 0), new Vector2(tiles.GetLength(0), tiles.GetLength(1)), new Vector2(0, tiles.GetLength(1)), Vector2.Zero}; for (int i = 0; i < v.Length - 1; i++) { PolygonShape ps = new PolygonShape(); ps.SetAsEdge(v[i] * Tile.Width / gameContent.b2Scale, v[i + 1] * Tile.Width / gameContent.b2Scale); groundBody.CreateFixture(ps, 0); } }
private void GenerateDrawVertexs() { Vector2[] vertexs = new Vector2[mCountBaseVertexs * 10]; for (int i = 1; i < mCountBaseVertexs - 2; i++) { for (int j = 0; j < 10; j++) { float x = (i - 1) * 10 * mScale + j * mScale; float y = CubicInterpolate( mTerrainBaseVertexs[i - 1], mTerrainBaseVertexs[i], mTerrainBaseVertexs[i + 1], mTerrainBaseVertexs[i + 2], j / 10f); vertexs[i * 10 + j] = new Vector2(x, y); } } int count = (mCountBaseVertexs - 3) * 10; mTerrainDrawVertexs = new Vector2[count + 3]; Array.Copy(vertexs, 10, mTerrainDrawVertexs, 0, count); for (int i = 0; i < count; i++) { mTerrainDrawVertexs[i].X -= ((mCountBaseVertexs - 4) * 10 * mScale + 9 * mScale) / 2f; mTerrainDrawVertexs[i].Y -= 50; } float y1 = -200; mTerrainDrawVertexs[count] = new Vector2(mTerrainDrawVertexs[count - 1].X, y1); mTerrainDrawVertexs[count + 1] = new Vector2(mTerrainDrawVertexs[0].X, y1); mTerrainDrawVertexs[count + 2] = mTerrainDrawVertexs[0]; for (int i = 0; i < count - 1; i++) { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(mTerrainDrawVertexs[i], mTerrainDrawVertexs[i + 1]); mBody.CreateFixture(shape, 0.0f); } }
public CharacterCollision() { // Ground body { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Collinear edges { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); } // Square tiles { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); } // Square made from edges notice how the edges are shrunk to account // for the polygon radius. This makes it so the square character does // not get snagged. However, ray casts can now go through the cracks. for (int i = 0; i < 4; i++) { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); ground.SetTransform(new Vector2(-2f * i, 0), 0); Vector2[] vs = new Vector2[4]; vs[0] = new Vector2(-1.0f, 3.0f); vs[1] = new Vector2(1.0f, 3.0f); vs[2] = new Vector2(1.0f, 5.0f); vs[3] = new Vector2(-1.0f, 5.0f); LoopShape shape = new LoopShape(); shape._count = 4; shape._vertices = vs; ground.CreateFixture(shape, 0.0f); //PolygonShape shape = new PolygonShape(); //float d = 2.0f * Settings.b2_polygonRadius; //shape.SetAsEdge(new Vector2(-1.0f + d, 3.0f), new Vector2(1.0f - d, 3.0f)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(1.0f, 3.0f + d), new Vector2(1.0f, 5.0f - d)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(1.0f - d, 5.0f), new Vector2(-1.0f + d, 5.0f)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(-1.0f, 5.0f - d), new Vector2(-1.0f, 3.0f + d)); //ground.CreateFixture(shape, 0.0f); } // Square character { BodyDef bd = new BodyDef(); bd.position = new Vector2(-3.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } #if false // Hexagon character { BodyDef bd = new BodyDef(); bd.position = new Vector2(-5.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); float angle = 0.0f; float delta = (float)Math.PI / 3.0f; Vector2[] vertices = new Vector2[6]; for (int i = 0; i < 6; ++i) { vertices[i] = new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle)); angle += delta; } PolygonShape shape = new PolygonShape(); shape.Set(vertices, 6); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } // Circle character { BodyDef bd = new BodyDef(); bd.position = new Vector2(3.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); CircleShape shape = new CircleShape(); shape._radius = 0.5f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } #endif }
public Fixture AttachEdge(float x1, float y1, float x2, float y2, float friction, float restitution) { var shape = new PolygonShape(); shape.SetAsEdge( World.B2Value(new Vector2(x1, y1)), World.B2Value(new Vector2(x2, y2))); var fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 0.0f; fixtureDef.friction = friction; fixtureDef.restitution = restitution; return _body.CreateFixture(fixtureDef); }
private void CreateGroundAndWalls() { var grounDef = new BodyDef(); grounDef.type = BodyType.Static; var groundFix = new FixtureDef(); groundFix.restitution = 1.0f; groundFix.friction = 0.0f; groundFix.density = 0.0f; var groundShape = new PolygonShape(); //groundShape.SetAsEdge(new Vector2(0, 8), new Vector2(4.8f, 8.0f)); groundShape.SetAsBox(wallTex.Width *ScaleFactor/2f, wallTex.Height*ScaleFactor/2f); groundBody = world.CreateBody(grounDef); groundBody.Position = new Vector2(2.4f, 4); groundFix.shape = groundShape; groundBody.CreateFixture(groundFix); groundShape.SetAsEdge(new Vector2(0, 0), new Vector2(0f, 8.0f)); var leftBody = world.CreateBody(grounDef); groundFix.shape = groundShape; leftBody.CreateFixture(groundFix); groundShape.SetAsEdge(new Vector2(4.8f, 0), new Vector2(4.8f, 8.0f)); var rightBody = world.CreateBody(grounDef); groundFix.shape = groundShape; rightBody.CreateFixture(groundFix); groundShape.SetAsEdge(new Vector2(0, 0), new Vector2(4.8f, 0)); var topBody = world.CreateBody(grounDef); groundFix.shape = groundShape; topBody.CreateFixture(groundFix); }
void LoadBoundry(int x, int y) { FixtureDef fd = new FixtureDef(); fd.userData = "string"; PolygonShape ps = new PolygonShape(); if (y > 0 && tiles[x, y].tileType != tiles[x, y - 1].tileType) { ps.SetAsEdge(new Vector2(x * Tile.Width, y * Tile.Height) / gameContent.b2Scale, new Vector2((x + 1) * Tile.Width, y * Tile.Height) / gameContent.b2Scale); fd.shape = ps; groundBody.CreateFixture(fd); } if (x > 0 && tiles[x, y].tileType != tiles[x - 1, y].tileType) { ps.SetAsEdge(new Vector2(x * Tile.Width, y * Tile.Height) / gameContent.b2Scale, new Vector2(x * Tile.Width, (y + 1) * Tile.Height) / gameContent.b2Scale); fd.shape = ps; groundBody.CreateFixture(fd); } }
private void LoadTiles(int levelIndex) { BodyDef bd = new BodyDef(); bd.type = BodyType.Static; PolygonShape pShape = new PolygonShape(); ground = world.CreateBody(bd); List<Vector2> lines = new List<Vector2>(); lines = GameContent.content.Load<List<Vector2>>("Levels/" + levelIndex.ToString("00")); for (int i = 0; i < lines[0].X; i++) apples.Add(new Apple(new Vector2(lines[i + 1].X, 600 - lines[i + 1].Y), ground, GameContent, world)); int a = (int)lines[0].X + 1; for (int i = 0; i < lines[0].Y; i++) collectables.Add(new Vector2(lines[a + i].X, 600 - lines[a + i].Y)); a = (int)lines[0].X + (int)lines[0].Y + 1; for (int i = a; i < lines.Count; i += 2) { pShape.SetAsEdge(new Vector2(lines[i].X, 600 - lines[i].Y), new Vector2(lines[i + 1].X, 600 - lines[i + 1].Y)); pShape._radius = 5; FixtureDef fd = new FixtureDef(); fd.shape = pShape; //fd.filter.categoryBits = 6; ground.CreateFixture(fd); } if (LevelIndex == 4) LoadGate(); }
void LoadGround() { Body body = world.CreateBody(new BodyDef()); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(Vector2.Zero, new Vector2(0, 600) / gameContent.Scale); body.CreateFixture(shape, 0); shape.SetAsEdge(new Vector2(0, 600) / gameContent.Scale, new Vector2(800, 600) / gameContent.Scale); body.CreateFixture(shape, 0); shape.SetAsEdge(new Vector2(800, 600) / gameContent.Scale, new Vector2(800, 0) / gameContent.Scale); body.CreateFixture(shape, 0); shape.SetAsEdge(new Vector2(800, 0) / gameContent.Scale, Vector2.Zero / gameContent.Scale); //body.CreateFixture(shape, 0); }
private void LoadGround() { BodyDef bd = new BodyDef(); bd.type = BodyType.Static; bd.position = Vector2.Zero; ground = world.CreateBody(bd); Vector2[] vertices = new Vector2[4]; vertices[0] = new Vector2(0, 0); vertices[1] = new Vector2(Width * TileWidth, 0); vertices[2] = new Vector2(Width * TileWidth, Height * TileHeight); vertices[3] = new Vector2(0, Height * TileHeight); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(vertices[0], vertices[1]); ground.CreateFixture(shape, 1); shape.SetAsEdge(vertices[1], vertices[2]); ground.CreateFixture(shape, 1); shape.SetAsEdge(vertices[2], vertices[3]); ground.CreateFixture(shape, 1); shape.SetAsEdge(vertices[3], vertices[0]); ground.CreateFixture(shape, 1); }