no step improvements

This commit is contained in:
GoaLitiuM
2021-06-28 17:27:04 +03:00
parent 6c5965e959
commit edbb0f9742
2 changed files with 28 additions and 17 deletions

View File

@@ -5,7 +5,7 @@
"Data": {
"UpdateFPS": 60.0,
"PhysicsFPS": 60.0,
"DrawFPS": 0.0,
"DrawFPS": 60.0,
"TimeScale": 1.0,
"MaxUpdateDeltaTime": 0.1
}

View File

@@ -38,6 +38,7 @@ namespace Game
private InputEvent onExit = new InputEvent("Exit");
private const float collisionMargin = 0.031f * 1.666f;
private const float slopeNormal = 0.7f;
Actor rootActor;
private RigidBody rigidBody;
@@ -237,31 +238,39 @@ namespace Game
SlideMoveHit slideMoveHit = SlideMove(ref position, ref velocity);
if (slideMoveHit == SlideMoveHit.Nothing)
{
// TODO: step down here
return slideMoveHit;
}
// hit something, try to step up
if (onGround)
{
Vector3 stepDelta = -Physics.Gravity.Normalized * stepSize;
Vector3 slidePosition = position;
Vector3 slideVelocity = velocity;
position = originalPosition;
velocity = originalVelocity;
Vector3 stepUp = position + (-Physics.Gravity.Normalized * stepSize);
// step up
Vector3 stepUp = position + stepDelta;
TraceInfo traceUp = TracePlayer(position, stepUp);
if (traceUp.fraction > 0f)
position = traceUp.endPosition;
// try moving from step up position
SlideMoveHit slideMoveStepHit = SlideMove(ref position, ref velocity);
Vector3 stepDown = position + (Physics.Gravity.Normalized * stepSize);
// step down
Vector3 stepDown = position - stepDelta;
TraceInfo traceDown = TracePlayer(position, stepDown);
if (traceDown.fraction < 1f && -Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal) < 0.7)
if (traceDown.fraction < 1f && -Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal) < slopeNormal)
{
// can't step down, slide move like normally
Console.Print("no stepping 1, frac: " + traceDown.fraction.ToString() + ", dot: " +
(-Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal)).ToString() +
", norm: " + traceDown.hitNormal.ToString());
Console.Print("no stepping 1, frac: " + traceDown.fraction + ", dot: " +
(-Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal)) +
", norm: " + traceDown.hitNormal);
position = slidePosition;
velocity = slideVelocity;
return slideMoveHit;
@@ -269,29 +278,31 @@ namespace Game
else if (traceDown.fraction > 0f)
position = traceDown.endPosition;
// add some margin from the ground in order to avoid getting stuck
if (traceDown.fraction < 1f)
position.Y += collisionMargin;
// ??
var d1 = -Vector3.Dot(Physics.Gravity.Normalized, position);
var d2 = -Vector3.Dot(Physics.Gravity.Normalized, originalPosition);
if (d1 < d2)
{
// ?
Console.Print("no stepping 2, " + d1.ToString() + " < " + d2.ToString());
Console.Print("no stepping 2, " + d1 + " < " + d2);
position = slidePosition;
velocity = slideVelocity;
return slideMoveHit;
}
Vector3 stepPosition = position;
Vector3 slidePosition2 = slidePosition;
Vector3 stepPosition2 = stepPosition;
Vector3 slidePosition2 = slidePosition; //down
Vector3 stepPosition2 = position; //up
// FIXME, negate gravity
slidePosition2.Y = 0f;
stepPosition2.Y = 0f;
// choose which one went furthest away from the original position
if ((stepPosition2 - originalPosition).Length < (slidePosition2 - originalPosition).Length)
// take the slide movement results if furthest away from original position
//if ((stepPosition2 - originalPosition).Length < (slidePosition2 - originalPosition).Length)
if ((slidePosition2 - originalPosition).Length >= (stepPosition2 - originalPosition).Length)
{
Console.Print("no stepping 3");
position = slidePosition;
@@ -355,7 +366,7 @@ namespace Game
timeleft *= 1.0f - fraction;
if (trace.hitNormal.Y > 0.7)
if (trace.hitNormal.Y > slopeNormal)
slideMoveHit |= SlideMoveHit.Floor;
else if (Math.Abs(trace.hitNormal.Y) < 0.0001f)
slideMoveHit |= SlideMoveHit.Step;
@@ -528,7 +539,7 @@ namespace Game
TraceInfo traceGround = TracePlayer(position, position + groundDelta);
if (!traceGround.startSolid && traceGround.fraction < 1f &&
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < 0.7f)
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < slopeNormal)
{
// slope
// clip velocity
@@ -546,7 +557,7 @@ namespace Game
}
if (!traceGround.startSolid && (traceGround.fraction >= 1f ||
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < 0.7f))
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < slopeNormal))
{
// falling or sliding down a slope
onGround = false;