Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.badlogic.androidgames.gamedev2d;
- import com.badlogic.androidgames.gamedev2d.framework.DynamicGameObject;
- import com.badlogic.androidgames.gamedev2d.framework.Game;
- import com.badlogic.androidgames.gamedev2d.framework.GameObject;
- import com.badlogic.androidgames.gamedev2d.framework.Input.*;
- import com.badlogic.androidgames.gamedev2d.framework.Screen;
- import com.badlogic.androidgames.gamedev2d.framework.gl.Vertices;
- import com.badlogic.androidgames.gamedev2d.framework.impl.GLGame;
- import com.badlogic.androidgames.gamedev2d.framework.impl.GLGraphics;
- import com.badlogic.androidgames.gamedev2d.framework.math.OverlapTester;
- import com.badlogic.androidgames.gamedev2d.framework.math.Vector2;
- import java.util.List;
- import javax.microedition.khronos.opengles.GL10;
- /**
- * Created by Kebir on 16.03.2018.
- */
- public class CollisionTest extends GLGame {
- @Override
- public Screen getStartScreen() {
- return new CollisionScreen(this);
- }
- class CollisionScreen extends Screen {
- final int NUM_TARGETS = 20;
- final float WORLD_WIDTH = 9.6f;
- final float WORLD_HEIGHT = 6.4f;
- GLGraphics glGraphics;
- Cannon cannon;
- DynamicGameObject cannonBall;
- GameObject[] targets;
- Vertices cannonVertices;
- Vertices cannonBallVertices;
- Vertices targetVertices;
- float cannonAngle = 0.0f;
- Vector2 touchPoint = new Vector2();
- Vector2 gravity = new Vector2(0, -7);
- public CollisionScreen(Game game) {
- super(game);
- glGraphics = ((GLGame)game).getGLGraphics();
- cannon = new Cannon(0, 0, 1, 1);
- cannonBall = new DynamicGameObject(0, 0, 0.1f, 0.1f);
- targets = new GameObject[NUM_TARGETS];
- for(int i=0; i < targets.length; ++i) {
- targets[i] = new GameObject(
- (float)Math.random() * WORLD_WIDTH,
- (float)Math.random() * WORLD_HEIGHT,
- 0.2f,
- 0.2f);
- }
- // the cannon
- float[] vertices = new float[] {
- -0.5f, -0.5f,
- 0.5f, 0.0f,
- -0.5f, 0.5f
- };
- cannonVertices = new Vertices(glGraphics, 3, 0, false, false);
- cannonVertices.setVertices(vertices, 0, vertices.length);
- // the cannon ball
- vertices = new float[] {
- 0.0f, 0.0f,
- 0.1f, 0.0f,
- 0.1f, 0.1f,
- 0.0f, 0.1f,
- };
- short[] indices = new short[] {
- 0, 1, 2,
- 2, 3, 0
- };
- cannonBallVertices = new Vertices(glGraphics, 4, 6, false, false);
- cannonBallVertices.setVertices(vertices, 0, vertices.length);
- cannonBallVertices.setIndices(indices, 0, indices.length);
- // the target
- vertices = new float[] {
- 0.0f, 0.0f,
- 0.2f, 0.0f,
- 0.2f, 0.2f,
- 0.0f, 0.2f
- };
- indices = new short[] {
- 0, 1, 2,
- 2, 3, 0
- };
- targetVertices = new Vertices(glGraphics, 4, 6, false, false);
- targetVertices.setVertices(vertices, 0, vertices.length);
- targetVertices.setIndices(indices, 0, indices.length);
- }
- @Override
- public void update(float deltaTime) {
- List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
- game.getInput().getKeyEvents();
- for(int i=0; i < touchEvents.size(); ++i) {
- TouchEvent event = touchEvents.get(i);
- touchPoint.x = (event.x / (float)glGraphics.getWidth()) * WORLD_WIDTH;
- touchPoint.y = (1 - (event.y / (float)glGraphics.getHeight())) * WORLD_HEIGHT;
- cannonAngle = touchPoint.sub(cannon.position).angle();
- if(event.type == TouchEvent.TOUCH_UP) {
- float radians = cannonAngle * Vector2.TO_RADIANS;
- float ballSpeed = touchPoint.dist(cannon.position);
- cannonBall.position.set(cannon.position);
- cannonBall.velocity.x = (float)Math.cos(radians) * ballSpeed;
- cannonBall.velocity.y = (float)Math.sin(radians) * ballSpeed;
- }
- }
- for(int j=0; j < targets.length; ++j) {
- if(OverlapTester.overlapRectangles(cannonBall.bounds, targets[j].bounds)) {
- targets[j].position.add(0.5f, 0.1f);
- targets[j].bounds.lowerLeft.add(0.5f, 0.1f);
- }
- }
- cannonBall.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
- cannonBall.position.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
- cannonBall.bounds.lowerLeft.set(cannonBall.position.x - cannonBall.bounds.width/2, cannonBall.position.y - cannonBall.bounds.height/2);
- }
- @Override
- public void present(float deltaTime) {
- GL10 gl = glGraphics.getGL();
- // clear the screen with black
- gl.glClearColor(0, 0, 0, 1);
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- // set the viewport to span the whole framebuffer
- gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
- // set up the projection matrix
- gl.glMatrixMode(GL10.GL_PROJECTION);
- gl.glLoadIdentity();
- gl.glOrthof(0.0f, WORLD_WIDTH, 0.0f, WORLD_HEIGHT, 1.0f, -1.0f);
- // the cannon
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glLoadIdentity();
- gl.glTranslatef(cannon.position.x, cannon.position.y, 0);
- gl.glRotatef(cannonAngle, 0, 0, 1);
- // color white for the cannon
- gl.glColor4f(1, 1, 1, 1);
- cannonVertices.bind();
- cannonVertices.draw(GL10.GL_TRIANGLES, 0, 3);
- cannonVertices.unbind();
- // the cannonBall
- gl.glLoadIdentity();
- gl.glTranslatef(cannonBall.position.x, cannonBall.position.y, 0);
- // color red for the cannonBall
- gl.glColor4f(1, 0, 0, 1);
- cannonBallVertices.bind();
- cannonBallVertices.draw(GL10.GL_TRIANGLES, 0, 6);
- cannonBallVertices.unbind();
- // the target(s)
- targetVertices.bind();
- for(int i=0; i < targets.length; ++i) {
- gl.glLoadIdentity();
- gl.glTranslatef(targets[i].position.x, targets[i].position.y, 0);
- // color blue for the targets
- gl.glColor4f(0, 0, 1, 1);
- targetVertices.draw(GL10.GL_TRIANGLES, 0, 6);
- }
- targetVertices.unbind();
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement