Advertisement
bekovski

collision

Mar 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. package com.badlogic.androidgames.gamedev2d;
  2.  
  3. import com.badlogic.androidgames.gamedev2d.framework.DynamicGameObject;
  4. import com.badlogic.androidgames.gamedev2d.framework.Game;
  5. import com.badlogic.androidgames.gamedev2d.framework.GameObject;
  6. import com.badlogic.androidgames.gamedev2d.framework.Input.*;
  7. import com.badlogic.androidgames.gamedev2d.framework.Screen;
  8. import com.badlogic.androidgames.gamedev2d.framework.gl.Vertices;
  9. import com.badlogic.androidgames.gamedev2d.framework.impl.GLGame;
  10. import com.badlogic.androidgames.gamedev2d.framework.impl.GLGraphics;
  11. import com.badlogic.androidgames.gamedev2d.framework.math.OverlapTester;
  12. import com.badlogic.androidgames.gamedev2d.framework.math.Vector2;
  13.  
  14. import java.util.List;
  15.  
  16. import javax.microedition.khronos.opengles.GL10;
  17.  
  18. /**
  19.  * Created by Kebir on 16.03.2018.
  20.  */
  21. public class CollisionTest extends GLGame {
  22.  
  23.     @Override
  24.     public Screen getStartScreen() {
  25.         return new CollisionScreen(this);
  26.     }
  27.  
  28.     class CollisionScreen extends Screen {
  29.  
  30.         final int NUM_TARGETS = 20;
  31.         final float WORLD_WIDTH = 9.6f;
  32.         final float WORLD_HEIGHT = 6.4f;
  33.  
  34.         GLGraphics glGraphics;
  35.         Cannon cannon;
  36.         DynamicGameObject cannonBall;
  37.         GameObject[] targets;
  38.        
  39.  
  40.         Vertices cannonVertices;
  41.         Vertices cannonBallVertices;
  42.         Vertices targetVertices;
  43.  
  44.         float cannonAngle = 0.0f;
  45.         Vector2 touchPoint = new Vector2();
  46.         Vector2 gravity = new Vector2(0, -7);
  47.  
  48.  
  49.         public CollisionScreen(Game game) {
  50.             super(game);
  51.  
  52.             glGraphics = ((GLGame)game).getGLGraphics();
  53.             cannon = new Cannon(0, 0, 1, 1);
  54.             cannonBall = new DynamicGameObject(0, 0, 0.1f, 0.1f);
  55.             targets = new GameObject[NUM_TARGETS];
  56.             for(int i=0; i < targets.length; ++i) {
  57.                 targets[i] = new GameObject(
  58.                         (float)Math.random() * WORLD_WIDTH,
  59.                         (float)Math.random() * WORLD_HEIGHT,
  60.                         0.2f,
  61.                         0.2f);
  62.             }
  63.  
  64.             // the cannon
  65.             float[] vertices = new float[] {
  66.                     -0.5f, -0.5f,
  67.                      0.5f,  0.0f,
  68.                     -0.5f,  0.5f
  69.             };
  70.             cannonVertices = new Vertices(glGraphics, 3, 0, false, false);
  71.             cannonVertices.setVertices(vertices, 0, vertices.length);
  72.  
  73.             // the cannon ball
  74.             vertices = new float[] {
  75.                     0.0f, 0.0f,
  76.                     0.1f, 0.0f,
  77.                     0.1f, 0.1f,
  78.                     0.0f, 0.1f,
  79.             };
  80.             short[] indices = new short[] {
  81.                     0, 1, 2,
  82.                     2, 3, 0
  83.             };
  84.             cannonBallVertices = new Vertices(glGraphics, 4, 6, false, false);
  85.             cannonBallVertices.setVertices(vertices, 0, vertices.length);
  86.             cannonBallVertices.setIndices(indices, 0, indices.length);
  87.  
  88.             // the target
  89.             vertices = new float[] {
  90.                     0.0f, 0.0f,
  91.                     0.2f, 0.0f,
  92.                     0.2f, 0.2f,
  93.                     0.0f, 0.2f
  94.             };
  95.             indices = new short[] {
  96.                     0, 1, 2,
  97.                     2, 3, 0
  98.             };
  99.             targetVertices = new Vertices(glGraphics, 4, 6, false, false);
  100.             targetVertices.setVertices(vertices, 0, vertices.length);
  101.             targetVertices.setIndices(indices, 0, indices.length);
  102.         }
  103.  
  104.         @Override
  105.         public void update(float deltaTime) {
  106.             List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
  107.             game.getInput().getKeyEvents();
  108.  
  109.             for(int i=0; i < touchEvents.size(); ++i) {
  110.                 TouchEvent event = touchEvents.get(i);
  111.  
  112.                 touchPoint.x = (event.x / (float)glGraphics.getWidth()) * WORLD_WIDTH;
  113.                 touchPoint.y = (1 - (event.y / (float)glGraphics.getHeight())) * WORLD_HEIGHT;
  114.  
  115.                 cannonAngle = touchPoint.sub(cannon.position).angle();
  116.  
  117.                 if(event.type == TouchEvent.TOUCH_UP) {
  118.                     float radians = cannonAngle * Vector2.TO_RADIANS;
  119.                     float ballSpeed = touchPoint.dist(cannon.position);
  120.  
  121.                     cannonBall.position.set(cannon.position);
  122.                     cannonBall.velocity.x = (float)Math.cos(radians) * ballSpeed;
  123.                     cannonBall.velocity.y = (float)Math.sin(radians) * ballSpeed;
  124.                 }
  125.             }
  126.  
  127.             for(int j=0; j < targets.length; ++j) {
  128.                 if(OverlapTester.overlapRectangles(cannonBall.bounds, targets[j].bounds)) {
  129.                     targets[j].position.add(0.5f, 0.1f);
  130.                     targets[j].bounds.lowerLeft.add(0.5f, 0.1f);
  131.                 }
  132.             }
  133.  
  134.             cannonBall.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
  135.             cannonBall.position.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
  136.             cannonBall.bounds.lowerLeft.set(cannonBall.position.x - cannonBall.bounds.width/2, cannonBall.position.y - cannonBall.bounds.height/2);
  137.         }
  138.  
  139.         @Override
  140.         public void present(float deltaTime) {
  141.             GL10 gl = glGraphics.getGL();
  142.  
  143.             // clear the screen with black
  144.             gl.glClearColor(0, 0, 0, 1);
  145.             gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  146.             // set the viewport to span the whole framebuffer
  147.             gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
  148.             // set up the projection matrix
  149.             gl.glMatrixMode(GL10.GL_PROJECTION);
  150.             gl.glLoadIdentity();
  151.             gl.glOrthof(0.0f, WORLD_WIDTH, 0.0f, WORLD_HEIGHT, 1.0f, -1.0f);
  152.  
  153.             // the cannon
  154.             gl.glMatrixMode(GL10.GL_MODELVIEW);
  155.             gl.glLoadIdentity();
  156.             gl.glTranslatef(cannon.position.x, cannon.position.y, 0);
  157.             gl.glRotatef(cannonAngle, 0, 0, 1);
  158.                 // color white for the cannon
  159.                 gl.glColor4f(1, 1, 1, 1);
  160.             cannonVertices.bind();
  161.             cannonVertices.draw(GL10.GL_TRIANGLES, 0, 3);
  162.             cannonVertices.unbind();
  163.  
  164.             // the cannonBall
  165.             gl.glLoadIdentity();
  166.             gl.glTranslatef(cannonBall.position.x, cannonBall.position.y, 0);
  167.                 // color red for the cannonBall
  168.                 gl.glColor4f(1, 0, 0, 1);
  169.             cannonBallVertices.bind();
  170.             cannonBallVertices.draw(GL10.GL_TRIANGLES, 0, 6);
  171.             cannonBallVertices.unbind();
  172.  
  173.             // the target(s)
  174.  
  175.             targetVertices.bind();
  176.             for(int i=0; i < targets.length; ++i) {
  177.                 gl.glLoadIdentity();
  178.                 gl.glTranslatef(targets[i].position.x, targets[i].position.y, 0);
  179.                     // color blue for the targets
  180.                     gl.glColor4f(0, 0, 1, 1);
  181.  
  182.                 targetVertices.draw(GL10.GL_TRIANGLES, 0, 6);
  183.             }
  184.             targetVertices.unbind();
  185.  
  186.         }
  187.  
  188.         @Override
  189.         public void pause() {
  190.  
  191.         }
  192.  
  193.         @Override
  194.         public void resume() {
  195.  
  196.         }
  197.  
  198.         @Override
  199.         public void dispose() {
  200.  
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement