public Car(Vector2 position, GameContent gameContent, World world) { this.world = world; this.gameContent = gameContent; BodyDef bd = new BodyDef(); bd.position = position / gameContent.Scale; bd.type = BodyType.Dynamic; bd.bullet = true; body = world.CreateBody(bd); body.SetLinearDamping(1f); body.SetAngularDamping(0.1f); float width = gameContent.playerCar.Width, height = gameContent.playerCar.Height; FixtureDef fd = new FixtureDef(); fd.density = 0.1f; //fd.restitution = .1f; CircleShape cs = new CircleShape(); cs._p = new Vector2(0, -(height - width / 2)) / gameContent.Scale; cs._radius = width / 2 / gameContent.Scale; fd.shape = cs; body.CreateFixture(fd); PolygonShape ps = new PolygonShape(); ps.SetAsBox(width / 2 / gameContent.Scale, (height - width / 2) / 2 / gameContent.Scale, new Vector2(0, -(height - width / 2) / 2) / gameContent.Scale, 0); fd.shape = ps; body.CreateFixture(fd); CreateWheels(); }
/// <summary> /// Creates a new ground component /// </summary> /// <param name="world">The Box2D world that will hold this component</param> /// <param name="content">Used ContentManager</param> /// <param name="pos">The position of this component</param> /// <param name="angle">The rotation angle of this component</param> /// <param name="width">The width of this component</param> /// <param name="height">The height of this component</param> public Ground(World world, ContentManager content, Vector2 pos, float angle, float width, float height) : base(world, content, pos, angle, width, height) { Name = "grass"; texture = content.Load<Texture2D>("Images/" + Name); PolygonShape shape = new PolygonShape(); shape.SetAsBox(width * 0.5f / Level.FACTOR, height * 0.5f / Level.FACTOR); body.CreateFixture(shape, 1.0f).SetFriction(1.0f); origin = new Vector2(screenPos.Width * 0.5f, texture.Height * 0.5f); sourceRect = new Rectangle(0, 0, (int)width, texture.Height); }
public Box(World world, Vector2 position, Vector2 size, string texture, bool isStatic, Player player, float health = 100) { ObjectType = EObjectType.Box; mHealth = health; mStartHealth = health; mIsDestroyed = false; mSize = size; mWorld = world; mTexture = texture; mPlayer = player; DestroyTime = null; PolygonShape polygonShape = new PolygonShape(); polygonShape.SetAsBox(size.X / 2f, size.Y / 2f); BodyDef bodyDef = new BodyDef(); bodyDef.position = position; bodyDef.bullet = true; if (isStatic) { bodyDef.type = BodyType.Static; } else { bodyDef.type = BodyType.Dynamic; } mBody = world.CreateBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape;//Форма fixtureDef.density = 0.1f;//Плотность fixtureDef.friction = 0.3f;//Сила трения fixtureDef.restitution = 0f;//Отскок Filter filter = new Filter(); filter.maskBits = (ushort)(EntityCategory.Player1 | EntityCategory.Player2); filter.categoryBits = (ushort)player.PlayerType; var fixture = mBody.CreateFixture(fixtureDef); fixture.SetFilterData(ref filter); MassData data = new MassData(); data.mass = 0.01f; mBody.SetUserData(this); }
public LabComponent(GameContent gameContent, World world) { this.gameContent = gameContent; EqScale = 0.4f; AtomScale = 0.7f; Width = (int)(gameContent.viewportSize.X * 2 / EqScale); Height = (int)(gameContent.viewportSize.Y * 1 / EqScale); gameContent.clampDistance = (int)(gameContent.viewportSize.X / 2 / EqScale); PolygonShape ps = new PolygonShape(); Body ground = world.CreateBody(new BodyDef()); Vector2 pos = new Vector2(Width / 2, Height) / gameContent.scale; ps.SetAsBox(Width / 2 / gameContent.scale, (float)gameContent.labTable.Height / gameContent.scale, pos, 0); ground.CreateFixture(ps, 0); }
public void addRect(float x, float y, float width, int height, object type) { PolygonShape ps = new PolygonShape(); ps.SetAsBox(width / PTM / 2, height / PTM / 2); FixtureDef fd = new FixtureDef(); fd.shape = ps; fd.restitution = 0.0f; fd.friction = 0.8f; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.Static; bd.position = new Vector2(x / PTM, y / PTM); bd.userData = type; Body body = world.CreateBody(bd); body.CreateFixture(fd); }
public Body CreateWall(World world, float ScaleFactor) { 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(texture.Width * ScaleFactor / 2f, texture.Height * ScaleFactor / 2f); var groundBody = world.CreateBody(grounDef); groundBody.Position = new Vector2(2.4f, 4); groundFix.shape = groundShape; groundBody.CreateFixture(groundFix); return groundBody; }
public PhysicsBox(Texture2D sprite_texture, World physicsWorld, float box_width, float box_height, float positionX, float positionY, float rot, float density) : base(sprite_texture) { this.scaleToFitTheseDimensions(box_width, box_height); this.position.X = positionX; this.position.Y = positionY; BodyDef dynamicBodyDef = new BodyDef(); dynamicBodyDef.type = BodyType.Dynamic; dynamicBodyDef.position = new Vector2(positionX/DynamicPhysicsGameObject.ScreenPixelsPerMeter, positionY/DynamicPhysicsGameObject.ScreenPixelsPerMeter); dynamicBodyDef.angle = rot; Body dynamicBody = physicsWorld.CreateBody(dynamicBodyDef); PolygonShape dynamicBoxShape = new PolygonShape(); dynamicBoxShape.SetAsBox((box_width/ScreenPixelsPerMeter) / 2.0f, (box_height/ScreenPixelsPerMeter) / 2.0f); //experiment with / 2.0f FixtureDef dynamicBoxFixtureDef = new FixtureDef(); dynamicBoxFixtureDef.shape = dynamicBoxShape; dynamicBoxFixtureDef.density = density; dynamicBoxFixtureDef.friction = 0.3f; dynamicBody.CreateFixture(dynamicBoxFixtureDef); this.physicsBody = dynamicBody; }
private void armed(object obj) { World world = Game1.GameInstance.getWorld(); Ship ship = PlayerManager.Instance().getPlayer(owner).playerShip; Body shipBody = ship.physicsObj.body; var bombShape = new PolygonShape(); bombShape.SetAsBox(5, 5); var fd = new FixtureDef(); fd.shape = bombShape; fd.restitution = 0.0f; fd.friction = 0.0f; fd.density = 0.0001f; fd.userData = this; BodyDef bd = new BodyDef(); bd.fixedRotation = true; bd.type = BodyType.Static; bd.position = orgPos; var body = world.CreateBody(bd); body.CreateFixture(fd); body.SetUserData(this); TimeSpan currentTime = Timer.GetCurrentTime(); TimeSpan t_1 = currentTime.Add(new TimeSpan(0, 0, 0, 0, 0)); CallBackData nodeData = new CallBackData(0, Timer.GetCurrentTime()); bombAnim(nodeData); GameObjManager.Instance().addGameObj(this); PhysicsMan.Instance().addPhysicsObj(this, body); playBombArmedSound(); }
protected Body CreateBox(float x, float y, float w, float h) { x /= WorldScale; y /= WorldScale; w /= WorldScale; h /= WorldScale; BodyDef bodyDef = new BodyDef(); bodyDef.position = new Vector2(x + w / 2f, y + h / 2f); bodyDef.type = BodyType.Static; Body body = world.CreateBody(bodyDef); PolygonShape polyShape = new PolygonShape(); polyShape.SetAsBox(w / 2f, h / 2f); //polyShape._vertexCount = 4; //polyShape._vertices[0] = new Vector2(0, 0); //polyShape._vertices[1] = new Vector2(w, 0); //polyShape._vertices[2] = new Vector2(w, h); //polyShape._vertices[3] = new Vector2(0, h); FixtureDef fd = new FixtureDef(); fd.density = 1; fd.shape = polyShape; fd.filter.groupIndex = 1; fd.filter.categoryBits = 1; fd.filter.maskBits = 0xFFFF; Fixture f = body.CreateFixture(fd); return body; }
public EdgeTest() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); Vector2 v1 = new Vector2(-10.0f, 0.0f); Vector2 v2 = new Vector2(-7.0f, -1.0f); Vector2 v3 = new Vector2(-4.0f, 0.0f); Vector2 v4 = new Vector2(0.0f, 0.0f); Vector2 v5 = new Vector2(4.0f, 0.0f); Vector2 v6 = new Vector2(7.0f, 1.0f); Vector2 v7 = new Vector2(10.0f, 0.0f); EdgeShape shape = new EdgeShape(); shape.Set(v1, v2); //shape._index1 = 0; //shape._index2 = 1; shape._hasVertex3 = true; shape._vertex3 = v3; ground.CreateFixture(shape, 0.0f); shape.Set(v2, v3); //shape._index1 = 1; //shape._index2 = 2; shape._hasVertex0 = true; shape._hasVertex3 = true; shape._vertex0 = v1; shape._vertex3 = v4; ground.CreateFixture(shape, 0.0f); shape.Set(v3, v4); //shape._index1 = 2; //shape._index2 = 3; shape._hasVertex0 = true; shape._hasVertex3 = true; shape._vertex0 = v2; shape._vertex3 = v5; ground.CreateFixture(shape, 0.0f); shape.Set(v4, v5); //shape._index1 = 3; //shape._index2 = 4; shape._hasVertex0 = true; shape._hasVertex3 = true; shape._vertex0 = v3; shape._vertex3 = v6; ground.CreateFixture(shape, 0.0f); shape.Set(v5, v6); //shape._index1 = 4; //shape._index2 = 5; shape._hasVertex0 = true; shape._hasVertex3 = true; shape._vertex0 = v4; shape._vertex3 = v7; ground.CreateFixture(shape, 0.0f); shape.Set(v6, v7); //shape._index1 = 5; //shape._index2 = 6; shape._hasVertex0 = true; shape._vertex0 = v5; ground.CreateFixture(shape, 0.0f); } { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-0.5f, 0.5f); bd.allowSleep = false; Body body = _world.CreateBody(bd); CircleShape shape = new CircleShape(); shape._radius = 0.5f; body.CreateFixture(shape, 1.0f); } { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.5f, 0.5f); bd.allowSleep = false; Body body = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); body.CreateFixture(shape, 1.0f); } }
private void LoadGate() { int height = 80; Vector2 jointPos = new Vector2(493, 439); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = jointPos + new Vector2(0, height / 2); gate = world.CreateBody(bd); PolygonShape pShape = new PolygonShape(); pShape.SetAsBox(0.2f, height / 2); FixtureDef fd = new FixtureDef(); fd.shape = pShape; fd.density = 0.3f; gate.CreateFixture(fd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.bodyA = gate; rjd.bodyB = ground; rjd.localAnchorA = new Vector2(0, -height / 2); rjd.localAnchorB = jointPos; world.CreateJoint(rjd); }
public Tiles() { { float a = 0.5f; BodyDef bd = new BodyDef(); bd.position.Y = -a; Body ground = _world.CreateBody(bd); #if true int N = 200; int M = 10; Vector2 position = new Vector2(); position.Y = 0.0f; for (int j = 0; j < M; ++j) { position.X = -N * a; for (int i = 0; i < N; ++i) { PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, a, position, 0.0f); ground.CreateFixture(shape, 0.0f); position.X += 2.0f * a; } position.Y -= 2.0f * a; } #else int N = 200; int M = 10; Vector2 position; position.x = -N * a; for (int i = 0; i < N; ++i) { position.y = 0.0f; for (int j = 0; j < M; ++j) { PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, a, position, 0.0f); ground.CreateFixture(shape, 0.0f); position.y -= 2.0f * a; } position.x += 2.0f * a; } #endif } { float a = 0.5f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, a); Vector2 x = new Vector2(-7.0f, 0.75f); Vector2 y; Vector2 deltaX = new Vector2(0.5625f, 1.25f); Vector2 deltaY = new Vector2(1.125f, 0.0f); for (int i = 0; i < e_count; ++i) { y = x; for (int j = i; j < e_count; ++j) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = y; Body body = _world.CreateBody(bd); body.CreateFixture(shape, 5.0f); y += deltaY; } x += deltaX; } } }
void CreateWheels() { float width = gameContent.playerCar.Width, height = gameContent.playerCar.Height; Vector2 halfws = wheelSize / 2 / gameContent.Scale; PolygonShape ps = new PolygonShape(); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; FixtureDef fd = new FixtureDef(); fd.density = 0.1f; // Steer Wheel bd.position = body.Position + new Vector2(0, -(height - width / 2)) / gameContent.Scale; ps.SetAsBox(halfws.X, halfws.Y); fd.shape = ps; steerWheel = world.CreateBody(bd); steerWheel.CreateFixture(fd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body, steerWheel, steerWheel.GetWorldCenter()); rjd.enableMotor = true; rjd.maxMotorTorque = 100; steerJoint = world.CreateJoint(rjd) as RevoluteJoint; // Drive Wheel bd.position = body.Position + new Vector2(0, -halfws.Y); ps.SetAsBox(halfws.X, halfws.Y); fd.shape = ps; driveWheel = world.CreateBody(bd); driveWheel.CreateFixture(fd); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.Initialize(body, driveWheel, driveWheel.GetWorldCenter(), new Vector2(1, 0)); pjd.lowerTranslation = pjd.upperTranslation = 0; pjd.enableLimit = true; world.CreateJoint(pjd); }
// Center Fences private void createTopFence() { World world = Game1.GameInstance.getWorld(); Rectangle gameScreenSize = Game1.GameInstance.gameScreenSize; // Sprite Animation /////////// Sprite wallSprite = (Sprite)DisplayManager.Instance().getDisplayObj(SpriteEnum.fenceCTop); DisplayManager.Instance().addDisplayObj(SpriteEnum.Wall, wallSprite); Sprite_Proxy wallProxy = new Sprite_Proxy(wallSprite, 150, 71, fenceScale, Color.White); Wall wall1 = new Wall(GameObjType.horzWalls, wallProxy); SBNode wallBatch = SpriteBatchManager.Instance().getBatch(batchEnum.boxs); wallBatch.addDisplayObject(wallProxy); ////////////////// // Box2D ///////////// var wallShape = new PolygonShape(); wallShape.SetAsBox(wall1.spriteRef.sprite.width / 2, wall1.spriteRef.sprite.height / 2); var fd = new FixtureDef(); fd.shape = wallShape; fd.restitution = 0.9f; fd.friction = 0.0f; fd.density = 1.0f; fd.userData = wall1; BodyDef bd = new BodyDef(); bd.type = BodyType.Static; bd.position = wall1.spriteRef.pos; var body = world.CreateBody(bd); body.CreateFixture(fd); body.SetUserData(wall1); body.Rotation = (float)(90.0f * (Math.PI / 180.0f)); GameObjManager.Instance().addGameObj(wall1); PhysicsMan.Instance().addPhysicsObj(wall1, body); ///////////////////// }
public Character CreateCharacter(CharacterDef charDef) { var bodydef = new BodyDef(); bodydef.type = charDef.BodyType; bodydef.angle = charDef.RotateZ; bodydef.fixedRotation = charDef.FixedRotation; bodydef.position.X = B2Value(charDef.X); bodydef.position.Y = B2Value(charDef.Y); Body body = _world.CreateBody(bodydef); var shape = new PolygonShape(); shape.SetAsBox(B2Value(charDef.Width*0.5f), B2Value(charDef.Height*0.5f)); var fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = charDef.Density; fixtureDef.friction = charDef.Fraction; fixtureDef.restitution = charDef.Restitution; body.CreateFixture(fixtureDef); var tex = XNADevicesManager.Instance.ContentManager.Load<Texture2D>(charDef.TexName); var sprite = new Sprite(tex); sprite.Alpha = charDef.Alpha; sprite.ScaleX = charDef.ScaleX; sprite.ScaleY = charDef.ScaleY; sprite.RotateX = charDef.RotateX; sprite.RotateY = charDef.RotateY; sprite.RotateZ = charDef.RotateZ; sprite.R = charDef.R; sprite.G = charDef.G; sprite.B = charDef.B; sprite.X = charDef.X; sprite.Y = charDef.Y; sprite.Z = charDef.Z; sprite.DrawRectangle = charDef.DrawRectangle; sprite.TransformOrigin = charDef.TransformOrigin; sprite.ZWriteEnable = charDef.ZWriteEnable; var character = new Character(sprite, body, this, charDef); character.Group = charDef.Group; character.Damage = charDef.Damage; character.HP = charDef.HP; character.MaxHP = charDef.MaxHP; body.SetUserData(character); return character; }
public void createMissile() { if (state == PlayerState.alive && missileAvailable()) { Ship pShip = playerShip; Body pShipBody = pShip.physicsObj.body; Sprite missileSprite = (Sprite)DisplayManager.Instance().getDisplayObj(SpriteEnum.Missile); Sprite_Proxy proxyMissile = new Sprite_Proxy(missileSprite, (int)pShip.spriteRef.pos.X, (int)pShip.spriteRef.pos.Y, 0.5f, pShip.spriteRef.color); Missile missile = new Missile(missileType, proxyMissile, id); SBNode missileBatch = SpriteBatchManager.Instance().getBatch(batchEnum.missiles); missileBatch.addDisplayObject(proxyMissile); World world = Game1.GameInstance.getWorld(); var missileShape = new PolygonShape(); missileShape.SetAsBox(3, 3); var fd = new FixtureDef(); fd.shape = missileShape; fd.restitution = 0.0f; fd.friction = 0.0f; fd.density = 0.0001f; fd.userData = missile; // Grab ship orientation vector Vector2 direction = new Vector2((float)(Math.Cos(pShipBody.GetAngle())), (float)(Math.Sin(pShipBody.GetAngle()))); direction.Normalize(); BodyDef bd = new BodyDef(); bd.fixedRotation = true; bd.type = BodyType.Dynamic; bd.position = (new Vector2(pShip.spriteRef.pos.X, pShip.spriteRef.pos.Y)) + (direction * 10); var body = world.CreateBody(bd); body.SetBullet(true); body.Rotation = pShipBody.Rotation; body.CreateFixture(fd); body.SetUserData(missile); direction *= 1000; body.ApplyLinearImpulse(direction, body.GetWorldCenter()); GameObjManager.Instance().addGameObj(missile); PhysicsMan.Instance().addPhysicsObj(missile, body); if (numMissiles < maxNumMissiles) numMissiles++; } }
public override void Initialize(ConfigSection section) { base.Initialize(section); if (section.Options.ContainsKey("texture")) { _textureName = section["texture"]; } var pos = section["position"].AsVector2(); _dim = section["dimensions"].AsVector2(); if (section.Options.ContainsKey("enterEvent")) { _enterEvent = section["enterEvent"]; } if (section.Options.ContainsKey("leaveEvent")) { _leaveEvent = section["leaveEvent"]; } if (section.Options.ContainsKey("enterEventData")) { _enterEventData = section["enterEventData"]; } if (section.Options.ContainsKey("leaveEventData")) { _leaveEventData = section["leaveEventData"]; } var bodyDef = new BodyDef(); bodyDef.userData = this; var fixtureDef = new FixtureDef(); var shape = new PolygonShape(); shape.SetAsBox(Game.level.ConvertToBox2D(_dim.X / 2), Game.level.ConvertToBox2D(_dim.Y / 2)); fixtureDef.shape = shape; fixtureDef.isSensor = true; fixtureDef.userData = new LevelElementInfo() { type = LevelElementType.Ground }; bodyDef.position = Game.level.ConvertToBox2D(pos); Body = Game.level.World.CreateBody(bodyDef); Body.CreateFixture(fixtureDef); }
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 }
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); }
public override void Initialize(ConfigSection section) { base.Initialize(section); TextureName = section["texture"]; _dimensions = section["dimensions"].AsVector2(); section.IfOptionExists("toggleEvent", opt => Game.Events[opt].addListener(onToggleEvent)); section.IfOptionExists("fadeTime", opt => FadeTime = opt); var stateName = section["state"]; if (stateName == "active") { State.setActive(); CurrentFadeTime = FadeTime; } else if (stateName == "inactive") { State.setInactive(); CurrentFadeTime = 0.0f; } else { throw new ArgumentException("Unsupported GameObject state: " + stateName); } section.IfOptionExists("movementSpeed", opt => MovementSpeed = opt); section.IfOptionExists("toggleWaypointEvent", opt => Game.Events[opt].addListener(onToggleWaypointEvent)); var bodyDef = new BodyDef(); var fixtureDef = new FixtureDef(); var shape = new PolygonShape(); shape.SetAsBox(Game.level.ConvertToBox2D(Dimensions.X / 2), Game.level.ConvertToBox2D(Dimensions.Y / 2)); fixtureDef.shape = shape; fixtureDef.userData = new LevelElementInfo() { type = LevelElementType.Ground }; bodyDef.type = BodyType.Kinematic; bodyDef.position = Game.level.ConvertToBox2D(section["position"].AsVector2()); bodyDef.active = State.IsActive; Body = Game.level.World.CreateBody(bodyDef); Body.CreateFixture(fixtureDef); section.IfOptionExists("waypointStart", opt => WaypointStart = opt.AsVector2(), () => WaypointStart = Pos); section.IfOptionExists("waypointEnd", opt => WaypointEnd = opt.AsVector2(), () => WaypointEnd = Pos); section.IfOptionExists("target", targetName => { if (targetName == "start") { _targetIndex.Value = 0; } else if (targetName == "end") { _targetIndex.Value = 1; } else { throw new ArgumentException("Unsupported target name: " + targetName); } }, () => _targetIndex.Value = 1); }
/// <summary> /// Creates a rectangle shaped part of the motorcycle or driver /// </summary> /// <param name="name">The Content name of the texture that belongs to this part</param> /// <param name="pos">The position of the part</param> /// <param name="width">The width of the rectangle shape</param> /// <param name="height">The height of the rectangle shape</param> /// <param name="angle">The rotation angle of the part</param> /// <param name="density">The density of the part</param> /// <param name="friction">The friction of the part</param> /// <param name="restitution">The restitution of the part</param> /// <returns></returns> private Body CreateBoxPart(String name, Vector2 pos, float width, float height, float angle, float density, float friction, float restitution) { PolygonShape shape = new PolygonShape(); shape.SetAsBox(width * 0.5f / Level.FACTOR, height * 0.5f / Level.FACTOR); Body body = CreatePart(shape, name, pos, angle, density, friction, restitution); ((UserData)body.GetUserData()).Width = width; ((UserData)body.GetUserData()).Height = height; return body; }
private void Load() { levelData = gameContent.content.Load<LevelData>("Levels/level" + levelIndex); soldiers.Add(new Soldier(Shape.Triangle, new Vector2(702, 185), gameContent, world)); soldiers.Add(new Soldier(Shape.Triangle, new Vector2(752, 185), gameContent, world)); soldiers.Add(new Soldier(Shape.Triangle, new Vector2(400, 160), gameContent, world)); soldiers.Add(new Soldier(Shape.Triangle, new Vector2(440, 171), gameContent, world)); soldiers.Add(new Soldier(Shape.Square, new Vector2(109, 316), gameContent, world)); soldiers.Add(new Soldier(Shape.Square, new Vector2(346, 444), gameContent, world)); PolygonShape pShape = new PolygonShape(); BodyDef bd = new BodyDef(); bd.type = BodyType.Static; for (int i = 0; i < levelData.Rectangles.Count; i++) { GroundData g = levelData.Rectangles[i]; Rectangle r = new Rectangle(0, 0, g.Width, g.Height); pShape.SetAsBox((float)r.Width / 2 / gameContent.b2Scale, (float)r.Height / 2 / gameContent.b2Scale); bd.position = g.Position / gameContent.b2Scale; Body b = world.CreateBody(bd); b.CreateFixture(pShape, 1); b.Rotation = g.Rotation / 180 * (float)Math.PI; } }
/// <summary> /// Call this to initialize a Behaviour with data supplied in a file. /// </summary> /// <param name="fileName">The file to load from.</param> public override void LoadContent(String fileName) { base.LoadContent(fileName); // Load the definiton for this object. SimulatedPhysicsDefinition def = GameObjectManager.pInstance.pContentManager.Load<SimulatedPhysicsDefinition>(fileName); // Create the body representing this object in the physical world. BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(640.0f, 340.0f)); bd.fixedRotation = true; bd.linearDamping = 1.0f; mBody = PhysicsManager.pInstance.pWorld.CreateBody(bd); /* // A bunch of code to create a capsule. Not using it because it has all the same problems as // a box when it comes to catching edges and such. var shape = new PolygonShape(); Vector2[] v = new Vector2[8]; Double increment = System.Math.PI * 2.0 / (v.Length - 2); Double theta = 0.0; Vector2 center = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, 8.0f)); Single radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f); for (Int32 i = 0; i < v.Length; i++) { v[i] = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta)); if (i == (v.Length / 2) - 1) { center = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, -8.0f)); i++; v[i] = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta)); } theta += increment; } //v = v.Reverse().ToArray(); shape.Set(v, v.Length); var fd = new FixtureDef(); fd.shape = shape; fd.restitution = 0.0f; fd.friction = 0.5f; fd.density = 1.0f; mBody.CreateFixture(fd); */ var shape = new PolygonShape(); shape.SetAsBox(PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f), PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f)); var fd = new FixtureDef(); fd.shape = shape; fd.restitution = 0.0f; fd.friction = 0.5f; fd.density = 1.0f; //mBody.CreateFixture(fd); var circle = new CircleShape(); circle._radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8); circle._p = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, 8.0f)); fd = new FixtureDef(); fd.shape = circle; fd.restitution = 0.0f; fd.friction = 0.5f; fd.density = 1.5f; mBody.CreateFixture(fd); circle = new CircleShape(); circle._radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8); circle._p = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, -8.0f)); fd = new FixtureDef(); fd.shape = circle; fd.restitution = 0.0f; fd.friction = 0.5f; fd.density = 1.5f; mBody.CreateFixture(fd); }
public Fixture AttachRectangle(float width, float height, Vector2 center, float angle, float density, float friction, float restitution) { var shape = new PolygonShape(); shape.SetAsBox( World.B2Value(width*0.5f), World.B2Value(height*0.5f), World.B2Value(center), angle); var fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; return _body.CreateFixture(fixtureDef); }
void SetFixtures() { FixtureDef fd = new FixtureDef(); fd.density = 1; fd.friction = .3f; fd.restitution = 0f; PolygonShape ps = new PolygonShape(); float area; fixtureCount = 0; List<Vector2> v = equipmentData.ContinuousEdges; for (int i = 0; i < v.Count - 1; i++) { area = 0; if (v[i + 1].X == -1 && v[i + 1].Y == -1) { i++; continue; } Vector2 c = (v[i] + v[i + 1] - 2 * origin) / 2 / gameContent.b2Scale; float h = 8f / 2 / gameContent.b2Scale; float w = Vector2.Distance(v[i], v[i + 1]) / 2 / gameContent.b2Scale; float theta = (float)Math.Atan((v[i + 1].Y - v[i].Y) / (v[i + 1].X - v[i].X)); ps.SetAsBox(w, h, c, theta); fd.shape = ps; area = h * w * 4; fd.userData = area; fixtureCount += 1; body.CreateFixture(fd); } List<Box> b = equipmentData.Boxes; for (int i = 0; i < b.Count; i++) { float hx = b[i].Width / 2 / gameContent.b2Scale, hy = b[i].Height / 2 / gameContent.b2Scale; ps.SetAsBox(hx, hy, (b[i].Position - origin) / gameContent.b2Scale, b[i].RotationInDeg / 180 * (float)Math.PI); fd.shape = ps; area = hx * hy * 4; fd.userData = area; fixtureCount += 1; body.CreateFixture(fd); } List<Circle> circles = equipmentData.Circles; for (int i = 0; i < circles.Count; i++) { CircleShape cs = new CircleShape(); cs._radius = circles[i].Diameter / 2 / gameContent.b2Scale; cs._p = (circles[i].Position - origin) / gameContent.b2Scale; fd.shape = cs; area = (float)Math.PI * cs._radius * cs._radius; fd.userData = area; fixtureCount += 1; body.CreateFixture(fd); } }
void LoadTile(char tileType, int x, int y) { switch (tileType) { case '.': tiles[x, y] = gameContent.road; break; case '#': tiles[x, y] = gameContent.block; Body b = world.CreateBody(new BodyDef()); b.Position = (new Vector2(x, y) * 100 + new Vector2(100) / 2) / gameContent.Scale; PolygonShape ps = new PolygonShape(); ps.SetAsBox(50f / gameContent.Scale, 50f / gameContent.Scale); b.CreateFixture(ps, 0); break; // Unknown tile type character default: throw new NotSupportedException(String.Format( "Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y)); } }