public static Body CreateRectangle(World world, float width, float height, float density, Vector2 position, PressPlay.FFWD.Component userData) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be more than 0 meters"); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be more than 0 meters"); } Body newBody = CreateBody(world, position); Vertices rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2); PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density); newBody.CreateFixture(rectangleShape, userData); return(newBody); }
/// <summary> /// Creates a capsule. /// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices) /// </summary> /// <returns></returns> public static Body CreateCapsule(World world, float height, float topRadius, int topEdges, float bottomRadius, int bottomEdges, float density, Vector2 position, object userData) { Vertices verts = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges); Body body; //There are too many vertices in the capsule. We decompose it. if (verts.Count >= Settings.MaxPolygonVertices) { List <Vertices> vertList = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Earclip); body = CreateCompoundPolygon(world, vertList, density, userData); body.Position = position; return(body); } body = CreatePolygon(world, verts, density, userData); body.Position = position; return(body); }
public override void LoadContent() { Vertices rect1 = PolygonTools.CreateRectangle(2, 2); Vertices rect2 = PolygonTools.CreateRectangle(2, 2); Vector2 trans = new Vector2(-2, 0); rect1.Translate(ref trans); trans = new Vector2(2, 0); rect2.Translate(ref trans); List <Vertices> vertices = new List <Vertices>(2); vertices.Add(rect1); vertices.Add(rect2); _rectangles = BodyFactory.CreateCompoundPolygon(World, vertices, 1); _rectangles.BodyType = BodyType.Dynamic; base.LoadContent(); }
public static PhysicsObject createFromTexture(Texture2D texture, float density = 1f) { int width = texture.Width; int height = texture.Height; uint[] data = new uint[texture.Width * texture.Height]; texture.GetData <uint>(data); PhysicsObject obj = new PhysicsObject(); for (int i = 0; i < Main.objects.Length; i++) { if (Main.objects[i].active == false) { Main.objects[i] = obj; obj.whoAmI = i; break; } } Vertices verts = PolygonTools.CreatePolygon(data, width, false); Vector2 centroid = -verts.GetCentroid(); obj.origin = Vector2.Zero;// centroid / 2; verts = SimplifyTools.ReduceByDistance(verts, 4f); List <Vertices> list = BayazitDecomposer.ConvexPartition(verts); Vector2 vScale = new Vector2(ConvertUnits.ToSimUnits(1)); foreach (Vertices v in list) { v.Scale(ref vScale); } obj.body = BodyFactory.CreateCompoundPolygon(Main.physicsWorld, list, 1f); obj.shape = 3; obj.active = true; obj.dwidth = width; obj.dheight = height; obj.body.BodyType = BodyType.Dynamic; Debug.print("New texture body with " + verts.Count + " vertices successfully added"); return(obj); }
private BuoyancyTest() { //Make a box //Bottom Body ground = BodyFactory.CreateBody(World); Vertices edge = PolygonTools.CreateEdge(new Vector2(0.0f, 0.0f), new Vector2(40.0f, 0.0f)); PolygonShape shape = new PolygonShape(edge); ground.CreateFixture(shape); //Left side shape.Set(PolygonTools.CreateEdge(new Vector2(0.0f, 0.0f), new Vector2(00.0f, 15.0f))); ground.CreateFixture(shape); //Right side shape.Set(PolygonTools.CreateEdge(new Vector2(40.0f, 0.0f), new Vector2(40.0f, 15.0f))); ground.CreateFixture(shape); //Buoyancy controller _aabbContainer = new AABBFluidContainer(new Vector2(0, 0), 40, 10); _waveContainer = new WaveContainer(new Vector2(0, 0), 40, 10); _waveContainer.WaveGeneratorStep = 0; FluidDragController buoyancyController = new FluidDragController(_waveContainer, 4f, 0.98f, 0.2f, World.Gravity); buoyancyController.Entry += EntryEventHandler; Vector2 offset = new Vector2(5, 0); //Bunch of balls for (int i = 0; i < 4; i++) { Fixture fixture = FixtureFactory.CreateCircle(World, 1, 1, new Vector2(15, 1) + offset * i); fixture.Body.BodyType = BodyType.Dynamic; buoyancyController.AddGeom(fixture); } World.Add(buoyancyController); }
public TexturePolygon(Texture2D texture, Vector2 position, float rotation, DrawingHelper.DrawingLevel drawingLevel, Game game, World world, bool textureCenter) : base(texture, position, drawingLevel, game, world) { // Fetch Texure data uint[] data = new uint[texture.Width * texture.Height]; texture.GetData(data); Vertices textureVertices = PolygonTools.CreatePolygon(data, texture.Width, true); // Set Polygon Centroid if (textureCenter) // Texture based center { Vector2 centroid = -textureVertices.GetCentroid(); textureVertices.Translate(ref centroid); Origin = new Vector2(texture.Width / 2, texture.Height / 2); } else // Vertice based center { Vector2 centroid = -textureVertices.GetCentroid(); textureVertices.Translate(ref centroid); Origin = new Vector2(-centroid.X, -centroid.Y); } // Simplify Polygon for performance textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f); List <Vertices> list = Triangulate.ConvexPartition(textureVertices, TriangulationAlgorithm.Bayazit); // Convert polygon to sim units Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)); foreach (Vertices vertice in list) { vertice.Scale(ref vertScale); } // create body compound. RigidBody = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Static); RigidBody.BodyType = BodyType.Static; RigidBody.Position = ConvertUnits.ToSimUnits(position); RigidBody.Rotation = rotation; }
private ExplosionTest() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); float[] xs = { -10.0f, -5.0f, 0.0f, 5.0f, 10.0f }; for (int j = 0; j < ColumnCount; ++j) { PolygonShape shape = new PolygonShape(1); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 0.5f); for (int i = 0; i < RowCount; ++i) { int n = j * RowCount + i; Debug.Assert(n < RowCount * ColumnCount); _indices[n] = n; const float x = 0.0f; Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(xs[j] + x, 0.752f + 1.54f * i); body.UserData = _indices[n]; _bodies[n] = body; Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.3f; //First column is unaffected by the explosion if (j == 0) { body.PhysicsLogicFilter.IgnorePhysicsLogic(PhysicsLogicType.Explosion); } } } _radius = 5; _force = 3; _realExplosion = new RealExplosion(World); }
public Pyramid(World world, ScreenManager screenManager, Vector2 position, int count, float density) { _batch = screenManager.SpriteBatch; Vertices rect = PolygonTools.CreateRectangle(1f / 2f, 1f / 2f); PolygonShape shape = new PolygonShape(rect, density); Vector2 rowStart = position; rowStart.Y -= 0.5f + count * 1.1f; Vector2 deltaRow = new Vector2(-0.625f, -1.1f); const float spacing = 1.25f; _boxes = new List <Body>(); for (int i = 0; i < count; ++i) { Vector2 pos = rowStart; for (int j = 0; j < i + 1; ++j) { Body body = world.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = pos; body.CreateFixture(shape); _boxes.Add(body); pos.X += spacing; } rowStart += deltaRow; } //GFX AssetCreator creator = screenManager.Assets; _box = new Sprite(creator.TextureFromVertices(rect, MaterialType.Dots, Color.SaddleBrown, 2f, 24f)); }
// Anything that is a fixture must have an object type for collision logic purposes /*public enum ObjectTypes * { * Bot = 1, * Weapon = 2, // Since weapons will be fixtures, they too need an object type. * Bullet = 3, * Wall = 4 * }*/ public SPRWorld(World world, int currentLevel) { int botHalfWidth = 31; // Half the bot's width (e.g. the distance from the centroid to the edge) m_World = world; this.m_Entities = new SortedDictionary <ulong, Entity>(); // Make polygons out of weapons and anything that needs collision. // NOTE: Stores the convex hull for each item, since collision detection // relies upon these verticies being convex polygons. String[] toPreload = new String[] { "Gun", "Axe", "Shield" }; foreach (String texture in toPreload) { Texture2D a = TextureStatic.Get(texture); uint[] data = new uint[a.Width * a.Height]; a.GetData <uint>(data); Vertices v = Melkman.GetConvexHull(PolygonTools.CreatePolygon(data, a.Width, a.Height)); Vector2 scale = new Vector2(Settings.MetersPerPixel, Settings.MetersPerPixel); v.Scale(ref scale); if (!computedSpritePolygons.ContainsKey(texture)) { computedSpritePolygons.Add(texture, v); } } //walls Vector2[] outer = { new Vector2(0, 0) * Settings.MetersPerPixel, new Vector2(0, 1920) * Settings.MetersPerPixel, new Vector2(1080, 1920) * Settings.MetersPerPixel, new Vector2(1080, 0) * Settings.MetersPerPixel }; Vector2[] inner = { new Vector2(200, 200) * Settings.MetersPerPixel, new Vector2(200, 1720) * Settings.MetersPerPixel, new Vector2(880, 1720) * Settings.MetersPerPixel, new Vector2(880, 200) * Settings.MetersPerPixel }; FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 100) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static; FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(100, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static; FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 980) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static; FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(1820, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static; Vector2[] edges = { new Vector2(-botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, botHalfWidth) * Settings.MetersPerPixel, new Vector2(-botHalfWidth, botHalfWidth) * Settings.MetersPerPixel }; this.battle = new Battle(this, botHalfWidth, world, currentLevel); GameWorld.audio.SongPlay("sprbattle"); }
public override void Initialize() { if (_world != null) { var borders = new List <Vertices>(); var bottom = PolygonTools.CreateRectangle( ConvertUnits.ToSimUnits(_screenBounds.Width), 0.01f, new Vector2(ConvertUnits.ToSimUnits(_screenBounds.Width / 2f), ConvertUnits.ToSimUnits(_screenBounds.Height + _ball.Texture.Height)), 0); var left = PolygonTools.CreateRectangle( 0.01f, ConvertUnits.ToSimUnits(_screenBounds.Height), new Vector2(0, ConvertUnits.ToSimUnits(_screenBounds.Height / 2f)), 0); var top = PolygonTools.CreateRectangle( ConvertUnits.ToSimUnits(_screenBounds.Width), 0.01f, new Vector2(ConvertUnits.ToSimUnits(_screenBounds.Width / 2f), 0), 0); var right = PolygonTools.CreateRectangle( 0.01f, ConvertUnits.ToSimUnits(_screenBounds.Height), new Vector2(ConvertUnits.ToSimUnits(_screenBounds.Width), ConvertUnits.ToSimUnits(_screenBounds.Height / 2f)), 0); borders.AddRange(new[] { bottom, left, top, right }); Body = BodyFactory.CreateCompoundPolygon(_world, borders, 1, 1); foreach (var fixture in Body.FixtureList) { fixture.Restitution = 1; fixture.Friction = 0; } // Paddle should not bounce off walls _paddle.Body.IgnoreCollisionWith(Body); } base.Initialize(); }
public static List <Fixture> AttachLineArc(float radians, int sides, float radius, Vector2 position, float angle, bool closed, Body body) { Vertices arc = PolygonTools.CreateArc(radians, sides, radius); arc.Rotate((MathHelper.Pi - radians) / 2 + angle); arc.Translate(ref position); List <Fixture> fixtures = new List <Fixture>(arc.Count); if (closed) { fixtures.Add(AttachLoopShape(arc, body)); } for (int i = 1; i < arc.Count; i++) { fixtures.Add(AttachEdge(arc[i], arc[i - 1], body)); } return(fixtures); }
private static Body CreateShip() { var body = new Body(world); body.BodyType = BodyType.Dynamic; body.LinearDamping = 0.5f; body.AngularDamping = 1f; // tip var rect1 = new PolygonShape(PolygonTools.CreateRectangle(0.25f, 0.5f, new Vector2(0, -0.5f), 0), 1); // tail var rect2 = new PolygonShape(PolygonTools.CreateRectangle(0.75f, 0.5f, new Vector2(0, 0.5f), 0), 3); body.CreateFixture(rect1); body.CreateFixture(rect2); body.OnCollision += (a, b, contact) => { var hit = false; var linear = a.Body.LinearVelocity - b.Body.LinearVelocity; var angular = a.Body.AngularVelocity - b.Body.AngularVelocity; if (Math.Abs(linear.X) > 4 || Math.Abs(linear.Y) > 4) { Console.WriteLine("OW linear" + DateTime.Now.Millisecond); hit = true; } if (!hit && Math.Abs(angular) > 4) { Console.WriteLine("OW angular" + DateTime.Now.Millisecond); } return(true); }; return(body); }
protected override Body CreatePhysics() { var newBody = BodyFactory.CreateBody(World, new Vector2()); if (density != 0) { newBody.BodyType = BodyType.Dynamic; } var rectangleVertices = PolygonTools.CreateEllipse(width / 2, height / 2, 20); //var rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2); var rectangleShape = new PolygonShape(rectangleVertices, density); var fixture = newBody.CreateFixture(rectangleShape); newBody.FixedRotation = true; fixture.Friction = friction; fixture.Restitution = restitution; SensorFixture = FixtureFactory.AttachRectangle(sensorWidth, 0.05f, 1f, sensorCenter, newBody, sensorName); SensorFixture.IsSensor = true; return(newBody); }
/// <summary> /// Creates a chain. /// </summary> /// <param name="world">The world.</param> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="linkWidth">The width.</param> /// <param name="linkHeight">The height.</param> /// <param name="numberOfLinks">The number of links.</param> /// <param name="linkDensity">The link density.</param> /// <returns></returns> public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight, int numberOfLinks, float linkDensity) { //Chain start / end Path path = new Path(); path.Add(start); path.Add(end); //A single chainlink PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity); //Use PathManager to create all the chainlinks based on the chainlink created before. List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic, numberOfLinks); //Attach all the chainlinks together with a revolute joint PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight), new Vector2(0, linkHeight), false, false); return(path); }
public override void Initialize() { base.Initialize(); //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); //Load texture that will represent the physics body Texture2D polygonTexture = GameInstance.Content.Load <Texture2D>("Texture"); //Create an array to hold the data from the texture uint[] data = new uint[polygonTexture.Width * polygonTexture.Height]; //Transfer the texture data to the array polygonTexture.GetData(data); //Find the vertices that makes up the outline of the shape in the texture Vertices verts = PolygonTools.CreatePolygon(data, polygonTexture.Width); //For now we need to scale the vertices (result is in pixels, we use meters) Vector2 scale = new Vector2(0.07f, -0.07f); verts.Scale(ref scale); //We also need to move the polygon so that (0,0) is the center of the polygon. Vector2 centroid = -verts.GetCentroid(); verts.Translate(ref centroid); _sw.Start(); //Create a single body with multiple fixtures Body compund = BodyFactory.CreateCompoundPolygon(World, Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Earclip), 1); compund.BodyType = BodyType.Dynamic; compund.Position = new Vector2(0, 20); _sw.Stop(); }
private void CreateWorld(World world) { world.Gravity = Vector2.Zero; List <Vertices> borders = new List <Vertices>(4); const float borderThickness = 0.1f; const float width = 16f; const float height = 9f; //Top borders.Add(PolygonTools.CreateRectangle(width, borderThickness, new Vector2(width, height), 0)); //Bottom borders.Add(PolygonTools.CreateRectangle(width, borderThickness, new Vector2(width, -height), 0)); //Left borders.Add(PolygonTools.CreateRectangle(borderThickness, height, new Vector2(0, 0), 0)); //Right borders.Add(PolygonTools.CreateRectangle(borderThickness, height, new Vector2(width + width, 0), 0)); Body body = world.CreateCompoundPolygon(borders, 1, new Vector2(0, 0)); foreach (Fixture fixture in body.FixtureList) { fixture.Restitution = 1f; fixture.Friction = 0; } Body circle = world.CreateBody(); circle.BodyType = BodyType.Dynamic; Fixture cfixture = circle.CreateCircle(0.32f, 1); cfixture.Restitution = 1f; cfixture.Friction = 0; circle.ApplyLinearImpulse(new Vector2(200, 50)); }
/// <summary> /// Creates a chain. /// </summary> /// <param name="world">The world.</param> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="linkWidth">The width.</param> /// <param name="linkHeight">The height.</param> /// <param name="fixStart">if set to <c>true</c> [fix start].</param> /// <param name="fixEnd">if set to <c>true</c> [fix end].</param> /// <param name="numberOfLinks">The number of links.</param> /// <param name="linkDensity">The link density.</param> /// <returns></returns> public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight, bool fixStart, bool fixEnd, int numberOfLinks, float linkDensity) { //Chain start / end Path path = new Path(); path.Add(start); path.Add(end); //A single chainlink PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity); //Use PathManager to create all the chainlinks based on the chainlink created before. List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic, numberOfLinks); if (fixStart) { //Fix the first chainlink to the world JointFactory.CreateFixedRevoluteJoint(world, chainLinks[0], new Vector2(0, -(linkHeight / 2)), chainLinks[0].Position); } if (fixEnd) { //Fix the last chainlink to the world JointFactory.CreateFixedRevoluteJoint(world, chainLinks[chainLinks.Count - 1], new Vector2(0, (linkHeight / 2)), chainLinks[chainLinks.Count - 1].Position); } //Attach all the chainlinks together with a revolute joint PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight), new Vector2(0, linkHeight), false, false); return(path); }
public Pyramid(World world, ScreenManager screenManager, Vector2 position, int count, float density) { _batch = screenManager.SpriteBatch; Vertices rect = PolygonTools.CreateRectangle(width / 2f, width / 2f); PolygonShape shape = new PolygonShape(rect, density); Vector2 rowStart = position; rowStart.Y -= 0.5f + count * width * 1.1f; Vector2 deltaRow = new Vector2(-(width + 0.2f) / 2f, width + 0.1f); float spacing = width + 0.5f; _boxes = new List <Body>(); for (int i = 0; i < count; ++i) { Vector2 pos = rowStart; for (int j = 0; j < i + 1; ++j) { Body body = BodyFactory.CreateBody(world); body.BodyType = BodyType.Dynamic; body.Position = pos; body.CreateFixture(shape); _boxes.Add(body); pos.X += spacing; } rowStart += deltaRow; } tex = screenManager.Content.Load <Texture2D>("Assets/Box"); //GFX _box = new Sprite(tex); }
public Agent(World world, ScreenManager screenManager, Vector2 position) { _batch = screenManager.SpriteBatch; _collidesWith = Category.All; _collisionCategories = Category.All; _agentBody = world.CreateBody(position); _agentBody.BodyType = BodyType.Dynamic; //Center _agentBody.CreateCircle(0.5f, 0.5f); //Left arm _agentBody.CreateRectangle(1.5f, 0.4f, 1f, new Vector2(-1f, 0f)); _agentBody.CreateCircle(0.5f, 0.5f, new Vector2(-2f, 0f)); //Right arm _agentBody.CreateRectangle(1.5f, 0.4f, 1f, new Vector2(1f, 0f)); _agentBody.CreateCircle(0.5f, 0.5f, new Vector2(2f, 0f)); //Top arm _agentBody.CreateRectangle(0.4f, 1.5f, 1f, new Vector2(0f, 1f)); _agentBody.CreateCircle(0.5f, 0.5f, new Vector2(0f, 2f)); //Bottom arm _agentBody.CreateRectangle(0.4f, 1.5f, 1f, new Vector2(0f, -1f)); _agentBody.CreateCircle(0.5f, 0.5f, new Vector2(0f, -2f)); //Bottom armTEST //_agentBody.CreateRectangle(0.4f, 1.5f, 1f, new Vector2(0f, -3f)); _agentBody.CreateCircle(0.5f, 0.5f, new Vector2(0f, -4f)); //GFX _box = new Sprite(screenManager.Assets.TextureFromVertices(PolygonTools.CreateRectangle(2.5f / 2f, 0.4f / 2f), MaterialType.Blank, Color.White, 1f, 24f)); _knob = new Sprite(screenManager.Assets.CircleTexture(1.5f, MaterialType.Waves, Color.White, 1f, 24f)); _offset = (2f); }
CirclePenetrationTest() { World.Gravity = Vector2.Zero; List <Vertices> borders = new List <Vertices>(4); const double borderWidth = 0.2f; const double width = 40f; const double height = 25f; //Bottom borders.Add(PolygonTools.CreateRectangle(width, borderWidth, new Vector2(0, height), 0)); //Left borders.Add(PolygonTools.CreateRectangle(borderWidth, height, new Vector2(-width, 0), 0)); //Top borders.Add(PolygonTools.CreateRectangle(width, borderWidth, new Vector2(0, -height), 0)); //Right borders.Add(PolygonTools.CreateRectangle(borderWidth, height, new Vector2(width, 0), 0)); Body body = BodyFactory.CreateCompoundPolygon(World, borders, 1, new Vector2(0, 20)); foreach (Fixture fixture in body.FixtureList) { fixture.Restitution = 1f; fixture.Friction = 0; } Body circle = BodyFactory.CreateCircle(World, 0.32f, 1); circle.BodyType = BodyType.Dynamic; circle.Restitution = 1f; circle.Friction = 0; circle.ApplyLinearImpulse(new Vector2(200, 50)); }
/// <summary> /// Creates a chain. /// </summary> /// <param name="world">The world.</param> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="linkWidth">The width.</param> /// <param name="linkHeight">The height.</param> /// <param name="numberOfLinks">The number of links.</param> /// <param name="linkDensity">The link density.</param> /// <param name="attachRopeJoint">Creates a rope joint between start and end. This enforces the length of the rope. Said in another way: it makes the rope less bouncy.</param> /// <returns></returns> public Path CreateChain(Vector2 start, Vector2 end, float linkWidth, float linkHeight, int numberOfLinks, float linkDensity, bool attachRopeJoint) { System.Diagnostics.Debug.Assert(numberOfLinks >= 2); //Chain start / end Path path = new Path(); path.Add(start); path.Add(end); //A single chainlink PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity); //Use PathManager to create all the chainlinks based on the chainlink created before. List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(this, path, shape, BodyType.Dynamic, numberOfLinks); //TODO //if (fixStart) //{ // //Fix the first chainlink to the world // JointFactory.CreateFixedRevoluteJoint(this, chainLinks[0], new Vector2(0, -(linkHeight / 2)), // chainLinks[0].Position); //} //if (fixEnd) //{ // //Fix the last chainlink to the world // JointFactory.CreateFixedRevoluteJoint(this, chainLinks[chainLinks.Count - 1], // new Vector2(0, (linkHeight / 2)), // chainLinks[chainLinks.Count - 1].Position); //} //Attach all the chainlinks together with a revolute joint PathManager.AttachBodiesWithRevoluteJoint(this, chainLinks, new Vector2(0, -linkHeight), new Vector2(0, linkHeight), false, false); if (attachRopeJoint) { JointFactory.CreateRopeJoint(this, chainLinks[0], chainLinks[^ 1], Vector2.Zero, Vector2.Zero);
public static List <Fixture> CreateCapsule(World world, float height, float endRadius, float density) { //Create the middle rectangle Vertices rectangle = PolygonTools.CreateRectangle(endRadius, height / 2); List <Vertices> list = new List <Vertices>(); list.Add(rectangle); List <Fixture> fixtures = CreateCompoundPolygon(world, list, density); //Create the two circles CircleShape topCircle = new CircleShape(endRadius, density); topCircle.Position = new Vector2(0, height / 2); fixtures.Add(fixtures[0].Body.CreateFixture(topCircle)); CircleShape bottomCircle = new CircleShape(endRadius, density); bottomCircle.Position = new Vector2(0, -(height / 2)); fixtures.Add(fixtures[0].Body.CreateFixture(bottomCircle)); return(fixtures); }
public static Body CreateRectangle(World world, float width, float height, float density, Vector2 position = new Vector2(), float rotation = 0, BodyType bodyType = BodyType.Static, object userData = null) { if (width <= 0) { throw new ArgumentOutOfRangeException(nameof(width), "Width must be more than 0 meters"); } if (height <= 0) { throw new ArgumentOutOfRangeException(nameof(height), "Height must be more than 0 meters"); } var newBody = CreateBody(world, position, rotation, bodyType); newBody.UserData = userData; var rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2); var rectangleShape = new PolygonShape(rectangleVertices, density); newBody.CreateFixture(rectangleShape); return(newBody); }
private void GetIntersection() { Vector2[] subjectPoints = PolygonTools.FixTransformBias(toCheck.polygon.points, toCheck.polygon.transform); Vector2[] cutPoints = PolygonTools.FixTransformBias(checker.polygon.points, checker.polygon.transform); _polyIntersections = PolygonTools.Clip(subjectPoints, cutPoints, ClipType.Intersection); for (int i = 0; i < _polyIntersections.Count; i++) { if (_currentIntersections.Count - 1 < i) { _currentIntersections.Add(Instantiate(intersectionTemplate)); } _currentIntersections[i].polygon.points = _polyIntersections[i]; _currentIntersections[i].gameObject.SetActive(true); } for (int i = _polyIntersections.Count; i < _currentIntersections.Count; i++) { _currentIntersections[i].gameObject.SetActive(false); } }
private ContinuousTest() { List <Vertices> list = new List <Vertices>(); list.Add(PolygonTools.CreateLine(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f))); list.Add(PolygonTools.CreateRectangle(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0)); _ground = BodyFactory.CreateCompoundPolygon(World, list, 0); _box = BodyFactory.CreateRectangle(World, 4, 0.2f, 1); _box.Position = new Vector2(0, 20); _box.BodyType = BodyType.Dynamic; //_box.Body.Rotation = 0.1f; //_angularVelocity = 46.661274f; _angularVelocity = Rand.RandomFloat(-50.0f, 50.0f); _box.LinearVelocity = new Vector2(0.0f, -100.0f); _box.AngularVelocity = _angularVelocity; Distance.GJKCalls = 0; Distance.GJKIters = 0; Distance.GJKMaxIters = 0; TimeOfImpact.TOICalls = 0; TimeOfImpact.TOIIters = 0; TimeOfImpact.TOIRootIters = 0; TimeOfImpact.TOIMaxRootIters = 0; }
/// <summary> /// Method for creating complex bodies. /// </summary> /// <param name="world">The new object will appear in this world</param> /// <param name="objectTexture">The new object will get this texture</param> /// <param name="Scale">The new object get scaled by this factor</param> /// <param name="Algorithm">The new object get triangulated by this triangulation algorithm</param> /// <returns>Returns the complex body</returns> public Body CreateComplexBody(World world, Texture2D objectTexture, float Scale, out Vector2 Origin, TriangulationAlgorithm Algorithm = TriangulationAlgorithm.Bayazit) { Body body = null; uint[] data = new uint[objectTexture.Width * objectTexture.Height]; objectTexture.GetData(data); Vertices textureVertices = PolygonTools.CreatePolygon(data, objectTexture.Width, false); Vector2 centroid = -textureVertices.GetCentroid(); textureVertices.Translate(ref centroid); Origin = -centroid; textureVertices = SimplifyTools.CollinearSimplify(textureVertices, 4f); List <Vertices> list = Triangulate.ConvexPartition(textureVertices, Algorithm); Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * Scale; foreach (Vertices vertices in list) { vertices.Scale(ref vertScale); } return(body = BodyFactory.CreateCompoundPolygon(world, list, 1f)); }
public Robot(Texture2D t, MainGame game, World w) : base(w) { timeCounter = 0.0f; texture = t; parent = game; Health = 100; state = RobotState.IDLE; direction = PlayerDirection.RIGHT; //frameCounter = 0; //frameRate = 1.0f / 24.0f; srcRect = new Rectangle(0, 0, width, height); destRect = new Rectangle((int)Position.X, (int)Position.Y, 50, 50); myWorld = w; lastFire = -1.0; fireRate = 1000; this.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(ConvertUnits.ToSimUnits(width / 2), ConvertUnits.ToSimUnits(height / 2)), 1.0f)); this.BodyType = BodyType.Dynamic; this.Restitution = 0.3f; this.Friction = 1.0f; this.IgnoreGravity = true; this.MarkedForRemoval = false; }
private VerticalStackTest() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyFactory.CreateEdge(World, new Vector2(20.0f, 0.0f), new Vector2(20.0f, 20.0f)); float[] xs = { 0.0f, -10.0f, -5.0f, 5.0f, 10.0f }; for (int j = 0; j < ColumnCount; ++j) { PolygonShape shape = new PolygonShape(1); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 0.5f); for (int i = 0; i < RowCount; ++i) { int n = j * RowCount + i; Debug.Assert(n < RowCount * ColumnCount); _indices[n] = n; const float x = 0.0f; //float x = Rand.RandomFloat-0.02f, 0.02f); //float x = i % 2 == 0 ? -0.025f : 0.025f; Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(xs[j] + x, 0.752f + 1.54f * i); body.UserData = _indices[n]; _bodies[n] = body; Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.3f; } } _bullet = null; }
public Agent(World world, PhysicsGameScreen screen, Vector2 position) { _collidesWith = Category.All; _collisionCategories = Category.All; _agentBody = BodyFactory.CreateBody(world, position); _agentBody.BodyType = BodyType.Dynamic; //Center FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody); //Left arm FixtureFactory.AttachRectangle(1.5f, 0.4f, 1f, new Vector2(-1f, 0f), _agentBody); FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(-2f, 0f)); //Right arm FixtureFactory.AttachRectangle(1.5f, 0.4f, 1f, new Vector2(1f, 0f), _agentBody); FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(2f, 0f)); //Top arm FixtureFactory.AttachRectangle(0.4f, 1.5f, 1f, new Vector2(0f, 1f), _agentBody); FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(0f, 2f)); //Bottom arm FixtureFactory.AttachRectangle(0.4f, 1.5f, 1f, new Vector2(0f, -1f), _agentBody); FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(0f, -2f)); _screen = screen; //GFX AssetCreator creator = _screen.ScreenManager.Assets; _box = new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(1.75f, 0.2f), MaterialType.Blank, Color.White, 1f)); _knob = new Sprite(creator.CircleTexture(0.5f, MaterialType.Blank, Color.Orange, 1f)); _offset = ConvertUnits.ToDisplayUnits(2f); }
private void CreateChassis(World world, Vector2 pivot, AssetCreator assets) { { PolygonShape shape = new PolygonShape(1f); shape.Vertices = PolygonTools.CreateRectangle(5f / 2f, 2.0f / 2f); _body = new Sprite(assets.TextureFromShape(shape, MaterialType.Blank, Color.Beige, 1f)); _chassis = world.CreateBody(); _chassis.BodyType = BodyType.Dynamic; _chassis.Position = pivot + _position; Fixture fixture = _chassis.CreateFixture(shape); fixture.CollisionGroup = -1; } { CircleShape shape = new CircleShape(1.6f, 1f); _engine = new Sprite(assets.TextureFromShape(shape, MaterialType.Waves, Color.Beige * 0.8f, 1f)); _wheel = world.CreateBody(); _wheel.BodyType = BodyType.Dynamic; _wheel.Position = pivot + _position; Fixture fixture = _wheel.CreateFixture(shape); fixture.CollisionGroup = -1; } { _motorJoint = new RevoluteJoint(_wheel, _chassis, _wheel.GetLocalPoint(_chassis.Position), Vector2.Zero); _motorJoint.CollideConnected = false; _motorJoint.MotorSpeed = _motorSpeed; _motorJoint.MaxMotorTorque = 400f; _motorJoint.MotorEnabled = true; world.Add(_motorJoint); } }