| 1 | package me.fridtjof.minecarttrains.managers; |
| 2 | |
| 3 | import me.fridtjof.minecarttrains.utils.Rails; |
| 4 | import org.bukkit.entity.Minecart; |
| 5 | import org.bukkit.util.Vector; |
| 6 | |
| 7 | import java.util.List; |
| 8 | |
| 9 | public class LinkPhysics |
| 10 | { |
| 11 | |
| 12 | private static final double STILL = 1.0E-4; |
| 13 | |
| 14 | private final LinkageManager linkageManager; |
| 15 | private final double aimedDistanceLow; |
| 16 | private final double aimedDistanceHigh; |
| 17 | private final double highSpeedGateSquared; |
| 18 | private final double stiffness; |
| 19 | private final double damping; |
| 20 | private final double maxCorrection; |
| 21 | private final double curveAlignment; |
| 22 | private final double stretchSlop; |
| 23 | private final double curveStretchSlop; |
| 24 | |
| 25 | public LinkPhysics(LinkageManager linkageManager, ConfigManager configManager) |
| 26 | { |
| 27 | this.linkageManager = linkageManager; |
| 28 | this.aimedDistanceLow = configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed"); |
| 29 | this.aimedDistanceHigh = configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed"); |
| 30 | double highSpeedGate = configManager.physicsConfig.getConfig().getDouble("link.high_speed.gate_speed"); |
| 31 | this.highSpeedGateSquared = highSpeedGate * highSpeedGate; |
| 32 | this.stiffness = configManager.physicsConfig.getConfig().getDouble("link.rigid.stiffness"); |
| 33 | this.damping = configManager.physicsConfig.getConfig().getDouble("link.rigid.damping"); |
| 34 | this.maxCorrection = configManager.physicsConfig.getConfig().getDouble("link.rigid.max_correction"); |
| 35 | this.curveAlignment = configManager.physicsConfig.getConfig().getDouble("link.rigid.curve_alignment"); |
| 36 | this.stretchSlop = configManager.physicsConfig.getConfig().getDouble("link.rigid.stretch_slop"); |
| 37 | this.curveStretchSlop = configManager.physicsConfig.getConfig().getDouble("link.rigid.curve_stretch_slop"); |
| 38 | } |
| 39 | |
| 40 | public void updateTrain(Minecart cart) |
| 41 | { |
| 42 | if (!linkageManager.markTrainSolved(cart)) |
| 43 | { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | List<Minecart> train = linkageManager.getTrain(cart); |
| 48 | if (train.size() < 2) |
| 49 | { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | Minecart leader = linkageManager.findLeader(cart); |
| 54 | int leaderIndex = indexOf(train, leader); |
| 55 | if (leaderIndex < 0) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | for (int i = leaderIndex + 1; i < train.size(); i++) |
| 61 | { |
| 62 | constrain(train.get(i), train.get(i - 1)); |
| 63 | } |
| 64 | for (int i = leaderIndex - 1; i >= 0; i--) |
| 65 | { |
| 66 | constrain(train.get(i), train.get(i + 1)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private void constrain(Minecart follower, Minecart anchor) |
| 71 | { |
| 72 | Vector followerPos = follower.getLocation().toVector(); |
| 73 | Vector anchorPos = anchor.getLocation().toVector(); |
| 74 | Vector between = followerPos.subtract(anchorPos); |
| 75 | double distance = between.length(); |
| 76 | if (distance < 1.0E-6) |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | Vector linkAxis = between.multiply(1.0 / distance); |
| 82 | Vector hint = follower.getVelocity(); |
| 83 | if (hint.lengthSquared() < STILL) |
| 84 | { |
| 85 | hint = anchor.getVelocity(); |
| 86 | } |
| 87 | if (hint.lengthSquared() < STILL) |
| 88 | { |
| 89 | hint = linkAxis; |
| 90 | } |
| 91 | |
| 92 | Vector rail = Rails.tangent(follower.getLocation(), hint); |
| 93 | if (rail.lengthSquared() < STILL) |
| 94 | { |
| 95 | rail = linkAxis.clone().setY(0); |
| 96 | if (rail.lengthSquared() < STILL) |
| 97 | { |
| 98 | return; |
| 99 | } |
| 100 | rail.normalize(); |
| 101 | } |
| 102 | |
| 103 | if (rail.dot(hint) < 0 && hint.lengthSquared() >= STILL) |
| 104 | { |
| 105 | rail.multiply(-1); |
| 106 | } |
| 107 | |
| 108 | double alignment = Math.abs(rail.dot(linkAxis)); |
| 109 | boolean onCurve = alignment < curveAlignment; |
| 110 | double aimed = aimedDistance(anchor); |
| 111 | double stretch = distance - aimed; |
| 112 | |
| 113 | double targetSpeed = anchor.getVelocity().length(); |
| 114 | Vector newVelocity = rail.clone().multiply(targetSpeed); |
| 115 | |
| 116 | double correction = spacingCorrection(stretch, onCurve, follower.getVelocity(), anchor.getVelocity(), rail, linkAxis); |
| 117 | if (Math.abs(correction) > 1.0E-4) |
| 118 | { |
| 119 | newVelocity.add(rail.clone().multiply(correction)); |
| 120 | } |
| 121 | |
| 122 | follower.setVelocity(newVelocity); |
| 123 | } |
| 124 | |
| 125 | private double spacingCorrection(double stretch, boolean onCurve, Vector followerVel, Vector anchorVel, Vector rail, Vector linkAxis) |
| 126 | { |
| 127 | double slop = onCurve ? curveStretchSlop : stretchSlop; |
| 128 | if (stretch <= slop) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | double towardAnchor = -Math.signum(rail.dot(linkAxis)); |
| 134 | if (towardAnchor == 0.0) |
| 135 | { |
| 136 | towardAnchor = -1; |
| 137 | } |
| 138 | |
| 139 | double relative = followerVel.clone().subtract(anchorVel).dot(rail); |
| 140 | double correction = towardAnchor * stiffness * (stretch - slop) - damping * relative; |
| 141 | return Math.max(-maxCorrection, Math.min(maxCorrection, correction)); |
| 142 | } |
| 143 | |
| 144 | private double aimedDistance(Minecart anchor) |
| 145 | { |
| 146 | if (anchor.getVelocity().lengthSquared() > highSpeedGateSquared) |
| 147 | { |
| 148 | return aimedDistanceHigh; |
| 149 | } |
| 150 | return aimedDistanceLow; |
| 151 | } |
| 152 | |
| 153 | private int indexOf(List<Minecart> train, Minecart cart) |
| 154 | { |
| 155 | for (int i = 0; i < train.size(); i++) |
| 156 | { |
| 157 | if (train.get(i).getUniqueId().equals(cart.getUniqueId())) |
| 158 | { |
| 159 | return i; |
| 160 | } |
| 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 | } |