Hello.py
From Box2D Wiki
This example does not include graphical output, only numbers.
#!/usr/bin/env python """ This is a simple example of building and running a simulation using Box2D. Here we create a large ground box and a small dynamic box. """ from Box2D2 import * # Define the size of the world. Simulation will still work # if bodies reach the end of the world, but it will be slower. worldAABB=b2AABB() worldAABB.lowerBound.Set(-100, -100) worldAABB.upperBound.Set(100, 100) # Define the gravity vector. gravity = b2Vec2(0, -10) # Do we want to let bodies sleep? doSleep = True # Construct a world object, which will hold and simulate the rigid bodies. world = b2World(worldAABB, gravity, doSleep) # Define the ground body. groundBodyDef = b2BodyDef() groundBodyDef.position.Set(0, -10) # 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. groundBody = world.CreateBody(groundBodyDef) # Define the ground box shape. groundShapeDef = b2PolygonDef() # The extents are the half-widths of the box. groundShapeDef.SetAsBox(50, 10) # 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 = b2BodyDef() bodyDef.position.Set(0, 4) body = world.CreateBody(bodyDef) # Define another box shape for our dynamic body. shapeDef = b2PolygonDef() shapeDef.SetAsBox(1, 1) # Set the box density to be non-zero, so it will be dynamic. shapeDef.density = 1 # Override the default friction. shapeDef.friction = 0.3 # 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. timeStep = 1.0 / 60 iterations = 10 # This is our little game loop. for i in range(60): # Instruct the world to perform a single step of simulation. It is # generally best to keep the time step and iterations fixed. world.Step(timeStep, iterations) # for SVN version of pyBox2D, you should use: # vel_iters, pos_iters = 10, 8 # world.Step(timeStep, vel_iters, pos_iters) # Now print the position and angle of the body. position = body.GetPosition() angle = body.GetAngle() print position, angle
The output is as follows:
b2Vec2(0,3.99722) 0.0 b2Vec2(0,3.99167) 0.0 b2Vec2(0,3.98333) 0.0 b2Vec2(0,3.97222) 0.0 b2Vec2(0,3.95833) 0.0 b2Vec2(0,3.94167) 0.0 b2Vec2(0,3.92222) 0.0 b2Vec2(0,3.9) 0.0 b2Vec2(0,3.875) 0.0 b2Vec2(0,3.84722) 0.0 b2Vec2(0,3.81667) 0.0 b2Vec2(0,3.78333) 0.0 b2Vec2(0,3.74722) 0.0 b2Vec2(0,3.70833) 0.0 b2Vec2(0,3.66667) 0.0 b2Vec2(0,3.62222) 0.0 b2Vec2(0,3.575) 0.0 b2Vec2(0,3.525) 0.0 b2Vec2(0,3.47222) 0.0 b2Vec2(0,3.41667) 0.0 b2Vec2(0,3.35833) 0.0 b2Vec2(0,3.29722) 0.0 b2Vec2(0,3.23333) 0.0 b2Vec2(0,3.16667) 0.0 b2Vec2(0,3.09722) 0.0 b2Vec2(0,3.025) 0.0 b2Vec2(0,2.95) 0.0 b2Vec2(0,2.87222) 0.0 b2Vec2(0,2.79167) 0.0 b2Vec2(0,2.70833) 0.0 b2Vec2(0,2.62222) 0.0 b2Vec2(0,2.53333) 0.0 b2Vec2(0,2.44167) 0.0 b2Vec2(0,2.34722) 0.0 b2Vec2(0,2.25) 0.0 b2Vec2(0,2.15) 0.0 b2Vec2(0,2.04722) 0.0 b2Vec2(0,1.94167) 0.0 b2Vec2(0,1.83333) 0.0 b2Vec2(0,1.72222) 0.0 b2Vec2(0,1.60833) 0.0 b2Vec2(0,1.49167) 0.0 b2Vec2(0,1.37222) 0.0 b2Vec2(0,1.25) 0.0 b2Vec2(0,1.125) 0.0 b2Vec2(0,0.997223) 0.0 b2Vec2(-4.35681e-005,0.997231) 4.35681213276e-005 b2Vec2(-3.40069e-005,0.997229) 3.40064725606e-005 b2Vec2(-3.38193e-005,0.997229) 3.38188328897e-005 b2Vec2(-3.38468e-005,0.997229) 3.38463287335e-005 b2Vec2(-3.38475e-005,0.997229) 3.38470563293e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469799317e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005 b2Vec2(-3.38474e-005,0.997229) 3.38469762937e-005
Note that the numbers like 3.38469762937e-005 are using scientific notation and are very small (0.0000384...).
