This commit is contained in:
GoaLitiuM
2021-05-30 18:55:57 +03:00
parent f821959896
commit d6b52edab3
4 changed files with 64 additions and 3057 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -23,6 +23,11 @@ namespace Cabrito
ulong drawTimeCount;
const double drawInterval = 0.25;
Stopwatch sw3;
double physicsTimeAvg = 0.0;
ulong physicsTimeCount;
const double physicsInterval = 0.25;
string currentRenderer = "Unknown";
RenderTask t;
@@ -45,6 +50,8 @@ namespace Cabrito
t.Render += OnDraw;
}
sw3 = Stopwatch.StartNew();
var settings = FlaxEditor.Content.Settings.GameSettings.Load();
timeSettings = settings.Time.CreateInstance<FlaxEditor.Content.Settings.TimeSettings>();
}
@@ -71,8 +78,9 @@ namespace Cabrito
ConsoleContentTextBox.accumDrawTimes = 0;
}
((Label)control.Control).Text = "FPS: " + ((int)Math.Round(1.0f / updateTimeAvg)).ToString();
((Label)control.Control).Text = "uFPS: " + ((int)Math.Round(1.0f / updateTimeAvg)).ToString();
label.Text += "\nrFPS: " + ((int)Math.Round(1.0f / drawTimeAvg)).ToString();
label.Text += "\npFPS: " + ((int)Math.Round(1.0f / physicsTimeAvg)).ToString();
label.Text += "\nCon: " + conTime.ToString() + "ms";
label.Text += "\n" + currentRenderer;
label.Text += "\nGC memory: " + (GC.GetTotalMemory(false)/1000000.0f).ToString() + "MB";
@@ -100,6 +108,18 @@ namespace Cabrito
}*/
}
public override void OnFixedUpdate()
{
physicsTimeCount++;
double elapsed = sw3.Elapsed.TotalSeconds;
if (elapsed >= physicsInterval)
{
sw3.Restart();
physicsTimeAvg = elapsed / physicsTimeCount;
physicsTimeCount = 0;
}
}
void OnDraw(RenderTask tt, GPUContext context)
{
drawTimeCount++;

View File

@@ -7,7 +7,7 @@ using FlaxEngine.Assertions;
namespace Game
{
public struct TraceInfo_Old
public struct TraceInfo
{
public RayCastHit[] hitInfos;
public bool startSolid;
@@ -52,22 +52,9 @@ namespace Game
rootActor = Actor.GetChild(0);
rigidBody = Actor.As<RigidBody>();
rigidBody.CollisionEnter += OnCollisionEnter;
rigidBody.TriggerEnter += OnTriggerEnter;
rigidBody.TriggerExit += OnTriggerExit;
/*proxyBody.CollisionEnter += delegate(Collision collision) {
if (collision.OtherActor.AttachedRigidBody != null)
{
proxyCollision = true;
}
};
proxyBody.CollisionExit += delegate(Collision collision) {
if (collision.OtherActor.AttachedRigidBody != null)
{
proxyCollision = false;
}
};*/
//rigidBody.CollisionEnter += OnCollisionEnter;
//rigidBody.TriggerEnter += OnTriggerEnter;
//rigidBody.TriggerExit += OnTriggerExit;
}
private List<PhysicsColliderActor> touchingActors = new List<PhysicsColliderActor>();
@@ -108,75 +95,13 @@ namespace Game
viewRoll = initialEulerAngles.Z;
}
/// <summary>
/// Checks for overlapping colliders in place using the player's rigidbody.
/// </summary>
/// <param name="position">Position</param>
/// <returns></returns>
private Collider[] TracePlayer(Vector3 position, float extra = 0.0f)
{
Collider[] colliders = null;
bool collided = false;
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
var boxCollider = Actor.GetChild<BoxCollider>();
var meshCollider = Actor.GetChild<MeshCollider>();
PhysicsColliderActor colliderActor = null;
if (capsuleCollider && capsuleCollider.IsActive)
{
colliderActor = capsuleCollider;
collided = Physics.OverlapCapsule(position,
capsuleCollider.Radius + extra, capsuleCollider.Height + extra,
out colliders, capsuleCollider.Orientation,
uint.MaxValue,
false);
}
else if (meshCollider && meshCollider.IsActive)
{
colliderActor = meshCollider;
collided = Physics.OverlapConvex(position,
meshCollider.CollisionData, meshCollider.Scale + extra,
out colliders, meshCollider.Orientation,
uint.MaxValue,
false);
}
else if (boxCollider && boxCollider.IsActive)
{
colliderActor = boxCollider;
collided = Physics.OverlapBox(position,
boxCollider.OrientedBox.Extents + extra,
out colliders, boxCollider.Orientation,
uint.MaxValue,
false);
}
else
Assert.Fail("No supported colliders found for rigidbody");
if (!collided)
return colliders;
List<Collider> collidersFiltered = new List<Collider>();
foreach (var collider in colliders)
{
if (collider == colliderActor)
continue;
collidersFiltered.Add(collider);
}
if (collidersFiltered.Count == 0)
return colliders; // self-collision?
return collidersFiltered.ToArray();
}
/// <summary>
/// Sweeps the player rigidbody in world and returns geometry which was hit during the trace.
/// </summary>
/// <param name="start">Start position</param>
/// <param name="end">End position</param>
/// <returns></returns>
private TraceInfo TracePlayer(Vector3 start, Vector3 end, float tolerance = 0.0f)
private TraceInfo TracePlayer(Vector3 start, Vector3 end)
{
TraceInfo traceInfo = new TraceInfo();
@@ -193,7 +118,7 @@ namespace Game
{
colliderActor = capsuleCollider;
collided = Physics.CapsuleCastAll(start,
capsuleCollider.Radius + tolerance, capsuleCollider.Height,
capsuleCollider.Radius, capsuleCollider.Height,
direction, out traceInfo.hitInfos, capsuleCollider.Orientation, maxDistance,
uint.MaxValue,
false);
@@ -202,7 +127,7 @@ namespace Game
{
colliderActor = meshCollider;
collided = Physics.ConvexCastAll(start,
meshCollider.CollisionData, meshCollider.Scale + tolerance,
meshCollider.CollisionData, meshCollider.Scale,
direction, out traceInfo.hitInfos, meshCollider.Orientation, maxDistance,
uint.MaxValue,
false);
@@ -211,7 +136,7 @@ namespace Game
{
colliderActor = boxCollider;
collided = Physics.BoxCastAll(start,
boxCollider.OrientedBox.Extents + tolerance,
boxCollider.OrientedBox.Extents,
direction, out traceInfo.hitInfos, boxCollider.Orientation, maxDistance, uint.MaxValue,
false);
}
@@ -296,7 +221,7 @@ namespace Game
}
}
private void SlideMove(ref Vector3 position, bool stepUp, ref Vector3 velocity, bool asdf = false)
private void SlideMove(ref Vector3 position, bool stepUp, ref Vector3 velocity)
{
if (velocity.IsZero)
return;
@@ -325,20 +250,6 @@ namespace Game
break;
}
if (physicsInteractions)
{
RigidBody rigidBody = Actor.As<RigidBody>();
foreach (var hit in trace.hitInfos)
{
if (hit.Collider.AttachedRigidBody == null || hit.Collider.AttachedRigidBody.IsKinematic)
continue;
Vector3 force = -hit.Normal * velocity.Length * rigidBody.Mass;
//Console.Print("move force: " + (force.Length / timeleft));
hit.Collider.AttachedRigidBody.AddForce(force, ForceMode.Force);
}
}
if (fraction > 0f)
{
position = trace.endPosition;
@@ -431,6 +342,25 @@ namespace Game
}
}
public override void OnUpdate()
{
float xAxis = InputManager.GetAxisRaw("Mouse X");
float yAxis = InputManager.GetAxisRaw("Mouse Y");
if (xAxis != 0.0f || yAxis != 0.0f)
{
var camera = rootActor.GetChild<Camera>();
viewPitch += yAxis;
viewYaw += xAxis;
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
// root orientation must be set first
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
}
}
[ReadOnly]
public bool onGround = false;
@@ -446,7 +376,7 @@ namespace Game
private const float airStrafeAcceleration = 70f * 0f; //CPM?
private const float strafeAcceleration = 10f; //QW
private const float airControl = 0f; //CPM
private bool physicsInteractions = true;
private bool physicsInteractions = false;
private bool jumped = false;
@@ -470,136 +400,6 @@ namespace Game
if (!inputDirection.IsZero)
wishVelocity = moveDirection.Normalized * MoveSpeed;
#if false
Collider[] overlaps = TracePlayer(rigidBody.Position, 0.001f * 0f);
if (overlaps.Length > 0)
{
// overlapping with other objects
//Console.Print("overlap");
Collider playerCollider = null;
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
var boxCollider = Actor.GetChild<BoxCollider>();
var meshCollider = Actor.GetChild<MeshCollider>();
if (capsuleCollider && capsuleCollider.IsActive)
{
playerCollider = capsuleCollider;
}
else if (meshCollider && meshCollider.IsActive)
{
playerCollider = meshCollider;
}
else if (boxCollider && boxCollider.IsActive)
{
playerCollider = boxCollider;
}
foreach (var overlap in overlaps)
{
if (Collider.ComputePenetration(playerCollider, overlap, out Vector3 direction, out float distance))
{
//Console.Print("overlap ok");
var type = overlap.Parent.GetType();
if (overlap.Parent.GetType() == typeof(RigidBody))
{
var otherRigidBody = overlap.Parent.As<RigidBody>();
var otherMomentum = otherRigidBody.LinearVelocity * otherRigidBody.Mass;
var myMomentum = rigidBody.LinearVelocity * rigidBody.Mass;
var newVelocity = (otherMomentum + myMomentum) / (otherRigidBody.Mass + rigidBody.Mass);
Console.Print(overlap.Parent.Name + ": "+ newVelocity.Length.ToString());
//otherRigidBody.LinearVelocity += -direction.Normalized * newVelocity;
otherRigidBody.AddForce(-direction.Normalized * newVelocity, ForceMode.Force);
}
position += direction * (distance + 0.001f);
/*var oldVelocity = velocity;
velocity = direction * (distance + 0.001f);
velocity /= Time.DeltaTime;
SlideMove(ref position, false, ref velocity);
velocity = oldVelocity;*/
//position += direction * (distance + 0.001f);
//velocity = Vector3.Zero;
break;
}
else
{
//Console.Print("overlap safepos");
position = safePosition;
velocity = Vector3.Zero;
break;
}
}
/*const float overlapResolveSpeed = 1.0f;
foreach (var overlap in overlaps)
{
var overlapRigidbody = overlap.Parent.GetType() == typeof(RigidBody) ? overlap.Parent.As<RigidBody>() : null;
if (overlapRigidbody != null)
{
Vector3 normal = (rigidBody.Position - overlapRigidbody.Position).Normalized;
position += normal * overlapResolveSpeed;
break;
}
else
{
position = safePosition;
velocity = Vector3.Zero;
break;
}
}*/
}
#endif
bool beingPushed = false;
if (touchingActors.Count > 0)
beingPushed = true;
/*bool beingPushed = false;
Collider[] overlaps = TracePlayer(rigidBody.Position);
if (overlaps.Length > 0)
{
//beingPushed = true;
Collider playerCollider = null;
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
var boxCollider = Actor.GetChild<BoxCollider>();
var meshCollider = Actor.GetChild<MeshCollider>();
if (capsuleCollider && capsuleCollider.IsActive)
{
playerCollider = capsuleCollider;
}
else if (meshCollider && meshCollider.IsActive)
{
playerCollider = meshCollider;
}
else if (boxCollider && boxCollider.IsActive)
{
playerCollider = boxCollider;
}
foreach (var overlap in overlaps)
{
if (Collider.ComputePenetration(playerCollider, overlap, out Vector3 direction, out float distance))
{
beingPushed = true;
}
else
{
}
}
}*/
// categorize position
onGround = true;
Vector3 groundDelta = Physics.Gravity.Normalized * (collisionMargin*2);
@@ -632,95 +432,9 @@ namespace Game
else
{
onGround = !traceGround.startSolid;
velocity.Y = 0f;
}
//onGround |= overlaps.Length > 0;
if (onGround && physicsInteractions && traceGround.hitInfos.Length > 0)
{
// apply resting force to rigid bodies under the player
//bool collided = false;
foreach (var hit in traceGround.hitInfos)
{
if (hit.Collider.AttachedRigidBody == null || hit.Collider.AttachedRigidBody.IsKinematic)
continue;
if (hit.Distance <= 0f)
continue;
//Console.Print(Physics.Gravity.Length.ToString());
Vector3 force = -hit.Normal * (Physics.Gravity.Length) * rigidBody.Mass * Time.DeltaTime;
hit.Collider.AttachedRigidBody.AddForceAtPosition(force, hit.Point, ForceMode.Impulse);
//collided = true;
//Console.Print("downforce: " + force.Length / Time.DeltaTime);
}
//if (collided)
// fraction = 1.0f; // finish movement and stop
}
/*if (!onGround)
Console.Print("air");
else
Console.Print("ground");*/
/*if (onGround)
{
// snap to ground
if (!traceGround.startSolid)
{
Vector3 newPos = rigidBody.Position;
if (traceGround.fraction < 1f)
{
//newPos += -Physics.Gravity.Normalized * traceGround.fraction;
}
rigidBody.Position = newPos;
}
}*/
/*if (traceGround.startSolid)
{
Console.Print("stuk: ");
rigidBody.Position = safePosition;
traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + Physics.Gravity.Normalized,
false);
//onGround = true;
//currentVelocity.Y = 0f;
}
if (!traceGround.startSolid)
{
foreach (var hitInfo in traceGround.hitInfos)
{
var dot = Vector3.Dot(Physics.Gravity.Normalized, hitInfo.Normal);
if (-dot >= 0.7) //~45deg slope
{
//Console.Print("d: " + hitInfo.Distance);
Vector3 newPos = rigidBody.Position;
if (hitInfo.Distance > 0f)
newPos += Physics.Gravity.Normalized * (hitInfo.Distance - 0.01f);
else
newPos += hitInfo.Normal * 0.1f;
rigidBody.Position = newPos;
onGround = true;
currentVelocity.Y = 0f;
break;
//if (currentVelocity.Length > 0.01f)
// Console.Print("groundvel: " + currentVelocity.ToString());
//currentVelocity.Y = 0.0f;
}
}
}*/
// jump
if (onGround)
{
@@ -735,7 +449,6 @@ namespace Game
jumped = false;
}
// ground friction
if (onGround)
{
@@ -754,8 +467,6 @@ namespace Game
velocity *= newspeed / currentSpeed;
}
//bool stepUp = false;
if (onGround) // ground acceleration
{
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishVelocity.Length, float.MaxValue, accelerationGround);
@@ -768,62 +479,22 @@ namespace Game
if (strafeAcceleration != 0f)
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishspeed, maxAirStrafeSpeed, strafeAcceleration);
//stepUp = true;
}
if (!onGround)
{
velocity += Physics.Gravity * Time.DeltaTime;
//Console.Print("grav");
}
//else
// Console.Print("Yv: " + currentVelocity.Y);
safePosition = rigidBody.Position;
currentVelocity = velocity;
if (!rigidBody.IsKinematic)
{
//rigidBody.IsKinematic = false;
//rigidBody.Position = position;
rigidBody.LinearVelocity = velocity;
//Console.Print("being pushed " + rigidBody.LinearVelocity.Length);
//rigidBody.AddForce(velocity + ((position - rigidBody.Position) * Time.DeltaTime), ForceMode.Impulse);
}
else
{
SlideMove(ref position, false, ref velocity);
//rigidBody.IsKinematic = true;
rigidBody.Position = position;
rigidBody.LinearVelocity = velocity;
}
/*if (proxyBody != null)
{
if (beingPushed)
{
//rigidBody.Position = proxyBody.Position;
rigidBody.LinearVelocity = proxyBody.LinearVelocity;
//rigidBody.Position += rigidBody.LinearVelocity * Time.DeltaTime;
}
else
{
var proxyVelocity = position - proxyBody.Position;
//proxyBody.LinearVelocity += proxyVelocity;
//rigidBody.Position = proxyBody.Position;
proxyBody.Position = position;
proxyBody.LinearVelocity = velocity;
}
}*/
//rigidBody.LinearVelocity = velocity;
//if (currentVelocity.Length > 0.01f)
// Console.Print("vel: " + currentVelocity.ToString());
}
void ApplyAcceleration(ref Vector3 velocity, Vector3 wishDir, float wishspeed, float maxWishspeed, float acceleration)
@@ -843,24 +514,5 @@ namespace Game
velocity += accelSpeed * wishDir;
}
public override void OnUpdate()
{
float xAxis = InputManager.GetAxisRaw("Mouse X");
float yAxis = InputManager.GetAxisRaw("Mouse Y");
if (xAxis != 0.0f || yAxis != 0.0f)
{
var camera = rootActor.GetChild<Camera>();
viewPitch += yAxis;
viewYaw += xAxis;
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
// root orientation must be set first
rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0);
camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
#if false
using System.Collections.Generic;
using FlaxEngine;
using Cabrito;
using System.Diagnostics;
@@ -7,7 +8,7 @@ using FlaxEngine.Assertions;
namespace Game
{
public struct TraceInfo
public struct TraceInfo_NK
{
public RayCastHit[] hitInfos;
public bool startSolid;
@@ -760,3 +761,4 @@ namespace Game
}
}
}
#endif