Advertisement
Guest User

Untitled

a guest
May 7th, 2014
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.91 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package cz.ascaria.borekboritel.controls;
  6.  
  7. import com.jme3.bullet.PhysicsSpace;
  8. import com.jme3.bullet.PhysicsTickListener;
  9. import com.jme3.bullet.collision.shapes.CollisionShape;
  10. import com.jme3.bullet.control.PhysicsControl;
  11. import com.jme3.export.InputCapsule;
  12. import com.jme3.export.JmeExporter;
  13. import com.jme3.export.JmeImporter;
  14. import com.jme3.export.OutputCapsule;
  15. import com.jme3.math.Vector3f;
  16. import com.jme3.renderer.RenderManager;
  17. import com.jme3.renderer.ViewPort;
  18. import com.jme3.scene.Spatial;
  19. import com.jme3.scene.control.Control;
  20. import cz.ascaria.bullet.objects.PhysicsSpaceShip;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  *
  25.  * @author Ascaria Quynn
  26.  */
  27. public class SpaceShipControl extends PhysicsSpaceShip implements PhysicsControl, PhysicsTickListener {
  28.  
  29.     protected Spatial spatial;
  30.     protected boolean enabled = true;
  31.     protected PhysicsSpace space = null;
  32.     protected boolean added = false;
  33.  
  34.     protected Vector3f linear = Vector3f.ZERO.clone();
  35.     protected Vector3f torque = Vector3f.ZERO.clone();
  36.  
  37.  
  38.     protected Vector3f ascent;
  39.     protected Vector3f accelerate;
  40.     protected Vector3f strafe;
  41.  
  42.     protected Vector3f yaw;
  43.     protected Vector3f pitch;
  44.     protected Vector3f roll;
  45.  
  46.     /**
  47.      * Creates a new PhysicsNode with the supplied collision shape
  48.      * @param shape
  49.      */
  50.     public SpaceShipControl(CollisionShape shape) {
  51.         super(shape);
  52.     }
  53.  
  54.     public SpaceShipControl(CollisionShape shape, float mass) {
  55.         super(shape, mass);
  56.     }
  57.  
  58.     public boolean isApplyPhysicsLocal() {
  59.         return motionState.isApplyPhysicsLocal();
  60.     }
  61.  
  62.     /**
  63.      * When set to true, the physics coordinates will be applied to the local
  64.      * translation of the Spatial
  65.      * @param applyPhysicsLocal
  66.      */
  67.     public void setApplyPhysicsLocal(boolean applyPhysicsLocal) {
  68.         motionState.setApplyPhysicsLocal(applyPhysicsLocal);
  69.     }
  70.  
  71.     public void setEnabled(boolean enabled) {
  72.         this.enabled = enabled;
  73.         if (physicsSpace != null) {
  74.             if (enabled && !added) {
  75.                 if(spatial != null) {
  76.                     setPhysicsLocation(motionState.isApplyPhysicsLocal() ? spatial.getLocalTranslation() : spatial.getWorldTranslation());
  77.                     setPhysicsRotation(motionState.isApplyPhysicsLocal() ? spatial.getLocalRotation() : spatial.getWorldRotation());
  78.                 }
  79.                 physicsSpace.addCollisionObject(this);
  80.                 physicsSpace.addTickListener(this);
  81.                 added = true;
  82.             } else if (!enabled && added) {
  83.                 physicsSpace.removeCollisionObject(this);
  84.                 physicsSpace.removeTickListener(this);
  85.                 added = false;
  86.             }
  87.         }
  88.     }
  89.  
  90.     public boolean isEnabled() {
  91.         return enabled;
  92.     }
  93.  
  94.     public Control cloneForSpatial(Spatial spatial) {
  95.         SpaceShipControl control = new SpaceShipControl(collisionShape, mass);
  96.         control.setAngularFactor(getAngularFactor());
  97.         control.setAngularSleepingThreshold(getAngularSleepingThreshold());
  98.         control.setAngularVelocity(getAngularVelocity());
  99.         control.setCcdMotionThreshold(getCcdMotionThreshold());
  100.         control.setCcdSweptSphereRadius(getCcdSweptSphereRadius());
  101.         control.setCollideWithGroups(getCollideWithGroups());
  102.         control.setCollisionGroup(getCollisionGroup());
  103.         control.setDamping(getLinearDamping(), getAngularDamping());
  104.         control.setFriction(getFriction());
  105.         control.setGravity(getGravity());
  106.         control.setKinematic(isKinematic());
  107.         control.setLinearSleepingThreshold(getLinearSleepingThreshold());
  108.         control.setLinearVelocity(getLinearVelocity());
  109.         control.setPhysicsLocation(getPhysicsLocation());
  110.         control.setPhysicsRotation(getPhysicsRotationMatrix());
  111.         control.setRestitution(getRestitution());
  112.  
  113.  
  114.         control.setApplyPhysicsLocal(isApplyPhysicsLocal());
  115.  
  116.         control.setSpatial(spatial);
  117.         return control;
  118.     }
  119.  
  120.     public void setSpatial(Spatial spatial) {
  121.         if (getUserObject() == null || getUserObject() == this.spatial) {
  122.             setUserObject(spatial);
  123.         }
  124.         this.spatial = spatial;
  125.         if (spatial == null) {
  126.             if (getUserObject() == spatial) {
  127.                 setUserObject(null);
  128.             }
  129.             this.spatial = null;
  130.             this.collisionShape = null;
  131.             return;
  132.         }
  133.         setPhysicsLocation(motionState.isApplyPhysicsLocal() ? spatial.getLocalTranslation() : spatial.getWorldTranslation());
  134.         setPhysicsRotation(motionState.isApplyPhysicsLocal() ? spatial.getLocalRotation() : spatial.getWorldRotation());
  135.     }
  136.  
  137.     public void setPhysicsSpace(PhysicsSpace space) {
  138.         createSpaceShip(space);
  139.         if (space == null) {
  140.             if (this.space != null) {
  141.                 this.space.removeCollisionObject(this);
  142.                 this.space.addTickListener(this);
  143.                 added = false;
  144.             }
  145.         } else {
  146.             if( this.space == space) {
  147.                 return;
  148.             }
  149.             space.addCollisionObject(this);
  150.             space.addTickListener(this);
  151.             added = true;
  152.         }
  153.         this.space = space;
  154.     }
  155.  
  156.     public PhysicsSpace getPhysicsSpace() {
  157.         return space;
  158.     }
  159.  
  160.     public void update(float tpf) {
  161.         if (enabled && spatial != null) {
  162.             if (getMotionState().applyTransform(spatial)) {
  163.                 spatial.getWorldTransform();
  164.             }
  165.         }
  166.     }
  167.  
  168.     public void render(RenderManager rm, ViewPort vp) {
  169.     }
  170.  
  171.     @Override
  172.     public void write(JmeExporter ex) throws IOException {
  173.         super.write(ex);
  174.         OutputCapsule oc = ex.getCapsule(this);
  175.         oc.write(enabled, "enabled", true);
  176.         oc.write(motionState.isApplyPhysicsLocal(), "applyLocalPhysics", false);
  177.         oc.write(spatial, "spatial", null);
  178.     }
  179.  
  180.     @Override
  181.     public void read(JmeImporter im) throws IOException {
  182.         super.read(im);
  183.         InputCapsule ic = im.getCapsule(this);
  184.         enabled = ic.readBoolean("enabled", true);
  185.         spatial = (Spatial) ic.readSavable("spatial", null);
  186.         motionState.setApplyPhysicsLocal(ic.readBoolean("applyLocalPhysics", false));
  187.         setUserObject(spatial);
  188.     }
  189.  
  190.     /**
  191.      * Apply ascending (positive value) or descending (negative value) force.
  192.      * @param value Value of the axis, from 0f to 1f.
  193.      */
  194.     public void ascent(float value) {
  195.         if(linear.y == 0f) {
  196.             linear.y = mass * movementPower * value;
  197.         }
  198.     }
  199.  
  200.     /**
  201.      * Apply accelerating (positive value) or reversing (negative value) force.
  202.      * @param value Value of the axis, from 0f to 1f.
  203.      */
  204.     public void accelerate(float value) {
  205.         if(linear.z == 0f) {
  206.             linear.z = mass * movementPower * value;
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * Apply strafing force to the left (positive value) or right (negative value) side.
  212.      * @param value Value of the axis, from 0f to 1f.
  213.      */
  214.     public void strafe(float value) {
  215.         if(linear.x == 0f) {
  216.             linear.x = mass * movementPower * value;
  217.         }
  218.     }
  219.  
  220.  
  221.     /**
  222.      * Apply yaw torque to the left (positive value) or to the right (negative value)
  223.      * @param value
  224.      */
  225.     public void yaw(float value) {
  226.         if(torque.y == 0f) {
  227.             torque.y = mass * rotationPower * value;
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Apply pitch torque to the up (negative value) or down (positive value)
  233.      * @param value
  234.      */
  235.     public void pitch(float value) {
  236.         if(torque.x == 0f) {
  237.             torque.x = mass * rotationPower * value;
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * Apply roll torque to the left (negative value) or to the right (positive value)
  243.      * @param value
  244.      */
  245.     public void roll(float value) {
  246.         if(torque.z == 0f) {
  247.             torque.z = mass * rotationPower * value;
  248.         }
  249.     }
  250.  
  251.     /**
  252.      * Method is called before each step, here you apply forces (change the state).
  253.      * @param space
  254.      * @param tpf
  255.      */
  256.     public void prePhysicsTick(PhysicsSpace space, float tpf) {
  257.         // Apply linear force
  258.         if(!linear.equals(Vector3f.ZERO)) {
  259.             applyCentralForce(getPhysicsRotation().mult(linear));
  260.             linear.zero();
  261.         }
  262.         // Apply torque force
  263.         if(!torque.equals(Vector3f.ZERO)) {
  264.             applyTorque(getPhysicsRotation().mult(torque));
  265.             torque.zero();
  266.         }
  267.     }
  268.  
  269.     /**
  270.      * Method is called after each step, here you poll the results (get the current state).
  271.      * @param space
  272.      * @param tpf
  273.      */
  274.     public void physicsTick(PhysicsSpace space, float tpf) {
  275.         //System.out.println("I TICK!!!!");
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement