Advertisement
Guest User

TestPhysics.java

a guest
Jul 11th, 2014
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. package com.cubes.test;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. import com.cubes.BlockChunkControl;
  7. import com.cubes.BlockChunkListener;
  8. import com.cubes.BlockTerrainControl;
  9. import com.cubes.CubesSettings;
  10. import com.cubes.Vector3Int;
  11. import com.cubes.test.blocks.Block_Grass;
  12. import com.jme3.app.SimpleApplication;
  13. import com.jme3.bullet.BulletAppState;
  14. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  15. import com.jme3.bullet.collision.shapes.MeshCollisionShape;
  16. import com.jme3.bullet.control.CharacterControl;
  17. import com.jme3.bullet.control.RigidBodyControl;
  18. import com.jme3.input.KeyInput;
  19. import com.jme3.input.controls.ActionListener;
  20. import com.jme3.input.controls.KeyTrigger;
  21. import com.jme3.light.PointLight;
  22. import com.jme3.math.ColorRGBA;
  23. import com.jme3.math.Vector3f;
  24. import com.jme3.post.FilterPostProcessor;
  25. import com.jme3.renderer.queue.RenderQueue;
  26. import com.jme3.scene.Geometry;
  27. import com.jme3.scene.Node;
  28. import com.jme3.shadow.EdgeFilteringMode;
  29. import com.jme3.shadow.PointLightShadowFilter;
  30. import com.jme3.shadow.PointLightShadowRenderer;
  31. import com.jme3.system.AppSettings;
  32. import com.jme3.util.SkyFactory;
  33.  
  34. public class TestPhysics extends SimpleApplication implements ActionListener{
  35.  
  36.     public static void main(String[] args){
  37.         Logger.getLogger("").setLevel(Level.SEVERE);
  38.         TestPhysics app = new TestPhysics();
  39.         app.start();
  40.     }
  41.  
  42.     public TestPhysics(){
  43.         settings = new AppSettings(true);
  44.         settings.setWidth(1280);
  45.         settings.setHeight(720);
  46.         settings.setTitle("Cubes Demo - Physics");
  47.         settings.setFrameRate(60);
  48.     }
  49.     private final Vector3Int terrainSize = new Vector3Int(100, 30, 100);
  50.     private BulletAppState bulletAppState;
  51.     private CharacterControl playerControl;
  52.     private Vector3f walkDirection = new Vector3f();
  53.     private boolean[] arrowKeys = new boolean[4];
  54.     private CubesSettings cubesSettings;
  55.     private BlockTerrainControl blockTerrain;
  56.     private Node terrainNode = new Node();
  57.  
  58.     @Override
  59.     public void simpleInitApp(){
  60.         bulletAppState = new BulletAppState();
  61.         stateManager.attach(bulletAppState);
  62.         initControls();
  63.         initBlockTerrain();
  64.         initPlayer();
  65.         cam.lookAtDirection(new Vector3f(1, 0, 1), Vector3f.UNIT_Y);
  66.     }
  67.  
  68.     private void initControls(){
  69.         inputManager.addMapping("move_left", new KeyTrigger(KeyInput.KEY_A));
  70.         inputManager.addMapping("move_right", new KeyTrigger(KeyInput.KEY_D));
  71.         inputManager.addMapping("move_up", new KeyTrigger(KeyInput.KEY_W));
  72.         inputManager.addMapping("move_down", new KeyTrigger(KeyInput.KEY_S));
  73.         inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
  74.         inputManager.addListener(this, "move_left");
  75.         inputManager.addListener(this, "move_right");
  76.         inputManager.addListener(this, "move_up");
  77.         inputManager.addListener(this, "move_down");
  78.         inputManager.addListener(this, "jump");
  79.     }
  80.    
  81.     private void initBlockTerrain(){
  82.         CubesTestAssets.registerBlocks();
  83.         //CubesTestAssets.initializeEnvironment(this);
  84.        
  85.         PointLight light = new PointLight();
  86.     light.setPosition(new Vector3f(1000, 1000, 1000));
  87.     light.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
  88.     light.setRadius(2000f);
  89.     this.rootNode.addLight(light);
  90.    
  91.     getRootNode().attachChild(SkyFactory.createSky(getAssetManager(), "Textures/cubes/sky.jpg", true));
  92.    
  93.     PointLightShadowRenderer plsr = new PointLightShadowRenderer(
  94.         this.assetManager, 2048);
  95.     plsr.setLight(light);
  96.     plsr.setShadowIntensity(0.3f);
  97.     getViewPort().addProcessor(plsr);
  98.    
  99.         cubesSettings = CubesTestAssets.getSettings(this);
  100.         blockTerrain = new BlockTerrainControl(cubesSettings, new Vector3Int(7, 1, 7));
  101.         blockTerrain.setBlocksFromNoise(new Vector3Int(), terrainSize, 0.8f, Block_Grass.class);
  102.         blockTerrain.addChunkListener(new BlockChunkListener(){
  103.  
  104.             @Override
  105.             public void onSpatialUpdated(BlockChunkControl blockChunk){
  106.                 Geometry optimizedGeometry = blockChunk.getOptimizedGeometry_Opaque();
  107.                 RigidBodyControl rigidBodyControl = optimizedGeometry.getControl(RigidBodyControl.class);
  108.                 if(rigidBodyControl == null){
  109.                     rigidBodyControl = new RigidBodyControl(0);
  110.                     optimizedGeometry.addControl(rigidBodyControl);
  111.                     bulletAppState.getPhysicsSpace().add(rigidBodyControl);
  112.                 }
  113.                 rigidBodyControl.setCollisionShape(new MeshCollisionShape(optimizedGeometry.getMesh()));
  114.             }
  115.         });
  116.         terrainNode.addControl(blockTerrain);
  117.         terrainNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
  118.         rootNode.attachChild(terrainNode);
  119.     }
  120.  
  121.     private void initPlayer(){
  122.         playerControl = new CharacterControl(new CapsuleCollisionShape((cubesSettings.getBlockSize() / 2), cubesSettings.getBlockSize() * 2), 0.05f);
  123.         playerControl.setJumpSpeed(25);
  124.         playerControl.setFallSpeed(20);
  125.         playerControl.setGravity(70);
  126.         playerControl.setPhysicsLocation(new Vector3f(5, terrainSize.getY() + 5, 5).mult(cubesSettings.getBlockSize()));
  127.         bulletAppState.getPhysicsSpace().add(playerControl);
  128.     }
  129.  
  130.     @Override
  131.     public void simpleUpdate(float lastTimePerFrame){
  132.         float playerMoveSpeed = ((cubesSettings.getBlockSize() * 6.5f) * lastTimePerFrame);
  133.         Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
  134.         Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
  135.         walkDirection.set(0, 0, 0);
  136.         if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
  137.         if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
  138.         if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
  139.         if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
  140.         walkDirection.setY(0);
  141.         playerControl.setWalkDirection(walkDirection);
  142.         cam.setLocation(playerControl.getPhysicsLocation());
  143.     }
  144.  
  145.     @Override
  146.     public void onAction(String actionName, boolean value, float lastTimePerFrame){
  147.         if(actionName.equals("move_up")){
  148.             arrowKeys[0] = value;
  149.         }
  150.         else if(actionName.equals("move_right")){
  151.             arrowKeys[1] = value;
  152.         }
  153.         else if(actionName.equals("move_left")){
  154.             arrowKeys[3] = value;
  155.         }
  156.         else if(actionName.equals("move_down")){
  157.             arrowKeys[2] = value;
  158.         }
  159.         else if(actionName.equals("jump")){
  160.             playerControl.jump();
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement