// We need separation create/destroy functions from the constructor/destructor because // the destructor cannot access the allocator (no destructor arguments allowed by C++). public void Create(Body body, FixtureDef def) { UserData = def.UserData; Friction = def.Friction; Restitution = def.Restitution; Body = body; Next = null; Filter.Set(def.Filter); IsSensor = def.IsSensor; Shape = def.Shape.Clone(); // Reserve proxy space int childCount = Shape.ChildCount; if (Proxies == null) { Proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; i++) { Proxies[i] = new FixtureProxy { Fixture = null, ProxyId = BroadPhase.NULL_PROXY }; } } if (Proxies.Length < childCount) { FixtureProxy[] old = Proxies; int newLen = MathUtils.Max(old.Length * 2, childCount); Proxies = new FixtureProxy[newLen]; Array.Copy(old, 0, Proxies, 0, old.Length); for (int i = 0; i < newLen; i++) { if (i >= old.Length) { Proxies[i] = new FixtureProxy(); } Proxies[i].Fixture = null; Proxies[i].ProxyId = BroadPhase.NULL_PROXY; } } ProxyCount = 0; m_density = def.Density; }
static void Main(string[] args) { // Static Body Vec2 gravity = new Vec2(0, -10); bool doSleep = true; World world = new World(gravity); world.SleepingAllowed = doSleep; BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position.set_Renamed(0, -10); Body groundBody = world.createBody(groundBodyDef); PolygonShape groundBox = new PolygonShape(); groundBox.setAsBox(50, 10); groundBody.createFixture(groundBox, 0); // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position.set_Renamed(0, 4); Body body = world.createBody(bodyDef); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.setAsBox(1, 1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = dynamicBox; fixtureDef.density = 1; fixtureDef.friction = 0.3f; body.createFixture(fixtureDef); // Setup world float timeStep = 1.0f / 60.0f; int velocityIterations = 6; int positionIterations = 2; // Run loop for (int i = 0; i < 60; ++i) { world.step(timeStep, velocityIterations, positionIterations); Vec2 position = body.Position; float angle = body.Angle; Console.WriteLine("{0:0.00} {1:0.00} {2:0.00}", position.x, position.y, angle); } }
// TODO djm: check out about this new fixture here /// <summary> /// Creates a fixture and attach it to this body. Use this function if you need to set some fixture /// parameters, like friction. Otherwise you can create the fixture directly from a shape. If the /// density is non-zero, this function automatically updates the mass of the body. Contacts are not /// created until the next time step. /// </summary> /// <param name="def">the fixture definition.</param> /// <warning>This function is locked during callbacks.</warning> public Fixture CreateFixture(FixtureDef def) { Debug.Assert(World.Locked == false); if (World.Locked == true) { return(null); } // djm TODO from pool? Fixture fixture = new Fixture(); fixture.Create(this, def); if ((Flags & TypeFlags.Active) == TypeFlags.Active) { BroadPhase broadPhase = World.ContactManager.BroadPhase; fixture.CreateProxies(broadPhase, Xf); } fixture.Next = FixtureList; FixtureList = fixture; ++FixtureCount; fixture.Body = this; // Adjust mass properties if needed. if (fixture.Density > 0.0f) { ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. World.Flags |= World.NEW_FIXTURE; return(fixture); }
// TODO djm: check out about this new fixture here /// <summary> /// Creates a fixture and attach it to this body. Use this function if you need to set some fixture /// parameters, like friction. Otherwise you can create the fixture directly from a shape. If the /// density is non-zero, this function automatically updates the mass of the body. Contacts are not /// created until the next time step. /// </summary> /// <param name="def">the fixture definition.</param> /// <warning>This function is locked during callbacks.</warning> public Fixture createFixture(FixtureDef def) { Debug.Assert(m_world.Locked == false); if (m_world.Locked == true) { return null; } // djm TODO from pool? Fixture fixture = new Fixture(); fixture.create(this, def); if ((m_flags & e_activeFlag) == e_activeFlag) { BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase; fixture.createProxies(broadPhase, m_xf); } fixture.m_next = m_fixtureList; m_fixtureList = fixture; ++m_fixtureCount; fixture.m_body = this; // Adjust mass properties if needed. if (fixture.m_density > 0.0f) { resetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. m_world.m_flags |= World.NEW_FIXTURE; return fixture; }
// We need separation create/destroy functions from the constructor/destructor because // the destructor cannot access the allocator (no destructor arguments allowed by C++). public void Create(Body body, FixtureDef def) { UserData = def.UserData; Friction = def.Friction; Restitution = def.Restitution; Body = body; Next = null; Filter.Set(def.Filter); IsSensor = def.IsSensor; Shape = def.Shape.Clone(); // Reserve proxy space int childCount = Shape.ChildCount; if (Proxies == null) { Proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; i++) { Proxies[i] = new FixtureProxy {Fixture = null, ProxyId = BroadPhase.NULL_PROXY}; } } if (Proxies.Length < childCount) { FixtureProxy[] old = Proxies; int newLen = MathUtils.Max(old.Length * 2, childCount); Proxies = new FixtureProxy[newLen]; Array.Copy(old, 0, Proxies, 0, old.Length); for (int i = 0; i < newLen; i++) { if (i >= old.Length) { Proxies[i] = new FixtureProxy(); } Proxies[i].Fixture = null; Proxies[i].ProxyId = BroadPhase.NULL_PROXY; } } ProxyCount = 0; m_density = def.Density; }
// We need separation create/destroy functions from the constructor/destructor because // the destructor cannot access the allocator (no destructor arguments allowed by C++). public virtual void create(Body body, FixtureDef def) { m_userData = def.userData; m_friction = def.friction; m_restitution = def.restitution; m_body = body; m_next = null; m_filter.set_Renamed(def.filter); m_isSensor = def.isSensor; m_shape = def.shape.Clone(); // Reserve proxy space int childCount = m_shape.ChildCount; if (m_proxies == null) { m_proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; i++) { m_proxies[i] = new FixtureProxy(); m_proxies[i].fixture = null; m_proxies[i].proxyId = BroadPhase.NULL_PROXY; } } if (m_proxies.Length < childCount) { FixtureProxy[] old = m_proxies; int newLen = MathUtils.max(old.Length * 2, childCount); m_proxies = new FixtureProxy[newLen]; Array.Copy(old, 0, m_proxies, 0, old.Length); for (int i = 0; i < newLen; i++) { if (i >= old.Length) { m_proxies[i] = new FixtureProxy(); } m_proxies[i].fixture = null; m_proxies[i].proxyId = BroadPhase.NULL_PROXY; } } m_proxyCount = 0; m_density = def.density; }
// TODO djm: check out about this new fixture here /// <summary> /// Creates a fixture and attach it to this body. Use this function if you need to set some fixture /// parameters, like friction. Otherwise you can create the fixture directly from a shape. If the /// density is non-zero, this function automatically updates the mass of the body. Contacts are not /// created until the next time step. /// </summary> /// <param name="def">the fixture definition.</param> /// <warning>This function is locked during callbacks.</warning> public Fixture CreateFixture(FixtureDef def) { Debug.Assert(World.Locked == false); if (World.Locked == true) { return null; } // djm TODO from pool? Fixture fixture = new Fixture(); fixture.Create(this, def); if ((Flags & TypeFlags.Active) == TypeFlags.Active) { BroadPhase broadPhase = World.ContactManager.BroadPhase; fixture.CreateProxies(broadPhase, Xf); } fixture.Next = FixtureList; FixtureList = fixture; ++FixtureCount; fixture.Body = this; // Adjust mass properties if needed. if (fixture.Density > 0.0f) { ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. World.Flags |= World.NEW_FIXTURE; return fixture; }
private void InitializeWorld() { // Static Body Vec2 gravity = new Vec2(0, -10); bool doSleep = true; world = new World(gravity); world.SleepingAllowed = doSleep; BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0, -10); Body groundBody = world.CreateBody(groundBodyDef); PolygonShape groundBox = new PolygonShape(); groundBox.SetAsBox(50, 10); groundBody.CreateFixture(groundBox, 0); { // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.Type = BodyType.Dynamic; bodyDef.Position.Set(5, 4); bodyDef.Angle = (float)(2 * Math.PI / 3); Body body = world.CreateBody(bodyDef); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.SetAsBox(1, 1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.Shape = dynamicBox; fixtureDef.Density = 1; fixtureDef.Friction = 0.3f; body.CreateFixture(fixtureDef); Bodies.Add(new BodyAdapter(body)); } { // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.Type = BodyType.Dynamic; bodyDef.Position.Set(5, 10); bodyDef.Angle = (float)(Math.PI / 3); Body body = world.CreateBody(bodyDef); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.SetAsBox(1, 1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.Shape = dynamicBox; fixtureDef.Density = 1; fixtureDef.Friction = 0.3f; body.CreateFixture(fixtureDef); Bodies.Add(new BodyAdapter(body)); } { // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.Type = BodyType.Dynamic; bodyDef.Position.Set(4.5f, 7); bodyDef.AngularVelocity = (float)(2 * Math.PI); Body body = world.CreateBody(bodyDef); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.SetAsBox(1, 1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.Shape = dynamicBox; fixtureDef.Density = 1; fixtureDef.Friction = 0.3f; body.CreateFixture(fixtureDef); Bodies.Add(new BodyAdapter(body)); } }