public Projectile(Player creator, float x, float y, float width, float height) : base(creator, 0, 0) { /* Create New Projectile Body */ BodyDef def = new BodyDef(); def.IsBullet = true; def.Position = creator.body.GetPosition() + new Vec2(x, y); projectile = creator.body.GetWorld().CreateBody(def); /* Create a fixture for the projectile */ PolygonDef fixdef = new PolygonDef(); fixdef.Density = 1.0f; fixdef.SetAsBox(width / 2, height / 2); fixdef.Filter.GroupIndex = creator.ID; fixture = projectile.CreateFixture(fixdef); fixture.Filter.CategoryBits = 0x0004; fixture.Filter.MaskBits = 0xFFFF; /* Made a 2nd fixture, one to observe all collisions */ fixdef.IsSensor = true; fix2 = projectile.CreateFixture(fixdef); fix2.UserData = this; /* Finally, give this projectile some mass */ projectile.SetMassFromShapes(); /* Also, make sure we destroy the projectile when it is time */ this.OnDestroy += Cleanup; }
public TimeOfImpact() { { PolygonDef sd = new PolygonDef(); sd.Density = 0.0f; sd.SetAsBox(0.1f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); bd.Angle = 0.0f; _body1 = _world.CreateBody(bd); _shape1 = _body1.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.25f, 0.25f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(9.6363468f, 28.050615f); bd.Angle = 1.6408679f; _body2 = _world.CreateBody(bd); _shape2 = (PolygonShape)_body2.CreateShape(sd); _body2.SetMassFromShapes(); } }
/// <summary> /// 初期化 /// </summary> /// <param name="shapeType">物理形状タイプ</param> /// <param name="world">登録するワールド</param> public PhysicalTriangleShape(PhysicalShapeType shapeType, PhysicalWorld world) { density = 1.0f; restitution = 0.3f; angle = 0.0f; groupIndex = 0; categoryBits = 0x0001; maskBits = 0xffff; b2BodyDef = new BodyDef(); b2PolygonDef = new PolygonDef(); vertexes = new List <asd.Vector2DF>(); b2PolygonDef.VertexCount = 3; vertexes.Add(new asd.Vector2DF(0, -1)); vertexes.Add(new asd.Vector2DF(1, 0)); vertexes.Add(new asd.Vector2DF(0, 1)); refWorld = world; physicalShapeType = shapeType; b2Body = refWorld.B2World.CreateBody(b2BodyDef); b2Body.CreateFixture(b2PolygonDef); if (physicalShapeType == PhysicalShapeType.Dynamic) { b2Body.SetMassFromShapes(); } world.Add(this); }
public VaryingRestitution() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); } { CircleDef sd = new CircleDef(); sd.Radius = 1.0f; sd.Density = 1.0f; float[] restitution = new float[7] { 0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f }; for (int i = 0; i < 7; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f + 3.0f * i, 20.0f); Body body = _world.CreateBody(bd); sd.Restitution = restitution[i]; body.CreateShape(sd); body.SetMassFromShapes(); } } }
public DistanceTest() { PolygonDef sd = new PolygonDef(); sd.SetAsBox(1.0f, 1.0f); sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body1 = _world.CreateBody(bd); _shape1 = _body1.CreateShape(sd); PolygonDef sd2 = new PolygonDef(); sd2.VertexCount = 3; sd2.Vertices[0].Set(-1.0f, 0.0f); sd2.Vertices[1].Set(1.0f, 0.0f); sd2.Vertices[2].Set(0.0f, 15.0f); sd2.Density = 1.0f; BodyDef bd2 = new BodyDef(); bd2.Position.Set(0.0f, 10.0f); _body2 = _world.CreateBody(bd2); _shape2 = _body2.CreateShape(sd2); _body2.SetMassFromShapes(); _world.Gravity = new Vec2(0.0f, 0.0f); }
/** * Creates a new box object */ /* public BoxObject(World world, Texture2D texture, float density, float friction, float restitution, float myScale, bool isPulley) : base(world) { // Initialize this.texture = texture; scale = myScale; if (isPulley) BodyDef.FixedRotation = true; // Determine dimensions float halfWidth = ((float)texture.Width / (2 * CASSWorld.SCALE)) * scale; float halfHeight = ((float)texture.Height / (2 * CASSWorld.SCALE)) * scale; // Create the collision shape PolygonDef shape = new PolygonDef(); shape.SetAsBox(halfWidth, halfHeight); shape.Density = density; shape.Friction = friction; shape.Restitution = restitution; shapes.Add(shape); } */ public BoxObject(World world, string textureName, float density, float friction, float restitution, float myScale, bool isPulley) : base(world) { // Initialize //Console.WriteLine(textureName); this.texture = GameEngine.TextureList[textureName]; TextureFilename = textureName; Height = texture.Height * myScale; Width = texture.Width * myScale; boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height); scale = myScale; if (isPulley) BodyDef.FixedRotation = true; // Determine dimensions float halfWidth = (float)texture.Width / (2 * CASSWorld.SCALE) * scale ; float halfHeight = (float)texture.Height / (2 * CASSWorld.SCALE) * scale; // Create the collision shape PolygonDef shape = new PolygonDef(); shape.SetAsBox(halfWidth, halfHeight); shape.Density = density; shape.Friction = friction; shape.Restitution = restitution; shape.UserData = textureName; // for DEBUGging purposes shapes.Add(shape); }
public Platform(World world, String texture, Vector2 midPoint, Vector2 size, float angle) { BodyDef bodydef = new BodyDef(); bodydef.Position = midPoint; bodydef.Angle = angle; body = world.CreateBody(bodydef); PolygonDef shapeDef = new PolygonDef(); this.size = size; // SetAsBox expects radius shapeDef.SetAsBox(size.X / 2F, size.Y / 2F); shapeDef.Density = 0.0f; shapeDef.Friction = 1.0f; Texture tex = new Texture(texture); sprite = new Sprite(tex); sprite.Origin = ((Vector2)sprite.Texture.Size) / 2F; sprite.Position = midPoint.PixelCoord; body.CreateShape(shapeDef); body.SetMassFromShapes(); }
public ShapeEditing() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); } BodyDef bodydef = new BodyDef(); bodydef.Position.Set(0.0f, 10.0f); _body = _world.CreateBody(bodydef); PolygonDef sd_ = new PolygonDef(); sd_.SetAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f); sd_.Density = 10.0f; _shape1 = _body.CreateShape(sd_); _body.SetMassFromShapes(); _shape2 = null; }
public Body AddRect(float x, float y, float w, float h, float angle, float density, float friction, float restetution, float mass, bool IsBullet = true, bool IsSensor = false, bool AllowSleep = false, short group_index = 1, Object userDate = null) { BodyDef bDef = new BodyDef(); bDef.Position.Set(x / metric, y / metric); bDef.Angle = angle; bDef.AllowSleep = AllowSleep; bDef.IsBullet = IsBullet; PolygonDef pDef = new PolygonDef(); pDef.Restitution = restetution; pDef.Friction = friction; pDef.Density = density; pDef.SetAsBox(w / metric / 2, h / metric / 2); pDef.Filter.GroupIndex = group_index; pDef.IsSensor = IsSensor; Body body = world.CreateBody(bDef); body.CreateShape(pDef); body.SetMassFromShapes(); float Inertia = body.GetInertia(); MassData md = new MassData(); md.I = Inertia; md.Mass = mass; body.SetMass(md); return(body); }
public TilePolygonObject(int id, float x, float y, TilePolygon polygon) : base(id, x, y) { X = x; Y = y; Polygon = polygon; var bodyDef = new BodyDef { Position = new Vec2(x.ToMeter(), y.ToMeter()), }; Body = Map.World.CreateBody(bodyDef); var dividedPolygon = DividePolygon(polygon.Points); _polygonDefs = dividedPolygon.Select(points => { points = MakePolygonCw(points); var polygonDef = new PolygonDef { VertexCount = points.Count, Vertices = points.Select(point => point.ToVec2()).ToArray() }; polygonDef.Filter.CategoryBits = (ushort)CategoryBits.Ground; polygonDef.Filter.MaskBits = (ushort)MaskBits.Ground; return(polygonDef); }).ToList(); _polygonDefs.ForEach(def => { Body.CreateShape(def); }); Body.SetUserData(this); }
public Prop SpawnProp(PropTemplate template, Vec2 position, bool make_player = false, bool is_trigger = false) { Body physics = null; Shape shape = null; if (template.Image.Height > 0) { physics = LevelContext.Context.PhysicsContext.CreateBody(new BodyDef() { LinearDamping = template.Mass * 10f * template.Friction, FixedRotation = true, Position = position, }); var box = new PolygonDef() { IsSensor = is_trigger, Density = template.CanMove ? 1.0f : 0.0f, Friction = template.Friction }; box.Filter.MaskBits = (ushort)(template.Collides ? 1 : 0); box.SetAsBox(template.PhysicsBounds.X / 2f, template.PhysicsBounds.Y / 2f, new Vec2(template.PhysicsBounds.X / 2, template.PhysicsBounds.Y / 2f), 0.0f); shape = physics.CreateShape(box); physics.SetMassFromShapes(); } var prop = new Prop(template, LevelContext.Context.ActiveLevel, physics, shape); _props.Add(prop); physics?.SetUserData(prop); if (make_player) { LevelContext.Context.Player = prop; } return(prop); }
public MyModel3D AddBox(float x, float y, float w, float h, float density, float friction, float restetution, string texture) { // Создается наша графическая модель MyModel3D model = new MyModel3D(models, x, -y, 0, Environment.CurrentDirectory + "\\" + $@"Assets\{texture}.png" /*PATH_RECT*/, new System.Windows.Size(w, h)); // Необходим для установи позиции, поворота, различных состояний и т.д. Советую поюзать свойства этих объектов BodyDef bDef = new BodyDef(); bDef.Position.Set(x + w, y + h); bDef.Angle = 0; // Наш полигон который описывает вершины PolygonDef pDef = new PolygonDef(); pDef.Restitution = restetution; pDef.Friction = friction; pDef.Density = density; pDef.SetAsBox(w / 2, h / 2); // Создание самого тела Body body = world.CreateBody(bDef); //body.ApplyForce(force, body.GetWorldCenter()); body.CreateShape(pDef); body.SetMassFromShapes(); body.SetUserData(model); // Это отличная функция, она на вход принемает объекты типа object, я ее использовал для того чтобы запихнуть и хранить в ней нашу графическую модель, и в методе step ее доставать и обновлять return(model); }
public override void InitTexture(ref World world, ref Body body, prim.Point point, ref prim.Size size, ref ImageTexture texture, ref MapInterface.ObjectTemplate temp, Graphics graphics, bool isDynamic = false) { Bitmap bitmap = temp.images["default"][0].image; SetPoints(graphics, bitmap, ref texture, point, ref size); BodyDef bodyDef = new BodyDef(); PolygonDef polyBox = new PolygonDef(); PolygonDef polyDef = new PolygonDef(); bodyDef.Position.Set(point.x, point.y); polyDef.Density = 1f; polyBox.Density = 1f; bodyDef.Angle = 0f; polyDef.VertexCount = temp.images["default"][0].collisionVectors.Count; polyBox.SetAsBox(size.w / 2, size.h / 2); List <prim.Point> points = new List <prim.Point>(); foreach (MapInterface.Line line in temp.images["default"][0].collisionVectors) { float nx1 = ((float)line.x1 / bitmap.Width) * (float)size.w; float nx2 = ((float)line.x2 / bitmap.Width) * (float)size.w; float ny1 = ((float)line.y1 / bitmap.Height) * (float)size.h; float ny2 = ((float)line.y2 / bitmap.Height) * (float)size.h; points.Add(new prim.Point(nx1, ny1)); points.Add(new prim.Point(nx2, ny2)); } AddCollisionPoints(ref world, ref temp, bodyDef, polyDef, points, ref body); if (isDynamic) { body.SetMassFromShapes(); } }
public PolyCollision() { _localPoints[0].state = ContactState.ContactRemoved; _localPoints[1].state = ContactState.ContactRemoved; { PolygonDef sd = new PolygonDef(); sd.Vertices[0].Set(-9.0f, -1.1f); sd.Vertices[1].Set(7.0f, -1.1f); sd.Vertices[2].Set(5.0f, -0.9f); sd.Vertices[3].Set(-11.0f, -0.9f); sd.VertexCount = 4; sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body1 = _world.CreateBody(bd); _body1.CreateFixture(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.5f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body2 = _world.CreateBody(bd); _body2.CreateFixture(sd); _body2.SetMassFromShapes(); } _world.Gravity = Vec2.Zero; }
public CCDTest() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(10.0f, 0.2f); sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -0.2f); Body body = _world.CreateBody(bd); body.CreateFixture(sd); sd.SetAsBox(0.2f, 1.0f, new Vec2(0.5f, 1.2f), 0.0f); body.CreateFixture(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(2.0f, 0.1f); sd.Density = 1.0f; sd.Restitution = 0; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); Body body = _world.CreateBody(bd); body.CreateFixture(sd); body.SetMassFromShapes(); body.SetLinearVelocity(new Vec2(0.0f, -100.0f)); body.SetAngularVelocity(Box2DX.Common.Math.Random(-50.0f, 50.0f)); } }
public PolyCollision() { _localPoints[0].state = ContactState.ContactRemoved; _localPoints[1].state = ContactState.ContactRemoved; { PolygonDef sd = new PolygonDef(); sd.Vertices[0].Set(-9.0f, -1.1f); sd.Vertices[1].Set(7.0f, -1.1f); sd.Vertices[2].Set(5.0f, -0.9f); sd.Vertices[3].Set(-11.0f, -0.9f); sd.VertexCount = 4; sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body1 = _world.CreateBody(bd); _body1.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.5f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body2 = _world.CreateBody(bd); _body2.CreateShape(sd); _body2.SetMassFromShapes(); } _world.Gravity = Vec2.Zero; }
/** * Creates a new drawn object */ public InstasteelObject(World world, string textureName, float amountofis, float density, float friction, float restitution, float myScale, bool isPulley) : base(world, blobtexturename, segmenttexturename, blobs) { shapes.Clear(); amountOfInstasteel = amountofis; this.texture = GameEngine.TextureList[textureName]; TextureFilename = textureName; Height = texture.Height * myScale; Width = texture.Width * myScale; boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height); scale = myScale; if (isPulley) BodyDef.FixedRotation = true; // Determine dimensions float halfWidth = (float)texture.Width / (2 * CASSWorld.SCALE) * scale ; float halfHeight = (float)texture.Height / (2 * CASSWorld.SCALE) * scale; // Create the collision shape PolygonDef shape = new PolygonDef(); shape.SetAsBox(halfWidth, halfHeight); shape.Density = density; shape.Friction = friction; shape.Restitution = restitution; shapes.Add(shape); }
private void addPolygon(Body body) { try { if (body.GetUserData() == null) { return; } GizmoComponents myGizmo = (GizmoComponents)body.GetUserData(); PolygonDef polygonDef = new PolygonDef(); polygonDef.Density = myGizmo.Density; polygonDef.Restitution = myGizmo.Restitution; polygonDef.Friction = myGizmo.Friction; for (int i = 0; i < myGizmo.Polygons.Count; i++) { polygonDef.VertexCount = myGizmo.Polygons[i].Count; for (int j = 0; j < myGizmo.Polygons[i].Count; j++) { polygonDef.Vertices[j].Set(myGizmo.Polygons[i][j].X, myGizmo.Polygons[i][j].Y); } body.CreateFixture(polygonDef); } } catch (Exception e) { MessageBox.Show("PhysicalWorld-addBaffleX函数出错"); } }
/// <summary> /// 物理エンジンの初期化を行う /// </summary> public static void Init() { // World生成 AABB worldAABB = new AABB(); worldAABB.LowerBound.Set(-500.0f, -500.0f); worldAABB.UpperBound.Set(500.0f, 500.0f); Vec2 gravity = new Vec2(0.0f, 10.0f); bool doSleep = true; m_world = new World(worldAABB, gravity, doSleep); // Ground作成 BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, 19.2f); Body groundBody = m_world.CreateBody(groundBodyDef); PolygonDef groundShapeDef = new PolygonDef(); groundShapeDef.SetAsBox(50.0f, 10.0f); groundShapeDef.Density = 1.0f; groundShapeDef.Friction = 0.3f; groundBody.CreateFixture(groundShapeDef); return; }
public void Reset() { if (b2Body != null) { refWorld.B2World.DestroyBody(b2Body); b2Body.Dispose(); } b2BodyDef = new BodyDef(); b2PolygonDef = new PolygonDef(); b2BodyDef.Position = PhysicalConvert.Tob2Vector(DrawingArea.Size / 2.0f + DrawingArea.Position); CenterPosition = DrawingArea.Size / 2.0f; b2BodyDef.Angle = Angle / 180.0f * 3.14f; b2PolygonDef.SetAsBox(PhysicalConvert.Tob2Single(DrawingArea.Width) / 2.0f, PhysicalConvert.Tob2Single(DrawingArea.Height) / 2.0f); b2PolygonDef.Density = Density; b2PolygonDef.Restitution = Restitution; b2PolygonDef.Friction = Friction; b2PolygonDef.Filter.GroupIndex = GroupIndex; b2PolygonDef.Filter.CategoryBits = CategoryBits; b2PolygonDef.Filter.MaskBits = MaskBits; b2Body = refWorld.B2World.CreateBody(b2BodyDef); b2Body.CreateFixture(b2PolygonDef); if (physicalShapeType == PhysicalShapeType.Dynamic) { b2Body.SetMassFromShapes(); } }
PolygonDef CreatePolygonDef(asd.Vector2DF vertex1, asd.Vector2DF vertex2, asd.Vector2DF vertex3) { PolygonDef b2PolygonDef = new PolygonDef(); var sortedVertexes = new List <asd.Vector2DF>(); sortedVertexes.Add(vertex1); sortedVertexes.Add(vertex2); sortedVertexes.Add(vertex3); sortedVertexes.Sort((a, b) => a.Degree.CompareTo(b.Degree)); b2PolygonDef.VertexCount = 3; for (int i = 0; i < 3; i++) { b2PolygonDef.Vertices[i] = PhysicalConvert.Tob2Vector(sortedVertexes[i]); } b2PolygonDef.Density = Density; b2PolygonDef.Restitution = Restitution; b2PolygonDef.Friction = Friction; b2PolygonDef.Filter = new FilterData() { GroupIndex = GroupIndex, CategoryBits = CategoryBits, MaskBits = MaskBits }; return(b2PolygonDef); }
public Player CreatePlayer(Texture2D crateTexture) { var crateShapeDef = new PolygonDef(); var cratePhysicsSize = GameUtils.PhysicsVec(new Vector2(crateTexture.Width, crateTexture.Height)); crateShapeDef.Vertices = new Vec2[4]; crateShapeDef.Vertices[0] = new Vec2(-(cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[1] = new Vec2((cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[2] = new Vec2((cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[3] = new Vec2(-(cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2)); crateShapeDef.VertexCount = 4; Logger.Info($"crate size = ({cratePhysicsSize.X},{cratePhysicsSize.Y})"); crateShapeDef.Density = GameData.PlayerDensity; crateShapeDef.Friction = GameData.PlayerFriction; crateShapeDef.Filter.CategoryBits = CollisionCategory.Player; crateShapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Wall | CollisionCategory.Alien | CollisionCategory.AlienProjectile | CollisionCategory.Pickup); var crateBodyDef = new BodyDef(); crateBodyDef.IsBullet = true; var playerPosition = new Vec2(GameData.PlayerStartX, GameData.PlayerStartY); crateBodyDef.Position.Set(playerPosition.X, playerPosition.Y); var crateBody = PhysicsWorld.CreateBody(crateBodyDef); var crateShape = crateBody.CreateShape(crateShapeDef); crateBody.SetMassFromShapes(); var player = new Player(Content, PhysicsWorld, crateTexture, GameWorld, crateShape, crateBody, AnimationFactory, WeaponInventory, FilteredInputListener, GameData, GameUtils); GameWorld.AddGameObject(player); return(player); }
public Projectile(Player creator, float x, float y, float width, float height) { this.creator = creator; this.Dead = false; BodyDef def = new BodyDef(); def.IsBullet = true; def.Position = creator.body.GetPosition() + new Vec2(x, y); projectile = creator.body.GetWorld().CreateBody(def); PolygonDef fixdef = new PolygonDef(); fixdef.Density = 1.0f; fixdef.SetAsBox(width / 2, height / 2); fixdef.Filter.GroupIndex = creator.ID; fix = projectile.CreateFixture(fixdef); fix.Filter.CategoryBits = 0x0004; fix.Filter.MaskBits = 0xFFFF; //fix.UserData = this; fixdef.IsSensor = true; fix2 = projectile.CreateFixture(fixdef); fix2.UserData = this; projectile.SetMassFromShapes(); UpdateTimer.Start(); updateSubscription = new GraphicsManager.Updater(GraphicsManager_Update); GraphicsManager.Update += updateSubscription; }
public Level MakeLevel(ComplexLS level_script) { var bounds = level_script.Read <ComplexLS>("BOUNDS"); Vector2 min, max; min.X = (float)bounds.Read <TokenLS <double> >("LEFT"); min.Y = (float)bounds.Read <TokenLS <double> >("BOTTOM"); max.X = (float)bounds.Read <TokenLS <double> >("RIGHT"); max.Y = (float)bounds.Read <TokenLS <double> >("TOP"); var boundsbody = GameContext.PhysicsContext.CreateBody(new BodyDef() { FixedRotation = true }); var def = new PolygonDef(); float hrangey = (max.Y - min.Y) / 2; float hrangex = (max.X - min.X) / 2; def.SetAsBox(1, hrangey, new Vec2(min.X - 1, hrangey), 0); boundsbody.CreateShape(def); def.SetAsBox(1, hrangey, new Vec2(max.X + 1, hrangey), 0); boundsbody.CreateShape(def); def.SetAsBox(hrangex, 1, new Vec2(hrangex, min.Y - 1), 0); boundsbody.CreateShape(def); def.SetAsBox(hrangex, 1, new Vec2(hrangex, max.Y + 1), 0); boundsbody.CreateShape(def); var lvl = new Level(GameContext, null); lvl.Backdrop = MakeSprite(level_script.Read <ComplexLS>("BACKDROP")); foreach (var sprop in level_script.Read <ComplexLS>("PROPS").ReadAll <ComplexLS>("PROP")) { lvl.Props.SpawnProp(sprop); } return(lvl); }
public InfoBody AddVert(float x, float y, Vec2[] vert, float angle, float density, float friction, float restetution, Bitmap image, object userDate = null) { float max_w = vert[0].X; float max_h = vert[0].Y; for (int i = 0; i < vert.Length; i++) { if (vert[i].X > max_w) { max_w = vert[i].X; } if (vert[i].Y > max_h) { max_h = vert[i].Y; } vert[i].X /= 2; vert[i].Y /= 2; vert[i].X /= metric; vert[i].Y /= metric; } Image g_image = new Image(game); g_image.SetImage(image); g_image.SetWidth(max_w); g_image.SetHeight(max_h); BodyDef bDef = new BodyDef(); bDef.Position.Set(x / metric, y / metric); bDef.Angle = angle; PolygonDef pDef = new PolygonDef(); pDef.Restitution = restetution; pDef.Friction = friction; pDef.Density = density; pDef.SetAsBox(max_w / metric / 2, max_h / metric / 2); pDef.Vertices = vert; Body body = world.CreateBody(bDef); body.CreateShape(pDef); body.SetMassFromShapes(); InfoBody info = new InfoBody(); info.image = g_image; info.body = body; info.userDate = userDate; body.SetUserData(info); return(info); }
/// <summary> /// Creates a projectile starting at the given location with the given rotation in radians /// </summary> /// <param name="name"></param> /// <param name="origin"></param> /// <param name="rotation"></param> /// <returns></returns> public Projectile CreateProjectile(string name, Vec2 origin, float rotation, ProjectileSource source) { var definition = _weaponDefinitions.FirstOrDefault(x => x.Name.ToLower().Equals(name.ToLower())); if (definition == null) { throw new Exception($"No WeaponDefinition found for {name}"); } var texture = _contentManager.Load <Texture2D>(definition.SpriteSheet); var shapeDef = new PolygonDef(); var physicsSize = GameUtils.PhysicsVec(new Vector2(definition.Width, definition.Height)); shapeDef.Vertices = new Vec2[4]; shapeDef.Vertices[0] = new Vec2(-(physicsSize.X / 2), -(physicsSize.Y / 2)); shapeDef.Vertices[1] = new Vec2((physicsSize.X / 2), -(physicsSize.Y / 2)); shapeDef.Vertices[2] = new Vec2((physicsSize.X / 2), (physicsSize.Y / 2)); shapeDef.Vertices[3] = new Vec2(-(physicsSize.X / 2), (physicsSize.Y / 2)); shapeDef.VertexCount = 4; shapeDef.Density = definition.Density; shapeDef.Friction = definition.Friction; //projectiles cannot collide with eachother if (source == ProjectileSource.Player) { shapeDef.Filter.CategoryBits = CollisionCategory.PlayerProjectile; shapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Alien | CollisionCategory.AlienProjectile | CollisionCategory.Wall); } else { shapeDef.Filter.CategoryBits = CollisionCategory.AlienProjectile; shapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Player | CollisionCategory.PlayerProjectile | CollisionCategory.Wall); } var bodyDef = new BodyDef(); bodyDef.IsBullet = true; bodyDef.Position.Set(origin.X, origin.Y); var body = _physicsWorld.CreateBody(bodyDef); var shape = body.CreateShape(shapeDef); body.SetMassFromShapes(); var velocityVector = GameUtils.RotationToVec2((float)(rotation * 180.0f / System.Math.PI)); body.SetLinearVelocity(velocityVector * definition.Velocity); var gameObject = new Projectile(_physicsWorld , definition , texture , shape , body , origin , rotation , GameData , GameUtils); GameWorld.AddGameObject(gameObject); return(gameObject); }
public sObject(byte ini) { Index = 0; Time = 0; body = null; bodyDef = new BodyDef(); polygonDef = new PolygonDef(); centerDis = new double[0]; centerAng = new double[0]; }
private Body CreateBody(BodyDef bodyDef, PolygonDef polygonDef) { var body = world?.CreateBody(bodyDef); body?.SetUserData(bodyDef.UserData); body?.CreateFixture(polygonDef); body?.SetMassFromShapes(); return(body); }
private static ShapeDef CreateBaseRectangleDef(PhysicsSetups setups, SizeF size) { ShapeDef RectangleDef = new PolygonDef(); (RectangleDef as PolygonDef).SetAsBox(size.Width / 2, size.Height / 2); RectangleDef.Restitution = setups.restetution; RectangleDef.Friction = setups.friction; RectangleDef.Density = setups.density; return(RectangleDef); }
public static BodyDefinitionWrapper CreateBodyDefinitionWrapper(PolygonDef fixture, Vector2 position, object userData = null) { var bodyDefinition = new BodyDef(); bodyDefinition.Position.Set(position.X, position.Y); bodyDefinition.FixedRotation = true; var bodyDefinitionWrapper = new BodyDefinitionWrapper(bodyDefinition, fixture, userData); return(bodyDefinitionWrapper); }
public DamageBox(Player creator, float x, float y, float width, float height) : base(creator, x, y) { PolygonDef def = new PolygonDef(); def.SetAsBox(width / 2, height / 2, new Vec2(x, y), 0); def.IsSensor = true; def.UserData = this; fixture = creator.body.CreateFixture(def); fixture.UserData = this; }
/// <summary> /// 初期化 /// </summary> /// <param name="shapeType">物理形状タイプ</param> /// <param name="world">登録するワールド</param> public PhysicsTriangleColliderNode(World world) : base(world) { b2PolygonDef = new PolygonDef(); vertexes = new List <Vector2F>(); vertexes.Add(new Vector2F(0, -1)); vertexes.Add(new Vector2F(1, 0)); vertexes.Add(new Vector2F(0, 1)); b2PolygonDef.Vertices = vertexes.Select(v => v.ToB2Vector()).ToArray(); b2PolygonDef.VertexCount = 3; B2Body = World.B2World.CreateBody(b2BodyDef); B2Body.CreateFixture(b2PolygonDef); }
/// <summary> /// creates an instance of the given alien definition name /// </summary> /// <param name="name"></param> public Alien Create(string name) { if (_alienDefinitions != null) { var definition = _alienDefinitions.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (definition == null) { throw new Exception($"No Alien definition found for name {name}"); } var rand = new Random((int)(DateTime.UtcNow - DateTime.MinValue).Ticks); var texture = _contentManager.Load <Texture2D>(definition.TextureName); var physicsSize = GameUtils.PhysicsVec(new Vector2(texture.Width * definition.Scale, texture.Height * definition.Scale)); var minX = GameData.MaxXDimension * .1; var minY = GameData.MaxYDimension * .1; var maxX = GameData.MaxXDimension - minX; var maxY = GameData.MaxYDimension - minY; var loc = new Vec2(rand.Next((int)minX, (int)maxX), rand.Next((int)minY, (int)maxY)); var shapeDef = new PolygonDef(); shapeDef.Vertices = new Vec2[definition.Vertices.Length]; for (var i = 0; i < definition.Vertices.Length; ++i) { var x = definition.Vertices[i].X * definition.Scale; var y = definition.Vertices[i].Y * definition.Scale; shapeDef.Vertices[i] = GameUtils.PhysicsVec(new Vector2(x, y)); } shapeDef.VertexCount = shapeDef.Vertices.Length; shapeDef.Density = definition.Density; shapeDef.Friction = definition.Friction; shapeDef.Filter.CategoryBits = CollisionCategory.Alien; shapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Wall | CollisionCategory.Player | CollisionCategory.PlayerProjectile | CollisionCategory.Alien); var bodyDef = new BodyDef(); bodyDef.IsBullet = true; bodyDef.Position.Set(loc.X, loc.Y); var body = _physicsWorld.CreateBody(bodyDef); var shape = body.CreateShape(shapeDef); body.SetMassFromShapes(); var healthBar = HealthBarFactory.Create("AlienHealthBar", 100, 5); var gameObject = new Alien(_contentManager, _physicsWorld, GameData, GameUtils, definition, _animationFactory, GameWorld, texture, shape, body, Player, healthBar); GameWorld.AddGameObject(gameObject); return(gameObject); } throw new Exception("Uninitialized definitions"); }
public InGameState() { //position the score display centered at the top ScoreDisplay.CharacterSize = 90; ScoreDisplay.DisplayedString = InGameState.WinCount0.ToString(); float leftSize = ScoreDisplay.GetLocalBounds().Width; ScoreDisplay.DisplayedString += " : "; float midSize = ScoreDisplay.GetLocalBounds().Width - leftSize; ScoreDisplay.DisplayedString += InGameState.WinCount1.ToString(); ScoreDisplay.Origin = new Vector2(leftSize + 0.5f * midSize, 0f); ScoreDisplay.Position = new Vector2(Constants.windowSizeX / 2 , Constants.windowSizeY / 14); ScoreDisplay.Color = new SFML.Graphics.Color(200, 255, 200); AABB aabb = new AABB(); aabb.LowerBound.Set(0.0f, 0.0f); aabb.UpperBound.Set(800, 600/*Constants.worldSizeX * Constants.screenRatio*/); physicsWorld = new World(aabb, new Vec2(0.0f, -9.81f), false); contactManager = Physics.ContactManager.g_contactManager; physicsWorld.SetContactListener(contactManager); // Set new Players and appending dekoHands ResetPlayers(); setDekoFlags(); //0xF0A58A4 groundPolygonAct = new Actors.PolygonActor(physicsWorld, new Vec2(0.0f, 15.0f), 0xFBA58A4, Actors.FunctionType.GradientNoise, 4); BackgroundBackSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundBack)); BackgroundBackSprite.Scale = new Vector2(Constants.windowSizeX / (float)BackgroundBackSprite.TextureRect.Width, Constants.windowSizeY / (float)BackgroundBackSprite.TextureRect.Height);//0.5F * Vector2.One; BackgroundFrontSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundFront)); BackgroundFrontSprite.Scale = BackgroundBackSprite.Scale; //left and right borders of the map BodyDef bodydef = new BodyDef(); bodydef.Position = new Vector2(0,0); bodydef.Angle = 0.0f; PolygonDef box = new PolygonDef(); box.SetAsBox(1f, Constants.worldSizeY); Body leftEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(leftEdge.CreateShape(box)); bodydef.Position = new Vector2(Constants.worldSizeX-1, 0); Body rightEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(rightEdge.CreateShape(box)); bodydef.Position = new Vector2(0, Constants.worldSizeY); box.SetAsBox(Constants.worldSizeX, 1f); Body topEdge = physicsWorld.CreateBody(bodydef); contactManager.addNonLethalShape(topEdge.CreateShape(box)); }
protected override void CreatePhysics(World world, float positionX, float positionY, float friction) { var bodyDef = new BodyDef(); bodyDef.Position.Set(positionX, positionY); this.body = world.CreateBody(bodyDef); var shapeDef = new PolygonDef(); shapeDef.SetAsBox(this.Width, this.Height); shapeDef.Density = this.Density; shapeDef.Friction = friction; shapeDef.Restitution = 0.3f; this.body.CreateShape(shapeDef); this.body.SetMassFromShapes(); }
public Bridge() { Body ground; { PolygonDef sd = new PolygonDef(); sd.ShapeType = ShapeType.e_polygonShape; sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position = new Vector(0, -10); ground = world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.125f); sd.Density = 20.0f; sd.Friction = 0.2f; BodyDef bd = new BodyDef(); bd.BodyType = BodyType.e_dynamicBody; RevoluteJointDef jd = new RevoluteJointDef(); const float numPlanks = 30; Body prevBody = ground; for (float i = 0; i < numPlanks; ++i) { bd.Position = new Vector(-14.5f + i, 5); Body body = world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); Vector anchor = new Vector(-15 + i, 5); jd.Initialize(prevBody, body, anchor); world.CreateJoint(jd); prevBody = body; } Vector anchor2 = new Vector(-15 + numPlanks, 5); jd.Initialize(prevBody, ground, anchor2); world.CreateJoint(jd); } }
public PhysicManager() { // Define the ground body. BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, -10.0f); // Call the body factory which creates the ground box shape. // The body is also added to the world. Body groundBody = world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); // The extents are the half-widths of the box. groundShapeDef.SetAsBox(50.0f, 10.0f); // Add the ground shape to the ground body. groundBody.CreateShape(groundShapeDef); // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.Position.Set(0.0f, 4.0f); Body body = world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox(1.0f, 1.0f); // Set the box density to be non-zero, so it will be dynamic. shapeDef.Density = 1.0f; // Override the default friction. shapeDef.Friction = 0.3f; // Add the shape to the body. body.CreateShape(shapeDef); // Now tell the dynamic body to compute it's mass properties base // on its shape. body.SetMassFromShapes(); // Prepare for simulation. Typically we use a time step of 1/60 of a // second (60Hz) and 10 iterations. This provides a high quality simulation // in most game scenarios. world.SetDebugDraw(new OpenGLDebugDraw()); }
BipedTest() { const float k_restitution = 1.4f; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); Body body = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.Density = 0.0f; sd.Restitution = k_restitution; sd.SetAsBox(0.1f, 10.0f, new Vec2(-10.0f, 0.0f), 0.0f); body.CreateShape(sd); sd.SetAsBox(0.1f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f); body.CreateShape(sd); sd.SetAsBox(0.1f, 10.0f, new Vec2(0.0f, -10.0f), 0.5f * Box2DX.Common.Settings.Pi); body.CreateShape(sd); sd.SetAsBox(0.1f, 10.0f, new Vec2(0.0f, 10.0f), -0.5f * Box2DX.Common.Settings.Pi); body.CreateShape(sd); } _biped = new Biped(_world, new Vec2(0.0f, 20.0f)); for (int i = 0; i < 8; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(5.0f, 20.0f + i); bd.IsBullet = true; Body body = _world.CreateBody(bd); body.SetLinearVelocity(new Vec2(0.0f, -100.0f)); body.SetAngularVelocity(Box2DX.Common.Math.Random(-50.0f, 50.0f)); CircleDef sd = new CircleDef(); sd.Radius = 0.25f; sd.Density = 15.0f; sd.Restitution = k_restitution; body.CreateShape(sd); body.SetMassFromShapes(); } }
public Pulleys() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { float a = 2.0f; float b = 4.0f; float y = 16.0f; float L = 12.0f; PolygonDef sd = new PolygonDef(); sd.SetAsBox(a, b); sd.Density = 5.0f; BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f, y); Body body1 = _world.CreateBody(bd); body1.CreateShape(sd); body1.SetMassFromShapes(); bd.Position.Set(10.0f, y); Body body2 = _world.CreateBody(bd); body2.CreateShape(sd); body2.SetMassFromShapes(); PulleyJointDef pulleyDef = new PulleyJointDef(); Vec2 anchor1 = new Vec2(-10.0f, y + b); Vec2 anchor2 = new Vec2(10.0f, y + b); Vec2 groundAnchor1 = new Vec2(-10.0f, y + b + L); Vec2 groundAnchor2 = new Vec2(10.0f, y + b + L); pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f); _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef); } }
public Buoyancy() { BuoyancyController bc = _bc; _world.AddController(bc); bc.offset = 15; bc.normal.Set(0, 1); bc.density = 2; bc.linearDrag = 2; bc.angularDrag = 1; for (int i = 0; i < 2; ++i) { PolygonDef sd = new PolygonDef(); sd.VertexCount = 3; sd.Vertices[0].Set(-0.5f, 0.0f); sd.Vertices[1].Set(0.5f, 0.0f); sd.Vertices[2].Set(0.0f, 1.5f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(-8.0f + 8.0f * i, 12.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); bc.AddBody(body); } for (int i = 0; i < 3; ++i) { CircleDef sd = new CircleDef(); sd.Radius = 0.5f; sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(-6.0f + 6.0f * i, 10.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); bc.AddBody(body); } }
public Prismatic() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(2.0f, 0.5f); sd.Density = 5.0f; sd.Friction = 0.05f; BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f, 10.0f); bd.Angle = 0.5f * Box2DX.Common.Settings.Pi; Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); PrismaticJointDef pjd = new PrismaticJointDef(); // Bouncy limit pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), new Vec2(1.0f, 0.0f)); // Non-bouncy limit //pjd.Initialize(ground, body, Vec2(-10.0f, 10.0f), Vec2(1.0f, 0.0f)); pjd.MotorSpeed = 10.0f; pjd.MaxMotorForce = 1000.0f; pjd.EnableMotor = true; pjd.LowerTranslation = 0.0f; pjd.UpperTranslation = 20.0f; pjd.EnableLimit = true; _joint = (PrismaticJoint)_world.CreateJoint(pjd); } }
public SimpleTest() { // Define the ground body. BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, -10.0f); // Call the body factory which creates the ground box shape. // The body is also added to the world. Body groundBody = _world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); // The extents are the half-widths of the box. groundShapeDef.SetAsBox(50.0f, 10.0f); // Add the ground shape to the ground body. groundBody.CreateShape(groundShapeDef); for (int i = 0; i < 1; i++) { // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.Position.Set(0.0f, 4.0f *(i+1)); Body body = _world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox(1.0f, 1.0f); // Set the box density to be non-zero, so it will be dynamic. shapeDef.Density = 1.0f; // Override the default friction. shapeDef.Friction = 0.3f; // Add the shape to the body. body.CreateShape(shapeDef); // Now tell the dynamic body to compute it's mass properties base // on its shape. body.SetMassFromShapes(); } }
public Revolute() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { CircleDef sd = new CircleDef(); sd.Radius = 0.5f; sd.Density = 5.0f; BodyDef bd = new BodyDef(); RevoluteJointDef rjd = new RevoluteJointDef(); bd.Position.Set(0.0f, 20.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); float w = 100.0f; body.SetAngularVelocity(w); body.SetLinearVelocity(new Vec2(-8.0f * w, 0.0f)); rjd.Initialize(ground, body, new Vec2(0.0f, 12.0f)); rjd.MotorSpeed = 1.0f * Box2DX.Common.Settings.Pi; rjd.MaxMotorTorque = 10000.0f; rjd.EnableMotor = false; rjd.LowerAngle = -0.25f * Box2DX.Common.Settings.Pi; rjd.UpperAngle = 0.5f * Box2DX.Common.Settings.Pi; rjd.EnableLimit = true; rjd.CollideConnected = true; _joint = (RevoluteJoint)_world.CreateJoint(rjd); } }
public HackObject(World world, int myWidth, int myHeight, int myX, int myY, float myFriction) : base(world) { width = myWidth; height = myHeight; x = myX; y = myY; float halfWidth = width / (2 * CASSWorld.SCALE); float halfHeight = height / (2 * CASSWorld.SCALE); PolygonDef shape = new PolygonDef(); shape.SetAsBox(halfWidth, halfHeight); shape.Friction = myFriction; shapes.Add(shape); BodyDef.Position.X = x; BodyDef.Position.Y = y; }
public Bridge() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.125f); sd.Density = 20.0f; sd.Friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); const int numPlanks = 30; Body prevBody = ground; for (int i = 0; i < numPlanks; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(-14.5f + 1.0f * i, 5.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); prevBody = body; } Vec2 anchor_ = new Vec2(-15.0f + 1.0f * numPlanks, 5.0f); jd.Initialize(prevBody, ground, anchor_); _world.CreateJoint(jd); } }
public VerticalStack() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f, new Vec2(0.0f, -10.0f), 0.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 0.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); sd.SetAsBox(0.1f, 10.0f, new Vec2(20.0f, 10.0f), 0.0f); ground.CreateShape(sd); } float[] xs = new float[5] { 0.0f, -10.0f, -5.0f, 5.0f, 10.0f }; for (int j = 0; j < 5; ++j) { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.5f); sd.Density = 1.0f; sd.Friction = 0.3f; for (int i = 0; i < 12; ++i) { BodyDef bd = new BodyDef(); // For this test we are using continuous physics for all boxes. // This is a stress test, you normally wouldn't do this for // performance reasons. bd.AllowSleep = true; bd.Position.Set(xs[j], 0.752f + 1.54f * i); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); } } _bullet = null; }
public Pyramid() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); float a = 0.5f; sd.SetAsBox(a, a); sd.Density = 5.0f; Vec2 x = new Vec2(-10.0f, 0.75f); Vec2 y; Vec2 deltaX = new Vec2(0.5625f, 2.0f); Vec2 deltaY = new Vec2(1.125f, 0.0f); for (int i = 0; i < 25; ++i) { y = x; for (int j = i; j < 25; ++j) { BodyDef bd = new BodyDef(); bd.Position = y; Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); y += deltaY; } x += deltaX; } } }
public Chain() { Body ground = null; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.6f, 0.125f); sd.Density = 20.0f; sd.Friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); jd.CollideConnected = false; const float y = 25.0f; Body prevBody = ground; for (int i = 0; i < 30; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(0.5f + i, y); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); Vec2 anchor = new Vec2(i, y); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); prevBody = body; } } }
public static void CreateMound(World world, float x, float y, float friction, float restitution) { // ==== Mound ==== BodyDef moundBodyDef = new BodyDef(); moundBodyDef.Position.Set(x, y); // Mound body. Body moundBody = world.CreateBody(moundBodyDef); // Define shape. PolygonDef moundShapeDef = new PolygonDef(); moundShapeDef.VertexCount = 3; moundShapeDef.Vertices[0].Set(0.20f, 0f); moundShapeDef.Vertices[1].Set(0f, 0.025f); moundShapeDef.Vertices[2].Set(-0.20f, 0f); moundShapeDef.Friction = friction; moundShapeDef.Restitution = restitution; moundShapeDef.Filter.CategoryBits = 0x3; // Add shape to body. moundBody.CreateShape(moundShapeDef); }
public RaycastTest() { Body ground = null; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); ground.CreateShape(sd); } { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 1.0f); laserBody = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(5.0f, 1.0f); sd.Density = 4.0f; laserBody.CreateShape(sd); laserBody.SetMassFromShapes(); Body body; //Create a few shapes bd.Position.Set(-5.0f, 10.0f); body = _world.CreateBody(bd); CircleDef cd = new CircleDef(); cd.Radius = 3; body.CreateShape(cd); bd.Position.Set(5.0f, 10.0f); body = _world.CreateBody(bd); body.CreateShape(cd); } }
/** * Creates a new box object */ public MovingObject(World world, string texturename, float density, float friction, float restitution, float myScale, bool isPulley, SwitchObject mySwitch, Vector2 myForce, float bound1, float bound2) : base(world) { BodyDef.FixedRotation = true; texture = GameEngine.TextureList[texturename]; TextureFilename = texturename; Height = texture.Height * myScale; Width = texture.Width * myScale; boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height); this.bound1 = bound1; this.bound2 = bound2; this.myForce = myForce; this.mySwitch = mySwitch; BodyDef.IsBullet = true; // Initialize this.texture = texture; scale = myScale; if (isPulley) BodyDef.FixedRotation = true; // Determine dimensions float halfWidth = ((float)texture.Width / (2 * CASSWorld.SCALE)) * scale; float halfHeight = ((float)texture.Height / (2 * CASSWorld.SCALE)) * scale; // Create the collision shape PolygonDef shape = new PolygonDef(); shape.SetAsBox(halfWidth, halfHeight); shape.Density = density; shape.Friction = friction; shape.Restitution = restitution; shapes.Add(shape); isMoving = false; }
public LineJoint() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 2.0f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 7.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); LineJointDef jd = new LineJointDef(); Vec2 axis = new Vec2(2.0f, 1.0f); axis.Normalize(); jd.Initialize(ground, body, new Vec2(0.0f, 8.5f), axis); jd.motorSpeed = 0.0f; jd.maxMotorForce = 100.0f; jd.enableMotor = true; jd.lowerTranslation = -4.0f; jd.upperTranslation = 4.0f; jd.enableLimit = true; _world.CreateJoint(jd); } }
Body CreateBox(float x,float y,float hwidth,float hheight,bool isStatic) { BodyDef groundBodyDef = new BodyDef (); groundBodyDef.Position.Set (x, y); groundBodyDef.FixedRotation = true; Body body = world.CreateBody (groundBodyDef); PolygonDef groundBox = new PolygonDef (); groundBox.SetAsBox (hwidth, hheight); Fixture fixture = body.CreateFixture (groundBox); // if (isStatic == false) { fixture.Density = 1; fixture.Friction = 0.3f; body.SetMassFromShapes (); // } if(isStatic) body.SetStatic (); Box box = new Box(body, hwidth, hheight); boxList.Add(box); return body; }
public SensorTest() { { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); ground.CreateShape(sd); CircleDef cd = new CircleDef(); cd.IsSensor = true; cd.Radius = 5.0f; cd.LocalPosition.Set(0.0f, 20.0f); _sensor = ground.CreateShape(cd); } { CircleDef sd = new CircleDef(); sd.Radius = 1.0f; sd.Density = 1.0f; for (int i = 0; i < 7; ++i) { BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f + 3.0f * i, 20.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); } } }
public SliderCrank() { Body ground = null; { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); ground.CreateShape(sd); } { // Define crank. PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 2.0f); sd.Density = 1.0f; RevoluteJointDef rjd = new RevoluteJointDef(); Body prevBody = ground; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 7.0f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f)); rjd.MotorSpeed = 1.0f * Box2DX.Common.Settings.Pi; rjd.MaxMotorTorque = 10000.0f; rjd.EnableMotor = true; _joint1 = (RevoluteJoint)_world.CreateJoint(rjd); prevBody = body; // Define follower. sd.SetAsBox(0.5f, 4.0f); bd.Position.Set(0.0f, 13.0f); body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); rjd.Initialize(prevBody, body, new Vec2(0.0f, 9.0f)); rjd.EnableMotor = false; _world.CreateJoint(rjd); prevBody = body; // Define piston sd.SetAsBox(1.5f, 1.5f); bd.Position.Set(0.0f, 17.0f); body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); rjd.Initialize(prevBody, body, new Vec2(0.0f, 17.0f)); _world.CreateJoint(rjd); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.Initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f)); pjd.MaxMotorForce = 1000.0f; pjd.EnableMotor = true; _joint2 = (PrismaticJoint)_world.CreateJoint(pjd); // Create a payload sd.Density = 2.0f; bd.Position.Set(0.0f, 23.0f); body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); } }