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); }
/// <summary> /// Creates a new jump 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 Jump(World world, ContentManager content, Vector2 pos, float angle, float width, float height) : base(world, content, pos, angle, width, height) { Name = "jump"; texture = content.Load<Texture2D>("Images/" + Name); origin = new Vector2(texture.Width * 0.5f, texture.Height * 0.5f); sourceRect = new Rectangle(1, 1, texture.Width, texture.Height); PolygonShape shape = new PolygonShape(); Vector2[] vertices = {new Vector2(-width*0.5f/ Level.FACTOR, height*0.5f/ Level.FACTOR), new Vector2(width*0.5f/ Level.FACTOR, -height*0.5f/ Level.FACTOR), new Vector2(width*0.5f/ Level.FACTOR, height*0.5f/ Level.FACTOR)}; shape.Set(vertices, 3); body.CreateFixture(shape, 1.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 }
/// <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 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); }
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); }
public Fixture AttachPolygon(Vector2[] verteces, float density, float friction, float restitution) { var shape = new PolygonShape(); shape.Set(verteces, (verteces.Length > Settings.b2_maxPolygonVertices ? Settings.b2_maxPolygonVertices : verteces.Length)); var fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; return _body.CreateFixture(fixtureDef); }
private void LoadShip() { Vector2 position = new Vector2(Width * TileWidth / 2, Height * TileHeight - 600 + FrontClearance); Texture2D texture = GameContent.ship; BodyDef bd = new BodyDef(); bd.fixedRotation = true; bd.type = BodyType.Dynamic; bd.linearDamping = 0.5f; bd.angularDamping = 0.1f; bd.position = position; ship = world.CreateBody(bd); ship.SetUserData(texture); PolygonShape pShape = new PolygonShape(); Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(0, -texture.Height / 2); vertices[1] = new Vector2(texture.Width / 2, texture.Height / 2); vertices[2] = new Vector2(-texture.Width / 2, texture.Height / 2); pShape.Set(vertices, 3); FixtureDef fd = new FixtureDef(); fd.density = 0.1f; fd.shape = pShape; fd.friction = 5; fd.restitution = .5f; ship.CreateFixture(fd); originalShipPos = ship.Position; }