Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: matheus23 (posted 2012-11-19 11:00:28, viewed 419 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250  
/*
 * Copyright (c) 2012 matheusdev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.matheusdev.test;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glViewport;

import javax.swing.JOptionPane;

import mdesl.graphics.SpriteBatch;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.matheusdev.util.FastRand;
import org.matheusdev.util.collision.Circle;
import org.matheusdev.util.collision.Collision;
import org.matheusdev.util.collision.Poly;
import org.matheusdev.util.collision.Quad;
import org.matheusdev.util.gameloop.GameLoop;
import org.matheusdev.util.gameloop.GameLooper;
import org.matheusdev.util.vecmath.Vec2;
import org.matheusdev.util.vecmath.Vec3;

/**
 * @author matheusdev
 *
 */
public class SATTest implements GameLooper {

   public static int objects;

   public static void main(String[] args) {
      do {
         try {
            objects = Integer.parseInt(JOptionPane.showInputDialog(
                  "How much Polygons? (each one will have 8 vertices)\n" +
                  "This window will return, if you enter invalid values."));
         } catch (Exception e) {}
      } while (objects <= 0);
      new GameLoop(new SATTest(), GameLoop.Type.LIMITED_DELTA, 60).start();
   }

   protected Poly[] polys;
   protected boolean[] collisions;
   protected Quad quad = new Quad(
         new Vec2(), 160, 160);

   protected SpriteBatch batch;
   protected boolean colliding;

   public SATTest() {
   }

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.NanoTime#getNanoTime()
    */
   @Override
   public long getNanoTime() {
      return (Sys.getTime() * 1_000_000_000) / Sys.getTimerResolution();
   }

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.GameLooper#init()
    */
   @Override
   public void init() {
      try {
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      try {
         batch = new SpriteBatch();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      resize(Display.getWidth(), Display.getHeight());

      polys = new Poly[objects];
      collisions = new boolean[objects];

      FastRand rand = new FastRand();

      for (int i = 0; i < polys.length; i++) {
         polys[i] = new Poly(
               new Vec2(-30 + rand.randFloat(-5, 5),  10 + rand.randFloat(-5, 5)),
               new Vec2(-10 + rand.randFloat(-5, 5),  30 + rand.randFloat(-5, 5)),
               new Vec2( 10 + rand.randFloat(-5, 5),  30 + rand.randFloat(-5, 5)),
               new Vec2( 30 + rand.randFloat(-5, 5),  10 + rand.randFloat(-5, 5)),
               new Vec2( 30 + rand.randFloat(-5, 5), -10 + rand.randFloat(-5, 5)),
               new Vec2( 10 + rand.randFloat(-5, 5), -30 + rand.randFloat(-5, 5)),
               new Vec2(-10 + rand.randFloat(-5, 5), -30 + rand.randFloat(-5, 5)),
               new Vec2(-30 + rand.randFloat(-5, 5), -10 + rand.randFloat(-5, 5)));
         polys[i].getMatrix().translate(new Vec2(rand.randFloat(0, 800), rand.randFloat(0, 600)))
                        .rotate(rand.randFloat(), Vec3.UP);
         polys[i].updateFromMatrix();
      }
   }

   public void resize(int w, int h) {
      glViewport(0, 0, w, h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, w, h, 0, -1, 1);
      glMatrixMode(GL_MODELVIEW);
      batch.resize(w, h);
   }

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.GameLooper#isCloseRequested()
    */
   @Override
   public boolean isCloseRequested() {
      return Display.isCloseRequested();
   }

   protected float rotation = 0;
   protected float time = 0;

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.GameLooper#tick(double)
    */
   @Override
   public void tick(double delta) {
      time += (float) delta;
      int mx = Mouse.getX();
      int my = (Display.getHeight() + 1) - Mouse.getY();

      int dwheel = Mouse.getDWheel();
      if (dwheel > 0) {
         rotation += 0.1;
      } else if (dwheel < 0) {
         rotation -= 0.1;
      }
      quad.getMatrix()
            .identity().translate(new Vec2(mx, my)).rotate(rotation, Vec3.UP);
      quad.updateFromMatrix();


      for (int i = 0; i < collisions.length; i++) {
         collisions[i] = false;
      }
      Collision.axesTested = 0;
      Collision.objectsTested = 0;
      long time = (Sys.getTime() * 1_000) / Sys.getTimerResolution();
      colliding = false;
      for (int i = 0; i < polys.length; i++) {
         if (Collision.polyVsPoly(quad, polys[i])) {
            collisions[i] = true;
            colliding = true;
         }
      }
      System.out.println("Time taken: " + (((Sys.getTime() * 1_000) / Sys.getTimerResolution())-time) + ", axis tested: " + Collision.axesTested + ", objects tested: " + Collision.objectsTested);
   }

   private int count = 0;

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.GameLooper#render(double)
    */
   @Override
   public void render(double fps) {
      glClear(GL_COLOR_BUFFER_BIT);

      glColor3f(1, 1, 1);
      for (int i = 0; i < polys.length; i++) {
         if (collisions[i]) {
            glColor3f(1, 0, 0);
         } else {
            glColor3f(1, 1, 1);
         }
         drawVertices(polys[i].getTransformedVertices());
      }
      if (colliding) {
         glColor3f(1, 0, 0);
      } else {
         glColor3f(1, 1, 1);
      }
      drawVertices(quad.getTransformedVertices());

      if (count == 60) {
         count = 0;
         System.out.println("Rendered, fps: " + fps);
      }
      count++;
      Display.update();
   }

   public void drawVertices(Vec2[] vertices) {
      glBegin(GL_LINE_LOOP);
      for (Vec2 vertex : vertices) {
         glVertex2f(vertex.x, vertex.y);
      }
      glEnd();
   }

   public void drawCircle(Circle circ, float interval) {
      float oneSegment = ((float) Math.PI / 180f) * interval;
      Vec2 vec = new Vec2(circ.getRadius(), 0);
      glBegin(GL_LINE_LOOP);
      for (float i = 0; i < 360f; i += interval) {
         glVertex2f(vec.x + circ.getCenter().x, vec.y + circ.getCenter().y);
         vec.rotateRad(oneSegment);
      }
      glEnd();
   }

   /* (non-Javadoc)
    * @see org.matheusdev.util.gameloop.GameLooper#dispose()
    */
   @Override
   public void dispose() {
      Display.destroy();
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (14 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (97 views)
2014-12-03 16:27:13
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!