@Override public boolean keyDown (int keyCode) { if (keyCode == Keys.D) m_platform.setType(BodyType.DynamicBody); if (keyCode == Keys.S) m_platform.setType(BodyType.StaticBody); if (keyCode == Keys.K) { m_platform.setType(BodyType.KinematicBody); m_platform.setLinearVelocity(tmp.set(-m_speed, 0)); m_platform.setAngularVelocity(0); } return false; }
public void create () { cam = new OrthographicCamera(48, 32); cam.position.set(0, 15, 0); renderer = new Box2DDebugRenderer(); world = new World(new Vector2(0, -10), true); Body body = world.createBody(new BodyDef()); CircleShape shape = new CircleShape(); shape.setRadius(1f); MassData mass = new MassData(); mass.mass = 1f; body.setMassData(mass); body.setFixedRotation(true); body.setType(BodyType.KinematicBody); body.createFixture(shape, 1); body.setBullet(true); body.setTransform(new Vector2(0, 0), body.getAngle()); body.setLinearVelocity(new Vector2(50f, 0)); }
/** Creates a circle object with the given position and radius. Resitution defaults to 0.6. */ public static Body createCircle(World world, float x, float y, float radius, boolean isStatic) { CircleShape sd = new CircleShape(); sd.setRadius(radius); FixtureDef fdef = new FixtureDef(); fdef.shape = sd; fdef.density = 1.0f; fdef.friction = 0.3f; fdef.restitution = 0.6f; BodyDef bd = new BodyDef(); bd.allowSleep = true; bd.position.set(x, y); Body body = world.createBody(bd); body.createFixture(fdef); if (isStatic) { body.setType(BodyDef.BodyType.StaticBody); } else { body.setType(BodyDef.BodyType.DynamicBody); } return body; }
@Override public CompletionEnum execute(float dt) { PrismaticJoint joint = (PrismaticJoint)executor.getPhysicsJoint(this.joint); joint.enableMotor(true); joint.setMaxMotorForce(maxMotorForceB); joint.setMotorSpeed(motorSpeedB); executor.getPhysicsObject(name).setType(BodyType.DynamicBody); return CompletionEnum.EXECUTING; }
@Override public CompletionEnum tick(float dt){ PrismaticJoint joint = (PrismaticJoint)executor.getPhysicsJoint(this.joint); if(joint.isMotorEnabled() && towardA && executor.getPhysicsObject(name).getWorldCenter().dst(pointA) < TURN_DISTANCE){ towardA = false; joint.setMaxMotorForce(maxMotorForceB); joint.setMotorSpeed(motorSpeedB); joint.enableMotor(false); executor.getPhysicsObject(name).setType(BodyType.StaticBody); timeChangeDirection = System.currentTimeMillis() + waitDuration; }else if(joint.isMotorEnabled() && !towardA && executor.getPhysicsObject(name).getWorldCenter().dst(pointB) < TURN_DISTANCE){ towardA = true; joint.setMaxMotorForce(maxMotorForceA); joint.setMotorSpeed(motorSpeedA); joint.enableMotor(false); executor.getPhysicsObject(name).setType(BodyType.StaticBody); timeChangeDirection = System.currentTimeMillis() + waitDuration; }else if(!joint.isMotorEnabled() && System.currentTimeMillis() > timeChangeDirection){ joint.enableMotor(true); executor.getPhysicsObject(name).setType(BodyType.DynamicBody); } return CompletionEnum.EXECUTING; }
/** * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), * and rotating the box counterclockwise through the given angle, with specified restitution. */ public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = Math.abs((xmax - xmin) / 2); float hy = Math.abs((ymax - ymin) / 2); PolygonShape wallshape = new PolygonShape(); // Don't set the angle here; instead call setTransform on the body below. This allows future // calls to setTransform to adjust the rotation as expected. wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; if (restitution>0) fdef.restitution = restitution; BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); wall.setTransform(cx, cy, angle); return wall; }
@Override public void createBodies(World world) { this.anchorBody = Box2DFactory.createCircle(world, this.cx, this.cy, 0.05f, true); // Joint angle is 0 when flipper is horizontal. // The flipper needs to be slightly extended past anchorBody to rotate correctly. float ext = (this.flipperLength > 0) ? -0.05f : +0.05f; // Width larger than 0.12 slows rotation? this.flipperBody = Box2DFactory.createWall(world, cx+ext, cy-0.12f, cx+flipperLength, cy+0.12f, 0f); flipperBody.setType(BodyDef.BodyType.DynamicBody); flipperBody.setBullet(true); flipperBody.getFixtureList().get(0).setDensity(5.0f); jointDef = new RevoluteJointDef(); jointDef.initialize(anchorBody, flipperBody, new Vector2(this.cx, this.cy)); jointDef.enableLimit = true; jointDef.enableMotor = true; // counterclockwise rotations are positive, so flip angles for flippers extending left jointDef.lowerAngle = (this.flipperLength>0) ? this.minangle : -this.maxangle; jointDef.upperAngle = (this.flipperLength>0) ? this.maxangle : -this.minangle; jointDef.maxMotorTorque = 1000f; this.joint = (RevoluteJoint)world.createJoint(jointDef); flipperBodySet = Collections.singletonList(flipperBody); this.setEffectiveMotorSpeed(-this.downspeed); // Force flipper to bottom when field is first created. }
Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); return wall;