85 lines
3.7 KiB
C#
85 lines
3.7 KiB
C#
using System.Numerics;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>Actor-owned accessors used by the base370/374/378 callbacks.</summary>
|
|
public interface IOriginalNormalBody
|
|
{
|
|
Vector4 ReadVelocity();
|
|
void WriteVelocity(Vector4 value);
|
|
void WriteVelocityXYZ(Vector3 value);
|
|
Vector4 ReadAcceleration();
|
|
void WriteAcceleration(Vector4 value);
|
|
bool ReflectWalls { get; }
|
|
float WallDamping { get; }
|
|
void AddAngularVelocity(Vector3 delta);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bounded472690/458890/472750 callbacks, with live accessor order and original
|
|
/// heading impulse. Not a Jimmy override or live scene-admission policy.
|
|
/// </summary>
|
|
public static class OriginalNormalContacts
|
|
{
|
|
public static void Ceiling(IOriginalNormalBody body)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(body);
|
|
var velocity = body.ReadVelocity();
|
|
if (velocity.Y > 0) body.WriteVelocity(velocity with {Y = 0});
|
|
var acceleration = body.ReadAcceleration();
|
|
if (acceleration.Y > 0) body.WriteAcceleration(acceleration with {Y = 0});
|
|
}
|
|
|
|
public static void GenericWall(IOriginalNormalBody body, Vector3 normal,
|
|
Func<Vector4,int>? headingUnits = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(body);
|
|
if (!body.ReflectWalls) return;
|
|
Reflect(body,normal,headingUnits);
|
|
}
|
|
|
|
public static void Reflect(IOriginalNormalBody body, Vector3 normal,
|
|
Func<Vector4,int>? headingUnits = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(body);
|
|
Finite(normal);
|
|
var velocity = body.ReadVelocity();
|
|
var horizontal = new Vector3(velocity.X,0,velocity.Z);
|
|
var magnitude = OriginalBodySupport.QuickMagnitude(horizontal);
|
|
if (magnitude <= 0) throw new NotSupportedException("Original zero-horizontal normalization is outside the finite contact contract");
|
|
var direction = Normalize(horizontal,velocity.W);
|
|
var dot = -((double)direction.Z*normal.Z + (double)direction.X*normal.X + (double)direction.Y*normal.Y);
|
|
var reflected = new Vector3((float)(direction.X + 2*dot*normal.X),
|
|
(float)(direction.Y + 2*dot*normal.Y),(float)(direction.Z + 2*dot*normal.Z));
|
|
var x = reflected.X*magnitude;
|
|
//472834 samples live Y again, after computing the horizontal reflection.
|
|
var y = body.ReadVelocity().Y;
|
|
var z = reflected.Z*magnitude;
|
|
var damping = body.WallDamping;
|
|
if (!float.IsFinite(damping)) throw new ArgumentOutOfRangeException(nameof(body));
|
|
body.WriteVelocityXYZ(new((float)(x-(double)damping*x),y,(float)(z-(double)damping*z)));
|
|
|
|
headingUnits ??= static vector => OriginalHeading.Angles(new(vector.X,vector.Y,vector.Z)).Yaw & 16383;
|
|
var oldHeading = headingUnits(direction)*0.02197265625f;
|
|
var outgoing = Normalize(new(x,0,z),0);
|
|
var newHeading = headingUnits(outgoing)*0.02197265625f;
|
|
double delta = oldHeading-newHeading;
|
|
if (delta > 180) delta -= 360;
|
|
else if (delta < -180) delta += 360;
|
|
body.AddAngularVelocity(new(0,(float)(delta*magnitude*0.001f),0));
|
|
}
|
|
|
|
private static Vector4 Normalize(Vector3 vector,float w)
|
|
{
|
|
var length = OriginalBodySupport.QuickMagnitudeIntermediate(vector);
|
|
if (length <= 0) throw new NotSupportedException("Original zero reflected normalization is outside the finite contact contract");
|
|
var reciprocal = 1/length;
|
|
return new((float)(vector.X*reciprocal),(float)(vector.Y*reciprocal),(float)(vector.Z*reciprocal),w);
|
|
}
|
|
private static void Finite(Vector3 vector)
|
|
{
|
|
if (!float.IsFinite(vector.X) || !float.IsFinite(vector.Y) || !float.IsFinite(vector.Z))
|
|
throw new ArgumentOutOfRangeException(nameof(vector));
|
|
}
|
|
}
|