diff --git a/Content/Scenes/MainScene.scene b/Content/Scenes/MainScene.scene index e4d5382..da11481 100644 --- a/Content/Scenes/MainScene.scene +++ b/Content/Scenes/MainScene.scene @@ -162,13 +162,13 @@ "Transform": { "Translation": { "X": 0.0, - "Y": 356.0, + "Y": 734.0, "Z": 0.0 } }, "Control": "FlaxEngine.GUI.Label", "Data": { - "Text": "uFPS: 64\nrFPS: 68\npFPS: 22\nCon: NaNms\nDirectX11\nGC memory: 18.61586MB", + "Text": "uFPS: 120\nrFPS: 120\npFPS: 30\nCon: NaNms\nDirectX11\nGC memory: 10.44299MB", "TextColor": { "R": 1.0, "G": 1.0, @@ -214,7 +214,7 @@ }, "Offsets": { "Left": 0.0, - "Right": 158.400009, + "Right": 143.0, "Top": -80.0, "Bottom": 96.0 }, @@ -254,8 +254,8 @@ "Name": "ContainerControl 0", "Transform": { "Translation": { - "X": 45122.0, - "Y": -188.0, + "X": 45676.0, + "Y": 1.0, "Z": 0.0 } }, diff --git a/Source/Game/PlayerMovement.cs b/Source/Game/PlayerMovement.cs index 35229e5..0a5ee48 100644 --- a/Source/Game/PlayerMovement.cs +++ b/Source/Game/PlayerMovement.cs @@ -1,9 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using FlaxEngine; -using Cabrito; using System.Diagnostics; using System.Threading.Tasks; using FlaxEngine.Assertions; +using Console = Cabrito.Console; namespace Game { @@ -221,14 +222,97 @@ namespace Game } } - private void SlideMove(ref Vector3 position, bool stepUp, ref Vector3 velocity) + private SlideMoveHit StepSlideMove(ref Vector3 position, ref Vector3 velocity, bool onGround) { if (velocity.IsZero) - return; + return SlideMoveHit.Nothing; Vector3 originalPosition = position; Vector3 originalVelocity = velocity; + SlideMoveHit slideMoveHit = SlideMove(ref position, ref velocity); + if (slideMoveHit == SlideMoveHit.Nothing) + return slideMoveHit; + + // hit something, try to step up + if (onGround) + { + Vector3 slidePosition = position; + Vector3 slideVelocity = velocity; + position = originalPosition; + velocity = originalVelocity; + + Vector3 stepUp = position + (-Physics.Gravity.Normalized * stepSize); + TraceInfo traceUp = TracePlayer(position, stepUp); + if (traceUp.fraction > 0f) + position = traceUp.endPosition; + + SlideMoveHit slideMoveStepHit = SlideMove(ref position, ref velocity); + + Vector3 stepDown = position + (Physics.Gravity.Normalized * stepSize); + TraceInfo traceDown = TracePlayer(position, stepDown); + if (traceDown.fraction < 1f && -Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal) < 0.7) + { + // can't step down, slide move like normally + position = slidePosition; + velocity = slideVelocity; + return slideMoveHit; + } + else if (traceDown.fraction > 0f) + position = traceDown.endPosition; + + 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) + { + // ? + position = slidePosition; + velocity = slideVelocity; + return slideMoveHit; + } + + Vector3 stepPosition = position; + Vector3 slidePosition2 = slidePosition; + Vector3 stepPosition2 = stepPosition; + + slidePosition2.Y = 0f; + stepPosition2.Y = 0f; + + // choose which one went furthest away from the original position + if ((stepPosition2 - originalPosition).Length < (slidePosition2 - originalPosition).Length) + { + position = slidePosition; + velocity = slideVelocity; + return slideMoveHit; + } + + slideMoveHit = slideMoveStepHit; + } + + return slideMoveHit; + } + + [Flags] + enum SlideMoveHit + { + Nothing = 0, + Step = 1, + Floor = 2, + Other = 4, + } + + private SlideMoveHit SlideMove(ref Vector3 position, ref Vector3 velocity) + { + if (velocity.IsZero) + return SlideMoveHit.Nothing; + + Vector3 originalPosition = position; + Vector3 originalVelocity = velocity; + SlideMoveHit slideMoveHit = SlideMoveHit.Nothing; + float timeleft = Time.DeltaTime; List hitNormals = new List(); @@ -261,6 +345,13 @@ namespace Game timeleft *= 1.0f - fraction; + if (trace.hitNormal.Y > 0.7) + slideMoveHit |= SlideMoveHit.Floor; + else if (Math.Abs(trace.hitNormal.Y) < 0.0001f) + slideMoveHit |= SlideMoveHit.Step; + else + slideMoveHit |= SlideMoveHit.Other; + // this doesn't seem to do anything, we never have any hitNormals stored here bool hitPreviousNormal = false; foreach (Vector3 normal in hitNormals) @@ -340,6 +431,8 @@ namespace Game break; } } + + return slideMoveHit; } public override void OnUpdate() @@ -376,6 +469,7 @@ namespace Game private const float airStrafeAcceleration = 70f * 0f; //CPM? private const float strafeAcceleration = 10f; //QW private const float airControl = 0f; //CPM + private const float stepSize = 16f; private bool physicsInteractions = false; private bool jumped = false; @@ -385,6 +479,15 @@ namespace Game [ReadOnly] public float CurrentVelocity { get { return currentVelocity.Length; } set {} } + [ReadOnly] + public float UPS { get + { + Vector3 horizontalSpeed = currentVelocity; + horizontalSpeed.Y = 0f; + return horizontalSpeed.Length; + + } set {} } + private Vector3 currentVelocity; public override void OnFixedUpdate() { @@ -394,7 +497,7 @@ namespace Game Vector3 moveDirection = rootTrans.TransformDirection(inputDirection); Vector3 position = rigidBody.Position; - Vector3 velocity = rigidBody.LinearVelocity;//currentVelocity; + Vector3 velocity = currentVelocity;//rigidBody.LinearVelocity; Vector3 wishVelocity = Vector3.Zero; if (!inputDirection.IsZero) @@ -432,15 +535,21 @@ namespace Game else { onGround = !traceGround.startSolid; - velocity.Y = 0f; } + // TODO: snap to ground here + // jump if (onGround) { if (!jumped && InputManager.GetAction("Jump")) { - //jumped = true; + // reset velocity from gravity + if (-Vector3.Dot(Physics.Gravity.Normalized, velocity) < 0 && + Vector3.Dot(velocity, traceGround.hitNormal) < -0.1) + { + velocity = Vector3.ProjectOnPlane(velocity, traceGround.hitNormal); + } velocity += Vector3.Up * jumpVelocity; onGround = false; @@ -490,10 +599,11 @@ namespace Game rigidBody.LinearVelocity = velocity; else { - SlideMove(ref position, false, ref velocity); + StepSlideMove(ref position, ref velocity, onGround); rigidBody.Position = position; - rigidBody.LinearVelocity = velocity; + currentVelocity = velocity; + //rigidBody.LinearVelocity = velocity; } }