Skip to content

Commit

Permalink
Merge pull request #13 from ryanhcode/master
Browse files Browse the repository at this point in the history
Collisions - Part 1
  • Loading branch information
Eriksonnaren authored Apr 1, 2022
2 parents 811dfad + b4d43f3 commit 1de76fc
Show file tree
Hide file tree
Showing 30 changed files with 1,834 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.eriksonn.createaeronautics.network.packet.*;
import com.eriksonn.createaeronautics.physics.SimulatedContraptionRigidbody;
import com.eriksonn.createaeronautics.physics.SubcontraptionRigidbody;
import com.eriksonn.createaeronautics.physics.collision.shape.ICollisionShape;
import com.eriksonn.createaeronautics.physics.collision.shape.MeshCollisionShape;
import com.eriksonn.createaeronautics.utils.AbstractContraptionEntityExtension;
import com.eriksonn.createaeronautics.utils.Matrix3dExtension;
import com.eriksonn.createaeronautics.world.FakeAirshipClientWorld;
Expand Down Expand Up @@ -86,6 +88,10 @@ protected void defineSynchedData() {
public AirshipContraptionEntity(EntityType<?> type, World world) {
super(type, world);
simulatedRigidbody = new SimulatedContraptionRigidbody(this);

// testing
// this.simulatedRigidbody.angularMomentum = new Vector3d(0, 0, 40);

System.out.println("New airship entity");
}

Expand Down Expand Up @@ -126,12 +132,12 @@ public void tickContraption() {
profiler.startTick();
fakeClientWorld.tick(() -> true);


for (ControlledContraptionEntity contraptionEntity : subContraptions.values()) {
contraptionEntity.tick();
}
fakeClientWorld.tickEntities();

fakeClientWorld.tickBlockEntities();
profiler.endTick();

if (invalid) {
Expand Down Expand Up @@ -162,28 +168,17 @@ public void tickContraption() {
//for(Map.Entry<UUID, SubcontraptionRigidbody> entry : simulatedRigidbody.subcontraptionRigidbodyMap.entrySet())
//{
// SubcontraptionRigidbody rigidbody = entry.getValue();
// Vector3d particlePos = rigidbody.toGlobal(new Vector3d(0,0,0));
// level.addParticle(new RedstoneParticleData(1,1,1,1),particlePos.x,particlePos.y,particlePos.z,0,0,0);
//}
// x

if(level.isClientSide && false) {
CompoundNBT tag = this.entityData.get(physicsDataAccessor);
if(tag.contains("velocity")) {
simulatedRigidbody.globalVelocity = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "velocity"));
//simulatedRigidbody.angularVelocity = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "angularVelocity"));
simulatedRigidbody.angularMomentum = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "angularMomentum"));
//simulatedRigidbody.momentum = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "momentum"));
simulatedRigidbody.orientation = simulatedRigidbody.arrayToQuat(readDoubleArray(tag, "orientation"));
}
}

//}

// if(level.isClientSide)



//Vector3d particlePos = toGlobalVector(new Vector3d(0,0,0),0);
//level.addParticle(new RedstoneParticleData(1,1,1,1),particlePos.x,particlePos.y,particlePos.z,0,0,0);
//this.getContraption().getContraptionWorld().tickBlockEntities();
// Vector3d particlePos = simulatedRigidbody.toGlobal(simulatedRigidbody.toLocal(simulatedRigidbody.toGlobal(new Vector3d(1,1,1))));
// level.addParticle(new RedstoneParticleData(1,1,1,1),particlePos.x,particlePos.y,particlePos.z,0,0,0);

}

Expand Down Expand Up @@ -212,17 +207,20 @@ private double[] readDoubleArray(CompoundNBT tag, String key) {

@Override
public void onSyncedDataUpdated(DataParameter<?> pKey) {
if (pKey == physicsDataAccessor) {
if (pKey.equals(physicsDataAccessor)) {
CompoundNBT tag = this.entityData.get((DataParameter<CompoundNBT>) pKey);


simulatedRigidbody.momentum = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "momentum"));
simulatedRigidbody.angularMomentum = simulatedRigidbody.arrayToVec(readDoubleArray(tag, "angularMomentum"));
simulatedRigidbody.orientation = simulatedRigidbody.arrayToQuat(readDoubleArray(tag, "orientation"));
}
}

