void InitPhysics() { var gravity = new b2Vec2(0.0f, -10.0f); world = new b2World(gravity); world.SetAllowSleeping(true); world.SetContinuousPhysics(true); var def = new b2BodyDef(); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = world.CreateBody(def); groundBody.SetActive(true); b2EdgeShape groundBox = new b2EdgeShape(); groundBox.Set(b2Vec2.Zero, new b2Vec2(900, 100)); b2FixtureDef fd = new b2FixtureDef(); fd.friction = 0.3f; fd.restitution = 0.1f; fd.shape = groundBox; groundBody.CreateFixture(fd); }
void InitPhysics() { CCSize s = Layer.VisibleBoundsWorldspace.Size; var gravity = new b2Vec2 (0.0f, -10.0f); world = new b2World (gravity); world.SetAllowSleeping (true); world.SetContinuousPhysics (true); var def = new b2BodyDef (); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = world.CreateBody (def); groundBody.SetActive (true); b2EdgeShape groundBox = new b2EdgeShape (); groundBox.Set (b2Vec2.Zero, new b2Vec2 (s.Width / PTM_RATIO, 0)); b2FixtureDef fd = new b2FixtureDef (); fd.shape = groundBox; groundBody.CreateFixture (fd); }
private void initPhysics() { CCSize s = CCDirector.SharedDirector.WinSize; var gravity = new b2Vec2(0.0f, -10.0f); _world = new b2World(gravity); float debugWidth = s.Width / PTM_RATIO * 2f; float debugHeight = s.Height / PTM_RATIO * 2f; CCDraw debugDraw = new CCDraw(new b2Vec2(debugWidth / 2f + 10, s.Height - debugHeight - 10), 2); debugDraw.AppendFlags(b2DrawFlags.e_shapeBit); _world.SetDebugDraw(debugDraw); _world.SetAllowSleeping(true); _world.SetContinuousPhysics(true); //m_debugDraw = new GLESDebugDraw( PTM_RATIO ); //world->SetDebugDraw(m_debugDraw); //uint32 flags = 0; //flags += b2Draw::e_shapeBit; // flags += b2Draw::e_jointBit; // flags += b2Draw::e_aabbBit; // flags += b2Draw::e_pairBit; // flags += b2Draw::e_centerOfMassBit; //m_debugDraw->SetFlags(flags); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. b2BodyDef def = b2BodyDef.Create(); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = _world.CreateBody(def); groundBody.SetActive(true); // Define the ground box shape. // bottom b2EdgeShape groundBox = new b2EdgeShape(); groundBox.Set(b2Vec2.Zero, new b2Vec2(s.Width / PTM_RATIO, 0)); b2FixtureDef fd = b2FixtureDef.Create(); fd.shape = groundBox; groundBody.CreateFixture(fd); // top groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, s.Height / PTM_RATIO), new b2Vec2(s.Width / PTM_RATIO, s.Height / PTM_RATIO)); fd.shape = groundBox; groundBody.CreateFixture(fd); // left groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, s.Height / PTM_RATIO), b2Vec2.Zero); fd.shape = groundBox; groundBody.CreateFixture(fd); // right groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(s.Width / PTM_RATIO, s.Height / PTM_RATIO), new b2Vec2(s.Width / PTM_RATIO, 0)); fd.shape = groundBox; groundBody.CreateFixture(fd); // _world.Dump(); }
public Mouse() { //m_destructionListener = new DestructionListener(); m_debugDraw = new CCBox2dDraw("fonts/arial-16"); b2Vec2 gravity = new b2Vec2(); gravity.Set(500, 500); m_world = new b2World(gravity); m_world.SetAllowSleeping(false); m_world.SetContinuousPhysics(true); m_world.SetDebugDraw(m_debugDraw); m_debugDraw.AppendFlags(b2DrawFlags.e_shapeBit | b2DrawFlags.e_aabbBit | b2DrawFlags.e_centerOfMassBit | b2DrawFlags.e_jointBit | b2DrawFlags.e_pairBit); m_world.SetContinuousPhysics(true); m_world.SetWarmStarting(true); }
public b2World N2b2World(JObject worldValue) { b2World world = new b2World(jsonToVec("gravity", worldValue)); world.SetAllowSleeping((bool)worldValue.GetValue("allowSleep")); world.ClearForces(); //world.Set(worldValue.getBoolean("autoClearForces")); world.SetWarmStarting((bool)worldValue.GetValue("warmStarting")); world.SetContinuousPhysics((bool)worldValue.GetValue("continuousPhysics")); world.SetSubStepping((bool)worldValue.GetValue("subStepping")); readCustomPropertiesFromJson(world, worldValue); int i = 0; JArray bodyValues = (JArray)worldValue["body"]; if (null != bodyValues) { int numBodyValues = bodyValues.Count; for (i = 0; i < numBodyValues; i++) { JObject bodyValue = (JObject)bodyValues[i]; b2Body body = N2b2Body(world, bodyValue); readCustomPropertiesFromJson(body, bodyValue); m_bodies.Add(body); m_indexToBodyMap.Add(i, body); } } // need two passes for joints because gear joints reference other joints JArray jointValues = (JArray)worldValue["joint"]; if (null != jointValues) { int numJointValues = jointValues.Count; for (i = 0; i < numJointValues; i++) { JObject jointValue = (JObject)jointValues[i]; if (jointValue["type"].ToString() != "gear") { b2Joint joint = j2b2Joint(world, jointValue); readCustomPropertiesFromJson(joint, jointValue); m_joints.Add(joint); } } for (i = 0; i < numJointValues; i++) { JObject jointValue = (JObject)jointValues[i]; if (jointValue["type"].ToString() == "gear") { b2Joint joint = j2b2Joint(world, jointValue); readCustomPropertiesFromJson(joint, jointValue); m_joints.Add(joint); } } } i = 0; JArray imageValues = (JArray)worldValue["image"]; if (null != imageValues) { int numImageValues = imageValues.Count; for (i = 0; i < numImageValues; i++) { JObject imageValue = (JObject)imageValues[i]; Nb2dJsonImage image = j2b2dJsonImage(imageValue); readCustomPropertiesFromJson(image, imageValue); m_images.Add(image); } } return world; }
static void SetupWorld(bool setupGround) { var gravity = new b2Vec2(0.0f, -10.0f); _world = new b2World(gravity); _world.SetAllowSleeping(true); _world.SetContinuousPhysics(true); _world.SetSubStepping(true); _world.SetWarmStarting(true); _world.SetDestructionListener(new Destructo()); _world.SetContactListener(new Contacto()); if (!setupGround) { return; } // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. b2BodyDef def = b2BodyDef.Create(); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = _world.CreateBody(def); groundBody.SetActive(true); // bottom b2EdgeShape groundBox = new b2EdgeShape(); groundBox.Set(b2Vec2.Zero, new b2Vec2(width, 0)); b2FixtureDef fd = b2FixtureDef.Create(); fd.shape = groundBox; groundBody.CreateFixture(fd); // top groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, height), new b2Vec2(width, height)); fd.shape = groundBox; groundBody.CreateFixture(fd); // left groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, height), b2Vec2.Zero); fd.shape = groundBox; groundBody.CreateFixture(fd); // right groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(width, height), new b2Vec2(width, 0)); fd.shape = groundBox; groundBody.CreateFixture(fd); _world.Dump(); }
static void Main(string[] args) { var gravity = new b2Vec2(0.0f, -10.0f); b2World _world = new b2World(gravity); _world.SetAllowSleeping(true); _world.SetContinuousPhysics(true); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. b2BodyDef def = b2BodyDef.Create(); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = _world.CreateBody(def); groundBody.SetActive(true); // Define the ground box shape. float width = 100f, height = 100f; // bottom b2EdgeShape groundBox = new b2EdgeShape(); groundBox.Set(b2Vec2.Zero, new b2Vec2(width, 0)); b2FixtureDef fd = b2FixtureDef.Create(); fd.shape = groundBox; groundBody.CreateFixture(fd); // top groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, height), new b2Vec2(width, height)); fd.shape = groundBox; groundBody.CreateFixture(fd); // left groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, height), b2Vec2.Zero); fd.shape = groundBox; groundBody.CreateFixture(fd); // right groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(width, height), new b2Vec2(width, 0)); fd.shape = groundBox; groundBody.CreateFixture(fd); _world.Dump(); Console.WriteLine("Enter the number of bodies you want to run?"); string s = Console.ReadLine(); Random ran = new Random(); for (int i = 0; i < int.Parse(s); i++) { def = b2BodyDef.Create(); def.position = new b2Vec2(width * (float)ran.NextDouble(), height * (float)ran.NextDouble()); def.type = b2BodyType.b2_dynamicBody; b2Body body = _world.CreateBody(def); // Define another box shape for our dynamic body. var dynamicBox = new b2PolygonShape(); dynamicBox.SetAsBox(.5f, .5f); //These are mid points for our 1m box // Define the dynamic body fixture. fd = b2FixtureDef.Create(); fd.shape = dynamicBox; fd.density = 1f; fd.friction = 0.3f; b2Fixture fixture = body.CreateFixture(fd); } int iter = 0; for (float dt = 0f; dt < 26f; ) { Update(_world, dt); dt += 1f / 30f; iter++; if (iter == 30) { Dump(_world); iter = 0; } } Console.WriteLine("hit <enter> to exit"); Console.ReadLine(); Dump(_world); }
private void InitB2World() { world = new Box2D.Dynamics.b2World(new Box2D.Common.b2Vec2(0, -100)); world.SetContactListener(new Myb2Listener()); world.SetAllowSleeping(true); world.SetContinuousPhysics(true); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. def = new b2BodyDef(); def.allowSleep = true; def.position = b2Vec2.Zero; def.type = b2BodyType.b2_staticBody; b2Body groundBody = world.CreateBody(def); groundBody.SetActive(true); // Define the ground box shape. // bottom b2EdgeShape groundBox = new b2EdgeShape(); groundBox.Set(b2Vec2.Zero, new b2Vec2(Resources.DisplayMetrics.WidthPixels, 0)); b2FixtureDef fd = new b2FixtureDef(); fd.shape = groundBox; groundBody.CreateFixture(fd); // top groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, Resources.DisplayMetrics.HeightPixels), new b2Vec2(Resources.DisplayMetrics.WidthPixels, Resources.DisplayMetrics.HeightPixels)); fd.shape = groundBox; groundBody.CreateFixture(fd); // left groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(0, Resources.DisplayMetrics.HeightPixels), b2Vec2.Zero); fd.shape = groundBox; groundBody.CreateFixture(fd); // right groundBox = new b2EdgeShape(); groundBox.Set(new b2Vec2(Resources.DisplayMetrics.WidthPixels, Resources.DisplayMetrics.HeightPixels), new b2Vec2(Resources.DisplayMetrics.WidthPixels, 0)); fd.shape = groundBox; groundBody.CreateFixture(fd); // bodydef = new b2BodyDef(); bodydef.active = true; bodydef.position = new b2Vec2(100, 100); bodydef.type = b2BodyType.b2_dynamicBody; b2Body buttonBody = world.CreateBody(bodydef); buttonBody.SetActive(true); var shape = new b2EdgeShape(); shape.Set(new b2Vec2(100, 100), new b2Vec2(150, 100)); buttonBody.CreateFixture(new b2FixtureDef() { density = 10, shape = shape, }); }