Skip to content

Commit

Permalink
Fix #1180 (#1200)
Browse files Browse the repository at this point in the history
Co-authored-by: Liruxo <46451644+Liruxo@users.noreply.github.com>
  • Loading branch information
Liruxo committed Apr 26, 2020
1 parent 17084ce commit 8cbf663
Showing 1 changed file with 180 additions and 170 deletions.
350 changes: 180 additions & 170 deletions src/main/java/com/flansmod/common/driveables/EntitySeat.java
Expand Up @@ -216,6 +216,179 @@ public void onUpdate()
minigunAngle += minigunSpeed;
}

@SideOnly(Side.CLIENT)
private void updateSeatRotation() {

Entity entityInThisSeat = getControllingPassenger();
boolean isThePlayer =
entityInThisSeat instanceof EntityPlayer && FlansMod.proxy.isThePlayer((EntityPlayer)entityInThisSeat);

if (!isThePlayer)
return;

// Move the seat accordingly
// Consider new Yaw and Yaw limiters

float targetX = playerLooking.getYaw();

float yawToMove = (targetX - looking.getYaw());
while(yawToMove > 180F)
{
yawToMove -= 360F;
}
while(yawToMove <= -180F)
{
yawToMove += 360F;
}

float signDeltaX = 0;
if(yawToMove > (seatInfo.aimingSpeed.x / 2) && !seatInfo.legacyAiming)
{
signDeltaX = 1;
}
else if(yawToMove < -(seatInfo.aimingSpeed.x / 2) && !seatInfo.legacyAiming)
{
signDeltaX = -1;
}
else
{
signDeltaX = 0;
}


// Calculate new yaw and consider yaw limiters
float newYaw = 0f;

if(seatInfo.legacyAiming || (signDeltaX == 0))
{
newYaw = playerLooking.getYaw();
}
else
{
newYaw = looking.getYaw() + signDeltaX * seatInfo.aimingSpeed.x;
}
// Since the yaw limiters go from -360 to 360, we need to find a pair of yaw values and check them both
float otherNewYaw = newYaw - 360F;
if(newYaw < 0)
otherNewYaw = newYaw + 360F;
if((!(newYaw >= seatInfo.minYaw) || !(newYaw <= seatInfo.maxYaw)) &&
(!(otherNewYaw >= seatInfo.minYaw) || !(otherNewYaw <= seatInfo.maxYaw)))
{
float newYawDistFromRange =
Math.min(Math.abs(newYaw - seatInfo.minYaw), Math.abs(newYaw - seatInfo.maxYaw));
float otherNewYawDistFromRange =
Math.min(Math.abs(otherNewYaw - seatInfo.minYaw), Math.abs(otherNewYaw - seatInfo.maxYaw));
// If the newYaw is closer to the range than the otherNewYaw, move newYaw into the range
if(newYawDistFromRange <= otherNewYawDistFromRange)
{
if(newYaw > seatInfo.maxYaw)
newYaw = seatInfo.maxYaw;
if(newYaw < seatInfo.minYaw)
newYaw = seatInfo.minYaw;
}
// Else, the otherNewYaw is closer, so move it in
else
{
if(otherNewYaw > seatInfo.maxYaw)
otherNewYaw = seatInfo.maxYaw;
if(otherNewYaw < seatInfo.minYaw)
otherNewYaw = seatInfo.minYaw;
// Then match up the newYaw with the otherNewYaw
if(newYaw < 0)
newYaw = otherNewYaw - 360F;
else newYaw = otherNewYaw + 360F;
}
}

// Calculate the new pitch and consider pitch limiters
float targetY = playerLooking.getPitch();

float pitchToMove = (targetY - looking.getPitch());
while(pitchToMove > 180F)
{
pitchToMove -= 360F;
}
while(pitchToMove <= -180F)
{
pitchToMove += 360F;
}

float signDeltaY = 0;
if(pitchToMove > (seatInfo.aimingSpeed.y / 2) && !seatInfo.legacyAiming)
{
signDeltaY = 1;
}
else if(pitchToMove < -(seatInfo.aimingSpeed.y / 2) && !seatInfo.legacyAiming)
{
signDeltaY = -1;
}
else
{
signDeltaY = 0;
}

float newPitch = 0f;


// Pitches the gun at the last possible moment in order to reach target pitch at the same time as target yaw.
float minYawToMove = 0f;

float currentYawToMove = 0f;

if(seatInfo.latePitch)
{
minYawToMove = ((float)Math
.sqrt((pitchToMove / seatInfo.aimingSpeed.y) * (pitchToMove / seatInfo.aimingSpeed.y))) *
seatInfo.aimingSpeed.x;
}
else
{
minYawToMove = 360f;
}

currentYawToMove = (float)Math.sqrt((yawToMove) * (yawToMove));

if(seatInfo.legacyAiming || (signDeltaY == 0))
{
newPitch = playerLooking.getPitch();
}
else if(!seatInfo.yawBeforePitch && currentYawToMove < minYawToMove)
{
newPitch = looking.getPitch() + signDeltaY * seatInfo.aimingSpeed.y;
}
else if(seatInfo.yawBeforePitch && signDeltaX == 0)
{
newPitch = looking.getPitch() + signDeltaY * seatInfo.aimingSpeed.y;
}
else if(seatInfo.yawBeforePitch)
{
newPitch = looking.getPitch();
}
else
{
newPitch = looking.getPitch();
}

if(newPitch > -seatInfo.minPitch)
newPitch = -seatInfo.minPitch;
if(newPitch < -seatInfo.maxPitch)
newPitch = -seatInfo.maxPitch;

// Now set the new angles
prevLooking = looking.clone();
looking.setAngles(newYaw, newPitch, 0F);

FlansMod.getPacketHandler().sendToServer(new PacketSeatUpdates(this));

playYawSound = signDeltaX != 0 && seatInfo.traverseSounds;

if(signDeltaY != 0 && !seatInfo.yawBeforePitch && currentYawToMove < minYawToMove)
{
playPitchSound = true;
}
else playPitchSound = signDeltaY != 0 && seatInfo.yawBeforePitch && signDeltaX == 0;
}

/**
* Set the position to be that of the driveable plus the local position, rotated
*/
Expand All @@ -236,6 +409,9 @@ public void updatePosition()
if(seatInfo == null)
seatInfo = driveable.getDriveableType().seats[seatID];

if (world.isRemote)
updateSeatRotation();

prevPlayerPosX = playerPosX;
prevPlayerPosY = playerPosY;
prevPlayerPosZ = playerPosZ;
Expand Down Expand Up @@ -385,18 +561,15 @@ public void onMouseMoved(int deltaX, int deltaY)
float lookSpeed = 4F;

// Angle stuff for the player
// Calculate the new pitch yaw while considering limiters
float newPlayerYaw = playerLooking.getYaw() + deltaX / lookSpeed * mc.gameSettings.mouseSensitivity;
float newPlayerPitch = playerLooking.getPitch() - deltaY / lookSpeed * mc.gameSettings.mouseSensitivity;

// Calculate the new pitch and consider pitch limiters
float newPlayerPitch = playerLooking.getPitch() -
deltaY / lookSpeed * mc.gameSettings.mouseSensitivity;
if(newPlayerPitch > -seatInfo.minPitch)
newPlayerPitch = -seatInfo.minPitch;
if(newPlayerPitch < -seatInfo.maxPitch)
newPlayerPitch = -seatInfo.maxPitch;

// Calculate new yaw and consider yaw limiters
float newPlayerYaw = playerLooking.getYaw() +
deltaX / lookSpeed * mc.gameSettings.mouseSensitivity;

// Since the yaw limiters go from -360 to 360, we need to find a pair of yaw values and check them both
float otherNewPlayerYaw = newPlayerYaw - 360F;
if(newPlayerYaw < 0)
Expand Down Expand Up @@ -436,169 +609,6 @@ public void onMouseMoved(int deltaX, int deltaY)
// Now set the new angles
playerLooking.setAngles(newPlayerYaw, newPlayerPitch, 0F);


// Move the seat accordingly


// Consider new Yaw and Yaw limiters


float targetX = playerLooking.getYaw();

float yawToMove = (targetX - looking.getYaw());
while(yawToMove > 180F)
{
yawToMove -= 360F;
}
while(yawToMove <= -180F)
{
yawToMove += 360F;
}

float signDeltaX = 0;
if(yawToMove > (seatInfo.aimingSpeed.x / 2) && !seatInfo.legacyAiming)
{
signDeltaX = 1;
}
else if(yawToMove < -(seatInfo.aimingSpeed.x / 2) && !seatInfo.legacyAiming)
{
signDeltaX = -1;
}
else
{
signDeltaX = 0;
}

// Calculate new yaw and consider yaw limiters
float newYaw = 0f;