@Override
public void onRemovedFromWorld() {
subContraptions.forEach((uuid, contraptionEntity) -> {
contraptionEntity.disassemble();
contraptionEntity.getContraption().addBlocksToWorld(contraptionEntity.level, ((ControlledContraptionEntityMixin)contraptionEntity).invokeMakeStructureTransform());
contraptionEntity.remove();
serverDestroySubContraption(contraptionEntity);
});
super.onRemovedFromWorld();
Expand All @@ -246,10 +244,8 @@ public void serverUpdate() {
}

CompoundNBT tag = new CompoundNBT();
putDoubleArray(tag, "velocity", simulatedRigidbody.vecToArray(simulatedRigidbody.globalVelocity));
//putDoubleArray(tag, "angularVelocity", simulatedRigidbody.vecToArray(simulatedRigidbody.angularVelocity));
//putDoubleArray(tag, "angularMomentum", simulatedRigidbody.vecToArray(simulatedRigidbody.angularVelocity));
//putDoubleArray(tag, "momentum", simulatedRigidbody.vecToArray(simulatedRigidbody.angularVelocity));
putDoubleArray(tag, "angularMomentum", simulatedRigidbody.vecToArray(simulatedRigidbody.angularMomentum));
putDoubleArray(tag, "momentum", simulatedRigidbody.vecToArray(simulatedRigidbody.momentum));
putDoubleArray(tag, "orientation", simulatedRigidbody.quatToArray(simulatedRigidbody.orientation));
this.entityData.set(physicsDataAccessor, tag);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eriksonn.createaeronautics.index;

import com.eriksonn.createaeronautics.CreateAeronautics;
import com.eriksonn.createaeronautics.physics.collision.shape.CollisionQuality;
import net.minecraftforge.common.ForgeConfigSpec;

/**
Expand All @@ -12,13 +13,21 @@ public class CAConfig {
public static final String SERVER_FILENAME = CreateAeronautics.MODID + "-server.toml";

public static final String PHYSICS_CATEGORY = "Physics";
public static final ForgeConfigSpec.EnumValue<CollisionQuality> COLLISION_QUALITY;
public static final ForgeConfigSpec.IntValue MAX_COLLISION_ITERATIONS;
public static final ForgeConfigSpec.DoubleValue LIGHT_BLOCK_WEIGHT;
public static final ForgeConfigSpec.DoubleValue DEFAULT_BLOCK_WEIGHT;

static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();

builder.push(PHYSICS_CATEGORY);
COLLISION_QUALITY = builder
.comment("What quality of collision detection should simulated contraptions use?")
.defineEnum("collisionQuality", CollisionQuality.GJKEPA);
MAX_COLLISION_ITERATIONS = builder
.comment("How many iterations should the collision engine be allowed run per tick?")
.defineInRange("maxIterations", 3, 1, 20);
LIGHT_BLOCK_WEIGHT = builder
.comment("Some blocks have the 'light' tag and will be given less weight than others. How much should these weigh?")
.defineInRange("lightBlockWeight", 0.2, 0, Double.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import net.minecraftforge.event.RegistryEvent;

public final class CADimensions {
public CADimensions()
{}
public CADimensions() {}
public void registerDimension(RegistryEvent.NewRegistry e) {
Registry.register(Registry.CHUNK_GENERATOR, AirshipDimensionManager.CHUNK_GENERATOR_ID, ChunkGenerator.CODEC);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.eriksonn.createaeronautics.physics;

import com.eriksonn.createaeronautics.physics.collision.shape.ICollisionShape;
import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption;
import com.simibubi.create.content.contraptions.components.structureMovement.bearing.SailBlock;
import net.minecraft.block.BlockState;
Expand All @@ -8,6 +10,7 @@
import net.minecraft.world.gen.feature.template.Template;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractContraptionRigidbody implements IRigidbody{
Expand All @@ -30,6 +33,8 @@ public void generateMassDependentParameters(Contraption contraption,Vector3d off
localMass+=blockMass;
}
}


localCenterOfMass=localCenterOfMass.scale(1.0/localMass);
for (Map.Entry<BlockPos, Template.BlockInfo> entry : contraption.getBlocks().entrySet()) {
if (!entry.getValue().state.isAir()) {
Expand Down Expand Up @@ -58,4 +63,19 @@ public void findSails(Contraption contraption)
}
public double getLocalMass() { return localMass; }
public Vector3d getLocalCenterOfMass(){ return localCenterOfMass; }

//#region Collision
HashMap<BlockPos, List<ICollisionShape>> collisionShapes = new HashMap<>();

public HashMap<BlockPos, List<ICollisionShape>> getCollisionShapes() {
return collisionShapes;
}

public void setCollisionShapes(HashMap<BlockPos, List<ICollisionShape>> collisionShapes) {
this.collisionShapes = collisionShapes;
}
//#endregion

public abstract AbstractContraptionEntity getContraption();
abstract public Vector3d getPlotOffset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IRigidbody {
//#region Interaction
void addForce(Vector3d pos,Vector3d force);
void addGlobalForce(Vector3d pos,Vector3d force);
void addVelocity(Vector3d pos,Vector3d velocity);
void addGlobalVelocity(Vector3d pos,Vector3d velocity);
void applyImpulse(Vector3d pos, Vector3d velocity);
void applyGlobalImpulse(Vector3d pos, Vector3d velocity);
//#endregion
}
Loading

0 comments on commit 1de76fc

Please sign in to comment.