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 boolean touchDown(int x, int y, int pointer, int newParam) { // translate the mouse coordinates to world coordinates testPoint.set(x, y, 0); camera.unproject(testPoint); // ask the world which bodies are within the given // bounding box around the mouse pointer hitBody = null; world.QueryAABB( callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f); // if we hit something we create a new mouse joint // and attach it to the hit body. if (hitBody != null) { MouseJointDef def = new MouseJointDef(); def.bodyA = groundBody; def.bodyB = hitBody; def.collideConnected = true; def.target.set(testPoint.x, testPoint.y); def.maxForce = 1000.0f * hitBody.getMass(); mouseJoint = (MouseJoint) world.createJoint(def); hitBody.setAwake(true); } else { for (Body box : boxes) world.destroyBody(box); boxes.clear(); createBoxes(); } return false; }
/** * attaches this object to a real world * * @param world */ public void attachTo(World world) { // Create a body from the defintion and add it to the world body = world.createBody(def); if (userData != null) { userData.attachPhysicalObject(this); } fixture = body.createFixture(fixtureDef); // fixtureDef.shape.dispose(); body.setUserData(userData); if (attrs.containsKey("mass")) { changeMass(Float.parseFloat(attrs.get("mass"))); } body.setAwake(true); }
@Override public void dispose() { body.setActive(false); body.setAwake(false); Iterator<Joint> joints = world.getJoints(); while (joints.hasNext()) { Joint joint = joints.next(); if (joint.getBodyA() == body || joint.getBodyB() == body) { world.destroyJoint(joint); } } world.destroyBody(body); body = null; }
@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(); }