Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main extends SimpleApplication {
- private RigidBodyControl wallCollision;
- private RigidBodyControl ballCollision;
- private BulletAppState bulletAppState;
- public static void main(String[] args) {
- Main app = new Main();
- app.start();
- }
- private boolean isRunning = false;
- protected Node ballNode;
- protected Node racketNode;
- private Geometry wall;
- @Override
- public void simpleInitApp() {
- flyCam.setMoveSpeed(6);
- //bullet state stuff
- bulletAppState = new BulletAppState();
- stateManager.attach(bulletAppState);
- bulletAppState.setDebugEnabled(true);
- //bulletAppState.getPhysicsSpace().
- //nodes
- ballNode = new Node("ball");
- racketNode = new Node("racket");
- //wall
- Box b = new Box(2f, 2f, 0.1f);
- Geometry wall = new Geometry("Box", b);
- Material wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- wallMat.setTexture("ColorMap", assetManager.loadTexture("Textures/wall.jpg"));
- wall.setMaterial(wallMat);
- //ball
- Spatial ball = assetManager.loadModel("Models/tennisball.obj");
- Material ballMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- ballMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ball.jpg"));
- ball.setMaterial(ballMat);
- ball.scale(0.00125f, 0.00125f, 0.00125f);
- //racket
- Spatial racket = assetManager.loadModel("Models/tennisRacket.obj");
- Material racketMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- racketMat.setTexture("ColorMap", assetManager.loadTexture("Textures/metal.jpg"));
- racket.setMaterial(racketMat);
- racket.scale(0.00025f, 0.00025f, 0.00025f);
- //set default positions
- ball.setLocalTranslation(new Vector3f(1,0,8.8f));
- racket.setLocalTranslation(new Vector3f(1,-1,9));
- wall.setLocalTranslation(new Vector3f(1,1,8));
- //load keymappings
- initKeys();
- //collision detection
- //wall
- CollisionShape wallShape = CollisionShapeFactory.createBoxShape(wall);
- wallCollision = new RigidBodyControl(wallShape, 0);
- wall.addControl(wallCollision);
- //ball
- CollisionShape ballShape = CollisionShapeFactory.createBoxShape(ball);
- ballCollision = new RigidBodyControl(ballShape, 0);
- ball.addControl(ballCollision);
- bulletAppState.getPhysicsSpace().add(wallCollision);
- bulletAppState.getPhysicsSpace().add(ballCollision);
- //attach models to nodes
- ballNode.attachChild(ball);
- racketNode.attachChild(racket);
- //attach nodes to root node
- rootNode.attachChild(racketNode);
- rootNode.attachChild(ballNode);
- rootNode.attachChild(wall);
- }
- private void initKeys() {
- //control mappings
- inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_L));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_K));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_I));
- // Add the names to the action listener.
- inputManager.addListener(actionListener, "Pause");
- inputManager.addListener(actionListener, "Start");
- //controls for movement of racket.
- inputManager.addListener(actionListener, "Left");
- inputManager.addListener(actionListener, "Right");
- inputManager.addListener(actionListener, "Up");
- inputManager.addListener(actionListener, "Down");
- }
- private final ActionListener actionListener = new ActionListener() {
- @Override
- public void onAction(String name, boolean keyPressed, float tpf) {
- final float MOVE_DISTANCE = 0.5f; //the distance the racket moves per click
- if (name.equals("Pause") && !keyPressed) {
- isRunning = !isRunning;
- }
- if (name.equals("Left") && !keyPressed) {
- System.out.println("move left");
- racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX() - MOVE_DISTANCE), (racketNode.getLocalTranslation().getY()), (racketNode.getLocalTranslation().getZ())));
- }
- if (name.equals("Right") && !keyPressed) {
- System.out.println("move right");
- racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX() + MOVE_DISTANCE), (racketNode.getLocalTranslation().getY()), (racketNode.getLocalTranslation().getZ())));
- }
- if (name.equals("Up") && !keyPressed) {
- System.out.println("move up");
- racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX()), (racketNode.getLocalTranslation().getY() + MOVE_DISTANCE), (racketNode.getLocalTranslation().getZ())));
- }
- if (name.equals("Down") && !keyPressed) {
- System.out.println("move down");
- racketNode.setLocalTranslation(new Vector3f( (racketNode.getLocalTranslation().getX()), (racketNode.getLocalTranslation().getY() - MOVE_DISTANCE), (racketNode.getLocalTranslation().getZ())));
- }
- }
- };
- @Override
- public void simpleUpdate(float tpf) {
- if(isRunning){
- //ballNode.move(new Vector3f( (ballNode.getLocalTranslation().getX()), (ballNode.getLocalTranslation().getY()), (ballNode.getLocalTranslation().getZ() - .00002f)));
- ballNode.setLocalTranslation(new Vector3f( (ballNode.getLocalTranslation().getX()), (ballNode.getLocalTranslation().getY()), (ballNode.getLocalTranslation().getZ() - .00002f)));
- }
- }
- @Override
- public void simpleRender(RenderManager rm) {
- //TODO: add render code
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement