use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class EventsClient method onRenderTickEvent.
@SubscribeEvent
public void onRenderTickEvent(RenderTickEvent event) {
final World world = Minecraft.getMinecraft().world;
if (world == null) {
// No ships to worry about.
return;
}
double partialTicks = event.renderTickTime;
if (Minecraft.getMinecraft().isGamePaused()) {
partialTicks = Minecraft.getMinecraft().renderPartialTicksPaused;
}
if (event.phase == Phase.START) {
lastPositionsMap.clear();
for (PhysicsObject wrapper : ValkyrienUtils.getPhysosLoadedInWorld(world)) {
wrapper.getShipTransformationManager().updateRenderTransform(partialTicks);
}
// variables so that Minecraft's interpolation code computes the correct value.
for (final Entity entity : world.getLoadedEntityList()) {
final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(entity);
if (entityShipMovementData.getLastTouchedShip() != null && entityShipMovementData.getTicksSinceTouchedShip() < VSConfig.ticksToStickToShip) {
final PhysicsObject shipPhysicsObject = ValkyrienUtils.getPhysObjWorld(world).getPhysObjectFromUUID(entityShipMovementData.getLastTouchedShip().getUuid());
if (shipPhysicsObject == null) {
// This ship doesn't exist anymore, just remove it.
IDraggable.class.cast(entity).setEntityShipMovementData(entityShipMovementData.withLastTouchedShip(null));
continue;
}
final ShipTransform prevTickTransform = shipPhysicsObject.getPrevTickShipTransform();
final ShipTransform shipRenderTransform = shipPhysicsObject.getShipTransformationManager().getRenderTransform();
final Vector3dc entityAddedVelocity = entityShipMovementData.getAddedLinearVelocity();
// The velocity the entity was moving without the added velocity from the ship
final double entityMovementX = entity.posX - entityAddedVelocity.x() - entity.lastTickPosX;
final double entityMovementY = entity.posY - entityAddedVelocity.y() - entity.lastTickPosY;
final double entityMovementZ = entity.posZ - entityAddedVelocity.z() - entity.lastTickPosZ;
// Compute the position the entity should be rendered at this frame
final Vector3d entityShouldBeHere = new Vector3d(entity.lastTickPosX, entity.lastTickPosY, entity.lastTickPosZ);
entityShouldBeHere.add(entityMovementX * partialTicks, entityMovementY * partialTicks, entityMovementZ * partialTicks);
prevTickTransform.transformPosition(entityShouldBeHere, TransformType.GLOBAL_TO_SUBSPACE);
shipRenderTransform.transformPosition(entityShouldBeHere, TransformType.SUBSPACE_TO_GLOBAL);
// Save the entity lastTickPos in the map
lastPositionsMap.put(entity, new Vector3d(entity.lastTickPosX, entity.lastTickPosY, entity.lastTickPosZ));
// Then update lastTickPos such that Minecraft's interpolation code will render entity at entityShouldBeHere.
entity.lastTickPosX = (entityShouldBeHere.x() - (entity.posX * partialTicks)) / (1 - partialTicks);
entity.lastTickPosY = (entityShouldBeHere.y() - (entity.posY * partialTicks)) / (1 - partialTicks);
entity.lastTickPosZ = (entityShouldBeHere.z() - (entity.posZ * partialTicks)) / (1 - partialTicks);
}
}
// endregion
} else {
// Once the rendering code has finished we restore the lastTickPos variables to their old values.
for (final Entity entity : world.getLoadedEntityList()) {
if (lastPositionsMap.containsKey(entity)) {
final Vector3dc entityLastPosition = lastPositionsMap.get(entity);
entity.lastTickPosX = entityLastPosition.x();
entity.lastTickPosY = entityLastPosition.y();
entity.lastTickPosZ = entityLastPosition.z();
}
}
}
}
use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class BlockCaptainsChair method onBlockActivated.
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!worldIn.isRemote) {
Optional<PhysicsObject> physicsObject = ValkyrienUtils.getPhysoManagingBlock(worldIn, pos);
if (physicsObject.isPresent()) {
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (tileEntity instanceof TileEntityCaptainsChair) {
Vector3d playerPos = new Vector3d(playerIn.posX, playerIn.posY, playerIn.posZ);
physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.SUBSPACE_TO_GLOBAL);
playerIn.posX = playerPos.x;
playerIn.posY = playerPos.y;
playerIn.posZ = playerPos.z;
// Only mount the player if they're standing on the ship.
final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(playerIn);
if (entityShipMovementData.getTicksSinceTouchedShip() == 0 && (entityShipMovementData.getLastTouchedShip() == physicsObject.get().getShipData())) {
Vector3dc localMountPos = getPlayerMountOffset(state, pos);
ValkyrienUtils.fixEntityToShip(playerIn, localMountPos, physicsObject.get());
}
((TileEntityCaptainsChair) tileEntity).setPilotEntity(playerIn);
physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transformPosition(playerPos, TransformType.GLOBAL_TO_SUBSPACE);
playerIn.posX = playerPos.x;
playerIn.posY = playerPos.y;
playerIn.posZ = playerPos.z;
}
}
}
return true;
}
use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class MixinEntityIntrinsic method onEntityPostMove.
/**
* The goal of this injection is to correctly setup {@link IDraggable#getEntityShipMovementData()} for this Entity.
* Specifically this code handles the last ship touched by the entity, as well as how many ticks ago that touch was.
*/
@Inject(method = "move", at = @At("RETURN"))
private void onEntityPostMove(CallbackInfo callbackInfo) {
final EntityShipMovementData oldEntityShipMovementData = thisClassAsDraggable.getEntityShipMovementData();
if (alteredMovement != null) {
// If alteredMovement isn't null then we're touching a ship.
final EntityShipMovementData newEntityShipMovementData = oldEntityShipMovementData.withLastTouchedShip(alteredMovement.shipTouched).withTicksSinceTouchedShip(0).withTicksPartOfGround(0);
thisClassAsDraggable.setEntityShipMovementData(newEntityShipMovementData);
EntityCollisionInjector.alterEntityMovementPost(thisClassAsAnEntity, alteredMovement);
} else {
if (this.collided) {
// If we collided and alteredMovement is null, then we're touching the ground.
final int newTicksPartOfGround = oldEntityShipMovementData.getTicksPartOfGround() + 1;
final EntityShipMovementData newEntityShipMovementData = new EntityShipMovementData(null, 0, newTicksPartOfGround, new Vector3d(), 0);
thisClassAsDraggable.setEntityShipMovementData(newEntityShipMovementData);
} else {
// If we're not collided and alteredMovement is null, then we're in the air.
final int newTicksPartOfGround;
if (oldEntityShipMovementData.getLastTouchedShip() != null) {
newTicksPartOfGround = 0;
} else {
newTicksPartOfGround = oldEntityShipMovementData.getTicksPartOfGround() + 1;
}
final EntityShipMovementData newEntityShipMovementData = oldEntityShipMovementData.withTicksSinceTouchedShip(oldEntityShipMovementData.getTicksSinceTouchedShip() + 1).withTicksPartOfGround(newTicksPartOfGround);
thisClassAsDraggable.setEntityShipMovementData(newEntityShipMovementData);
}
}
}
use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class EntityCollisionInjector method alterEntityMovement.
// Returns false if game should use default collision
@Nullable
public static IntermediateMovementVariableStorage alterEntityMovement(Entity entity, MoverType type, double dx, double dy, double dz) {
final double origDx = dx;
final double origDy = dy;
final double origDz = dz;
final double origPosX = entity.posX;
final double origPosY = entity.posY;
final double origPosZ = entity.posZ;
boolean isLiving = entity instanceof EntityLivingBase;
Vec3d velocity = new Vec3d(dx, dy, dz);
Polygon playerBeforeMove = new Polygon(entity.getEntityBoundingBox());
List<Polygon> colPolys = getCollidingPolygonsAndDoBlockCols(entity, velocity);
PhysicsObject worldBelow = null;
IDraggable draggable = EntityDraggable.getDraggableFromEntity(entity);
final EntityShipMovementData lastTickEntityShipMovementData = draggable.getEntityShipMovementData();
Vector3d total = new Vector3d();
// Used to reset the player position after collision processing, effectively
// using the player to integrate their velocity
double posOffestX = 0;
double posOffestY = 0;
double posOffestZ = 0;
// True IFF the player is on a ladder
boolean isPlayerOnLadder = false;
// region Ladder movement
if (entity instanceof EntityLivingBase) {
final EntityLivingBase base = (EntityLivingBase) entity;
final List<PhysicsObject> collidingShips = ((IHasShipManager) entity.getEntityWorld()).getManager().getPhysObjectsInAABB(base.getEntityBoundingBox());
final Iterable<Triple<PhysicsObject, BlockPos, IBlockState>> ladderCollisions = getLadderCollisions(base, collidingShips);
// For now, just ignore the y component. I may or may not use it later.
final float forward = ((EntityLivingBase) entity).moveForward;
final float strafe = ((EntityLivingBase) entity).moveStrafing;
final double f1 = Math.sin(Math.toRadians(entity.rotationYaw));
final double f2 = Math.cos(Math.toRadians(entity.rotationYaw));
final double intendedXVel = strafe * f2 - forward * f1;
final double intendedYVel = 0;
final double intendedZVel = forward * f2 + strafe * f1;
final Vector3dc originalVelocityDirection = new Vector3d(intendedXVel, intendedYVel, intendedZVel).normalize();
final World world = entity.world;
final Polygon playerPolygon = new Polygon(base.getEntityBoundingBox());
for (final Triple<PhysicsObject, BlockPos, IBlockState> ladderCollision : ladderCollisions) {
final IBlockState ladderState = ladderCollision.getRight();
EnumFacing ladderFacing = null;
// For now, we only support a few blocks
if (ladderState.getPropertyKeys().contains(BlockHorizontal.FACING)) {
ladderFacing = ladderState.getValue(BlockHorizontal.FACING);
}
// We need the EnumFacing of the ladder for the code to work. If we couldn't find it then just give up :/
if (ladderFacing != null) {
final Vector3d ladderNormal = JOML.convertDouble(ladderFacing.getDirectionVec());
final ShipTransform shipTransform = ladderCollision.getLeft().getShipTransform();
// Grow the ladder BB by a small margin (makes the ladder experience better imo)
final AxisAlignedBB ladderBB = ladderCollision.getRight().getBoundingBox(world, ladderCollision.getMiddle()).offset(ladderCollision.getMiddle()).grow(.4);
final Polygon ladderPoly = new Polygon(ladderBB, shipTransform.getSubspaceToGlobal());
// Determine if the player is actually colliding with the ladder
final PhysPolygonCollider collider = new PhysPolygonCollider(playerPolygon, ladderPoly, ladderCollision.getLeft().getShipTransformationManager().normals);
collider.processData();
shipTransform.transformDirection(ladderNormal, TransformType.SUBSPACE_TO_GLOBAL);
// Don't use "floor ladders"
final boolean isLadderFacingDown = ladderNormal.y > .8;
if (isLadderFacingDown) {
continue;
}
// If the ladder is facing up, then let the player use them like monkey bars
final boolean isLadderFacingUp = ladderNormal.y < -.8;
// Whether or not the player is actually colliding with a ladder, since it is close to one we give the player ladder movement.
dx = MathHelper.clamp(dx, -.15, .15);
dz = MathHelper.clamp(dz, -.15, .15);
base.fallDistance = 0;
if (!isLadderFacingUp) {
// Use ladders like normal
if (dy < -.15) {
dy = -.15;
}
final boolean isPlayerGoingTowardsLadder = originalVelocityDirection.dot(ladderNormal) < -.1;
final boolean isPlayerSneakingOnLadder = base.isSneaking() && base instanceof EntityPlayer;
if (isPlayerSneakingOnLadder && dy < 0) {
dy = 0;
}
if (!collider.seperated && isPlayerGoingTowardsLadder) {
dy = .2;
}
} else {
// Use ladders like monkey bars
dy = .2;
}
worldBelow = ladderCollision.getLeft();
isPlayerOnLadder = true;
break;
}
}
}
// endregion
final Vector3dc velVec = new Vector3d(dx, dy, dz);
for (Polygon poly : colPolys) {
if (poly instanceof ShipPolygon) {
ShipPolygon shipPoly = (ShipPolygon) poly;
try {
EntityPolygonCollider fast = new EntityPolygonCollider(playerBeforeMove, shipPoly, shipPoly.normals, velVec.add(total, new Vector3d()));
if (!fast.arePolygonsSeparated()) {
// fastCollisions.add(fast);
worldBelow = shipPoly.shipFrom;
Vector3d response = fast.getCollisions()[fast.getMinDistanceIndex()].getResponse();
// TODO: Add more potential yResponses
double stepSquared = entity.stepHeight * entity.stepHeight;
// Do not do stair stepping if the player is on a ladder.
boolean isStep = isLiving && entity.onGround && !isPlayerOnLadder;
if (response.y >= 0 && VSMath.canStandOnNormal(fast.getCollisionAxes()[fast.getMinDistanceIndex()])) {
Vector3d slowButStopped = new Vector3d(0, -fast.getCollisions()[fast.getMinDistanceIndex()].getCollisionPenetrationDistance() / fast.getCollisionAxes()[fast.getMinDistanceIndex()].y(), 0);
response = slowButStopped;
}
if (isStep) {
EntityLivingBase living = (EntityLivingBase) entity;
if (Math.abs(living.moveForward) > .01 || Math.abs(living.moveStrafing) > .01) {
for (int i = 3; i < 6; i++) {
Vector3d tempResponse = fast.getCollisions()[i].getResponse();
if (tempResponse.y > 0 && VSMath.canStandOnNormal(fast.getCollisions()[i].getCollisionNormal()) && tempResponse.lengthSquared() < stepSquared) {
if (tempResponse.lengthSquared() < .1) {
// Too small to be a real step, let it through
response = tempResponse;
} else {
// System.out.println("Try Stepping!");
AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(tempResponse.x, tempResponse.y, tempResponse.z);
// Don't allow the player to step if the step will put them in another polygon.
boolean collidesWithAnything = false;
{
final AxisAlignedBB newEntityBBShrunk = axisalignedbb.shrink(.15);
final Polygon newEntityBBShrunkPolygon = new Polygon(newEntityBBShrunk);
for (Polygon potentialStepCollision : colPolys) {
if (potentialStepCollision == poly) {
// Don't run this on ourself
continue;
}
if (potentialStepCollision.getEnclosedAABB().intersects(newEntityBBShrunk)) {
// Finer check
ShipPolygon potentialStepCollisionShipPoly = (ShipPolygon) potentialStepCollision;
final EntityPolygonCollider checkIfStepCollidesWithBlock = new EntityPolygonCollider(newEntityBBShrunkPolygon, potentialStepCollisionShipPoly, potentialStepCollisionShipPoly.normals, new Vector3d());
checkIfStepCollidesWithBlock.processData();
if (!checkIfStepCollidesWithBlock.arePolygonsSeparated()) {
collidesWithAnything = true;
break;
}
}
}
}
if (!collidesWithAnything) {
entity.setEntityBoundingBox(axisalignedbb);
// I think this correct, but it may create more problems than it solves
response.zero();
entity.resetPositionToBB();
}
}
}
}
}
}
// total.add(response);
if (Math.abs(response.x) > .01D) {
total.x += response.x;
}
if (Math.abs(response.y) > .01D) {
total.y += response.y;
}
if (Math.abs(response.z) > .01D) {
total.z += response.z;
}
entity.posX += response.x;
entity.posY += response.y;
entity.posZ += response.z;
posOffestX += response.x;
posOffestY += response.y;
posOffestZ += response.z;
AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(response.x, response.y, response.z);
entity.setEntityBoundingBox(axisalignedbb);
entity.resetPositionToBB();
}
} catch (Exception e) {
// Do nothing
}
}
}
AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(-posOffestX, -posOffestY, -posOffestZ);
entity.setEntityBoundingBox(axisalignedbb);
entity.resetPositionToBB();
// We are on the ship that we are riding
if (entity.ridingEntity instanceof EntityMountable) {
final EntityMountable entityMountable = (EntityMountable) entity.ridingEntity;
if (entityMountable.getReferencePosOptional().isPresent()) {
final Optional<PhysicsObject> physicsObjectOptional = ValkyrienUtils.getPhysoManagingBlock(entity.world, entityMountable.getReferencePosOptional().get());
if (physicsObjectOptional.isPresent()) {
worldBelow = physicsObjectOptional.get();
}
}
}
if (worldBelow == null) {
return null;
}
dx += total.x;
dy += total.y;
dz += total.z;
boolean alreadyOnGround = entity.onGround && (dy == origDy) && origDy < 0;
Vector3d original = new Vector3d(origDx, origDy, origDz);
Vector3d newMov = new Vector3d(dx - origDx, dy - origDy, dz - origDz);
entity.collidedHorizontally = original.dot(newMov) < 0;
entity.collidedVertically = isDifSignificant(dy, origDy);
entity.onGround = entity.collidedVertically && origDy < 0 || alreadyOnGround;
entity.collided = entity.collidedHorizontally || entity.collidedVertically;
// entity.resetPositionToBB();
double motionYBefore = entity.motionY;
float oldFallDistance = entity.fallDistance;
Vector3d dxyz = new Vector3d(dx, dy, dz);
;
Vector3d origDxyz = new Vector3d(origDx, origDy, origDz);
Vector3d origPosXyz = new Vector3d(origPosX, origPosY, origPosZ);
return new IntermediateMovementVariableStorage(dxyz, origDxyz, origPosXyz, alreadyOnGround, motionYBefore, oldFallDistance, worldBelow.getShipData());
}
use of org.valkyrienskies.mod.common.entity.EntityShipMovementData in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class PlayerMovementDataGenerator method generatePlayerMovementDataForClient.
/**
* Only works on the client.
*/
public static PlayerMovementData generatePlayerMovementDataForClient() {
final EntityPlayerSP entityPlayer = Minecraft.getMinecraft().player;
final EntityShipMovementData entityShipMovementData = ValkyrienUtils.getEntityShipMovementDataFor(entityPlayer);
final ShipData lastTouchedShip = entityShipMovementData.getLastTouchedShip();
final UUID lastTouchedShipId = lastTouchedShip != null ? lastTouchedShip.getUuid() : null;
final Vector3d playerPosInLocal = new Vector3d(entityPlayer.posX, entityPlayer.posY, entityPlayer.posZ);
final Vector3d playerLookInLocal = JOML.convert(entityPlayer.getLook(1));
final boolean onGround = entityPlayer.onGround;
if (lastTouchedShip != null) {
final ShipTransform shipTransform = lastTouchedShip.getShipTransform();
shipTransform.transformPosition(playerPosInLocal, TransformType.GLOBAL_TO_SUBSPACE);
shipTransform.transformDirection(playerLookInLocal, TransformType.GLOBAL_TO_SUBSPACE);
}
return new PlayerMovementData(lastTouchedShipId, entityShipMovementData.getTicksSinceTouchedShip(), entityShipMovementData.getTicksPartOfGround(), playerPosInLocal, playerLookInLocal, onGround);
}
Aggregations