mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2024-11-26 06:18:00 +01:00
Seprate the strafing code to its own class so it can be used outside of Minelp
This commit is contained in:
parent
502f86714f
commit
1b3e160c3e
3 changed files with 56 additions and 43 deletions
|
@ -0,0 +1,52 @@
|
||||||
|
package com.minelittlepony.transform;
|
||||||
|
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
|
||||||
|
import com.minelittlepony.util.math.MathUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates roll and incline for a player based on their motion vectors.
|
||||||
|
*
|
||||||
|
* @author Polyakovich - Big thank you to this dude and his meta-physics friend for working this all out.
|
||||||
|
*/
|
||||||
|
public abstract class MotionCompositor {
|
||||||
|
|
||||||
|
protected double calculateRoll(EntityPlayer player, double motionX, double motionY, double motionZ) {
|
||||||
|
|
||||||
|
// since model roll should probably be calculated from model rotation rather than entity rotation...
|
||||||
|
double roll = MathUtil.sensibleAngle(player.prevRenderYawOffset - player.renderYawOffset);
|
||||||
|
double horMotion = Math.sqrt(motionX * motionX + motionZ * motionZ);
|
||||||
|
float modelYaw = MathUtil.sensibleAngle(player.renderYawOffset);
|
||||||
|
|
||||||
|
// detecting that we're flying backwards and roll must be inverted
|
||||||
|
if (Math.abs(MathUtil.sensibleAngle((float) Math.toDegrees(Math.atan2(motionX, motionZ)) + modelYaw)) > 90) {
|
||||||
|
roll *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ayyy magic numbers (after 5 - an approximation of nice looking coefficients calculated by hand)
|
||||||
|
|
||||||
|
// roll might be zero, in which case Math.pow produces +Infinity. Anything x Infinity = NaN.
|
||||||
|
double pow = roll != 0 ? Math.pow(Math.abs(roll), -0.191) : 0;
|
||||||
|
|
||||||
|
roll *= horMotion * 5 * (3.6884f * pow);
|
||||||
|
|
||||||
|
assert !Float.isNaN((float)roll);
|
||||||
|
|
||||||
|
return MathHelper.clamp(roll, -54, 54);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected double calculateIncline(EntityPlayer player, double motionX, double motionY, double motionZ) {
|
||||||
|
double dist = Math.sqrt(motionX * motionX + motionZ * motionZ);
|
||||||
|
double angle = Math.atan2(motionY, dist);
|
||||||
|
|
||||||
|
if (!player.capabilities.isFlying) {
|
||||||
|
angle /= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
angle = MathUtil.clampLimit(angle, Math.PI / 3);
|
||||||
|
|
||||||
|
return Math.toDegrees(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,56 +1,17 @@
|
||||||
package com.minelittlepony.transform;
|
package com.minelittlepony.transform;
|
||||||
|
|
||||||
import com.minelittlepony.model.AbstractPonyModel;
|
import com.minelittlepony.model.AbstractPonyModel;
|
||||||
import com.minelittlepony.util.math.MathUtil;
|
|
||||||
|
|
||||||
import net.minecraft.client.entity.AbstractClientPlayer;
|
import net.minecraft.client.entity.AbstractClientPlayer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
import net.minecraft.entity.EntityLivingBase;
|
||||||
import net.minecraft.util.math.MathHelper;
|
|
||||||
|
|
||||||
public class PostureFlight implements PonyPosture<AbstractClientPlayer> {
|
public class PostureFlight extends MotionCompositor implements PonyPosture<AbstractClientPlayer> {
|
||||||
@Override
|
@Override
|
||||||
public boolean applies(EntityLivingBase entity) {
|
public boolean applies(EntityLivingBase entity) {
|
||||||
return entity instanceof AbstractClientPlayer;
|
return entity instanceof AbstractClientPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected double calculateRoll(AbstractClientPlayer player, double motionX, double motionY, double motionZ) {
|
|
||||||
|
|
||||||
// since model roll should probably be calculated from model rotation rather than entity rotation...
|
|
||||||
double roll = MathUtil.sensibleAngle(player.prevRenderYawOffset - player.renderYawOffset);
|
|
||||||
double horMotion = Math.sqrt(motionX * motionX + motionZ * motionZ);
|
|
||||||
float modelYaw = MathUtil.sensibleAngle(player.renderYawOffset);
|
|
||||||
|
|
||||||
// detecting that we're flying backwards and roll must be inverted
|
|
||||||
if (Math.abs(MathUtil.sensibleAngle((float) Math.toDegrees(Math.atan2(motionX, motionZ)) + modelYaw)) > 90) {
|
|
||||||
roll *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ayyy magic numbers (after 5 - an approximation of nice looking coefficients calculated by hand)
|
|
||||||
|
|
||||||
// roll might be zero, in which case Math.pow produces +Infinity. Anything x Infinity = NaN.
|
|
||||||
double pow = roll != 0 ? Math.pow(Math.abs(roll), -0.191) : 0;
|
|
||||||
|
|
||||||
roll *= horMotion * 5 * (3.6884f * pow);
|
|
||||||
|
|
||||||
assert !Float.isNaN((float)roll);
|
|
||||||
|
|
||||||
return MathHelper.clamp(roll, -54, 54);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected double calculateIncline(AbstractClientPlayer player, double motionX, double motionY, double motionZ) {
|
|
||||||
double dist = Math.sqrt(motionX * motionX + motionZ * motionZ);
|
|
||||||
double angle = Math.atan2(motionY, dist);
|
|
||||||
|
|
||||||
if (!player.capabilities.isFlying) {
|
|
||||||
angle /= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
angle = MathUtil.clampLimit(angle, Math.PI / 3);
|
|
||||||
|
|
||||||
return Math.toDegrees(angle);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void transform(AbstractPonyModel model, AbstractClientPlayer player, double motionX, double motionY, double motionZ, float pitch, float yaw, float ticks) {
|
public void transform(AbstractPonyModel model, AbstractClientPlayer player, double motionX, double motionY, double motionZ, float pitch, float yaw, float ticks) {
|
||||||
model.motionPitch = (float) calculateIncline(player, motionX, motionY, motionZ);
|
model.motionPitch = (float) calculateIncline(player, motionX, motionY, motionZ);
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package com.minelittlepony.transform;
|
package com.minelittlepony.transform;
|
||||||
|
|
||||||
import net.minecraft.client.entity.AbstractClientPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
|
||||||
public class PostureSwimming extends PostureFlight {
|
public class PostureSwimming extends PostureFlight {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected double calculateRoll(AbstractClientPlayer player, double motionX, double motionY, double motionZ) {
|
protected double calculateRoll(EntityPlayer player, double motionX, double motionY, double motionZ) {
|
||||||
|
|
||||||
motionX *= 2;
|
motionX *= 2;
|
||||||
motionZ *= 2;
|
motionZ *= 2;
|
||||||
|
@ -14,7 +14,7 @@ public class PostureSwimming extends PostureFlight {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected double calculateIncline(AbstractClientPlayer player, double motionX, double motionY, double motionZ) {
|
protected double calculateIncline(EntityPlayer player, double motionX, double motionY, double motionZ) {
|
||||||
return super.calculateIncline(player, motionX, motionY, motionZ);
|
return super.calculateIncline(player, motionX, motionY, motionZ);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue