@Override public void update(float deltaTime) { List<TouchEvent> touchEvents = game.getInput().getTouchEvents(); game.getInput().getKeyEvents(); int len = touchEvents.size(); for (int i = 0; i < len; i++) { TouchEvent event = touchEvents.get(i); camera.touchToWorld(touchPos.set(event.x, event.y)); cannon.angle = touchPos.sub(cannon.position).angle(); if (event.type == TouchEvent.TOUCH_UP) { float radians = cannon.angle * Vector2.TO_RADIANS; float ballSpeed = touchPos.len() * 2; ball.position.set(cannon.position); ball.velocity.x = FloatMath.cos(radians) * ballSpeed; ball.velocity.y = FloatMath.sin(radians) * ballSpeed; ball.bounds.lowerLeft.set(ball.position.x - 0.1f, ball.position.y - 0.1f); } } ball.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime); ball.position.add(ball.velocity.x * deltaTime, ball.velocity.y * deltaTime); ball.bounds.lowerLeft.add(ball.velocity.x * deltaTime, ball.velocity.y * deltaTime); List<GameObject> colliders = grid.getPotentialColliders(ball); len = colliders.size(); for (int i = 0; i < len; i++) { GameObject collider = colliders.get(i); if (OverlapTester.overlapRectangle(ball.bounds, collider.bounds)) { grid.removeObject(collider); targets.remove(collider); } } if (ball.position.y > 0) { camera.position.set(ball.position); camera.zoom = 1 + ball.position.y / WORLD_HEIGHT; } else { camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2); camera.zoom = 1; } }
public Camera2DScreen(Game game) { super(game); GLGraphics glGraphics = ((GLGame) game).getGLGraphics(); gl = glGraphics.getGL(); camera = new Camera2D(glGraphics, WORLD_WIDTH, WORLD_HEIGHT); cannon = new Cannon(0, 0, 1, 1); ball = new DynamicGameObject(0, 0, 0.2f, 0.2f); targets = new ArrayList<GameObject>(NUM_TARGETS); grid = new SpatialHashGrid(WORLD_WIDTH, WORLD_HEIGHT, 2.5f); for (int i = 0; i < NUM_TARGETS; i++) { GameObject target = new GameObject( (float) Math.random() * WORLD_WIDTH, (float) Math.random() * WORLD_HEIGHT, 0.5f, 0.5f); grid.insertStaticObject(target); targets.add(target); } cannonVertices = new Vertices(gl, 3, 0, false, false); cannonVertices.setVertices(new float[] {-0.5f, -0.5f, 0.5f, 0.0f, -0.5f, 0.5f}, 0, 6); ballVertices = new Vertices(gl, 4, 6, false, false); ballVertices.setVertices( new float[] {-0.1f, -0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, 0.1f}, 0, 8); ballVertices.setIndices(new short[] {0, 1, 2, 2, 3, 0}, 0, 6); targetVertices = new Vertices(gl, 4, 6, false, false); targetVertices.setVertices( new float[] {-0.25f, -0.25f, 0.25f, -0.25f, 0.25f, 0.25f, -0.25f, 0.25f}, 0, 8); targetVertices.setIndices(new short[] {0, 1, 2, 2, 3, 0}, 0, 6); }