Search in sources :

Example 1 with ShipPilot

use of org.valkyrienskies.mod.common.ships.ship_world.ShipPilot in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class PhysicsCalculations method calculateForces.

private void calculateForces() {
    applyAirDrag();
    applyGravity();
    Vector3d blockForce = new Vector3d();
    Vector3d inBodyWO = new Vector3d();
    Vector3d crossVector = new Vector3d();
    World worldObj = getParent().getWorld();
    if (VSConfig.doPhysicsBlocks) {
        // We want to loop through all the physics nodes in a sorted order. Priority Queue handles that.
        Queue<IPhysicsBlockController> nodesPriorityQueue = new PriorityQueue<>(parent.getPhysicsControllersInShip());
        while (nodesPriorityQueue.size() > 0) {
            IPhysicsBlockController controller = nodesPriorityQueue.poll();
            controller.onPhysicsTick(parent, this, this.getPhysicsTimeDeltaPerPhysTick());
        }
        SortedMap<IBlockTorqueProvider, List<BlockPos>> torqueProviders = new TreeMap<>();
        BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
        // Note that iterating over "activeForcePositions" is not thread safe, so this could lead to problems.
        // However we're going to completely replace this code soon anyways, so its not a big deal.
        parent.getShipData().activeForcePositions.forEachUnsafe((x, y, z) -> {
            mutablePos.setPos(x, y, z);
            IBlockState state = getParent().getChunkAt(mutablePos.getX() >> 4, mutablePos.getZ() >> 4).getBlockState(mutablePos);
            Block blockAt = state.getBlock();
            if (blockAt instanceof IBlockForceProvider) {
                try {
                    BlockPhysicsDetails.getForceFromState(state, mutablePos, worldObj, getPhysicsTimeDeltaPerPhysTick(), getParent(), blockForce);
                    Vector3dc otherPosition = ((IBlockForceProvider) blockAt).getCustomBlockForcePosition(worldObj, mutablePos, state, getParent(), getPhysicsTimeDeltaPerPhysTick());
                    if (otherPosition != null) {
                        inBodyWO.set(otherPosition);
                        inBodyWO.sub(physCenterOfMass);
                        getParent().getShipTransformationManager().getCurrentPhysicsTransform().transformDirection(inBodyWO, TransformType.SUBSPACE_TO_GLOBAL);
                    } else {
                        inBodyWO.set(mutablePos.getX() + .5, mutablePos.getY() + .5, mutablePos.getZ() + .5);
                        inBodyWO.sub(physCenterOfMass);
                        getParent().getShipTransformationManager().getCurrentPhysicsTransform().transformDirection(inBodyWO, TransformType.SUBSPACE_TO_GLOBAL);
                    }
                    addForceAtPoint(inBodyWO, blockForce, crossVector);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (blockAt instanceof IBlockTorqueProvider) {
                // Add it to the torque sorted map; we do this so the torque dampeners can run
                // after the gyroscope stabilizers.
                IBlockTorqueProvider torqueProviderBlock = (IBlockTorqueProvider) blockAt;
                if (!torqueProviders.containsKey(torqueProviderBlock)) {
                    torqueProviders.put(torqueProviderBlock, new LinkedList<>());
                }
                torqueProviders.get(torqueProviderBlock).add(new BlockPos(x, y, z));
            }
        });
        // Now add the torque from the torque providers, in a sorted order!
        for (IBlockTorqueProvider torqueProviderBlock : torqueProviders.keySet()) {
            List<BlockPos> blockPositions = torqueProviders.get(torqueProviderBlock);
            for (BlockPos pos : blockPositions) {
                this.convertTorqueToVelocity();
                Vector3dc torqueVector = torqueProviderBlock.getTorqueInGlobal(this, pos);
                if (torqueVector != null) {
                    torque.add(torqueVector);
                }
            }
        }
    }
    // Add pilot input
    final ShipPilot parentPilot = parent.getShipPilot();
    if (parentPilot != null) {
        final Vector3dc pilotForce = parentPilot.getBlockForceInShipSpace(parent, physTickTimeDelta);
        final Vector3dc pilotTorque = parentPilot.getTorqueInGlobal(this);
        this.linearVelocity.fma(getInvMass(), pilotForce);
        this.torque.add(pilotTorque);
    }
    convertTorqueToVelocity();
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IBlockForceProvider(org.valkyrienskies.mod.common.block.IBlockForceProvider) IBlockTorqueProvider(org.valkyrienskies.mod.common.block.IBlockTorqueProvider) World(net.minecraft.world.World) PriorityQueue(java.util.PriorityQueue) TreeMap(java.util.TreeMap) ShipPilot(org.valkyrienskies.mod.common.ships.ship_world.ShipPilot) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) Block(net.minecraft.block.Block) LinkedList(java.util.LinkedList) List(java.util.List) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

LinkedList (java.util.LinkedList)1 List (java.util.List)1 PriorityQueue (java.util.PriorityQueue)1 TreeMap (java.util.TreeMap)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1 Vector3d (org.joml.Vector3d)1 Vector3dc (org.joml.Vector3dc)1 IBlockForceProvider (org.valkyrienskies.mod.common.block.IBlockForceProvider)1 IBlockTorqueProvider (org.valkyrienskies.mod.common.block.IBlockTorqueProvider)1 ShipPilot (org.valkyrienskies.mod.common.ships.ship_world.ShipPilot)1