Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. public class Main extends SimpleApplication {
  2.    
  3.     private RigidBodyControl wallCollision;
  4.     private RigidBodyControl ballCollision;
  5.     private BulletAppState bulletAppState;
  6.  
  7.     public static void main(String[] args) {
  8.         Main app = new Main();
  9.         app.start();
  10.     }
  11.  
  12.     private boolean isRunning = false;
  13.    
  14.     protected Node ballNode;
  15.     protected Node racketNode;
  16.    
  17.     private Geometry wall;
  18.    
  19.     @Override
  20.     public void simpleInitApp() {
  21.  
  22.         flyCam.setMoveSpeed(6);
  23.        
  24.         //bullet state stuff
  25.         bulletAppState = new BulletAppState();
  26.         stateManager.attach(bulletAppState);
  27.         bulletAppState.setDebugEnabled(true);
  28.         //bulletAppState.getPhysicsSpace().
  29.         //nodes
  30.         ballNode = new Node("ball");
  31.         racketNode = new Node("racket");
  32.        
  33.         //wall
  34.         Box b = new Box(2f, 2f, 0.1f);
  35.         Geometry wall = new Geometry("Box", b);
  36.         Material wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  37.         wallMat.setTexture("ColorMap", assetManager.loadTexture("Textures/wall.jpg"));
  38.         wall.setMaterial(wallMat);
  39.        
  40.         //ball
  41.         Spatial ball = assetManager.loadModel("Models/tennisball.obj");
  42.         Material ballMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  43.         ballMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ball.jpg"));
  44.         ball.setMaterial(ballMat);
  45.         ball.scale(0.00125f, 0.00125f, 0.00125f);
  46.        
  47.         //racket  
  48.         Spatial racket = assetManager.loadModel("Models/tennisRacket.obj");
  49.         Material racketMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  50.         racketMat.setTexture("ColorMap", assetManager.loadTexture("Textures/metal.jpg"));
  51.         racket.setMaterial(racketMat);
  52.         racket.scale(0.00025f, 0.00025f, 0.00025f);
  53.        
  54.         //set default positions
  55.         ball.setLocalTranslation(new Vector3f(1,0,8.8f));
  56.         racket.setLocalTranslation(new Vector3f(1,-1,9));
  57.         wall.setLocalTranslation(new Vector3f(1,1,8));
  58.        
  59.         //load keymappings
  60.         initKeys();
  61.        
  62.         //collision detection
  63.         //wall
  64.         CollisionShape wallShape = CollisionShapeFactory.createBoxShape(wall);
  65.         wallCollision = new RigidBodyControl(wallShape, 0);
  66.         wall.addControl(wallCollision);
  67.        
  68.         //ball
  69.         CollisionShape ballShape = CollisionShapeFactory.createBoxShape(ball);
  70.         ballCollision = new RigidBodyControl(ballShape, 0);
  71.         ball.addControl(ballCollision);
  72.        
  73.         bulletAppState.getPhysicsSpace().add(wallCollision);
  74.         bulletAppState.getPhysicsSpace().add(ballCollision);
  75.  
  76.         //attach models to nodes
  77.         ballNode.attachChild(ball);
  78.         racketNode.attachChild(racket);
  79.        
  80.         //attach nodes to root node
  81.         rootNode.attachChild(racketNode);
  82.         rootNode.attachChild(ballNode);
  83.         rootNode.attachChild(wall);
  84.     }
  85.    
  86.     private void initKeys() {
  87.        
  88.         //control mappings
  89.         inputManager.addMapping("Pause",  new KeyTrigger(KeyInput.KEY_P));      
  90.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
  91.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_L));
  92.         inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_K));
  93.         inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_I));
  94.  
  95.         // Add the names to the action listener.
  96.         inputManager.addListener(actionListener, "Pause");
  97.         inputManager.addListener(actionListener, "Start");
  98.        
  99.         //controls for movement of racket.
  100.         inputManager.addListener(actionListener, "Left");
  101.         inputManager.addListener(actionListener, "Right");
  102.         inputManager.addListener(actionListener, "Up");
  103.         inputManager.addListener(actionListener, "Down");
  104.  
  105.     }
  106.  
  107.     private final ActionListener actionListener = new ActionListener() {
  108.         @Override
  109.         public void onAction(String name, boolean keyPressed, float tpf) {
  110.             final float MOVE_DISTANCE = 0.5f; //the distance the racket moves per click
  111.             if (name.equals("Pause") && !keyPressed) {
  112.                 isRunning = !isRunning;
  113.             }
  114.            
  115.             if (name.equals("Left") && !keyPressed) {
  116.                 System.out.println("move left");
  117.                 racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX() - MOVE_DISTANCE), (racketNode.getLocalTranslation().getY()), (racketNode.getLocalTranslation().getZ())));
  118.        
  119.             }
  120.             if (name.equals("Right") && !keyPressed) {
  121.                 System.out.println("move right");
  122.                 racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX() + MOVE_DISTANCE), (racketNode.getLocalTranslation().getY()), (racketNode.getLocalTranslation().getZ())));
  123.        
  124.             }
  125.             if (name.equals("Up") && !keyPressed) {
  126.                 System.out.println("move up");
  127.                 racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX()), (racketNode.getLocalTranslation().getY() + MOVE_DISTANCE), (racketNode.getLocalTranslation().getZ())));
  128.        
  129.             }
  130.             if (name.equals("Down") && !keyPressed) {
  131.                 System.out.println("move down");
  132.                 racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX()), (racketNode.getLocalTranslation().getY() - MOVE_DISTANCE), (racketNode.getLocalTranslation().getZ())));
  133.        
  134.             }
  135.         }
  136.     };
  137.  
  138.     @Override
  139.     public void simpleUpdate(float tpf) {
  140.  
  141.         if(isRunning){
  142.             //ballNode.move(new Vector3f( (ballNode.getLocalTranslation().getX()), (ballNode.getLocalTranslation().getY()), (ballNode.getLocalTranslation().getZ()  - .00002f)));
  143.            ballNode.setLocalTranslation(new Vector3f( (ballNode.getLocalTranslation().getX()), (ballNode.getLocalTranslation().getY()), (ballNode.getLocalTranslation().getZ()  - .00002f)));
  144.         }
  145.        
  146.     }
  147.  
  148.     @Override
  149.     public void simpleRender(RenderManager rm) {
  150.         //TODO: add render code
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement