def __init__(self): # the camera self.camera = None # the renderer self.renderer = None self.batch = None self.font = None # our box2D world self.world = None # ground body to connect the mouse joint to self.groundBody = None # our mouse joint self.mouseJoint = None # a hit body self.hitBody = None self.testPoint = Vector3() self.callback = Box2DTest.QueryCallbackNew(self) # temp vector self.tmp = Vector2() # another temporary vector self.target = Vector2()
def create(self): ''' setup the camera. In Box2D we operate on a meter scale, pixels won't do it. So we use an orthographic camera with a viewport of 48 meters in width and 32 meters in height. we also position the camera so that it looks at (0, 16) (that's where the middle of the screen will be located). ''' self.camera = OrthographicCamera(48, 32) self.camera.position.set(0, 15, 0) # create the debug renderer self.renderer = Box2DDebugRenderer() # create the world self.world = World(Vector2(0, -10), True) # we also need an invisible zero size ground body # to which we can connect the mouse joint bodyDef = BodyDef() self.groundBody = self.world.createBody(bodyDef) # call abstract method to populate the world self.createWorld(self.world) self.batch = SpriteBatch() self.font = BitmapFont(Gdx.files.internal('../data/arial-15.fnt'), False) Gdx.input.setInputProcessor(self)
def createWorld(self, world): bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.set(Vector2(-40, 0), Vector2(40, 0)) ground.createFixture(shape, 0.0) shape.dispose() shape = PolygonShape() shape.setAsBox(0.6, 0.125) fd = FixtureDef() fd.shape = shape fd.density = 20.0 fd.friction = 0.2 jd = RevoluteJointDef() jd.collideConnected = False y = 25.0 prevBody = ground for i in range(30): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(0.5 + i, y) body = world.createBody(bd) body.createFixture(fd) anchor = Vector2(i, y) jd.initialize(prevBody, body, anchor) world.createJoint(jd) prevBody = body shape.dispose()
def createWorld(self, world): bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.set(Vector2(-40, 0), Vector2(40, 0)) ground.createFixture(shape, 0) shape.dispose() shape = PolygonShape() shape.setAsBox(0.5, 0.125) fd = FixtureDef() fd.shape = shape fd.density = 20.0 jd = WeldJointDef() prevBody = ground for i in range(self.e_count): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-14.5 + 1.0 * i, 5.0) body = world.createBody(bd) body.createFixture(fd) anchor = Vector2(-15.0 + 1 * i, 5.0) jd.initialize(prevBody, body, anchor) world.createJoint(jd) prevBody = body shape.dispose() shape = PolygonShape() shape.setAsBox(0.5, 0.125) fd = FixtureDef() fd.shape = shape fd.density = 20.0 jd = WeldJointDef() prevBody = ground for i in range(self.e_count): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-14.5 + 1.0 * i, 15.0) bd.gravityScale = 10.0 body = world.createBody(bd) body.createFixture(fd) anchor = Vector2(-15.0 + 1.0 * i, 15.0) jd.initialize(prevBody, body, anchor) world.createJoint(jd) prevBody = body shape.dispose() shape = PolygonShape() shape.setAsBox(0.5, 0.125) fd = FixtureDef() fd.shape = shape fd.density = 20.0 jd = WeldJointDef() prevBody = ground for i in range(self.e_count): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-4.5 + 1.0 * i, 5.0) body = world.createBody(bd) body.createFixture(fd) if i > 0: anchor = Vector2(-5.0 + 1.0 * i, 5.0) jd.initialize(prevBody, body, anchor) world.createJoint(jd) prevBody = body shape.dispose() shape = PolygonShape() shape.setAsBox(0.5, 0.125) fd = FixtureDef() fd.shape = shape fd.density = 20.0 jd = WeldJointDef() prevBody = ground for i in range(self.e_count): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(5.5 + 1.0 * i, 10.0) bd.gravityScale = 10.0 body = world.createBody(bd) body.createFixture(fd) if i > 0: anchor = Vector2(5.0 + 1.0 * i, 10.0) jd.initialize(prevBody, body, anchor) world.createJoint(jd) prevBody = body shape.dispose() for i in range(2): vertices = [] vertices.append(Vector2(-0.5, 0)) vertices.append(Vector2(0.5, 0)) vertices.append(Vector2(0, 1.5)) shape = PolygonShape() shape.set(vertices) fd = FixtureDef() fd.shape = shape fd.density = 1.0 bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-8.0 + 8.0 * i, 12.0) body = world.createBody(bd) body.createFixture(fd) shape.dispose() for i in range(2): shape = CircleShape() shape.setRadius(0.5) fd = FixtureDef() fd.shape = shape fd.density = 1.0 bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-6.0 + 6.0 * i, 10.0) body = world.createBody(bd) body.createFixture(fd) shape.dispose()
def createWorld(self, world): world.setGravity(Vector2(0, 0)) k_restitution = 0.4 bd = BodyDef() bd.position.set(0, 20) ground = world.createBody(bd) shape = EdgeShape() sd = FixtureDef() sd.shape = shape sd.density = 0 sd.restitution = k_restitution shape.set(Vector2(-20, -20), Vector2(-20, 20)) ground.createFixture(sd) shape.set(Vector2(20, -20), Vector2(20, 20)) ground.createFixture(sd) shape.set(Vector2(-20, 20), Vector2(20, 20)) ground.createFixture(sd) shape.set(Vector2(-20, -20), Vector2(20, -20)) ground.createFixture(sd) shape.dispose() xf1 = Transform(Vector2(), 0.3524 * Math.PI) xf1.setPosition(xf1.mul(Vector2(1, 0))) vertices = [None, None, None] vertices[0] = xf1.mul(Vector2(-1, 0)) vertices[1] = xf1.mul(Vector2(1, 0)) vertices[2] = xf1.mul(Vector2(0, 0.5)) poly1 = PolygonShape() poly1.set(vertices) sd1 = FixtureDef() sd1.shape = poly1 sd1.density = 4.0 xf2 = Transform(Vector2(), -0.3524 * Math.PI) xf2.setPosition(xf2.mul(Vector2(-1, 0))) vertices[0] = xf2.mul(Vector2(-1, 0)) vertices[1] = xf2.mul(Vector2(1, 0)) vertices[2] = xf2.mul(Vector2(0, 0.5)) poly2 = PolygonShape() poly2.set(vertices) sd2 = FixtureDef() sd2.shape = poly2 sd2.density = 2.0 bd = BodyDef() bd.type = BodyType.DynamicBody bd.angularDamping = 5.0 bd.linearDamping = 0.1 bd.position.set(0, 2) bd.angle = Math.PI bd.allowSleep = False self.m_body = world.createBody(bd) self.m_body.createFixture(sd1) self.m_body.createFixture(sd2) poly1.dispose() poly2.dispose() shape = PolygonShape() shape.setAsBox(0.5, 0.5) fd = FixtureDef() fd.shape = shape fd.density = 1.0 fd.friction = 0.3 for i in range(10): bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(0, 5 + 1.54 * i) body = world.createBody(bd) body.createFixture(fd) gravity = 10.0 I = body.getInertia() mass = body.getMass() radius = Math.sqrt(2 * I / mass) jd = FrictionJointDef() jd.localAnchorA.set(0, 0) jd.localAnchorB.set(0, 0) jd.bodyA = ground jd.bodyB = body jd.collideConnected = True jd.maxForce = mass * gravity jd.maxTorque = mass * radius * gravity world.createJoint(jd) shape.dispose()
def __init__(self): super(ApplyForce, self).__init__() self.m_body = None self.tmp = Vector2()
def createWorld(self, world): bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.set(Vector2(-20, 0), Vector2(20, 0)) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.setRadius(0) shape.set(Vector2(-8, 1), Vector2(-6, 1)) ground.createFixture(shape, 0) shape.set(Vector2(-6, 1), Vector2(-4, 1)) ground.createFixture(shape, 0) shape.set(Vector2(-4, 1), Vector2(-2, 1)) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() ground = world.createBody(bd) shape = PolygonShape() shape.setAsBox(1, 1, Vector2(4, 3), 0) ground.createFixture(shape, 0) shape.setAsBox(1, 1, Vector2(6, 3), 0) ground.createFixture(shape, 0) shape.setAsBox(1, 1, Vector2(8, 3), 0) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() d = 2 * 2 * 0.005 shape.setRadius(0) shape.set(Vector2(-1 + d, 3), Vector2(1 - d, 3)) ground.createFixture(shape, 0) shape.set(Vector2(1, 3 + d), Vector2(1, 5 - d)) ground.createFixture(shape, 0) shape.set(Vector2(1 - d, 5), Vector2(-1 + d, 5)) ground.createFixture(shape, 0) shape.set(Vector2(-1, 5 - d), Vector2(-1, 3 + d)) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() bd.position.set(-3, 5) bd.type = BodyType.DynamicBody bd.fixedRotation = True bd.allowSleep = False body = world.createBody(bd) shape = PolygonShape() shape.setAsBox(0.5, 0.5) fd = FixtureDef() fd.shape = shape fd.density = 20.0 body.createFixture(fd) shape.dispose() bd = BodyDef() bd.position.set(-5, 5) bd.type = BodyType.DynamicBody bd.fixedRotation = True bd.allowSleep = False body = world.createBody(bd) angle = 0.0 delta = Math.PI / 3 vertices = [] for i in range(6): vertices.append( Vector2(0.5 * Math.cos(angle), 0.5 * Math.sin(angle))) angle += delta shape = PolygonShape() shape.set(vertices) fd = FixtureDef() fd.shape = shape fd.density = 20.0 body.createFixture(fd) shape.dispose() bd = BodyDef() bd.position.set(3, 5) bd.type = BodyType.DynamicBody bd.fixedRotation = True bd.allowSleep = False body = world.createBody(bd) shape = CircleShape() shape.setRadius(0.5) fd = FixtureDef() fd.shape = shape fd.density = 20.0 body.createFixture(fd) shape.dispose()
def createWorld(self, world): shape = EdgeShape() shape.set(Vector2(-40.0, 0), Vector2(40, 0)) fd = FixtureDef() fd.shape = shape fd.friction = 0.3 bd = BodyDef() ground = world.createBody(bd) ground.createFixture(fd) shape.dispose() vertices = [] vertices.append(Vector2(-1, 0)) vertices.append(Vector2(1, 0)) vertices.append(Vector2(0, 2)) polygon = PolygonShape() polygon.set(vertices) triangleShapeDef = FixtureDef() triangleShapeDef.shape = polygon triangleShapeDef.density = 1.0 triangleShapeDef.filter.groupIndex = self.k_smallGroup triangleShapeDef.filter.categoryBits = self.k_triangleCategory triangleShapeDef.filter.maskBits = self.k_triangleMask triangleBodyDef = BodyDef() triangleBodyDef.type = BodyType.DynamicBody triangleBodyDef.position.set(-5, 2) body1 = world.createBody(triangleBodyDef) body1.createFixture(triangleShapeDef) vertices[0].scl(2) vertices[1].scl(2) vertices[2].scl(2) polygon.set(vertices) triangleShapeDef.filter.groupIndex = self.k_largeGroup triangleBodyDef.position.set(-5, 6) triangleBodyDef.fixedRotation = True body2 = world.createBody(triangleBodyDef) body2.createFixture(triangleShapeDef) bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-5, 10) body = world.createBody(bd) p = PolygonShape() p.setAsBox(0.5, 1.0) body.createFixture(p, 1) jd = PrismaticJointDef() jd.bodyA = body2 jd.bodyB = body jd.enableLimit = True jd.localAnchorA.set(0, 4) jd.localAnchorB.set(0, 0) jd.localAxisA.set(0, 1) jd.lowerTranslation = -1 jd.upperTranslation = 1 world.createJoint(jd) p.dispose() polygon.setAsBox(1, 0.5) boxShapeDef = FixtureDef() boxShapeDef.shape = polygon boxShapeDef.density = 1 boxShapeDef.restitution = 0.1 boxShapeDef.filter.groupIndex = self.k_smallGroup boxShapeDef.filter.categoryBits = self.k_boxCategory boxShapeDef.filter.maskBits = self.k_boxMask boxBodyDef = BodyDef() boxBodyDef.type = BodyType.DynamicBody boxBodyDef.position.set(0, 2) body3 = world.createBody(boxBodyDef) body3.createFixture(boxShapeDef) polygon.setAsBox(2, 1) boxShapeDef.filter.groupIndex = self.k_largeGroup boxBodyDef.position.set(0, 6) body4 = world.createBody(boxBodyDef) body4.createFixture(boxShapeDef) circle = CircleShape() circle.setRadius(1) circleShapeDef = FixtureDef() circleShapeDef.shape = circle circleShapeDef.density = 1.0 circleShapeDef.filter.groupIndex = self.k_smallGroup circleShapeDef.filter.categoryBits = self.k_circleCategory circleShapeDef.filter.maskBits = self.k_circleMask circleBodyDef = BodyDef() circleBodyDef.type = BodyType.DynamicBody circleBodyDef.position.set(5, 2) body5 = world.createBody(circleBodyDef) body5.createFixture(circleShapeDef) circle.setRadius(2) circleShapeDef.filter.groupIndex = self.k_largeGroup circleBodyDef.position.set(5, 6) body6 = world.createBody(circleBodyDef) body6.createFixture(circleShapeDef)
def createWorld(self, world): world.setContactListener(self) bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.set(Vector2(-20, 0), Vector2(20, 0)) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.setRadius(0) shape.set(Vector2(-8, 1), Vector2(-6, 1)) ground.createFixture(shape, 0) shape.set(Vector2(-6, 1), Vector2(-4, 1)) ground.createFixture(shape, 0) shape.set(Vector2(-4, 1), Vector2(-2, 1)) ground.createFixture(shape, 0) shape.dispose() bd = BodyDef() ground = world.createBody(bd) shape = PolygonShape() shape.setAsBox(1, 1, Vector2(4, 3), 0) ground.createFixture(shape, 0) shape.setAsBox(1, 1, Vector2(6, 3), 0) ground.createFixture(shape, 0) shape.setAsBox(1, 1, Vector2(8, 3), 0) ground.createFixture(shape, 0) shape.dispose()
def createWorld(self, world): ground = None bd = BodyDef() ground = world.createBody(bd) shape = EdgeShape() shape.set(Vector2(-20, 0), Vector2(20, 0)) fd = FixtureDef() fd.shape = shape ground.createFixture(fd) shape.dispose() bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(0, 3.0) self.m_attachment = world.createBody(bd) shape = PolygonShape() shape.setAsBox(0.5, 2.0) self.m_attachment.createFixture(shape, 2.0) shape.dispose() bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(-4.0, 5.0) self.m_platform = world.createBody(bd) shape = PolygonShape() shape.setAsBox(0.5, 4.0, Vector2(4.0, 0), 0.5 * Math.PI) fd = FixtureDef() fd.shape = shape fd.friction = 0.6 fd.density = 2.0 self.m_platform.createFixture(fd) shape.dispose() rjd = RevoluteJointDef() rjd.initialize(self.m_attachment, self.m_platform, Vector2(0, 5.0)) rjd.maxMotorTorque = 50.0 rjd.enableMotor = True world.createJoint(rjd) pjd = PrismaticJointDef() pjd.initialize(ground, self.m_platform, Vector2(0, 5.0), Vector2(1, 0)) pjd.maxMotorForce = 1000.0 pjd.enableMotor = True pjd.lowerTranslation = -10.0 pjd.upperTranslation = 10.0 pjd.enableLimit = True world.createJoint(pjd) self.m_speed = 3.0 bd = BodyDef() bd.type = BodyType.DynamicBody bd.position.set(0, 8.0) body = world.createBody(bd) shape = PolygonShape() shape.setAsBox(0.75, 0.75) fd = FixtureDef() fd.shape = shape fd.friction = 0.6 fd.density = 2.0 body.createFixture(fd) shape.dispose()
def __init__(self): self.m_attachment = None self.m_platform = None self.m_speed = None self.tmp = Vector2()