public static void simulateWorld() { // Step to update time and accumulator Physics.accumulateTimeStep(); isLockedLocal = false; // step as many times it needs to while (accumulator >= Physics.TIMESTEP) { isLockedLocal = true; ///// WORLD Step//////////////////// /** */ WORLD.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); ///// WORLD Step//////////////////// cleanRemovableBodies(); accumulator -= Physics.TIMESTEP; } isLockedLocal = false; // Interpolate the remainder from accumulator // Obs.: nice way to also attribute the remainder to alphaTime at the same time Physics.interpolateTimeStep((alphaTime = (float) accumulator / Physics.TIMESTEP)); }
private void update(float deltaTime) { world.step(1 / 60f, 6, 2); inputUpdate(deltaTime); cameraUpdate(deltaTime); batch.setProjectionMatrix(camera.combined); }
private void updatePhysics() { // Update // float deltaTime = Gdx.graphics.getRawDeltaTime(); float deltaTime = 1 / 60f; // System.out.println(deltaTime); tweenManager.update(deltaTime); // Setting delta time world.step(deltaTime, 10, 10); Vector2 planetPos = planetModel.getPosition().sub(planetModelOrigin); planetSprite.setPosition(planetPos.x, planetPos.y); planetSprite.setOrigin(planetModelOrigin.x, planetModelOrigin.y); planetSprite.setRotation(planetModel.getAngle() * MathUtils.radiansToDegrees); Vector2 planetCorePos = planetCoreModel.getPosition().sub(planetCoreModelOrigin); planetCoreSprite.setPosition(planetCorePos.x, planetCorePos.y); planetCoreSprite.setOrigin(planetCoreModelOrigin.x, planetCoreModelOrigin.y); planetCoreSprite.setRotation(planetCoreModel.getAngle() * MathUtils.radiansToDegrees); for (int i = 0; i < MAX_BALLS; i++) { Vector2 ballPos = ballModels[i].getPosition(); ballSprites[i].setPosition( ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2); ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees); } }
@Override public void render() { // Update getInput(); b2dWorld.step(1f / 60f, 6, 2); camera.update(); cameraBak.update(); manager.update(); background.update(); // Draw Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); // Background batch.setProjectionMatrix(cameraBak.combined); batch.begin(); background.draw(batch); batch.end(); batch.setProjectionMatrix(camera.combined); debugMatrix = batch.getProjectionMatrix().cpy().scale(PPM, PPM, 0); // debugRenderer.render(b2dWorld, debugMatrix); batch.begin(); manager.draw(batch); batch.end(); // GUI batch.setProjectionMatrix(cameraBak.combined); batch.begin(); drawGUI(batch); batch.end(); }
public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // tell the camera to update its matrices. camera.update( car.body.getPosition().x * PIXELS_PER_METER, car.body.getPosition().y * PIXELS_PER_METER); spriteBatch.setProjectionMatrix(camera.getCombined()); if (Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isTouched()) { car.setAccelerate(Car.ACC_ACCELERATE); } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) { car.setAccelerate(Car.ACC_BRAKE); } else { car.setAccelerate(Car.ACC_NONE); } if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.getAccelerometerY() < -2.5) { car.setSteer(Car.STEER_HARD_LEFT); } else if (Gdx.input.getAccelerometerY() < -1) { car.setSteer(Car.STEER_LEFT); } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.getAccelerometerY() > 2.5) { car.setSteer(Car.STEER_HARD_RIGHT); } else if (Gdx.input.getAccelerometerY() > 1) { car.setSteer(Car.STEER_RIGHT); } else { car.setSteer(Car.STEER_NONE); } car.update(Gdx.app.getGraphics().getDeltaTime()); /** * Have box2d update the positions and velocities (and etc) of all tracked objects. The second * and third argument specify the number of iterations of velocity and position tests to perform * -- higher is more accurate but is also slower. */ world.step(Gdx.app.getGraphics().getDeltaTime(), 3, 3); world.clearForces(); // draw the sprites spriteBatch.begin(); playerSprite.setPosition( PIXELS_PER_METER * car.body.getPosition().x - playerTexture.getRegionWidth() / 2, PIXELS_PER_METER * car.body.getPosition().y - playerTexture.getRegionHeight() / 2); playerSprite.setRotation((MathUtils.radiansToDegrees * car.body.getAngle())); playerSprite.setFlip(false, true); playerSprite.setScale(0.3f); playerSprite.draw(spriteBatch); spriteBatch.end(); /** Draw this last, so we can see the collision boundaries on top of the sprites and map. */ debugRenderer.render( world, camera.getCombined().scale(PIXELS_PER_METER, PIXELS_PER_METER, PIXELS_PER_METER)); }
@Override public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); mainGame.spriteBatch.setProjectionMatrix(camera.combined); world.step(1 / 45f, 6, 2); // Renderable.update for (Renderable r : renderables) { r.update(); } if (Gdx.input.isTouched()) { Gdx.app.debug(TAG, "Fire"); } mainGame.spriteBatch.begin(); // Renderable.render for (Renderable r : renderables) { r.render(mainGame.spriteBatch); } mainGame.spriteBatch.end(); }
public void render() { // the artistic drawing function that draws everything out (such talent) // make the frame ticker thing that actually makes pretty pictures move // in this case you are updating the world you just made // apparently you aren't suppose to update this in the render loop but w/e // also have no idea what 6 & 2 mean so again w/e (sensai plssss) world.step(Gdx.graphics.getDeltaTime(), 6, 2); // move the sprite with the body! sprite.setPosition(body.getPosition().x, body.getPosition().y); // just a limit for when it falls off the screen since there is no ground LOL System.out.println(body.getPosition().y); if (body.getPosition().y < (10)) { body.setAwake(false); } else { body.setAwake(true); } // Again not box2dStuff just cool things I added HandleTouch(); // the Background color // This doesn't look like it does anything? Gdx.gl.glClearColor(1, 1, 1, 1); // clears the background after each fram update // Same with this? Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // this is where the sprite img is rendered and updated etc. batch.begin(); batch.draw(sprite, sprite.getX(), sprite.getY()); batch.end(); }
@Override public void render() { // Update // Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); world.step(1 / 60f, 10, 10); for (int i = 0; i < MAX_BALL_COUNT; i++) { Vector2 pos = ballModels[i] .getPosition() .sub(ballSprites[i].getWidth() / 2, ballSprites[i].getHeight() / 2); float angleDeg = ballModels[i].getAngle() * MathUtils.radiansToDegrees; ballSprites[i].setPosition(pos.x, pos.y); ballSprites[i].setRotation(angleDeg); } // Render // GL10 gl = Gdx.gl10; // gl.glClearColor(1, 1, 1, 1); // gl.glClear(GL10.GL_COLOR_BUFFER_BIT); spriteBatch.setProjectionMatrix(camera.combined); spriteBatch.begin(); vialSprite.draw(spriteBatch); for (int i = 0; i < MAX_BALL_COUNT; i++) ballSprites[i].draw(spriteBatch); spriteBatch.end(); spriteBatch .getProjectionMatrix() .setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); spriteBatch.begin(); font.draw(spriteBatch, "Touch the screen to restart", 5, 25); spriteBatch.end(); }
@Override public void create() { tweenManager = new TweenManager(); Tween.registerAccessor(Sprite.class, new SpriteAccessor()); // Box2d world = new World(new Vector2(0, 0), true); // No gravity, space dah. world.step(1 / 60f, 6, 2); rayHandler = new RayHandler(world); rayHandler.setAmbientLight(0, 0, 0, 1f); // rayHandler.setAmbientLight(1f, 1f, 1f, 1f); rayHandler.setCulling(true); rayHandler.setBlur(true); rayHandler.setBlurNum(3); rayHandler.setShadows(true); RayHandler.isDiffuse = true; RayHandler.setGammaCorrection(true); controls = new GameControls(this); Gdx.input.setCursorCatched(true); camera = new OrthographicCamera(1080, 1920); spriteBatch = new SpriteBatch(); player = new PlayerShip(rayHandler, tweenManager); background = new Texture("backgroundbw.png"); enemies = new EnemyHandler(rayHandler, tweenManager); }
@Override public void update() { orthoCam.update(); synchronized (world) { if (world == null) System.out.println("null"); world.step(dt, 6, 2); } }
public void step(float delta) { // Lock the world, update in discrete time steps locked = true; discretizer.update(delta); while (discretizer.step()) { world.step(discretizer.getStepSize(), velocityIterations, positionIterations); } locked = false; }
public void doPhysicsStep(float deltaTime) { // fixed time step // max frame time to avoid spiral of death (on slow devices) float frameTime = Math.min(deltaTime, 0.25f); accumulator_ += frameTime; while (accumulator_ >= this.TIME_STEP) { world_.step(this.TIME_STEP, this.VELOCITY_ITERATIONS, this.POSITION_ITERATIONS); accumulator_ -= this.TIME_STEP; } }
public void render(OrthographicCamera camera) { // update the world with a fixed time step world.step(Gdx.app.getGraphics().getDeltaTime() * 10, 8, 3); if (mouseJoint != null) mouseJoint.setTarget(bob.getBody().getWorldCenter()); // Destroy mouseJoint? (drop item) if (destroyMousejoint == true) { if (!world.isLocked()) { world.destroyJoint(mouseJoint); mouseJoint = null; destroyMousejoint = false; } } // Delete any bodies up for deletion if (!bodiesToDelete.isEmpty()) { // Make sure it is safe to delete!! if (!world.isLocked()) { for (Body body : bodiesToDelete) { world.destroyBody(body); body.setUserData(null); body = null; } bodiesToDelete.clear(); // Don't forget to clear the null bodies! } } // Create any bodies up for creation if (!bodiesToCreate.isEmpty()) { // Make sure it is safe to create!! if (!world.isLocked()) { for (BodyDef body : bodiesToCreate) { world.createBody(body); } bodiesToCreate.clear(); // Don't forget to clear! } } // Create any joints up for creation if (!jointsToCreate.isEmpty()) { // Make sure it is safe to create!! if (!world.isLocked()) { for (JointDef body : jointsToCreate) { mouseJoint = (MouseJoint) world.createJoint(body); } jointsToCreate.clear(); // Don't forget to clear! } } // render the world using the debug renderer renderer.render(world, camera.combined); }
public void render() { Gdx.gl.glClearColor(.2f, .2f, .2f, 1); // 0->1 = 0->255 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); rayHandler.updateAndRender(); if (this.started) { float deltaTime = Gdx.app.getGraphics().getDeltaTime(); if (deltaTime > .1f) deltaTime = .1f; world.step(deltaTime, 10, 10); // Increase the score score.addValue((int) this.joe.getSpeed() * 10 * Gdx.app.getGraphics().getDeltaTime()); joe.render(); if (joe.isOutOfScreen()) { this.Pause(); RunningJoe.getInstance().setScreen(new GameOverScreen()); } this.camera.translate(RunningJoe.PixToMeter(this.joe.getSpeed()), 0); } camera.update(); if (RunningJoe.DEV_MODE) RunningJoe.debugRenderer.render(world, camera.combined); // Draw bodies textures spriteBatch.begin(); for (Background bg : this.vecBg) { bg.DrawTexture(spriteBatch); } // Draw Joe in the World joe.draw(spriteBatch); // Generate ground and draw it in the World this.listBlock.render(); this.listBlock.draw(spriteBatch); this.obstacleFactory.generateRjObstacles(this, spriteBatch); fire.draw(spriteBatch); foreground.DrawTexture(spriteBatch); spriteBatch.end(); }
@Override public void render() { if (platform.getPosition().x > 10) { platform.setLinearVelocity(-PLATFORM_VELOCITY, 0); } else if (platform.getPosition().x < -10) { platform.setLinearVelocity(PLATFORM_VELOCITY, 0); } Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); world.step(1 / 60f, 6, 2); dDebugRenderer.render(world, cam.combined); }
@Override public void render() { camera.update(); // Step the physics simulation forward at a rate of 60hz world.step(1f / 60f, 6, 2); sprite.setPosition( (body.getPosition().x * PIXELS_TO_METERS) - sprite.getWidth() / 2, (body.getPosition().y * PIXELS_TO_METERS) - sprite.getHeight() / 2); sprite.setRotation((float) Math.toDegrees(body2.getAngle())); sprite2.setPosition( (body2.getPosition().x * PIXELS_TO_METERS) - sprite2.getWidth() / 2, (body2.getPosition().y * PIXELS_TO_METERS) - sprite2.getHeight() / 2); sprite2.setRotation((float) Math.toDegrees(body.getAngle())); Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw( sprite, sprite.getX(), sprite.getY(), sprite.getOriginX(), sprite.getOriginY(), sprite.getWidth(), sprite.getHeight(), sprite.getScaleX(), sprite.getScaleY(), sprite.getRotation()); batch.draw( sprite2, sprite2.getX(), sprite2.getY(), sprite2.getOriginX(), sprite2.getOriginY(), sprite2.getWidth(), sprite2.getHeight(), sprite2.getScaleX(), sprite2.getScaleY(), sprite2.getRotation()); batch.end(); }
@Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); float delta = Gdx.graphics.getDeltaTime(); if (delta > MAX_DELTA_TIME) { delta = MAX_DELTA_TIME; } mAccumulator += delta; while (mAccumulator >= mSecondsPerStep) { mWorld.step(mSecondsPerStep, mVelocityIter, mPositionIter); mAccumulator -= mSecondsPerStep; } if ((spatials != null) && (spatials.size > 0)) { batch.setProjectionMatrix(camera.combined); batch.begin(); for (int i = 0; i < spatials.size; i++) { spatials.get(i).render(batch, 0); } batch.end(); } if ((polySpatials != null) && (polySpatials.size > 0)) { polygonBatch.setProjectionMatrix(camera.combined); polygonBatch.begin(); for (int i = 0; i < polySpatials.size; i++) { polySpatials.get(i).render(polygonBatch, 0); } polygonBatch.end(); } batch.setProjectionMatrix(textCam.combined); batch.begin(); bitmapFont.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 10, 20); batch.end(); debugRender.render(mWorld, camera.combined); }
public void update(float delta) { if (levels.get(currentLevel - 1).isWon == true) { if (currentLevel >= levels.size()) { Gdx.app.exit(); } gravBot.getBody().setTransform(.16f, 0, 0); gravBot.getBody().setLinearVelocity(new Vector2(0, 0)); gravBot.getBody().setAngularVelocity(0); destroyLevel(); currentLevel++; startLevel(currentLevel); } gameWorldPhysics.step(delta, 3, 3); gravBot.update(delta); for (Obstacle obstacles : obs) { obstacles.update(delta); } clampToScreen(); runTime += delta; }
@Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.position.set(circleBody.getPosition().x, circleBody.getPosition().y, 0); camera.update(); renderer.render(world, camera.combined); System.out.println( "player.getPosition().x = " + player.getPosition().x + "\nplayer.getPosition().y = " + player.getPosition().y + "\ncamera.position = " + camera.position); Sprite sprite; sprite = (Sprite) circleBody.getUserData(); // set position and width and height and makes sure it is in the center sprite.setBounds( convertToWorld(circleBody.getPosition().x) - sprite.getWidth() / 2, convertToWorld(circleBody.getPosition().y) - sprite.getHeight() / 2, convertToWorld(circleShape.getRadius() * 2), convertToWorld(circleShape.getRadius() * 2)); tiledMapRenderer.setView(camera); tiledMapRenderer.render(); System.out.println( "Bouncing circle: " + circleBody.getMass() + "\n" + "Player: " + player.getMass()); // world.step(1/45f, 6, 2); world.step(Gdx.graphics.getDeltaTime(), 4, 4); player.setAwake(true); // camera.project(point.set(player.getPosition().x, player.getPosition().y, 0)); // logger.log(); batch.begin(); // Circle.draw(batch); sprite.draw(batch); batch.end(); }
@Override public void update(float deltaTime) { super.update(deltaTime); debugRenderer.render(world, camera.combined); float frameTime = Math.min(deltaTime, 0.25f); accumulator += frameTime; if (accumulator >= MAX_STEP_TIME) { world.step(MAX_STEP_TIME, 6, 2); accumulator -= MAX_STEP_TIME; // Entity Queue for (Entity entity : bodiesQueue) { TransformComponent t = tm.get(entity); BodyComponent body = bm.get(entity); Vector2 pos = body.body.getPosition(); pos.x = t.pos.x; pos.y = t.pos.y; body.body.setTransform(pos, t.rotation * MathUtils.degreesToRadians); } } bodiesQueue.clear(); }
@Override public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.graphics.setTitle(String.format(GAME_TITLE, Gdx.graphics.getFramesPerSecond())); world.step(TIME_STEP, 8, 3); viewport.getCamera().position.x = ninjaRabbit.getBody() == null ? 0.0f : ninjaRabbit.getBody().getPosition().x + viewport.getWorldWidth() / 4.0f; viewport.getCamera().update(); batch.setProjectionMatrix(viewport.getCamera().combined); tileMapRenderer.setView((OrthographicCamera) viewport.getCamera()); tileMapRenderer.render(); batch.begin(); ninjaRabbit.update(batch); batch.end(); b2dRenderer.render(world, viewport.getCamera().combined); }
@Override public void update(float deltaTime) { super.update(deltaTime); float frameTime = Math.min(deltaTime, 0.25f); accumulator += frameTime; if (accumulator >= MAX_STEP_TIME) { world.step(MAX_STEP_TIME, 6, 2); accumulator -= MAX_STEP_TIME; // Entity Queue for (Entity entity : bodiesQueue) { TransformComponent tfm = tm.get(entity); BodyComponent bodyComp = bm.get(entity); Vector2 position = bodyComp.body.getPosition(); tfm.position.x = position.x; tfm.position.y = position.y; tfm.rotation = bodyComp.body.getAngle() * MathUtils.radiansToDegrees; } } bodiesQueue.clear(); }
public void update(float dt) { handleInput(); world.step(dt, 6, 2); Array<Body> bodies = cl.getBodiesToKill(); for (int i = 0; i < bodies.size; i++) { Body b = bodies.get(i); coins.removeValue((Coin) b.getUserData(), true); world.destroyBody(b); player.collectCoin(); } bodies.clear(); player.update(dt); for (int i = 0; i < coins.size; i++) { coins.get(i).update(dt); } if (player.getBody().getLinearVelocity().x > 0) { Game.ass.loadTexture("res/images/bunny.png", "bunny"); } else if (player.getBody().getLinearVelocity().x < 0) { Game.ass.loadTexture("res/images/bunny2.png", "bunny"); } }
@Override public void act(float delta) { super.act(delta); timeStep = delta / 1000.0f; world.step(timeStep, velocityIterations, positionIterations); Scene scene = (Scene) gui.getScene().getElement(); if (scene != null) { for (SceneElement e : scene.getSceneElements()) { ValueMap valueMap = game.getGameState(); Body b = (Body) valueMap.getValue(e.getId(), VAR_PH_BODY, null); if (b != null) { valueMap.setValue(e, SceneElement.VAR_X, (float) (b.getWorldCenter().x * WORLD_SCALE)); valueMap.setValue(e, SceneElement.VAR_Y, (float) (b.getWorldCenter().y * WORLD_SCALE)); valueMap.setValue(e, SceneElement.VAR_ROTATION, (float) Math.toDegrees(b.getAngle())); } } } else { stop(); } }
private void update() { tokine.update(); world.step(Gdx.graphics.getDeltaTime(), 3, 3); }
void update(float delta) { world.step(delta, 6, 300); // detect allowed for player space if (playerBody.getPosition().x > frontEdge) { frontEdge = playerBody.getPosition().x; backEdge = frontEdge - (16 + 4) < 0 ? 0 : frontEdge - (16 + 4); } // broadcast player position shared.pPos.update(pos -> pos.set(playerBody.getPosition())); // broadcast player cable blocks position for (int idx = 0; idx < cable.getBodyList().size(); idx++) { final Vector2 blockPos = cable.getBodyList().get(idx).getPosition(); shared.cableDots.update(idx, pos -> pos.set(blockPos)); } // broadcast bots positions bots.entrySet() .forEach( (kv) -> { int id = kv.getKey(); shared.bots.update(id, data -> data.pos.set(kv.getValue().getPosition())); }); if (playerInAttack) { // raycast via bots Vector2 fireTo = new Vector2(); Vector2 pPos = playerBody.getPosition(); if (shared.pDirection.get().dirX == DirectionX.RIGHT) fireTo.x = pPos.x + 8; else fireTo.x = pPos.x - 8; switch (shared.pDirection.get().dirY) { case UP: fireTo.y = pPos.y + 8; break; case MIDDLE: fireTo.y = pPos.y; break; case DOWN: fireTo.y = pPos.y - 8; break; } world.rayCast( (fixture, point, normal, fraction) -> { if (fixture.getUserData() == null) return -1; if (fixture.getUserData().getClass() == BotUserData.class) { BotUserData data = (BotUserData) fixture.getUserData(); data.health -= delta; // System.out.print("Bot with id:" + data.id + "on damage!"); if (data.health < 0) { if (!data.killed) { System.out.println("Bot with id:" + data.id + "died!"); botKilled.write( data.id, () -> { System.out.println("World locked? =>" + world.isLocked()); Body botBody = bots.get(data.id); world.destroyBody(botBody); bots.remove(data.id); shared.bots.free(data.id); }); data.killed = true; } } } return -1; }, pPos, fireTo); } }
public void Update(float delta) { if (m_FadeIn < 1) { m_FadeIn += delta / 10; m_BackgroundMusic.setVolume(m_GameScreen.m_Settings.m_BackgroundMusicVolume * m_FadeIn); } // update and render the tilemap m_TiledMapRenderer.setView(m_GameScreen.m_Camera); m_TiledMapRenderer.render(); // update physics system m_PhysicsWorld.step(1f / 60f, 6, 2); // update and render the artemis entity system m_ArtemisWorld.setDelta(delta); m_ArtemisWorld.process(); m_SpineRenderSystem.process(); m_GameScreen.m_Camera.update(); m_GameScreen.GetSpriteBatch().setProjectionMatrix(m_GameScreen.m_Camera.combined); // update the player's light distance from watch float distance = 225; if (!m_WatchSpine.m_State.getAnimation().getName().contentEquals("tick")) { m_ClockTicking = false; } if (m_WatchSpine.m_State.getAnimation().getName().contentEquals("tick")) { if (!m_ClockTicking) { m_ClockTick.play(m_GameScreen.m_Settings.m_GameSoundVolume / 2); m_ClockTicking = true; } float time = m_WatchSpine.m_State.getTime(); if (time < 10.0f) { float timePercentageComplete = (10 - time) / 10.0f; distance += (timePercentageComplete * 300); m_LightPowerDistance = timePercentageComplete * 300; } } else if (m_WatchSpine.m_State.getAnimation().getName().contentEquals("wind")) { float time = m_WatchSpine.m_State.getTime(); float timePercentageComplete = time / m_WatchSpine.m_State.getAnimation().getDuration(); distance += (timePercentageComplete * 300); m_LightPowerDistance = timePercentageComplete * 300; } // update the players light distance sensor m_PlayerEntity .m_LightDistanceSensor .getShape() .setRadius((m_LightPowerDistance * WORLD_TO_BOX) / 2); if (m_PlayerEntity.m_Entity != null) { BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class); if (bodyComponent != null) { bodyComponent.m_Light.setDistance(distance * WORLD_TO_BOX); } } // render the lighting m_RayHandler.setCombinedMatrix(m_GameScreen.m_Camera.combined.scl(BOX_TO_WORLD)); m_RayHandler.updateAndRender(); // draw border m_BorderSprite.setColor(1, 1, 1, m_FadeIn * 0.5f); m_BorderSprite.setPosition( m_GameScreen.m_Camera.position.x - 400, m_GameScreen.m_Camera.position.y - 300); m_GameScreen.GetSpriteBatch().begin(); m_BorderSprite.draw(m_GameScreen.GetSpriteBatch()); m_GameScreen.GetSpriteBatch().end(); // draw the vials m_VialOne.m_Skeleton.getColor().set(1, 1, 1, m_FadeIn * 0.75f); m_VialOne.m_Skeleton.setX(m_GameScreen.m_Camera.position.x - 375); m_VialOne.m_Skeleton.setY(m_GameScreen.m_Camera.position.y - 275); m_VialOne.m_Skeleton.getRootBone().setRotation(m_VialOne.m_Rotation); m_VialOne.m_State.update(Gdx.graphics.getDeltaTime()); m_VialOne.m_State.apply(m_VialOne.m_Skeleton); m_VialOne.m_Skeleton.updateWorldTransform(); m_VialTwo.m_Skeleton.getColor().set(1, 1, 1, m_FadeIn * 0.75f); m_VialTwo.m_Skeleton.setX(m_GameScreen.m_Camera.position.x - 305); m_VialTwo.m_Skeleton.setY(m_GameScreen.m_Camera.position.y - 275); m_VialTwo.m_Skeleton.getRootBone().setRotation(m_VialTwo.m_Rotation); m_VialTwo.m_State.update(Gdx.graphics.getDeltaTime()); m_VialTwo.m_State.apply(m_VialTwo.m_Skeleton); m_VialTwo.m_Skeleton.updateWorldTransform(); m_VialThree.m_Skeleton.getColor().set(1, 1, 1, m_FadeIn * 0.75f); m_VialThree.m_Skeleton.setX(m_GameScreen.m_Camera.position.x - 390); m_VialThree.m_Skeleton.setY(m_GameScreen.m_Camera.position.y - 205); m_VialThree.m_Skeleton.getRootBone().setRotation(m_VialThree.m_Rotation); m_VialThree.m_State.update(Gdx.graphics.getDeltaTime()); m_VialThree.m_State.apply(m_VialThree.m_Skeleton); m_VialThree.m_Skeleton.updateWorldTransform(); // draw watch last m_WatchSpine.m_Skeleton.setX(m_GameScreen.m_Camera.position.x + 300); m_WatchSpine.m_Skeleton.setY(m_GameScreen.m_Camera.position.y + 200); m_WatchSpine.m_State.update(Gdx.graphics.getDeltaTime()); m_WatchSpine.m_State.apply(m_WatchSpine.m_Skeleton); m_WatchSpine.m_Skeleton.updateWorldTransform(); m_GameScreen.GetSpriteBatch().begin(); m_SpineRenderSystem.m_Renderer.draw(m_GameScreen.GetSpriteBatch(), m_VialOne.m_Skeleton); m_SpineRenderSystem.m_Renderer.draw(m_GameScreen.GetSpriteBatch(), m_VialTwo.m_Skeleton); m_SpineRenderSystem.m_Renderer.draw(m_GameScreen.GetSpriteBatch(), m_VialThree.m_Skeleton); m_SpineRenderSystem.m_Renderer.draw(m_GameScreen.GetSpriteBatch(), m_WatchSpine.m_Skeleton); m_GameScreen.GetSpriteBatch().end(); }
@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(); }
@Override public void render() { Gdx.gl.glClearColor(0.1f, 0.1f, 0.2f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); world.step(1 / 60f, 6, 2); camera.position.set(ship.getPosition().x, ship.getPosition().y, 0); ship.fixture.getBody().setAngularVelocity(0); Vector2 direction = null; if (Gdx.input.isKeyJustPressed(Input.Keys.Q) || Gdx.input.isTouched()) { ship.shoot(); ship.mouseMoved(camera, Gdx.input.getX(), WINDOW_HEIGHT - Gdx.input.getY()); } if (Gdx.input.isKeyPressed(Input.Keys.A)) { direction = new Vector2(-1, 0); ship.applyForce(direction); } if (Gdx.input.isKeyPressed(Input.Keys.D)) { direction = new Vector2(1, 0); ship.applyForce(direction); } if (Gdx.input.isKeyPressed(Input.Keys.S)) { direction = new Vector2(0, -1); ship.applyForce(direction); } if (Gdx.input.isKeyPressed(Input.Keys.W)) { direction = new Vector2(0, 1); ship.applyForce(direction); } if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) { ship.stop(); } if (Gdx.input.isKeyJustPressed(Input.Keys.N)) { Random random = new Random(); float size = random.nextFloat() * 30 + 20; float x = random.nextFloat() * (WINDOW_WIDTH / 2 - size); float y = random.nextFloat() * (WINDOW_HEIGHT / 2 - size); System.out.printf("Spawned %f at %f %f\n", size, x, y); final Asteroid asteroid = Asteroid.createIn(world, size, x, y); asteroids.add(asteroid); } camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); ship.draw(batch); for (Asteroid asteroid : asteroids) { asteroid.draw(batch); } BitmapFont font = new BitmapFont(); Body body = ship.fixture.getBody(); Vector2 velocity = body.getLinearVelocity(); batch.end(); hudBatch.begin(); String state = String.format( "Velocity: %f %f\nPosition: %f %f\nZoom: %f", velocity.x, velocity.y, body.getPosition().x, body.getPosition().y, camera.zoom); font.draw(hudBatch, state, 50, 50); hudBatch.end(); box2dRenderer.render(world, camera.combined); }
@Override public void render() { // first we update the world. For simplicity // we use the delta time provided by the Graphics // instance. Normally you'll want to fix the time // step. long start = TimeUtils.nanoTime(); world.step(Gdx.graphics.getDeltaTime(), 8, 3); float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f; // next we clear the color buffer and set the camera // matrices Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); // next we render the ground body renderBox(groundBody, 50, 1); // next we render each box via the SpriteBatch. // for this we have to set the projection matrix of the // spritebatch to the camera's combined matrix. This will // make the spritebatch work in world coordinates batch.getProjectionMatrix().set(camera.combined); batch.begin(); for (int i = 0; i < boxes.size(); i++) { Body box = boxes.get(i); Vector2 position = box.getPosition(); // that's the box's center position float angle = MathUtils.radiansToDegrees * box.getAngle(); // the rotation angle around the center batch.draw( textureRegion, position.x - 1, position.y - 1, // the bottom left corner of the box, unrotated 1f, 1f, // the rotation center relative to the bottom left corner of the box 2, 2, // the width and height of the box 1, 1, // the scale on the x- and y-axis angle); // the rotation angle } batch.end(); // next we use the debug renderer. Note that we // simply apply the camera again and then call // the renderer. the camera.apply() call is actually // not needed as the opengl matrices are already set // by the spritebatch which in turn uses the camera matrices :) debugRenderer.render(world, camera.combined); // finally we render all contact points renderer.setProjectionMatrix(camera.combined); renderer.begin(ShapeType.Point); renderer.setColor(0, 1, 0, 1); for (int i = 0; i < world.getContactCount(); i++) { Contact contact = world.getContactList().get(i); // we only render the contact if it actually touches if (contact.isTouching()) { // get the world manifold from which we get the // contact points. A manifold can have 0, 1 or 2 // contact points. WorldManifold manifold = contact.getWorldManifold(); int numContactPoints = manifold.getNumberOfContactPoints(); for (int j = 0; j < numContactPoints; j++) { Vector2 point = manifold.getPoints()[j]; renderer.point(point.x, point.y, 0); } } } renderer.end(); // finally we render the time it took to update the world // for this we have to set the projection matrix again, so // we work in pixel coordinates batch .getProjectionMatrix() .setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.begin(); font.draw( batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, 20); batch.end(); }