public void ApplyAttribtues(FixtureDef def) { if (friction != 0.2f) { def.friction = friction; } if (restitution != 0) { def.restitution = restitution; } if (density != 0) { def.density = density; } if (categoryBits != 0x0001) { def.filter.categoryBits = categoryBits; } if (maskBits != 0xFFFF) { def.filter.maskBits = maskBits; } if (groupIndex != 0) { def.filter.groupIndex = groupIndex; } if (isSensor != false) { def.isSensor = isSensor; } }
public PhysicsCircle(Texture2D sprite_texture, World physicsWorld, float radius, float positionX, float positionY, float rotation, float density) : base(sprite_texture) { this.scaleToFitTheseDimensions(radius*2.0f, radius*2.0f); this.position.X = positionX; this.position.Y = positionY; this.rotation = rotation; BodyDef dynamicBodyDef = new BodyDef(); dynamicBodyDef.type = BodyType.Dynamic; dynamicBodyDef.position = new Vector2(positionX / ScreenPixelsPerMeter, positionY / ScreenPixelsPerMeter); dynamicBodyDef.linearDamping = 0.0f; Body dynamicBody = physicsWorld.CreateBody(dynamicBodyDef); CircleShape dynamicCircleShape = new CircleShape(); dynamicCircleShape._radius = radius/DynamicPhysicsGameObject.ScreenPixelsPerMeter; //dynamicBoxShape. //dynamicBoxShape.SetAsBox(box_width / 2.0f, box_height / 2.0f); //experiment with / 2.0f FixtureDef dynamicCircleFixtureDef = new FixtureDef(); dynamicCircleFixtureDef.shape = dynamicCircleShape; dynamicCircleFixtureDef.density = density; dynamicCircleFixtureDef.friction = 0.3f; dynamicBody.CreateFixture(dynamicCircleFixtureDef); this.physicsBody = dynamicBody; }
public Body CreateBall(World world, float ScaleFactor) { var bodyDef = new BodyDef(); bodyDef.type = BodyType.Dynamic; var ballShape = new CircleShape(); ballShape._radius = (texture.Width / 2f) * ScaleFactor; var ballFixture = new FixtureDef(); ballFixture.friction = 0.0f; // no friction ballFixture.restitution = 1.0f; // give the ball a perfect bounce ballFixture.density = 1.0f; ballFixture.shape = ballShape; var ballBody = world.CreateBody(bodyDef); ballBody.CreateFixture(ballFixture); // ballBody.Position = new Vector2(((float)r.NextDouble() * 4.5f + .3f), (float)r.NextDouble() * 4.5f + .3f); //ballBodies.Add(ballBody); return ballBody; }
public Enemy(World world, GameContent gameContent, int index, Vector2 position) { this.gameContent = gameContent; walk = new Animation(gameContent.enemy[index], 2, 0.15f, true, new Vector2(0.5f)); animationPlayer.PlayAnimation(walk); if (index == 0) linearImpulse = 1f / 2; else linearImpulse = 0.75f / 2; BodyDef bd = new BodyDef(); bd.position = position / gameContent.b2Scale; bd.type = BodyType.Dynamic; bd.linearDamping = 10; body = world.CreateBody(bd); CircleShape cs = new CircleShape(); cs._radius = (float)(walk.FrameWidth - 1) / gameContent.b2Scale / 2; FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.filter.groupIndex = -1; body.CreateFixture(fd); body.SetUserData(this); }
public Soldier(Shape shape, Vector2 position, GameContent gameContent, World world) { this.gameContent = gameContent; this.world = world; idle = new Animation(gameContent.idle[(int)shape], 20f, false); walk = new Animation(gameContent.walk[(int)shape], 0.15f, true); animationPlayer.PlayAnimation(idle); MaxHealth = 10; health = gameContent.random.Next(2, 10); MaxReloadTime = gameContent.random.Next(50); reloadTime = 0; CircleShape cShape = new CircleShape(); cShape._radius = (Size + 2) / 2 / gameContent.b2Scale; BodyDef bd = new BodyDef(); bd.fixedRotation = true; bd.type = BodyType.Dynamic; bd.position = position / gameContent.b2Scale; body = world.CreateBody(bd); //body.SetLinearDamping(10); FixtureDef fd = new FixtureDef(); fd.shape = cShape; fd.restitution = 0.5f; fd.friction = .1f; fd.density = .1f; body.CreateFixture(fd); body.SetUserData(this); }
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(); }
public Player(GameContent gameContent, World world, Vector2 position) { this.gameContent = gameContent; this.world = world; idle = new Animation(gameContent.playerIdle, 2, 2f, true, new Vector2(0.5f)); walk = new Animation(gameContent.playerWalk, 2, 0.2f, true, new Vector2(0.5f)); die = new Animation(gameContent.playerDie, 2, 0.2f, false, new Vector2(0.5f)); animationPlayer.PlayAnimation(idle); BodyDef bd = new BodyDef(); bd.position = position / gameContent.b2Scale; bd.type = BodyType.Dynamic; bd.linearDamping = 10; body = world.CreateBody(bd); CircleShape cs = new CircleShape(); cs._radius = (float)(idle.FrameWidth - 1) / gameContent.b2Scale / 2; FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.filter.groupIndex = -1; body.CreateFixture(fd); }
// We need separation create/destroy functions from the constructor/destructor because // the destructor cannot access the allocator or broad-phase (no destructor arguments allowed by C++). internal void Create(Body body, FixtureDef def) { _userData = def.userData; _friction = def.friction; _restitution = def.restitution; _body = body; _next = null; _filter = def.filter; _isSensor = def.isSensor; _shape = def.shape.Clone(); // Reserve proxy space int childCount = _shape.GetChildCount(); _proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; ++i) { _proxies[i] = new FixtureProxy(); _proxies[i].fixture = null; _proxies[i].proxyId = BroadPhase.NullProxy; } _proxyCount = 0; _density = def.density; }
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 void createShip1(Vector2 pos, float _rot) { World world = Game1.GameInstance.getWorld(); //////////////// For Sprite System use /////////////// Sprite shipSprite = (Sprite)DisplayManager.Instance().getDisplayObj(SpriteEnum.Ship); Sprite_Proxy proxyShip = new Sprite_Proxy(shipSprite, pos.X, pos.Y, shipScale, Color.Blue); Ship p1 = new Ship(GameObjType.p1ship, proxyShip); SBNode shipBatch = SpriteBatchManager.Instance().getBatch(batchEnum.ships); shipBatch.addDisplayObject(proxyShip); ////////////////////////////////////// // Box2D Body Setup///////////////////////// var shipShape = new PolygonShape(); Vector2[] verts = new Vector2[5]; verts[0] = new Vector2(-5.0f, -5.0f); verts[1] = new Vector2(4.8f, -0.10f); verts[2] = new Vector2(5.0f, 0.00f); verts[3] = new Vector2(4.8f, 0.10f); verts[4] = new Vector2(-5.0f, 5.0f); shipShape.Set(verts, 5); shipShape._centroid = new Vector2(0, 0); var fd = new FixtureDef(); fd.shape = shipShape; fd.restitution = 0.9f; fd.friction = 0.0f; fd.density = 1.0f; fd.userData = p1; BodyDef bd = new BodyDef(); bd.allowSleep = false; bd.fixedRotation = true; bd.type = BodyType.Dynamic; bd.position = p1.spriteRef.pos; var body = world.CreateBody(bd); body.CreateFixture(fd); body.SetUserData(p1); body.Rotation = _rot; /////////////////////////////////////// // Set sprite body reference PhysicsMan.Instance().addPhysicsObj(p1, body); ////////////////// // Set Player's ship and add it to the GameObjManager ////////////// PlayerManager.Instance().getPlayer(PlayerID.one).setShip(p1); GameObjManager.Instance().addGameObj(p1); }
/// Creates a fixture from a shape and attach it to this body. /// This is a convenience function. Use FixtureDef if you need to set parameters /// like friction, restitution, user data, or filtering. /// If the density is non-zero, this function automatically updates the mass of the body. /// @param shape the shape to be cloned. /// @param density the shape density (set to zero for static bodies). /// @warning This function is locked during callbacks. public Fixture CreateFixture(Shape shape, float density) { FixtureDef def = new FixtureDef(); def.shape = shape; def.density = density; return(CreateFixture(def)); }
/// <summary> /// We need separation create/destroy functions from the constructor/destructor because /// the destructor cannot access the allocator or broad-phase (no destructor arguments allowed by C++). /// </summary> /// <param name="body"></param> /// <param name="def"></param> internal void Create(Body body, FixtureDef def) { _userData = def.userData; _friction = def.friction; _restitution = def.restitution; _body = body; _next = null; _filter = def.filter; _isSensor = def.isSensor; _shape = def.shape.Clone(); _density = def.density; }
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 void addCharacterSprite(Sprite sprite) { CircleShape cs = new CircleShape(); cs._radius = sprite.Texture2D.Width / PTM; FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.restitution = 0.5f; fd.friction = 0.5f; fd.density = 1.5f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(sprite.X / PTM, sprite.Y / PTM); bd.userData = sprite; Body body = world.CreateBody(bd); body.CreateFixture(fd); }
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 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; }
/// Creates a fixture and attach it to this body. Use this function if you need /// to set some fixture parameters, like friction. Otherwise you can create the /// fixture directly from a shape. /// If the density is non-zero, this function automatically updates the mass of the body. /// Contacts are not created until the next time step. /// @param def the fixture definition. /// @warning This function is locked during callbacks. public Fixture CreateFixture(FixtureDef def) { Debug.Assert(_world.IsLocked == false); if (_world.IsLocked == true) { return(null); } Fixture fixture = new Fixture(); fixture.Create(this, def); if ((_flags & BodyFlags.Active) == BodyFlags.Active) { BroadPhase broadPhase = _world._contactManager._broadPhase; fixture.CreateProxies(broadPhase, ref _xf); } fixture._next = _fixtureList; _fixtureList = fixture; ++_fixtureCount; fixture._body = this; // Adjust mass properties if needed. if (fixture._density > 0.0f) { ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. _world._flags |= WorldFlags.NewFixture; return(fixture); }
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; }
/// <summary> /// Creates one part of the motorcycle or driver /// </summary> /// <param name="shape">The shape of the part</param> /// <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="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 CreatePart(Shape shape, String name, Vector2 pos, float angle, float density, float friction, float restitution) { Body body = CreateBody(name, pos, angle); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; body.CreateFixture(fixtureDef); parts.Add(body); return body; }
/// <summary> /// Adds a polygon to a body /// </summary> /// <param name="body">The body where the polygon should be added</param> /// <param name="vertices">The vertices of the polygon</param> /// <param name="density">The density of the polygon</param> /// <param name="friction">The friction of the polygon</param> /// <param name="restitution">The restitution of the polygon</param> private void AddPolygonToBody(Body body, Vector2[] vertices, float density, float friction, float restitution) { PolygonShape shape = new PolygonShape(); shape.Set(vertices, vertices.Length); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; body.CreateFixture(fixtureDef); }
public List<Body> generateLevel() { String pathToCollisionFile = level.levelConfig.GlobalSection["collision"]; var vertices = new Vector2[4]; var startPointsStatic = new List<Vector2>(); var grays = new List<Vector2>(); var map = new Bitmap(pathToCollisionFile); for (int x = 0; x < map.Size.Width; ++x) { for (int y = 0; y < map.Size.Height; ++y) { var pixel = map.GetPixel(x, y); if (isRed(pixel)) { startPointsStatic.Add(new Vector2(x, y)); } } } foreach (var startPoint in startPointsStatic) { vertices = GetVertices(map, startPoint); var bodyDef = new BodyDef(); bodyDef.type = BodyType.Static; bodyDef.angle = 0; bodyDef.position = level.ConvertToBox2D(startPoint); var body = level.World.CreateBody(bodyDef); var edges = new List<EdgeInfo>(); if (vertices.Length < 2) { throw new Exception(); } for (int i = 1; i < vertices.Length; ++i) { var vertexA = vertices[i - 1]; var vertexB = vertices[i]; var edgeInfo = new EdgeInfo(); edgeInfo.shape = new EdgeShape(); edgeInfo.isVertical = vertexA.X == vertexB.X; edgeInfo.shape.Set(vertexA, vertexB); edges.Add(edgeInfo); } foreach (var edge in edges) { var fixture = new FixtureDef(); var elementInfo = new LevelElementInfo(); fixture.shape = edge.shape; if (edge.isVertical) { fixture.friction = 0.0f; elementInfo.type = LevelElementType.Wall; } else { fixture.friction = level.GroundFriction; elementInfo.type = LevelElementType.Ground; } fixture.userData = elementInfo; body.CreateFixture(fixture); } bodyList.Add(body); } return bodyList; }
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); }
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; }
protected void AddPoly(Body body2Body, V2DShape polygon) { Shape shape; if (polygon.IsCircle) { CircleShape circDef = new CircleShape(); circDef._radius = polygon.Radius / (V2DScreen.WorldScale * State.Scale.X); Vector2 lp = new Vector2(polygon.CenterX / V2DScreen.WorldScale, polygon.CenterY / V2DScreen.WorldScale); circDef._p = lp; shape = circDef; } else { float[] pts = polygon.Data; PolygonShape polyDef = new PolygonShape(); shape = polyDef; int len= (int)(pts.Length / 2); Vector2[] v2s = new Vector2[len]; for (int i = 0; i < len; i++) { float px = pts[i * 2]; float py = pts[i * 2 + 1]; v2s[i] = new Vector2( px / V2DScreen.WorldScale * State.Scale.X, py / V2DScreen.WorldScale * State.Scale.Y); } polyDef.Set(v2s, len); } FixtureDef fd = new FixtureDef(); fd.shape = shape; if (instanceName.IndexOf("s_") == 0) { isStatic = true; fd.density = 0.0f; } else { fd.density = density; } fd.friction = friction; fd.restitution = restitution; if (groupIndex != 0) { fd.filter.groupIndex = groupIndex; } if (attributeProperties != null) { attributeProperties.ApplyAttribtues(fd); } body.CreateFixture(fd); }
/// <summary> /// Creates a fixture from a shape and attach it to this body. /// This is a convenience function. Use FixtureDef if you need to set parameters /// like friction, restitution, user data, or filtering. /// If the density is non-zero, this function automatically updates the mass of the body. /// @warning This function is locked during callbacks. /// </summary> /// <param name="shape">the shape to be cloned.</param> /// <param name="density">the shape density (set to zero for static bodies).</param> /// <returns></returns> public Fixture CreateFixture(Shape shape, float density) { FixtureDef def = new FixtureDef(); def.shape = shape; def.density = density; return CreateFixture(def); }
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); } }
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 }
/// <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 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 override void Initialize(ConfigSection section) { base.Initialize(section); textureName = section["texture"]; var pos = section["position"].AsVector2(); SideLength = section["sideLength"]; Speed = section["speed"]; MaxSpeed = section["maxSpeed"]; JumpImpulse = section["jumpImpulse"]; JumpThreshold = section["jumpThreshold"]; var bodyDef = new BodyDef(); bodyDef.type = BodyType.Dynamic; bodyDef.angle = 0; bodyDef.position = Game.level.ConvertToBox2D(pos); bodyDef.inertiaScale = section["inertiaScale"]; bodyDef.linearDamping = section["linearDamping"]; bodyDef.angularDamping = section["angularDamping"]; bodyDef.userData = this; Body = Game.level.World.CreateBody(bodyDef); var shape = new PolygonShape(); var offset = SideLength / 2; shape.Set(new Vector2[]{ Game.level.ConvertToBox2D(new Vector2(-offset, -offset)), Game.level.ConvertToBox2D(new Vector2(offset, -offset)), Game.level.ConvertToBox2D(new Vector2(offset, offset)), Game.level.ConvertToBox2D(new Vector2(-offset, offset)) } , 4); var fixture = new FixtureDef(); fixture.restitution = section["restitution"]; fixture.density = section["density"]; fixture.shape = shape; fixture.friction = section["friction"]; Body.CreateFixture(fixture); }
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(); }
public Atom(Symbol symbol, Vector2 position, GameContent gameContent, World world) { this.gameContent = gameContent; this.world = world; if (symbol == Symbol.Ra) eye = EyeState.Angry; this.symbol = symbol; this.symbolStr = symbol.ToString(); symbolCenter = gameContent.symbolFont.MeasureString(this.symbolStr); symbolCenter.X *= 0.5f; symbolCenter.Y *= 0.92f; bondsLeft = (int)symbol; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = this.position = position / gameContent.b2Scale; bd.bullet = true; body = world.CreateBody(bd); body.SetUserData(this); CircleShape cs = new CircleShape(); cs._radius = gameContent.atomRadius / gameContent.b2Scale; FixtureDef fd = new FixtureDef(); fd.shape = cs; fd.restitution = 0.2f; fd.friction = 0.5f; fixture = body.CreateFixture(fd); electroShockAnimation = new Animation(gameContent.electroShock, 3, 0.1f, true, new Vector2(0.5f, 0.5f)); radiationSmoke = new RadiationSmoke(gameContent, this); // Collide only with Ground but not with itself and bonded Filter mouseFilter = new Filter(); mouseFilter.categoryBits = 0x0002; mouseFilter.maskBits = 0x0001; mouseFilter.groupIndex = -2; // Collide with every thing atomFilter = new Filter(); atomFilter.categoryBits = 0x0001; atomFilter.maskBits = 0x0001; atomFilter.groupIndex = 1; fixture.SetFilterData(ref atomFilter); SetMode(false, false); }
/// <summary> /// Creates a fixture and attach it to this body. Use this function if you need /// to set some fixture parameters, like friction. Otherwise you can create the /// fixture directly from a shape. /// If the density is non-zero, this function automatically updates the mass of the body. /// Contacts are not created until the next time step. /// @warning This function is locked during callbacks. /// </summary> /// <param name="def">the fixture definition.</param> /// <returns></returns> public Fixture CreateFixture(FixtureDef def) { Debug.Assert(_world.IsLocked == false); if (_world.IsLocked == true) { return null; } Fixture fixture = new Fixture(); fixture.Create(this, def); if ((_flags & BodyFlags.Active) == BodyFlags.Active) { BroadPhase broadPhase = _world._contactManager._broadPhase; fixture.CreateProxy(broadPhase, ref _xf); } fixture._next = _fixtureList; _fixtureList = fixture; ++_fixtureCount; fixture._body = this; // Adjust mass properties if needed. if (fixture._density > 0.0f) { ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. _world._flags |= WorldFlags.NewFixture; return fixture; }