public void applySteering(Body body) { // For Kinematic & Static bodies, we set the velocity BodyType type = body.getType(); if (type == BodyType.KinematicBody || type == BodyType.StaticBody) { body.setLinearVelocity(velocity); body.setAngularVelocity(rotation); } else { // body is dynamic, so we apply an impulse body.applyLinearImpulse(velocity, body.getWorldCenter()); body.applyAngularImpulse(rotation); } }
@Override public void draw() { Gdx.gl.glClearColor(0, 0.2f, 1, 1); camera.position.interpolate( new Vector3(camera.position.x, camY, 0), 0.1f, Interpolation.pow2Out); camera.update(); batch.setProjectionMatrix(camera.combined); if (!pause) world.step(1 / 60f, 8, 3); world.getBodies(bodies); batch.begin(); for (i = 0; i < 5; i++) { if (backgrounds[i] .getBoundingRectangle() .overlaps( new Rectangle( camera.position.x - camera.viewportWidth / 2, camera.position.y - camera.viewportHeight / 2, camera.viewportWidth, camera.viewportHeight))) { backgrounds[i].draw(batch); } } traySprite.draw(batch); mpHandler.update( Gdx.graphics.getDeltaTime(), true, batch, tap, new Vector2(unprojected.x, unprojected.y)); for (i = 0; i < bodies.size; i++) { scbd = bodies.get(i); if (scbd != tmpbd) { bodyY.add(scbd.getPosition().y); } if (scbd.getWorldCenter().y < -2 || scbd.getWorldCenter().x < -1.3f || scbd.getWorldCenter().x > 6.5f) { mpHandler.removeFromBody(scbd); world.destroyBody(scbd); if (!gameOver) { if (score > MainClass.highScore) { newHighScoreSound.play(MainClass.sound); } else { gameOverSound.play(MainClass.sound); } gameOver = true; scoreLabel.addAction(new AlphaAction()); muffinSource.remove(); pauseButton.remove(); gameOverDialog.show(this, score); } } if (scbd.getType() == BodyDef.BodyType.DynamicBody) { AdjustToBody(muffinSprite, muffinOrigin, scbd, 1.228f, 1); muffinSprite.draw(batch); if (scbd.isAwake()) { if (Math.abs(scbd.getLinearVelocity().len2()) > 10) { AdjustToBody(scaredmuffinface, muffinOrigin, scbd, 1.228f, 1); scaredmuffinface.draw(batch); } else { AdjustToBody(awakemuffinface, muffinOrigin, scbd, 1.228f, 1); awakemuffinface.draw(batch); } } else { AdjustToBody(normalmuffinface, muffinOrigin, scbd, 1.228f, 1); normalmuffinface.draw(batch); } } } batch.end(); camY = 0; if (bodyY.size > 5) { bodyY.sort(); for (i = bodyY.size - 3; i < bodyY.size; i++) { camY += bodyY.get(i); } camY /= 3; camY += 2; } if (camY < 4) camY = 4; if (drag) { tmpbd.setLinearVelocity( (unprojected.x - 0.5f - tmpbd.getPosition().x) * 20, (unprojected.y - 0.5f - tmpbd.getPosition().y) * 20); } super.draw(); bodies.clear(); bodyY.clear(); }
/** * Creates an array of PolySpatials based on fixture information from the scene. Note that * fixtures create aligned textures. * * @param scene */ private void createPolySpatialsFromRubeFixtures(RubeScene scene) { Array<Body> bodies = scene.getBodies(); EarClippingTriangulator ect = new EarClippingTriangulator(); if ((bodies != null) && (bodies.size > 0)) { polySpatials = new Array<PolySpatial>(); Vector2 bodyPos = new Vector2(); // for each body in the scene... for (int i = 0; i < bodies.size; i++) { Body body = bodies.get(i); bodyPos.set(body.getPosition()); Array<Fixture> fixtures = body.getFixtureList(); if ((fixtures != null) && (fixtures.size > 0)) { // for each fixture on the body... for (int j = 0; j < fixtures.size; j++) { Fixture fixture = fixtures.get(j); String textureName = (String) scene.getCustom(fixture, "TextureMask", null); if (textureName != null) { String textureFileName = "data/" + textureName; Texture texture = textureMap.get(textureFileName); TextureRegion textureRegion = null; if (texture == null) { texture = new Texture(textureFileName); texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat); textureMap.put(textureFileName, texture); textureRegion = new TextureRegion(texture); textureRegionMap.put(texture, textureRegion); } else { textureRegion = textureRegionMap.get(texture); } // only handle polygons at this point -- no chain, edge, or circle fixtures. if (fixture.getType() == Shape.Type.Polygon) { PolygonShape shape = (PolygonShape) fixture.getShape(); int vertexCount = shape.getVertexCount(); float[] vertices = new float[vertexCount * 2]; // static bodies are texture aligned and do not get drawn based off of the related // body. if (body.getType() == BodyType.StaticBody) { for (int k = 0; k < vertexCount; k++) { shape.getVertex(k, mTmp); mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees); mTmp.add( bodyPos); // convert local coordinates to world coordinates to that textures // are // aligned vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER; vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER; } short[] triangleIndices = ect.computeTriangles(vertices).toArray(); PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices); PolySpatial spatial = new PolySpatial(region, Color.WHITE); polySpatials.add(spatial); } else { // all other fixtures are aligned based on their associated body. for (int k = 0; k < vertexCount; k++) { shape.getVertex(k, mTmp); vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER; vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER; } short[] triangleIndices = ect.computeTriangles(vertices).toArray(); PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices); PolySpatial spatial = new PolySpatial(region, body, Color.WHITE); polySpatials.add(spatial); } } else if (fixture.getType() == Shape.Type.Circle) { CircleShape shape = (CircleShape) fixture.getShape(); float radius = shape.getRadius(); int vertexCount = (int) (12f * radius); float[] vertices = new float[vertexCount * 2]; System.out.println("SpatialFactory: radius: " + radius); if (body.getType() == BodyType.StaticBody) { mTmp3.set(shape.getPosition()); for (int k = 0; k < vertexCount; k++) { // set the initial position mTmp.set(radius, 0); // rotate it by 1/vertexCount * k mTmp.rotate(360f * k / vertexCount); // add it to the position. mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees); mTmp.add(mTmp3); mTmp.add( bodyPos); // convert local coordinates to world coordinates to that textures // are aligned vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER; vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER; } short[] triangleIndices = ect.computeTriangles(vertices).toArray(); PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices); PolySpatial spatial = new PolySpatial(region, Color.WHITE); polySpatials.add(spatial); } else { mTmp3.set(shape.getPosition()); for (int k = 0; k < vertexCount; k++) { // set the initial position mTmp.set(radius, 0); // rotate it by 1/vertexCount * k mTmp.rotate(360f * k / vertexCount); // add it to the position. mTmp.add(mTmp3); vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER; vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER; } short[] triangleIndices = ect.computeTriangles(vertices).toArray(); PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices); PolySpatial spatial = new PolySpatial(region, body, Color.WHITE); polySpatials.add(spatial); } } } } } } } }