if(seatInfo.legacyAiming || (signDeltaX == 0 && deltaX == 0))
{
newYaw = playerLooking.getYaw();
}
else
{
newYaw = looking.getYaw() + signDeltaX * seatInfo.aimingSpeed.x;
}
// Since the yaw limiters go from -360 to 360, we need to find a pair of yaw values and check them both
float otherNewYaw = newYaw - 360F;
if(newYaw < 0)
otherNewYaw = newYaw + 360F;
if((!(newYaw >= seatInfo.minYaw) || !(newYaw <= seatInfo.maxYaw)) &&
(!(otherNewYaw >= seatInfo.minYaw) || !(otherNewYaw <= seatInfo.maxYaw)))
{
float newYawDistFromRange =
Math.min(Math.abs(newYaw - seatInfo.minYaw), Math.abs(newYaw - seatInfo.maxYaw));
float otherNewYawDistFromRange =
Math.min(Math.abs(otherNewYaw - seatInfo.minYaw), Math.abs(otherNewYaw - seatInfo.maxYaw));
// If the newYaw is closer to the range than the otherNewYaw, move newYaw into the range
if(newYawDistFromRange <= otherNewYawDistFromRange)
{
if(newYaw > seatInfo.maxYaw)
newYaw = seatInfo.maxYaw;
if(newYaw < seatInfo.minYaw)
newYaw = seatInfo.minYaw;
}
// Else, the otherNewYaw is closer, so move it in
else
{
if(otherNewYaw > seatInfo.maxYaw)
otherNewYaw = seatInfo.maxYaw;
if(otherNewYaw < seatInfo.minYaw)
otherNewYaw = seatInfo.minYaw;
// Then match up the newYaw with the otherNewYaw
if(newYaw < 0)
newYaw = otherNewYaw - 360F;
else newYaw = otherNewYaw + 360F;
}
}

// Calculate the new pitch and consider pitch limiters
float targetY = playerLooking.getPitch();

float pitchToMove = (targetY - looking.getPitch());
while(pitchToMove > 180F)
{
pitchToMove -= 360F;
}
while(pitchToMove <= -180F)
{
pitchToMove += 360F;
}

float signDeltaY = 0;
if(pitchToMove > (seatInfo.aimingSpeed.y / 2) && !seatInfo.legacyAiming)
{
signDeltaY = 1;
}
else if(pitchToMove < -(seatInfo.aimingSpeed.y / 2) && !seatInfo.legacyAiming)
{
signDeltaY = -1;
}
else
{
signDeltaY = 0;
}

float newPitch = 0f;


// Pitches the gun at the last possible moment in order to reach target pitch at the same time as target yaw.
float minYawToMove = 0f;

float currentYawToMove = 0f;

if(seatInfo.latePitch)
{
minYawToMove = ((float)Math
.sqrt((pitchToMove / seatInfo.aimingSpeed.y) * (pitchToMove / seatInfo.aimingSpeed.y))) *
seatInfo.aimingSpeed.x;
}
else
{
minYawToMove = 360f;
}

currentYawToMove = (float)Math.sqrt((yawToMove) * (yawToMove));

if(seatInfo.legacyAiming || (signDeltaY == 0 && deltaY == 0))
{
newPitch = playerLooking.getPitch();
}
else if(!seatInfo.yawBeforePitch && currentYawToMove < minYawToMove)
{
newPitch = looking.getPitch() + signDeltaY * seatInfo.aimingSpeed.y;
}
else if(seatInfo.yawBeforePitch && signDeltaX == 0)
{
newPitch = looking.getPitch() + signDeltaY * seatInfo.aimingSpeed.y;
}
else if(seatInfo.yawBeforePitch)
{
newPitch = looking.getPitch();
}
else
{
newPitch = looking.getPitch();
}

if(newPitch > -seatInfo.minPitch)
newPitch = -seatInfo.minPitch;
if(newPitch < -seatInfo.maxPitch)
newPitch = -seatInfo.maxPitch;

// Now set the new angles
looking.setAngles(newYaw, newPitch, 0F);

FlansMod.getPacketHandler().sendToServer(new PacketSeatUpdates(this));

playYawSound = signDeltaX != 0 && seatInfo.traverseSounds;

if(signDeltaY != 0 && !seatInfo.yawBeforePitch && currentYawToMove < minYawToMove)
{
playPitchSound = true;
}
else playPitchSound = signDeltaY != 0 && seatInfo.yawBeforePitch && signDeltaX == 0;
}
}

Expand Down

0 comments on commit 8cbf663

Please sign in to comment.