Skip to content

Commit

Permalink
remove files
Browse files Browse the repository at this point in the history
  • Loading branch information
notgiven688 committed Oct 27, 2024
1 parent c8760b8 commit aa87db0
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 67 deletions.
8 changes: 0 additions & 8 deletions docs/docs/00_intro.md

This file was deleted.

Binary file removed docs/docs/02_documentation/img/contact.png
Binary file not shown.
Binary file removed docs/docs/02_documentation/img/filter.png
Binary file not shown.
Binary file removed docs/impress/collision.odp
Binary file not shown.
Binary file removed docs/impress/contact.odp
Binary file not shown.
Binary file removed docs/impress/filter.odp
Binary file not shown.
34 changes: 17 additions & 17 deletions src/Jitter2/Collision/DynamicTree.RayCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public partial class DynamicTree
public struct RayCastResult
{
public IDynamicTreeProxy Entity;
public float Fraction;
public float Lambda;
public JVector Normal;
public bool Hit;
}
Expand Down Expand Up @@ -82,40 +82,40 @@ public Ray(in JVector origin, in JVector direction)
/// <param name="direction">Direction of the ray. Does not have to be normalized.</param>
/// <param name="pre">Optional pre-filter which allows to skip shapes in the detection.</param>
/// <param name="post">Optional post-filter which allows to skip detections.</param>
/// <param name="shape">The shape which was hit.</param>
/// <param name="proxy">The shape which was hit.</param>
/// <param name="normal">The normal of the surface where the ray hits. Zero if ray does not hit.</param>
/// <param name="fraction">Distance from the origin to the ray hit point in units of the ray's directin.</param>
/// <param name="lambda">Distance from the origin to the ray hit point in units of the ray's direction.</param>
/// <returns>True if the ray hits, false otherwise.</returns>
public bool RayCast(JVector origin, JVector direction, RayCastFilterPre? pre, RayCastFilterPost? post,
out IDynamicTreeProxy? shape, out JVector normal, out float fraction)
out IDynamicTreeProxy? proxy, out JVector normal, out float lambda)
{
Ray ray = new(origin, direction)
{
FilterPre = pre,
FilterPost = post
};
var result = QueryRay(ray);
shape = result.Entity;
proxy = result.Entity;
normal = result.Normal;
fraction = result.Fraction;
lambda = result.Lambda;
return result.Hit;
}

/// <inheritdoc cref="RayCast(JVector, JVector, RayCastFilterPre?, RayCastFilterPost?, out IDynamicTreeProxy?, out JVector, out float)"/>
/// <param name="maxFraction">Maximum fraction of the ray's length to consider for intersections.</param>
public bool RayCast(JVector origin, JVector direction, float maxFraction, RayCastFilterPre? pre, RayCastFilterPost? post,
out IDynamicTreeProxy? shape, out JVector normal, out float fraction)
/// <param name="maxLambda">Maximum lambda of the ray's length to consider for intersections.</param>
public bool RayCast(JVector origin, JVector direction, float maxLambda, RayCastFilterPre? pre, RayCastFilterPost? post,
out IDynamicTreeProxy? proxy, out JVector normal, out float lambda)
{
Ray ray = new(origin, direction)
{
FilterPre = pre,
FilterPost = post,
Lambda = maxFraction
Lambda = maxLambda
};
var result = QueryRay(ray);
shape = result.Entity;
proxy = result.Entity;
normal = result.Normal;
fraction = result.Fraction;
lambda = result.Lambda;
return result.Hit;
}

Expand All @@ -128,7 +128,7 @@ private RayCastResult QueryRay(in Ray ray)
stack.Push(root);

RayCastResult result = new();
result.Fraction = ray.Lambda;
result.Lambda = ray.Lambda;

while (stack.Count > 0)
{
Expand All @@ -143,10 +143,10 @@ private RayCastResult QueryRay(in Ray ray)
if (ray.FilterPre != null && !ray.FilterPre(node.Proxy)) continue;

Unsafe.SkipInit(out RayCastResult res);
res.Hit = irc.RayCast(ray.Origin, ray.Direction, out res.Normal, out res.Fraction);
res.Hit = irc.RayCast(ray.Origin, ray.Direction, out res.Normal, out res.Lambda);
res.Entity = node.Proxy;

if (res.Hit && res.Fraction < result.Fraction)
if (res.Hit && res.Lambda < result.Lambda)
{
if (ray.FilterPost != null && !ray.FilterPost(res)) continue;
result = res;
Expand All @@ -161,8 +161,8 @@ private RayCastResult QueryRay(in Ray ray)
bool lres = lnode.ExpandedBox.RayIntersect(ray.Origin, ray.Direction, out float enterl);
bool rres = rnode.ExpandedBox.RayIntersect(ray.Origin, ray.Direction, out float enterr);

if (enterl > result.Fraction) lres = false;
if (enterr > result.Fraction) rres = false;
if (enterl > result.Lambda) lres = false;
if (enterr > result.Lambda) rres = false;

if (lres && rres)
{
Expand Down
49 changes: 23 additions & 26 deletions src/Jitter2/Collision/NarrowPhase/NarrowPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ public bool PointTest(in ISupportMappable supportA, in JVector origin)
return true;
}

public bool RayCast(in ISupportMappable supportA, in JVector origin, in JVector direction, out float fraction, out JVector normal)
public bool RayCast(in ISupportMappable supportA, in JVector origin, in JVector direction, out float lambda, out JVector normal)
{
const float CollideEpsilon = 1e-4f;
const int MaxIter = 34;

normal = JVector.Zero;
fraction = float.PositiveInfinity;

float lambda = 0.0f;
lambda = 0.0f;

JVector r = direction;
JVector x = origin;
Expand Down Expand Up @@ -120,6 +118,7 @@ public bool RayCast(in ISupportMappable supportA, in JVector origin, in JVector

if (VdotR >= -NumericEpsilon)
{
lambda = float.PositiveInfinity;
return false;
}

Expand All @@ -141,8 +140,6 @@ public bool RayCast(in ISupportMappable supportA, in JVector origin, in JVector

converged:

fraction = lambda;

float nlen2 = normal.LengthSquared();

if (nlen2 > NumericEpsilon)
Expand All @@ -154,7 +151,7 @@ public bool RayCast(in ISupportMappable supportA, in JVector origin, in JVector
}

public bool Sweep(ref MinkowskiDifference mkd, in JVector sweep,
out JVector p1, out JVector p2, out JVector normal, out float fraction)
out JVector p1, out JVector p2, out JVector normal, out float lambda)
{
const float CollideEpsilon = 1e-4f;
const int MaxIter = 34;
Expand All @@ -166,7 +163,7 @@ public bool Sweep(ref MinkowskiDifference mkd, in JVector sweep,

JVector posB = mkd.PositionB;

fraction = 0.0f;
lambda = 0.0f;

p1 = p2 = JVector.Zero;

Expand All @@ -192,13 +189,13 @@ public bool Sweep(ref MinkowskiDifference mkd, in JVector sweep,

if (VdotR >= -1e-12f)
{
fraction = float.PositiveInfinity;
lambda = float.PositiveInfinity;
return false;
}

fraction -= VdotW / VdotR;
lambda -= VdotW / VdotR;

mkd.PositionB = posB + fraction * r;
mkd.PositionB = posB + lambda * r;
normal = v;
}

Expand Down Expand Up @@ -709,21 +706,21 @@ public static bool PointTest(in ISupportMappable support, in JMatrix orientation
/// <param name="position">The position of the shape in world space.</param>
/// <param name="origin">The origin of the ray.</param>
/// <param name="direction">The direction of the ray; normalization is not necessary.</param>
/// <param name="fraction">Specifies the hit point of the ray, calculated as 'origin + fraction * direction'.</param>
/// <param name="lambda">Specifies the hit point of the ray, calculated as 'origin + lambda * direction'.</param>
/// <param name="normal">
/// The normalized normal vector perpendicular to the surface, pointing outwards. If the ray does not
/// hit, this parameter will be zero.
/// </param>
/// <returns>Returns true if the ray intersects with the shape; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool RayCast(in ISupportMappable support, in JQuaternion orientation,
in JVector position, in JVector origin, in JVector direction, out float fraction, out JVector normal)
in JVector position, in JVector origin, in JVector direction, out float lambda, out JVector normal)
{
// rotate the ray into the reference frame of bodyA..
JVector tdirection = JVector.TransposedTransform(direction, orientation);
JVector torigin = JVector.TransposedTransform(origin - position, orientation);

bool result = solver.RayCast(support, torigin, tdirection, out fraction, out normal);
bool result = solver.RayCast(support, torigin, tdirection, out lambda, out normal);

// ..rotate back.
JVector.Transform(normal, orientation, out normal);
Expand All @@ -737,16 +734,16 @@ public static bool RayCast(in ISupportMappable support, in JQuaternion orientati
/// <param name="support">The support function of the shape.</param>
/// <param name="origin">The origin of the ray.</param>
/// <param name="direction">The direction of the ray; normalization is not necessary.</param>
/// <param name="fraction">Specifies the hit point of the ray, calculated as 'origin + fraction * direction'.</param>
/// <param name="lambda">Specifies the hit point of the ray, calculated as 'origin + lambda * direction'.</param>
/// <param name="normal">
/// The normalized normal vector perpendicular to the surface, pointing outwards. If the ray does not
/// hit, this parameter will be zero.
/// </param>
/// <returns>Returns true if the ray intersects with the shape; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool RayCast(in ISupportMappable support, in JVector origin, in JVector direction, out float fraction, out JVector normal)
public static bool RayCast(in ISupportMappable support, in JVector origin, in JVector direction, out float lambda, out JVector normal)
{
return solver.RayCast(support, origin, direction, out fraction, out normal);
return solver.RayCast(support, origin, direction, out lambda, out normal);
}

/// <summary>
Expand Down Expand Up @@ -1068,14 +1065,14 @@ public static bool MPREPA(in ISupportMappable supportA, in ISupportMappable supp
/// Zero if no hit is detected.</param>
/// <param name="pointB">Collision point on shapeB in world space at t = 0, where collision will occur.
/// Zero if no hit is detected.</param>
/// <param name="fraction">Time of impact. Infinity if no hit is detected.</param>
/// <param name="lambda">Time of impact. Infinity if no hit is detected.</param>
/// <returns>True if the shapes hit, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Sweep(in ISupportMappable supportA, in ISupportMappable supportB,
in JQuaternion orientationA, in JQuaternion orientationB,
in JVector positionA, in JVector positionB,
in JVector sweepA, in JVector sweepB,
out JVector pointA, out JVector pointB, out JVector normal, out float fraction)
out JVector pointA, out JVector pointB, out JVector normal, out float lambda)
{
Unsafe.SkipInit(out MinkowskiDifference mkd);

Expand All @@ -1092,7 +1089,7 @@ public static bool Sweep(in ISupportMappable supportA, in ISupportMappable suppo
JVector.ConjugatedTransform(sweep, orientationA, out sweep);

// ..perform toi calculation
bool res = solver.Sweep(ref mkd, sweep, out pointA, out pointB, out normal, out fraction);
bool res = solver.Sweep(ref mkd, sweep, out pointA, out pointB, out normal, out lambda);

if (!res) return false;

Expand All @@ -1107,10 +1104,10 @@ public static bool Sweep(in ISupportMappable supportA, in ISupportMappable suppo
// transform back from the relative velocities

// This is where the collision will occur in world space:
// pointA += fraction * sweepA;
// pointB += fraction * sweepA; // sweepA is not a typo
// pointA += lambda * sweepA;
// pointB += lambda * sweepA; // sweepA is not a typo

pointB += fraction * (sweepA - sweepB);
pointB += lambda * (sweepA - sweepB);

return true;
}
Expand All @@ -1121,12 +1118,12 @@ public static bool Sweep(in ISupportMappable supportA, in ISupportMappable suppo
/// </summary>
/// <param name="pointA">Collision point on shapeA in world space. Zero if no hit is detected.</param>
/// <param name="pointB">Collision point on shapeB in world space. Zero if no hit is detected.</param>
/// <param name="fraction">Time of impact. Infinity if no hit is detected.</param>
/// <param name="lambda">Time of impact. Infinity if no hit is detected.</param>
/// <returns>True if the shapes hit, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Sweep(in ISupportMappable supportA, in ISupportMappable supportB,
in JQuaternion orientationB, in JVector positionB, in JVector sweepB,
out JVector pointA, out JVector pointB, out JVector normal, out float fraction)
out JVector pointA, out JVector pointB, out JVector normal, out float lambda)
{
Unsafe.SkipInit(out MinkowskiDifference mkd);

Expand All @@ -1136,6 +1133,6 @@ public static bool Sweep(in ISupportMappable supportA, in ISupportMappable suppo
mkd.OrientationB = orientationB;

// ..perform toi calculation
return solver.Sweep(ref mkd, sweepB, out pointA, out pointB, out normal, out fraction);
return solver.Sweep(ref mkd, sweepB, out pointA, out pointB, out normal, out lambda);
}
}
58 changes: 56 additions & 2 deletions src/Jitter2/Dynamics/RigidBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public sealed class RigidBody : IListIndex, IDebugDrawable

public readonly ulong RigidBodyId;

private float restitution = 0.0f;
private float friction = 0.2f;

/// <summary>
/// Due to performance considerations, the data used to simulate this body (e.g., velocity or position)
/// is stored within a contiguous block of unmanaged memory. This refers to the raw memory location
Expand Down Expand Up @@ -156,8 +159,59 @@ internal void RaiseEndCollide(Arbiter arbiter)
internal JMatrix inverseInertia = JMatrix.Identity;
internal float inverseMass = 1.0f;

public float Friction { get; set; } = 0.2f;
public float Restitution { get; set; } = 0.0f;
/// <summary>
/// Gets or sets the friction coefficient for this object.
/// </summary>
/// <remarks>
/// The friction coefficient determines the resistance to sliding motion.
/// Higher values create more friction, while lower values allow easier sliding.
/// A typical value ranges between 0 (no friction) and 1 (maximum friction).
/// Default is 0.2.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the value is not between 0 and 1.
/// </exception>
public float Friction
{
get => friction;
set
{
if (value < 0.0f || value > 1.0f)
{
throw new ArgumentOutOfRangeException(nameof(value),
"Friction must be between 0 and 1.");
}

friction = value;
}
}

/// <summary>
/// Gets or sets the restitution (bounciness) of this object.
/// </summary>
/// <remarks>
/// The restitution value determines how much energy is retained after a collision,
/// with 0 representing an inelastic collision (no bounce) and 1 representing a perfectly elastic collision (full bounce).
/// Values between 0 and 1 create a partially elastic collision effect.
/// Default is 0.0.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if the value is not between 0 and 1.
/// </exception>
public float Restitution
{
get => restitution;
set
{
if (value < 0.0f || value > 1.0f)
{
throw new ArgumentOutOfRangeException(nameof(value),
"Restitution must be between 0 and 1.");
}

restitution = value;
}
}

private readonly int hashCode;

Expand Down
2 changes: 1 addition & 1 deletion src/Jitter2/SoftBodies/DynamicTreeCollisionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static bool Filter(IDynamicTreeProxy proxyA, IDynamicTreeProxy proxyB)
{
if (proxyA is RigidBodyShape rbsA && proxyB is RigidBodyShape rbsB)
{
if (rbsA.RigidBody == rbsB.RigidBody) return false;
return rbsA.RigidBody != rbsB.RigidBody;
}
else if (proxyA is SoftBodyShape softBodyShapeA &&
proxyB is SoftBodyShape softBodyShapeB)
Expand Down
4 changes: 2 additions & 2 deletions src/Jitter2/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ public void Clear()
}

/// <summary>
/// Removes the specified body from the world. This operation also automatically discards any associated contacts
/// and constraints.
/// Removes the specified body from the world.
/// This operation also automatically discards any associated contacts and constraints.
/// </summary>
public void Remove(RigidBody body)
{
Expand Down
6 changes: 3 additions & 3 deletions src/JitterDemo/Demos/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ private bool CanJump(out RigidBody? floor, out JVector hitPoint)
// ...or the more traditional way of using a raycast
bool hit = world.RayCast(Body.Position, -JVector.UnitY, preFilter, null,
out floor, out JVector normal, out float fraction);
out floor, out JVector normal, out float lambda);
float delta = fraction - capsuleHalfHeight;
float delta = lambda - capsuleHalfHeight;
hitPoint = Body.Position - JVector.UnitY * fraction;
hitPoint = Body.Position - JVector.UnitY * lambda;
return (hit && delta < 0.04f && floor != null);
*/
}
Expand Down
Loading

0 comments on commit aa87db0

Please sign in to comment.