use of org.joml.Quaterniond in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class MixinEntityRenderer method orientCamera.
@Inject(method = "orientCamera", at = @At("HEAD"), cancellable = true)
private void orientCamera(float partialTicks, CallbackInfo ci) {
EntityShipMountData mountData = ValkyrienUtils.getMountedShipAndPos(mc.getRenderViewEntity());
if (mountData.getMountedShip() == null) {
// Do nothing. We don't want to mess with camera code unless we have to.
return;
} else {
// Take over the camera orientation entirely. Don't let anything else touch it.
ci.cancel();
}
Entity entity = this.mc.getRenderViewEntity();
Vector3d eyeVector = new Vector3d(0, entity.getEyeHeight(), 0);
if (entity instanceof EntityLivingBase && ((EntityLivingBase) entity).isPlayerSleeping()) {
eyeVector.y += .7D;
}
double d0 = entity.prevPosX + (entity.posX - entity.prevPosX) * partialTicks;
double d1 = entity.prevPosY + (entity.posY - entity.prevPosY) * partialTicks;
double d2 = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * partialTicks;
// Probably overkill, but this should 100% fix the crash in issue #78
if (mountData.isMounted() && mountData.getMountedShip().getShipRenderer().offsetPos != null) {
final ShipTransform renderTransform = mountData.getMountedShip().getShipTransformationManager().getRenderTransform();
renderTransform.transformDirection(eyeVector, TransformType.SUBSPACE_TO_GLOBAL);
Vector3d playerPosition = JOML.convert(mountData.getMountPos());
renderTransform.transformPosition(playerPosition, TransformType.SUBSPACE_TO_GLOBAL);
d0 = playerPosition.x;
d1 = playerPosition.y;
d2 = playerPosition.z;
}
d0 += eyeVector.x;
d1 += eyeVector.y;
d2 += eyeVector.z;
if (entity instanceof EntityLivingBase && ((EntityLivingBase) entity).isPlayerSleeping()) {
if (!this.mc.gameSettings.debugCamEnable) {
// VS code starts here
if (mountData.isMounted()) {
Vector3d playerPosInLocal = JOML.convert(mountData.getMountPos());
playerPosInLocal.sub(.5D, .6875, .5);
playerPosInLocal.round();
BlockPos bedPos = new BlockPos(playerPosInLocal.x, playerPosInLocal.y, playerPosInLocal.z);
IBlockState state = this.mc.world.getBlockState(bedPos);
Block block = state.getBlock();
float angleYaw = 0;
if (block != null && block.isBed(state, entity.world, bedPos, entity)) {
angleYaw = block.getBedDirection(state, entity.world, bedPos).getHorizontalIndex() * 90;
angleYaw += 180;
}
entity.rotationYaw = entity.prevRotationYaw = angleYaw;
entity.rotationPitch = entity.prevRotationPitch = 0;
} else {
BlockPos blockpos = new BlockPos(entity);
IBlockState iblockstate = this.mc.world.getBlockState(blockpos);
net.minecraftforge.client.ForgeHooksClient.orientBedCamera(this.mc.world, blockpos, iblockstate, entity);
GlStateManager.rotate(entity.prevRotationYaw + (entity.rotationYaw - entity.prevRotationYaw) * partialTicks + 180.0F, 0.0F, -1.0F, 0.0F);
GlStateManager.rotate(entity.prevRotationPitch + (entity.rotationPitch - entity.prevRotationPitch) * partialTicks, -1.0F, 0.0F, 0.0F);
}
}
} else if (this.mc.gameSettings.thirdPersonView > 0) {
double d3 = this.thirdPersonDistancePrev + (4.0F - this.thirdPersonDistancePrev) * partialTicks;
IShipPilot shipPilot = (IShipPilot) Minecraft.getMinecraft().player;
if (shipPilot.isPilotingShip()) {
// TODO: Make this number scale with the Ship
d3 = 15D;
}
if (this.mc.gameSettings.debugCamEnable) {
GlStateManager.translate(0.0F, 0.0F, (float) (-d3));
} else {
float f1 = entity.rotationYaw;
float f2 = entity.rotationPitch;
if (this.mc.gameSettings.thirdPersonView == 2) {
f2 += 180.0F;
}
double d4 = -MathHelper.sin(f1 * 0.017453292F) * MathHelper.cos(f2 * 0.017453292F) * d3;
double d5 = MathHelper.cos(f1 * 0.017453292F) * MathHelper.cos(f2 * 0.017453292F) * d3;
double d6 = (-MathHelper.sin(f2 * 0.017453292F)) * d3;
for (int i = 0; i < 8; ++i) {
float f3 = (i & 1) * 2 - 1;
float f4 = (i >> 1 & 1) * 2 - 1;
float f5 = (i >> 2 & 1) * 2 - 1;
f3 = f3 * 0.1F;
f4 = f4 * 0.1F;
f5 = f5 * 0.1F;
IShipPilot pilot = (IShipPilot) Minecraft.getMinecraft().player;
((IWorldVS) this.mc.world).excludeShipFromRayTracer(((IShipPilot) this.mc.player).getPilotedShip());
// RayTraceResult raytraceresult = EntityMoveInjectionMethods.rayTraceBlocksIgnoreShip(Minecraft.getMinecraft().world, new Vec3d(d0 + f3, d1 + f4, d2 + f5), new Vec3d(d0 - d4 + f3 + f5, d1 - d6 + f4, d2 - d5 + f5), false, false, false, pilot.getPilotedShip());
RayTraceResult raytraceresult = mc.world.rayTraceBlocks(new Vec3d(d0 + (double) f3, d1 + (double) f4, d2 + (double) f5), new Vec3d(d0 - d4 + (double) f3 + (double) f5, d1 - d6 + (double) f4, d2 - d5 + (double) f5));
((IWorldVS) this.mc.world).unexcludeShipFromRayTracer(((IShipPilot) this.mc.player).getPilotedShip());
if (raytraceresult != null) {
double d7 = raytraceresult.hitVec.distanceTo(new Vec3d(d0, d1, d2));
if (d7 < d3) {
d3 = d7;
}
}
}
if (this.mc.gameSettings.thirdPersonView == 2) {
GlStateManager.rotate(180.0F, 0.0F, 1.0F, 0.0F);
}
GlStateManager.rotate(entity.rotationPitch - f2, 1.0F, 0.0F, 0.0F);
GlStateManager.rotate(entity.rotationYaw - f1, 0.0F, 1.0F, 0.0F);
GlStateManager.translate(0.0F, 0.0F, (float) (-d3));
GlStateManager.rotate(f1 - entity.rotationYaw, 0.0F, 1.0F, 0.0F);
GlStateManager.rotate(f2 - entity.rotationPitch, 1.0F, 0.0F, 0.0F);
}
} else {
GlStateManager.translate(0.0F, 0.0F, 0.05F);
}
if (!this.mc.gameSettings.debugCamEnable) {
float yaw = entity.prevRotationYaw + (entity.rotationYaw - entity.prevRotationYaw) * partialTicks + 180.0F;
float pitch = entity.prevRotationPitch + (entity.rotationPitch - entity.prevRotationPitch) * partialTicks;
float roll = 0.0F;
if (entity instanceof EntityAnimal) {
EntityAnimal entityanimal = (EntityAnimal) entity;
yaw = entityanimal.prevRotationYawHead + (entityanimal.rotationYawHead - entityanimal.prevRotationYawHead) * partialTicks + 180.0F;
}
IBlockState state = ActiveRenderInfo.getBlockStateAtEntityViewpoint(this.mc.world, entity, partialTicks);
net.minecraftforge.client.event.EntityViewRenderEvent.CameraSetup event = new net.minecraftforge.client.event.EntityViewRenderEvent.CameraSetup(EntityRenderer.class.cast(this), entity, state, partialTicks, yaw, pitch, roll);
net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
GlStateManager.rotate(event.getRoll(), 0.0F, 0.0F, 1.0F);
GlStateManager.rotate(event.getPitch(), 1.0F, 0.0F, 0.0F);
GlStateManager.rotate(event.getYaw(), 0.0F, 1.0F, 0.0F);
}
if (mountData.isMounted() && mountData.getMountedShip().getShipRenderer().offsetPos != null) {
final ShipTransform renderTransform = mountData.getMountedShip().getShipTransformationManager().getRenderTransform();
Quaterniond orientationQuat = renderTransform.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
Vector3dc radians = orientationQuat.getEulerAnglesXYZ(new Vector3d());
float moddedPitch = (float) Math.toDegrees(radians.x());
float moddedYaw = (float) Math.toDegrees(radians.y());
float moddedRoll = (float) Math.toDegrees(radians.z());
GlStateManager.rotate(-moddedRoll, 0.0F, 0.0F, 1.0F);
GlStateManager.rotate(-moddedYaw, 0.0F, 1.0F, 0.0F);
GlStateManager.rotate(-moddedPitch, 1.0F, 0.0F, 0.0F);
}
GlStateManager.translate(-eyeVector.x, -eyeVector.y, -eyeVector.z);
d0 = entity.prevPosX + (entity.posX - entity.prevPosX) * partialTicks + eyeVector.x;
d1 = entity.prevPosY + (entity.posY - entity.prevPosY) * partialTicks + eyeVector.y;
d2 = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * partialTicks + eyeVector.z;
this.cloudFog = this.mc.renderGlobal.hasCloudFog(d0, d1, d2, partialTicks);
}
use of org.joml.Quaterniond in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class PhysicsCalculations method integrateAngularVelocity.
/**
* Implementation is based on https://gafferongames.com/post/physics_in_3d/
*/
private void integrateAngularVelocity() {
// The body angular velocity vector, in World coordinates
if (angularVelocity.lengthSquared() < .001) {
// Angular velocity is zero, so the rotation hasn't changed.
return;
}
// If a ship is rotating too fast, then clamp its max rotational speed
if (angularVelocity.lengthSquared() > VSConfig.shipMaxAngularSpeed * VSConfig.shipMaxAngularSpeed) {
angularVelocity.normalize().mul(VSConfig.shipMaxAngularSpeed);
}
Vector3dc angularVelInBody = new Vector3d(angularVelocity);
AxisAngle4d axisAngle4d = new AxisAngle4d(angularVelInBody.length() * getPhysicsTimeDeltaPerPhysTick(), angularVelInBody.x(), angularVelInBody.y(), angularVelInBody.z());
axisAngle4d.normalize();
// Take the product of the current rotation with the change in rotation that results from
// the angular velocity. Then change our pitch/yaw/roll based on the result.
Quaterniondc rotationQuat = new Quaterniond(axisAngle4d);
physRotation = physRotation.premul(rotationQuat, new Quaterniond()).normalize();
}
use of org.joml.Quaterniond in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class PhysicsCalculations method generatePhysicsTransform.
public void generatePhysicsTransform() {
// Create a new physics transform.
ShipTransform parentTransform = getParent().getShipData().getShipTransform();
physRotation = parentTransform.getSubspaceToGlobal().getNormalizedRotation(new Quaterniond());
physX = parentTransform.getPosX();
physY = parentTransform.getPosY();
physZ = parentTransform.getPosZ();
physCenterOfMass = parentTransform.getCenterCoord();
ShipTransform physicsTransform = new ShipTransform(physX, physY, physZ, physRotation, physCenterOfMass);
getParent().getShipTransformationManager().setCurrentPhysicsTransform(physicsTransform);
// We're doing this afterwards to prevent from prevPhysicsTransform being null.
getParent().getShipTransformationManager().updatePreviousPhysicsTransform();
}
use of org.joml.Quaterniond in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class ShipTransformationManager method updateRenderTransform.
public void updateRenderTransform(double partialTick) {
if (partialTick == 0) {
renderTransform = prevTickTransform;
return;
} else if (partialTick == 1) {
renderTransform = currentTickTransform;
return;
}
ShipTransform prev = prevTickTransform;
ShipTransform cur = currentTickTransform;
Vector3dc shipCenter = parent.getCenterCoord();
Vector3d prevPos = new Vector3d(shipCenter);
Vector3d curPos = new Vector3d(shipCenter);
prev.transformPosition(prevPos, TransformType.SUBSPACE_TO_GLOBAL);
cur.transformPosition(curPos, TransformType.SUBSPACE_TO_GLOBAL);
Vector3d deltaPos = curPos.sub(prevPos, new Vector3d());
deltaPos.mul(partialTick);
Vector3d partialPos = new Vector3d(prevPos);
// Now partialPos is complete
partialPos.add(deltaPos);
Quaterniondc prevRot = prev.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
Quaterniondc curRot = cur.rotationQuaternion(TransformType.SUBSPACE_TO_GLOBAL);
Quaterniondc partialRot = prevRot.slerp(curRot, partialTick, new Quaterniond()).normalize();
// Put it all together to get the render transform.
renderTransform = new ShipTransform(partialPos.x, partialPos.y, partialPos.z, partialRot, parent.getCenterCoord());
}
Aggregations