Advertisement
Earthcomputer

DistanceJoint.java

Apr 18th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1.     private final Vector3f anchorA, anchorB;
  2.     private final float distance;
  3.  
  4.     private Vector3f rA = new Vector3f(), rB = new Vector3f();
  5.     private Vector3f u = new Vector3f();
  6.     private float constraintMass;
  7.  
  8.     public DistanceJoint(short entityA, short entityB, Vector3f anchorA, Vector3f anchorB, float distance) {
  9.         super(entityA, entityB);
  10.         this.anchorA = anchorA;
  11.         this.anchorB = anchorB;
  12.         this.distance = distance;
  13.     }
  14.  
  15.     @Override
  16.     public void initVelocityConstraints(Level level) {
  17.         PositionComponent posA = level.getComponent(entityA, PositionComponent.class);
  18.         PositionComponent posB = level.getComponent(entityB, PositionComponent.class);
  19.         PhysicsComponent physA = level.getComponent(entityA, PhysicsComponent.class);
  20.         PhysicsComponent physB = level.getComponent(entityB, PhysicsComponent.class);
  21.  
  22.         rA.set(anchorA).rotate(posA.getRot()).mul(posA.getScale());
  23.         rB.set(anchorB).rotate(posB.getRot()).mul(posB.getScale());
  24.         u.set(rB).add(posB.getPos()).sub(rA).sub(posA.getPos());
  25.         if (u.lengthSquared() < 0.000001f)
  26.             u.set(0, 0, 0);
  27.         else
  28.             u.normalize();
  29.  
  30.         // massInv = massInvA + massInvB + (rA x u)T * inertiaInvA * (rA x u) + (rB x u)T * inertiaInvB * (rB x u)
  31.         float massInv = 1 / physA.mass + 1 / physB.mass;
  32.  
  33.         Vector3f cross = new Vector3f(rA).cross(u);
  34.         massInv += cross.mul(physA.inertiaInv, new Vector3f()).dot(cross);
  35.  
  36.         cross.set(rB).cross(u);
  37.         massInv += cross.mul(physB.inertiaInv, new Vector3f()).dot(cross);
  38.  
  39.         constraintMass = massInv == 0 ? 0 : 1 / massInv;
  40.     }
  41.  
  42.     @Override
  43.     public void solveVelocityConstraints(Level level) {
  44.         PositionComponent posA = level.getComponent(entityA, PositionComponent.class);
  45.         PositionComponent posB = level.getComponent(entityB, PositionComponent.class);
  46.         PhysicsComponent physA = level.getComponent(entityA, PhysicsComponent.class);
  47.         PhysicsComponent physB = level.getComponent(entityB, PhysicsComponent.class);
  48.  
  49.         // cDot = u . (vA + (wA x rA) - vB - (wB x rB))
  50.         float cDot = new Vector3f(physB.getVelocity()).add(physB.getAngularVelocity(posB.getRot()).cross(rB))
  51.                 .sub(physA.getVelocity()).sub(physA.getAngularVelocity(posA.getRot()).cross(rA))
  52.                 .dot(u);
  53.  
  54.         float impulse = -constraintMass * (cDot); // TODO: bias
  55.         Vector3f dp = u.mul(impulse, new Vector3f());
  56.  
  57.         physA.momentum.sub(dp);
  58.         physA.angularMomentum.sub(rA.cross(dp, new Vector3f()));
  59.         physB.momentum.add(dp);
  60.         physB.angularMomentum.add(rB.cross(dp, new Vector3f()));
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement