Fixed #431 - Mechanical nodes now decelerate itself

This commit is contained in:
Calclavia 2014-03-27 21:58:14 +08:00
parent afc996fae5
commit 9844af336b

View file

@ -1,37 +1,38 @@
package resonantinduction.mechanical.energy.grid; package resonantinduction.mechanical.energy.grid;
import java.util.Iterator; import calclavia.api.resonantinduction.IMechanicalNode;
import java.util.Map.Entry; import calclavia.lib.grid.INodeProvider;
import calclavia.lib.grid.Node;
import calclavia.lib.grid.TickingGrid;
import codechicken.multipart.TMultiPart;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.core.ResonantInduction; import resonantinduction.core.ResonantInduction;
import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.Vector3;
import calclavia.api.resonantinduction.IMechanicalNode;
import calclavia.lib.grid.INodeProvider; import java.util.Iterator;
import calclavia.lib.grid.Node; import java.util.Map.Entry;
import calclavia.lib.grid.TickingGrid;
import codechicken.multipart.TMultiPart;
/** /**
* A mechanical node for mechanical energy. * A mechanical node for mechanical energy.
* * <p/>
* Useful Formula: * Useful Formula:
* * <p/>
* Power is the work per unit time. * Power is the work per unit time.
* Power (W) = Torque (Strength of the rotation, Newton Meters) x Speed (Angular Velocity, RADIAN * Power (W) = Torque (Strength of the rotation, Newton Meters) x Speed (Angular Velocity, RADIAN
* PER SECOND). * PER SECOND).
* *OR* * *OR*
* Power = Torque / Time * Power = Torque / Time
* * <p/>
* Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if * Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if
* optimal.) * optimal.)
* *
* @author Calclavia * @author Calclavia
*/ */
public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalNode> implements IMechanicalNode public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalNode>
implements IMechanicalNode
{ {
public double torque = 0; public double torque = 0;
public double prevAngularVelocity, angularVelocity = 0; public double prevAngularVelocity, angularVelocity = 0;
@ -71,7 +72,9 @@ public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalN
prevAngularVelocity = angularVelocity; prevAngularVelocity = angularVelocity;
if (!ResonantInduction.proxy.isPaused()) if (!ResonantInduction.proxy.isPaused())
{
angle += angularVelocity * deltaTime; angle += angularVelocity * deltaTime;
}
if (angle % (Math.PI * 2) != angle) if (angle % (Math.PI * 2) != angle)
{ {
@ -89,16 +92,24 @@ public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalN
double torqueLoss = Math.min(Math.abs(getTorque()), (Math.abs(getTorque() * getTorqueLoad()) + getTorqueLoad() / 10) * deltaTime); double torqueLoss = Math.min(Math.abs(getTorque()), (Math.abs(getTorque() * getTorqueLoad()) + getTorqueLoad() / 10) * deltaTime);
if (torque > 0) if (torque > 0)
{
torque -= torqueLoss; torque -= torqueLoss;
}
else else
{
torque += torqueLoss; torque += torqueLoss;
}
double velocityLoss = Math.min(Math.abs(getAngularVelocity()), (Math.abs(getAngularVelocity() * getAngularVelocityLoad()) + getAngularVelocityLoad() / 10) * deltaTime); double velocityLoss = Math.min(Math.abs(getAngularVelocity()), (Math.abs(getAngularVelocity() * getAngularVelocityLoad()) + getAngularVelocityLoad() / 10) * deltaTime);
if (angularVelocity > 0) if (angularVelocity > 0)
{
angularVelocity -= velocityLoss; angularVelocity -= velocityLoss;
}
else else
{
angularVelocity += velocityLoss; angularVelocity += velocityLoss;
}
power = getEnergy() / deltaTime; power = getEnergy() / deltaTime;
@ -121,11 +132,27 @@ public class MechanicalNode extends Node<INodeProvider, TickingGrid, MechanicalN
int inversion = inverseRotation ? -1 : 1; int inversion = inverseRotation ? -1 : 1;
if (Math.abs(torque + inversion * (adjacentMech.getTorque() / ratio * acceleration)) < Math.abs(adjacentMech.getTorque() / ratio)) double applyTorque = inversion * adjacentMech.getTorque() / ratio * acceleration;
torque = torque + inversion * (adjacentMech.getTorque() / ratio * acceleration);
if (Math.abs(angularVelocity + inversion * (adjacentMech.getAngularVelocity() * ratio * acceleration)) < Math.abs(adjacentMech.getAngularVelocity() * ratio)) if (Math.abs(torque + applyTorque) < Math.abs(adjacentMech.getTorque() / ratio))
angularVelocity = angularVelocity + (inversion * adjacentMech.getAngularVelocity() * ratio * acceleration); {
torque += applyTorque;
}
else
{
torque -= applyTorque;
}
double applyVelocity = inversion * adjacentMech.getAngularVelocity() * ratio * acceleration;
if (Math.abs(angularVelocity + applyVelocity) < Math.abs(adjacentMech.getAngularVelocity() * ratio))
{
angularVelocity += applyVelocity;
}
else
{
angularVelocity -= applyVelocity;
}
/** /**
* Set all current rotations * Set all current rotations