Guest User

Angular drag

a guest
Dec 13th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 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.zoneofuprising.engines;
  6.  
  7. import com.jme3.bullet.control.RigidBodyControl;
  8. import com.jme3.math.Vector3f;
  9. import cz.ascaria.zoneofuprising.utils.FasterMath;
  10.  
  11. /**
  12.  *
  13.  * @author Ascaria Quynn
  14.  */
  15. public class RotationCompensator extends BaseCompensator {
  16.  
  17.     private Vector3f momentum = new Vector3f();
  18.     private Vector3f angular = new Vector3f();
  19.  
  20.     private float drag = 100f;
  21.  
  22.     public RotationCompensator() {
  23.     }
  24.    
  25.     public RotationCompensator(float drag) {
  26.         this.drag = drag;
  27.     }
  28.  
  29.     @Override
  30.     public Vector3f compensateAngular(float tpf) {
  31.         angularCompensation.zero();
  32.         if(!enabled) {
  33.             return angularCompensation;
  34.         }
  35.         // Prepare variables
  36.         rigidBody.getAngularVelocity(angular);
  37.         //angular.multLocal(Math.abs(angular.x), Math.abs(angular.y), Math.abs(angular.z));
  38.         momentum.set(angular);
  39.         // Compute counter rotation force and momentum
  40.         angular.multLocal(rigidBody.getMass() * drag).negateLocal();
  41.         //System.out.println("angular drag " + angular.length());
  42.         momentum.multLocal(rigidBody.getMass() / (tpf * tpf)).negateLocal();
  43.         //System.out.println("momentum " + momentum.length());
  44.         // If the angular compensation is lower than the total momentum, but big enough to do some work
  45.         if(FasterMath.betweenExcl(angular.lengthSquared(), 1000f, momentum.lengthSquared())) {
  46.             // Apply counter rotation resistance
  47.             //System.out.println("applying angular drag");
  48.             angularCompensation.set(angular);
  49.         } else if(momentum.lengthSquared() > 0.00001f) {
  50.             // Apply counter momentum resistance, when angular compensation is too big or too small
  51.             //System.out.println("applying momentum mmmmmmmmmmmmmmmmmmmmmmmm");
  52.             angularCompensation.set(momentum);
  53.         }
  54.         // Return compensation result
  55.         return angularCompensation;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment