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

Add some micro-optimizations to IntersectsSphere #4213

Closed
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
42 changes: 22 additions & 20 deletions Source/ACE.Server/Physics/Sphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,62 +287,64 @@ public TransitionState IntersectsSphere(Transition transition, bool isCreature)
/// <returns>The collision result for this transition path</returns>
public static TransitionState IntersectsSphere(Vector3 center, float radius, Transition transition, bool isCreature)
{
var globSphere = transition.SpherePath.GlobalSphere[0];
var spherePath = transition.SpherePath;
var globSphere = spherePath.GlobalSphere[0];
var disp = globSphere.Center - center;

Sphere globSphere_ = null;
Vector3 disp_ = Vector3.Zero;
bool twoSpheres = spherePath.NumSphere > 1;

if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
globSphere_ = transition.SpherePath.GlobalSphere[1];
globSphere_ = spherePath.GlobalSphere[1];
disp_ = globSphere_.Center - center;
}

var radsum = globSphere.Radius + radius - PhysicsGlobals.EPSILON;

if (transition.SpherePath.ObstructionEthereal || transition.SpherePath.InsertType == InsertType.Placement)
if (spherePath.ObstructionEthereal || spherePath.InsertType == InsertType.Placement)
{
if (disp.LengthSquared() <= radsum * radsum)
return TransitionState.Collided;

if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
if (CollidesWithSphere(disp_, radsum))
return TransitionState.Collided;
}
return TransitionState.OK;
}

if (transition.SpherePath.StepDown)
if (spherePath.StepDown)
{
if (isCreature)
return TransitionState.OK;

return StepSphereDown(center, radius, transition, globSphere, ref disp, radsum);
}

if (transition.SpherePath.CheckWalkable)
if (spherePath.CheckWalkable)
{
if (CollidesWithSphere(disp, radsum))
return TransitionState.Collided;

if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
if (CollidesWithSphere(disp_, radsum))
return TransitionState.Collided;
}
return TransitionState.OK;
}

if (!transition.SpherePath.Collide)
if (!spherePath.Collide)
{
if (transition.ObjectInfo.State.HasFlag(ObjectInfoState.Contact) || transition.ObjectInfo.State.HasFlag(ObjectInfoState.OnWalkable))
{
if (CollidesWithSphere(disp, radsum))
return StepSphereUp(center, transition, disp, radsum);

if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
if (CollidesWithSphere(disp_, radsum))
return SlideSphere(center, transition, globSphere_, 1);
Expand All @@ -359,7 +361,7 @@ public static TransitionState IntersectsSphere(Vector3 center, float radius, Tra
if (CollidesWithSphere(disp, radsum))
return LandOnSphere(center, transition);

if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
if (CollidesWithSphere(disp_, radsum))
return CollideWithPoint(center, transition, globSphere_, radsum, 1);
Expand All @@ -373,16 +375,16 @@ public static TransitionState IntersectsSphere(Vector3 center, float radius, Tra

if (!CollidesWithSphere(disp, radsum))
{
if (transition.SpherePath.NumSphere > 1)
if (twoSpheres)
{
if (!CollidesWithSphere(disp_, radsum))
return TransitionState.OK;
}
}

// handles movement interpolation
var blockOffset = transition.SpherePath.GetCurPosCheckPosBlockOffset();
var movement = transition.SpherePath.GlobalCurrCenter[0].Center - globSphere.Center - blockOffset;
var blockOffset = spherePath.GetCurPosCheckPosBlockOffset();
var movement = spherePath.GlobalCurrCenter[0].Center - globSphere.Center - blockOffset;
radsum += PhysicsGlobals.EPSILON;
var lenSq = movement.LengthSquared();
var diff = -Vector3.Dot(movement, disp);
Expand All @@ -393,23 +395,23 @@ public static TransitionState IntersectsSphere(Vector3 center, float radius, Tra
if (t > 1)
t = diff * 2 - t;
var time = (float)t / lenSq;
var timecheck = (1 - time) * transition.SpherePath.WalkInterp;
if (timecheck >= transition.SpherePath.WalkInterp || timecheck < -0.1f)
var timecheck = (1 - time) * spherePath.WalkInterp;
if (timecheck >= spherePath.WalkInterp || timecheck < -0.1f)
return TransitionState.Collided;

movement *= time;
disp = (disp + movement) / radsum;

if (!transition.SpherePath.IsWalkableAllowable(disp.Z))
if (!spherePath.IsWalkableAllowable(disp.Z))
return TransitionState.OK;

var disp2 = globSphere.Center - disp * globSphere.Radius;

var contactPlane = new Plane(disp, -Vector3.Dot(disp, disp2));
transition.CollisionInfo.SetContactPlane(contactPlane, true);
transition.CollisionInfo.ContactPlaneCellID = transition.SpherePath.CheckPos.ObjCellID;
transition.SpherePath.WalkInterp = timecheck;
transition.SpherePath.AddOffsetToCheckPos(movement, globSphere.Radius);
transition.CollisionInfo.ContactPlaneCellID = spherePath.CheckPos.ObjCellID;
spherePath.WalkInterp = timecheck;
spherePath.AddOffsetToCheckPos(movement, globSphere.Radius);

return TransitionState.Adjusted;
}
Expand Down