Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation specific limb constant angles #15374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,24 @@ public void LockFlipping(float time = 0.2f)

protected void UpdateConstantTorque(float deltaTime)
{
if (character.IsIncapacitated) { return; }
if (character.Stun > 0.01f) { return; }
foreach (var limb in Limbs)
{
if (limb.IsSevered) { continue; }
if (Math.Abs(limb.Params.ConstantTorque) > 0)
float angleToApply = limb.Params.ConstantAngle;
float torqueToApply = limb.Params.ConstantTorque;
float movementFactor = Math.Max(character.AnimController.Collider.LinearVelocity.Length() * 0.5f, 1);
if (CurrentAnimationParams.LimbConstantAnglesData.ContainsKey(limb.Params.ID))
{
// TODO: not sure if this works on ground
float movementFactor = Math.Max(character.AnimController.Collider.LinearVelocity.Length() * 0.5f, 1);
limb.body.SmoothRotate(MainLimb.Rotation + MathHelper.ToRadians(limb.Params.ConstantAngle) * Dir, limb.Mass * limb.Params.ConstantTorque * movementFactor, wrapAngle: true);
var constantAngleData = CurrentAnimationParams.LimbConstantAnglesData[limb.Params.ID];

angleToApply = constantAngleData.ConstantAngle;
torqueToApply = constantAngleData.ConstantTorque;
}
if (torqueToApply > 0f)
{
limb.body.SmoothRotate(MainLimb.Rotation + MathHelper.ToRadians(angleToApply) * Dir, limb.Mass * torqueToApply * movementFactor, wrapAngle: true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ public float TorsoAngle
[Serialize(50.0f, IsPropertySaveable.Yes, description: "How much torque is used to rotate the torso to the correct orientation."), Editable(MinValueFloat = 0, MaxValueFloat = 1000, ValueStep = 1)]
public float TorsoTorque { get; set; }

public struct LimbConstantAngleData
{
public float ConstantAngle { get; set; }
public float ConstantTorque { get; set; }

public LimbConstantAngleData(float constantAngle, float constantTorque)
{
ConstantAngle = constantAngle;
ConstantTorque = constantTorque;
}
}
/// <summary>
/// Key = limb id, value = LimbConstantAngleData struct with two properties: constant angle and constant torque
/// </summary>
public Dictionary<int, LimbConstantAngleData> LimbConstantAnglesData { get; set; } = new Dictionary<int, LimbConstantAngleData>();

[Serialize(null, IsPropertySaveable.Yes), Editable]
public string LimbConstantAngles
{
get => ParseLimbConstantAngles(LimbConstantAnglesData);
set => SetLimbConstantAngles(LimbConstantAnglesData, value);
}

[Header("Legs")]
[Serialize(25.0f, IsPropertySaveable.Yes, description: "How much torque is used to rotate the feet to the correct orientation."), Editable(MinValueFloat = 0, MaxValueFloat = 1000, ValueStep = 1)]
public float FootTorque { get; set; }
Expand Down Expand Up @@ -486,6 +509,12 @@ protected static string ParseFootAngles(Dictionary<int, float> footAngles)
return string.Join(",", footAngles.Select(kv => kv.Key + ": " + kv.Value.ToString("G", CultureInfo.InvariantCulture)).ToArray());
}

protected static string ParseLimbConstantAngles(Dictionary<int, LimbConstantAngleData> limbAngles)
{
//convert to the format "id1:angle;torque,id2:angle;torque"
return string.Join(",", limbAngles.Select(kv => $"{kv.Key}: {kv.Value.ConstantAngle.ToString("G", CultureInfo.InvariantCulture)};" + $"{kv.Value.ConstantTorque.ToString("G", CultureInfo.InvariantCulture)}"));
}

protected static void SetFootAngles(Dictionary<int, float> footAngles, string value)
{
footAngles.Clear();
Expand All @@ -509,6 +538,42 @@ protected static void SetFootAngles(Dictionary<int, float> footAngles, string va
}
}

protected static void SetLimbConstantAngles(Dictionary<int, LimbConstantAngleData> limbAngles, string value)
{
limbAngles.Clear();
if (string.IsNullOrEmpty(value))
{
return;
}

string[] keyValuePairs = value.Split(',');
foreach (string joinedKvp in keyValuePairs)
{
string[] keyValuePair = joinedKvp.Split(':');
if (keyValuePair.Length != 2 ||
!int.TryParse(keyValuePair[0].Trim(), out int limbIndex))
{
DebugConsole.ThrowError("Failed to parse limb constant angles (" + value + ")");
continue;
}

string[] dataValues = keyValuePair[1].Split(';');
if (dataValues.Length != 2 ||
!float.TryParse(dataValues[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out float constantAngle) ||
!float.TryParse(dataValues[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out float constantTorque))
{
DebugConsole.ThrowError($"Failed to parse angle or torque for limb {limbIndex} ({keyValuePair[1]})");
continue;
}

limbAngles[limbIndex] = new LimbConstantAngleData
{
ConstantAngle = constantAngle,
ConstantTorque = constantTorque
};
}
}

public static Type GetParamTypeFromAnimType(AnimationType type, bool isHumanoid)
{
if (isHumanoid)
Expand Down