private BuoyancyTest() { World.Gravity = new Vector2(0, -9.82f); BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0)); float offset = 5; for (int i = 0; i < 3; i++) { Body rectangle = BodyFactory.CreateRectangle(World, 2, 2, 1, new Vector2(-30 + offset, 20)); rectangle.Rotation = Rand.RandomFloat(0, 3.14f); rectangle.BodyType = BodyType.Dynamic; offset += 7; } for (int i = 0; i < 3; i++) { Body rectangle = BodyFactory.CreateCircle(World, 1, 1, new Vector2(-30 + offset, 20)); rectangle.Rotation = Rand.RandomFloat(0, 3.14f); rectangle.BodyType = BodyType.Dynamic; offset += 7; } AABB container = new AABB(new Vector2(0, 10), 60, 10); BuoyancyController buoyancy = new BuoyancyController(container, 2, 2, 1, World.Gravity); World.AddController(buoyancy); }
// Use this for initialization void Start () { Vector3 p = transform.position; FVector2 aa = new FVector2(p.x + StartPoint.x, p.y + StartPoint.y); FVector2 bb = new FVector2(p.x + EndPoint.x, p.y + EndPoint.y); aabb = new AABB(aa, bb); buoyancyController = new BuoyancyController(aabb, Density, LinearDragCoef, RotationalDragCoef, FSWorldComponent.PhysicsWorld.Gravity); FSWorldComponent.PhysicsWorld.AddController(buoyancyController); }
// Use this for initialization void Start() { Vector3 p = transform.position; FVector2 aa = new FVector2(p.x + StartPoint.x, p.y + StartPoint.y); FVector2 bb = new FVector2(p.x + EndPoint.x, p.y + EndPoint.y); aabb = new AABB(aa, bb); buoyancyController = new BuoyancyController(aabb, Density, LinearDragCoef, RotationalDragCoef, FSWorldComponent.PhysicsWorld.Gravity); FSWorldComponent.PhysicsWorld.AddController(buoyancyController); }
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.CreateFixture(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.CreateFixture(sd); body.SetMassFromShapes(); bc.AddBody(body); } }
/// <summary> /// Call this to draw shapes and other debug draw data. /// </summary> private void DrawDebugData() { if ((Flags & DebugViewFlags.Shape) == DebugViewFlags.Shape) { foreach (Body b in World.BodyList) { Transform xf; b.GetTransform(out xf); foreach (Fixture f in b.FixtureList) { if (b.Enabled == false) { DrawShape(f, xf, InactiveShapeColor); } else if (b.BodyType == BodyType.Static) { DrawShape(f, xf, StaticShapeColor); } else if (b.BodyType == BodyType.Kinematic) { DrawShape(f, xf, KinematicShapeColor); } else if (b.Awake == false) { DrawShape(f, xf, SleepingShapeColor); } else { DrawShape(f, xf, DefaultShapeColor); } } } } if ((Flags & DebugViewFlags.ContactPoints) == DebugViewFlags.ContactPoints) { const float axisScale = 0.3f; for (int i = 0; i < _pointCount; ++i) { ContactPoint point = _points[i]; if (point.State == PointState.Add) { DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.95f, 0.3f)); } else if (point.State == PointState.Persist) { DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.3f, 0.95f)); } if ((Flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals) { Vector2 p1 = point.Position; Vector2 p2 = p1 + axisScale * point.Normal; DrawSegment(p1, p2, new Color(0.4f, 0.9f, 0.4f)); } } _pointCount = 0; } if ((Flags & DebugViewFlags.PolygonPoints) == DebugViewFlags.PolygonPoints) { foreach (Body body in World.BodyList) { foreach (Fixture f in body.FixtureList) { PolygonShape polygon = f.Shape as PolygonShape; if (polygon != null) { Transform xf; body.GetTransform(out xf); for (int i = 0; i < polygon.Vertices.Count; i++) { Vector2 tmp = FarseerPhysics.Common.MathUtils.Mul(ref xf, polygon.Vertices[i]); DrawPoint(tmp, 0.1f, Color.Red); } } } } } if ((Flags & DebugViewFlags.Joint) == DebugViewFlags.Joint) { foreach (Joint j in World.JointList) { DrawJoint(j); } } if ((Flags & DebugViewFlags.AABB) == DebugViewFlags.AABB) { Color color = new Color(0.9f, 0.3f, 0.9f); IBroadPhase bp = World.ContactManager.BroadPhase; foreach (Body body in World.BodyList) { if (body.Enabled == false) { continue; } foreach (Fixture f in body.FixtureList) { for (int t = 0; t < f.ProxyCount; ++t) { FixtureProxy proxy = f.Proxies[t]; AABB aabb; bp.GetFatAABB(proxy.ProxyId, out aabb); DrawAABB(ref aabb, color); } } } } if ((Flags & DebugViewFlags.CenterOfMass) == DebugViewFlags.CenterOfMass) { foreach (Body b in World.BodyList) { Transform xf; b.GetTransform(out xf); xf.p = b.WorldCenter; DrawTransform(ref xf); } } if ((Flags & DebugViewFlags.Controllers) == DebugViewFlags.Controllers) { for (int i = 0; i < World.ControllerList.Count; i++) { Controller controller = World.ControllerList[i]; BuoyancyController buoyancy = controller as BuoyancyController; if (buoyancy != null) { AABB container = buoyancy.Container; DrawAABB(ref container, Color.LightBlue); } } } if ((Flags & DebugViewFlags.DebugPanel) == DebugViewFlags.DebugPanel) { DrawDebugPanel(); } }
public override void Start() { base.Start(); AABB waterBounds = new AABB(new FVector2(0f, -360f / physScale), new FVector2(640f / physScale, -200f / physScale)); controller = new BuoyancyController(waterBounds, 2.0f, 5f, 2f, FSWorldComponent.PhysicsWorld.Gravity); // add the controller FSWorldComponent.PhysicsWorld.AddController(controller); bodies = new List <Body>(); Body tbody; // Spawn in a bunch of crap for (int i = 0; i < 5; i++) { tbody = BodyFactory.CreateRectangle(FSWorldComponent.PhysicsWorld, (Random.value * 5f + 10f) / physScale, (Random.value * 5f + 10f) / physScale, 1f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale)); tbody.Rotation = Random.value * Mathf.PI; tbody.BodyType = BodyType.Dynamic; bodies.Add(tbody); } for (int i = 0; i < 5; i++) { tbody = BodyFactory.CreateCircle(FSWorldComponent.PhysicsWorld, (Random.value * 5f + 10f) / physScale, 0.5f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale)); tbody.Rotation = Random.value * Mathf.PI; tbody.BodyType = BodyType.Dynamic; bodies.Add(tbody); } for (int i = 0; i < 15; i++) { Vertices vlist = new Vertices(); if (Random.value > 0.66f) { vlist.Add(new FVector2((10f + Random.value * 10f) / physScale, (-10f - Random.value * 10f) / physScale)); vlist.Add(new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale)); vlist.Add(new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale)); vlist.Add(new FVector2((-10f - Random.value * 10f) / physScale, (-10f - Random.value * 10f) / physScale)); } else if (Random.value > 0.5f) { FVector2 v00 = new FVector2(0f, (-10f - Random.value * 10f) / physScale); FVector2 v02 = new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale); FVector2 v03 = new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale); FVector2 v01 = new FVector2(v00.X + v02.X, v00.Y + v02.Y); v01 *= Random.value / 2f + 0.8f; FVector2 v04 = new FVector2(v03.X + v00.X, v03.Y + v00.Y); v04 *= Random.value / 2f + 0.8f; vlist.Add(v04); vlist.Add(v03); vlist.Add(v02); vlist.Add(v01); vlist.Add(v00); } else { vlist.Add(new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale)); vlist.Add(new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale)); vlist.Add(new FVector2(0f, (-10f - Random.value * 10f) / physScale)); } tbody = BodyFactory.CreateCompoundPolygon(FSWorldComponent.PhysicsWorld, new List <Vertices> { vlist }, 1f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale)); tbody.Rotation = Random.value * Mathf.PI; tbody.BodyType = BodyType.Dynamic; bodies.Add(tbody); } //Add some exciting bath toys tbody = new Body(FSWorldComponent.PhysicsWorld); tbody.Position = new FVector2(50f / physScale, -300f / physScale); tbody.BodyType = BodyType.Dynamic; FixtureFactory.AttachRectangle(80f / physScale, 20f / physScale, 3f, FVector2.Zero, tbody); bodies.Add(tbody); tbody = new Body(FSWorldComponent.PhysicsWorld); tbody.Position = new FVector2(300f / physScale, -300f / physScale); tbody.BodyType = BodyType.Dynamic; FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale, new FVector2(30f / physScale, 0f), 0f, tbody); FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale, new FVector2(-30f / physScale, 0f), 0f, tbody); FixtureFactory.AttachRectangle(60f / physScale, 4f / physScale, 2f, FVector2.Zero, tbody); FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale, new FVector2(0f, 30f / physScale), 0f, tbody); FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale, new FVector2(0f, -30f / physScale), 0f, tbody); FixtureFactory.AttachRectangle(4f / physScale, 60f / physScale, 2f, FVector2.Zero, tbody); }
/// <summary> /// Call this to draw shapes and other debug draw data. /// </summary> private void DrawDebugData() { if ((Flags & DebugViewFlags.Controllers) == DebugViewFlags.Controllers) { for (int i = 0; i < World.ControllerList.Count; i++) { Controller controller = World.ControllerList[i]; BuoyancyController buoyancy = controller as BuoyancyController; if (buoyancy != null) { AABB container = buoyancy.Container; debugRenderer.DrawAABB(container, Color.LightBlue); } SeaCurrentsController seaCurrents = controller as SeaCurrentsController; if (seaCurrents != null) { debugRenderer.DrawCircle(seaCurrents.Position, seaCurrents.Radius, Color.LightBlue); FP.Vector2 dir = seaCurrents.Direction; dir.Normalize(); debugRenderer.DrawArrow(seaCurrents.Position, seaCurrents.Position + dir * seaCurrents.Strength, 5, 8, true, Color.Green); } } } if ((Flags & DebugViewFlags.Shape) == DebugViewFlags.Shape) { foreach (Body b in World.BodyList) { FP.Transform xf; b.GetTransform(out xf); foreach (Fixture f in b.FixtureList) { if (b.Enabled == false) { debugRenderer.DrawShape(f, xf, InactiveShapeColor); } else if (b.BodyType == BodyType.Static) { debugRenderer.DrawShape(f, xf, StaticShapeColor); } else if (b.BodyType == BodyType.Kinematic) { debugRenderer.DrawShape(f, xf, KinematicShapeColor); } else if (b.Awake == false) { debugRenderer.DrawShape(f, xf, SleepingShapeColor); } else { debugRenderer.DrawShape(f, xf, DefaultShapeColor); } if (f.Body == SelectedBody) { Color color = new Color(0.9f, 0.3f, 0.3f); IBroadPhase bp = World.ContactManager.BroadPhase; for (int t = 0; t < f.ProxyCount; ++t) { FixtureProxy proxy = f.Proxies[t]; AABB aabb; bp.GetFatAABB(proxy.ProxyId, out aabb); debugRenderer.DrawAABB(aabb, color); } } } } } if ((Flags & DebugViewFlags.ContactPoints) == DebugViewFlags.ContactPoints) { const float axisScale = 0.3f; for (int i = 0; i < _pointCount; ++i) { ContactPoint point = _points[i]; if (point.State == PointState.Add) { debugRenderer.DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.95f, 0.3f)); } else if (point.State == PointState.Persist) { debugRenderer.DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.3f, 0.95f)); } if ((Flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals) { Vector2 p1 = point.Position; Vector2 p2 = p1 + axisScale * point.Normal; debugRenderer.DrawSegment(p1, p2, new Color(0.4f, 0.9f, 0.4f)); } } _pointCount = 0; } if ((Flags & DebugViewFlags.PolygonPoints) == DebugViewFlags.PolygonPoints) { foreach (Body body in World.BodyList) { foreach (Fixture f in body.FixtureList) { PolygonShape polygon = f.Shape as PolygonShape; if (polygon != null) { FP.Transform xf; body.GetTransform(out xf); for (int i = 0; i < polygon.Vertices.Count; i++) { Vector2 tmp = FP.MathUtils.Mul(ref xf, polygon.Vertices[i]).ToMGVector2(); debugRenderer.DrawPoint(tmp, 0.1f, Color.Red); } } } } } if ((Flags & DebugViewFlags.Joint) == DebugViewFlags.Joint) { foreach (Joint j in World.JointList) { debugRenderer.DrawJoint(j); } } if ((Flags & DebugViewFlags.AABB) == DebugViewFlags.AABB) { Color color = new Color(0.9f, 0.3f, 0.9f); IBroadPhase bp = World.ContactManager.BroadPhase; foreach (Body body in World.BodyList) { if (body.Enabled == false) { continue; } foreach (Fixture f in body.FixtureList) { for (int t = 0; t < f.ProxyCount; ++t) { FixtureProxy proxy = f.Proxies[t]; AABB aabb; bp.GetFatAABB(proxy.ProxyId, out aabb); debugRenderer.DrawAABB(aabb, color); } } } } if ((Flags & DebugViewFlags.CenterOfMass) == DebugViewFlags.CenterOfMass) { foreach (Body b in World.BodyList) { FP.Transform xf; b.GetTransform(out xf); xf.p = b.WorldCenter; DrawTransform(ref xf); } } if (SelectedBody != null) { debugRenderer.DrawArrow(SelectedBody.Position, SelectedBody.Position + SelectedBody.LinearVelocity, 4, 8, false, Color.Green); } }
/// <summary> /// Call this to draw shapes and other debug draw data. /// </summary> private void DrawDebugData() { if (this.HasFlag(DebugViewFlags.Shape)) { foreach (Body b in World.BodyList) { Color color; if (b.Enabled == false) { color = InactiveShapeColor; } else if (b.BodyType == BodyType.Static) { color = StaticShapeColor; } else if (b.BodyType == BodyType.Kinematic) { color = KinematicShapeColor; } else if (b.Awake == false) { color = SleepingShapeColor; } else { color = DefaultShapeColor; } //if(b.HasContacts) //{ // color = Color.Red; //} Transform xf = b.GetTransform(); foreach (Fixture f in b.FixtureList) { DrawShape(f, xf, color); } } } if (this.HasFlag(DebugViewFlags.ContactPoints)) { const float axisScale = 0.3f; for (int i = 0; i < _pointCount; ++i) { ContactPoint point = _points[i]; if (point.State == PointState.Add) { DrawPoint(point.Position, 0.1f, ColorHelper.FromPercentages(0.3f, 0.95f, 0.3f)); } else if (point.State == PointState.Persist) { DrawPoint(point.Position, 0.1f, ColorHelper.FromPercentages(0.3f, 0.3f, 0.95f)); } if ((Flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals) { Vector2 p1 = point.Position; Vector2 p2 = p1 + axisScale * point.Normal; DrawSegment(p1, p2, ColorHelper.FromPercentages(0.4f, 0.9f, 0.4f)); } } _pointCount = 0; } if (this.HasFlag(DebugViewFlags.PolygonPoints)) { foreach (Body body in World.BodyList) { foreach (Fixture f in body.FixtureList) { PolygonShape polygon = f.Shape as PolygonShape; if (polygon != null) { Transform xf = body.GetTransform(); for (int i = 0; i < polygon.Vertices.Count; i++) { Vector2 tmp = Transform.Multiply(polygon.Vertices[i], ref xf); DrawPoint(tmp, 0.1f, PolygonVertexColor); } } } } } if (this.HasFlag(DebugViewFlags.Joint)) { foreach (Joint j in World.JointList) { DrawJoint(j); } } if (this.HasFlag(DebugViewFlags.AABB)) { var bodyBroadphase = World.ContactManager.BroadPhase; foreach (Body body in World.BodyList) { if (body.Enabled == false) { continue; } var bodyTransform = body.GetTransform(); // render body AABBs AABB aabb; bodyBroadphase.GetFatAABB(body.BroadphaseProxyId, out aabb); DrawAABB(ref aabb, BodyAabbColor); // render fixture AABBs var fixtureTree = body.FixtureTree; foreach (Fixture f in body.FixtureList) { for (int t = 0; t < f.ProxyCount; ++t) { FixtureProxy proxy = f.Proxies[t]; fixtureTree.GetFatAABB(proxy.ProxyId, out aabb); // move fixture to align with body in world-space AABB.Transform(ref bodyTransform, ref aabb); DrawAABB(ref aabb, FixtureAabbColor); } } //this.DrawString() } } if (this.HasFlag(DebugViewFlags.CenterOfMass)) { foreach (Body b in World.BodyList) { Transform xf = b.GetTransform(); xf.Position = b.WorldCenter; DrawTransform(ref xf); } } if (this.HasFlag(DebugViewFlags.Controllers)) { for (int i = 0; i < World.ControllerList.Count; i++) { Controller controller = World.ControllerList[i]; BuoyancyController buoyancy = controller as BuoyancyController; if (buoyancy != null) { AABB container = buoyancy.Container; DrawAABB(ref container, Color.LightBlue); } } } if (this.World.HibernationEnabled) { // render hibernated body AABBs if (this.HasFlag(DebugViewFlags.HibernatedBodyAABBs)) { var hiberatedWorld = this.World.HibernationManager.HibernatedWorld; var bodyBroadphase = hiberatedWorld.ContactManager.BroadPhase; foreach (Body body in hiberatedWorld.BodyList) { if (body.Enabled == false) { continue; } var bodyTransform = body.GetTransform(); AABB aabb; bodyBroadphase.GetFatAABB(body.BroadphaseProxyId, out aabb); DrawAABB(ref aabb, this.HibernatedBodyAabbColor); } } if (this.HasFlag(DebugViewFlags.ActiveAreas)) { // render active areas Color independentActiveAreaColor = ColorHelper.FromPercentages(0.9f, 0.3f, 0.3f); Color bodyActiveAreaColor = ColorHelper.FromPercentages(0.8f, 0.4f, 0.3f); foreach (var activeArea in this.World.HibernationManager.ActiveAreas) { if (activeArea.AreaType == ActiveAreaType.Independent) { this.DrawAABB(ref activeArea.AABB, independentActiveAreaColor); // draw connections to all bodies foreach (var areaBody in activeArea.AreaBodies) { this.DrawSegment(activeArea.AABB.Center, areaBody.AABB.Center, independentActiveAreaColor); } } else { this.DrawAABB(ref activeArea.AABB, bodyActiveAreaColor); } /* UNCOMMENT TO: render number of bodies within each active area * Vector2 position = new Vector2(activeArea.AABB.LowerBound.X, activeArea.AABB.UpperBound.Y); * position = GameInstance.ConvertWorldToScreen(position); * DebugView.DrawString((int)position.X, (int)position.Y - 5, "Contains " + activeArea.Bodies.Count.ToString()); */ } } } }