Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (206)
games submitted by our members
Games in WIP (781)
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: Lunch (posted 2016-12-06 16:01:40, viewed 930 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  
public class BulletCam extends ApplicationAdapter implements InputProcessor {

   public static OrthographicCamera Camera;
   public static ModelBatch Modelbatch;
   public static AssetManager Assets;
   public ArrayList<GameObject> ObjectsArray;

   public static btCollisionWorld CollisionWorld;
   public static btBroadphaseInterface Broadphase;
   public static btCollisionConfiguration CollisionConfig;
   public static btDispatcher Dispatcher;
   public static ClosestRayResultCallback RayCallback;
   public static MyContactListener Contact;

   private float startX, startY;
   private final Vector3 tmpV1 = new Vector3();
   private final Vector3 tmpV2 = new Vector3();
   public float translateUnits = 22f;

   public Ray Ray;
   public Vector3 RayFrom = new Vector3(), RayTo = new Vector3();

   public boolean Loading;

   public GameObject Floor, Cube1, Cube2;

   class MyContactListener extends ContactListener {
      @Override
      public boolean onContactAdded (int userValue0, int partId0, int index0, int userValue1, int partId1, int index1) {

         return true;
      }
   }

   public class GameObject extends ModelInstance implements Disposable {

      public final btCollisionObject Body;
      public final Vector3 Center = new Vector3();
      public final Vector3 Dimension = new Vector3();
      public final float Radius;

      public final BoundingBox Bounds = new BoundingBox();

      public GameObject(Model model, btCollisionShape btCollisionShape) {
         super(model);
         calculateBoundingBox(Bounds);
         Bounds.getCenter(Center);
         Bounds.getDimensions(Dimension);
         Radius = Dimension.len() / 2f;
         Body = new btCollisionObject();
         Body.setCollisionShape(btCollisionShape);
      }

      @Override
      public void dispose() {
         Body.dispose();
      }
   }
   
   @Override
   public void create () {
      Bullet.init();
      Camera = new OrthographicCamera(16, 16 * (Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth()));
      Camera.position.set(5, 5, 10);
      Camera.direction.set(-1, -1, -1);
      Camera.up.set(0, 1, 0);
      Camera.near = 1;
      Camera.far = 100;

      Assets = new AssetManager();

      Modelbatch = new ModelBatch();
      ObjectsArray = new ArrayList<GameObject>();

      RayCallback = new ClosestRayResultCallback(Vector3.Zero, Vector3.Z);
      CollisionConfig = new btDefaultCollisionConfiguration();
      Dispatcher = new btCollisionDispatcher(CollisionConfig);
      Broadphase = new btDbvtBroadphase();
      CollisionWorld = new btCollisionWorld(Dispatcher, Broadphase, CollisionConfig);
      Contact = new MyContactListener();

      Loading = true;

      Assets.load("ExampleMap.g3dj", Model.class);
      Assets.load("ExampleCube1.g3dj", Model.class);
      Assets.load("ExampleCube2.g3dj", Model.class);
      Assets.update();

      Gdx.input.setInputProcessor(this);
   }

   @Override
   public void render () {
      Gdx.gl.glClearColor(0, 0, 0, 1);

      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

      Modelbatch.begin(Camera);
      Modelbatch.render(ObjectsArray);
      Modelbatch.end();

      if (Loading && Assets.update()) {
         LoadObjects();
      }

      Camera.update();
      CollisionWorld.updateAabbs();
      CollisionWorld.performDiscreteCollisionDetection();

   }

   private void LoadObjects() {
      Floor = new GameObject(Assets.get("ExampleMap.g3dj", Model.class), new btBoxShape(new Vector3(40f,0.1f,40f)));
      Floor.transform.setToTranslation(40,0,40);
      Floor.Body.setUserValue(ObjectsArray.size());
      Floor.Body.setWorldTransform(Floor.transform);
      Floor.Body.setCollisionFlags(Floor.Body.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
      ObjectsArray.add(Floor);
      CollisionWorld.addCollisionObject(Floor.Body);

      Cube1 = new GameObject(Assets.get("ExampleCube1.g3dj", Model.class), new btBoxShape(new Vector3(1f,1f,1f)));
      Cube1.transform.setToTranslation(1,1,1);
      Cube1.Body.setUserValue(ObjectsArray.size());
      Cube1.Body.setWorldTransform(Cube1.transform);
      Cube1.Body.setCollisionFlags(Cube1.Body.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
      ObjectsArray.add(Cube1);
      CollisionWorld.addCollisionObject(Cube1.Body);

      Cube2 = new GameObject(Assets.get("ExampleCube2.g3dj", Model.class), new btBoxShape(new Vector3(1f,1f,1f)));
      Cube2.transform.setToTranslation(4,1,8);
      Cube2.Body.setUserValue(ObjectsArray.size());
      Cube2.Body.setWorldTransform(Cube2.transform);
      Cube2.Body.setCollisionFlags(Cube2.Body.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
      ObjectsArray.add(Cube2);
      CollisionWorld.addCollisionObject(Cube2.Body);

      Loading = false;

      System.out.println("Loaded");

   }

   @Override
   public void dispose() {
      CollisionWorld.dispose();
      Modelbatch.dispose();
      Assets.dispose();
      for (GameObject obj: ObjectsArray) {
         obj.dispose();
      }
   }

   @Override
   public boolean keyDown(int keycode) {
      return false;
   }

   @Override
   public boolean keyUp(int keycode) {
      return false;
   }

   @Override
   public boolean keyTyped(char character) {
      return false;
   }

   @Override
   public boolean touchDown(int screenX, int screenY, int pointer, int button) {

      startX = screenX;
      startY = screenY;

      Ray = Camera.getPickRay(screenX, screenY);
      RayFrom.set(Ray.origin);
      RayTo.set(Ray.direction.scl(50)).add(Ray.origin);

      RayCallback.setCollisionObject(null);
      RayCallback.setClosestHitFraction(1f);
      RayCallback.setRayFromWorld(RayFrom);
      RayCallback.setRayToWorld(RayTo);

      CollisionWorld.rayTest(RayFrom, RayTo, RayCallback);

      if (RayCallback.hasHit()) {
         System.out.println("HIT");
      }
      return true;
   }

   @Override
   public boolean touchUp(int screenX, int screenY, int pointer, int button) {
      return false;
   }

   @Override
   public boolean touchDragged(int screenX, int screenY, int pointer) {
      final float deltaX = (screenX - startX) / Gdx.graphics.getWidth();
      final float deltaY = (startY - screenY) / Gdx.graphics.getHeight();
      startX = screenX;
      startY = screenY;
      return DragProcess(deltaX, deltaY);
   }

   private boolean DragProcess(float deltaX, float deltaY) {
      Camera.translate(tmpV1.set(Camera.direction).crs(Camera.up).nor().scl(-deltaX * translateUnits));
      Camera.translate(tmpV2.set(Camera.up).scl(-deltaY * translateUnits));
      return true;
   }

   @Override
   public boolean mouseMoved(int screenX, int screenY) {
      return false;
   }

   @Override
   public boolean scrolled(int amount) {
      return false;
   }
}





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
 
Galdo (228 views)
2017-01-12 13:44:09

Archive (396 views)
2017-01-02 05:31:41

0AndrewShepherd0 (855 views)
2016-12-16 03:58:39

0AndrewShepherd0 (792 views)
2016-12-15 21:50:57

Lunch (929 views)
2016-12-06 16:01:40

ral0r2 (1157 views)
2016-11-23 16:08:26

ClaasJG (1262 views)
2016-11-10 17:36:32

CoffeeChemist (1302 views)
2016-11-05 00:46:53

jay4842 (1386 views)
2016-11-01 19:04:52

theagentd (1206 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21
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!