juttuja
This commit is contained in:
@@ -66,78 +66,49 @@ namespace Game
|
||||
|
||||
private TraceInfo TracePlayer(Vector3 start, Vector3 end)
|
||||
{
|
||||
bool collided = false;
|
||||
|
||||
TraceInfo traceInfo = new TraceInfo();
|
||||
|
||||
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
|
||||
var boxCollider = Actor.GetChild<BoxCollider>();
|
||||
PhysicsColliderActor colliderActor = null;
|
||||
|
||||
Vector3 delta = end - start;
|
||||
float maxDistance = delta.Length;
|
||||
Vector3 direction = delta.Normalized;
|
||||
|
||||
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;
|
||||
|
||||
//start.Y -= capsuleCollider.Height / 2;
|
||||
//start.Y -= capsuleCollider.Radius / 2;
|
||||
|
||||
/*collided = Physics.BoxCastAll(start,
|
||||
new Vector3(capsuleCollider.Radius,
|
||||
(capsuleCollider.Height + capsuleCollider.Radius) / 2,
|
||||
capsuleCollider.Radius),
|
||||
delta.Normalized, out hitInfos, Quaternion.Identity, delta.Length, uint.MaxValue,
|
||||
false);*/
|
||||
|
||||
/*traceInfo.startSolid = Physics.OverlapCapsule(start, capsuleCollider.Radius, capsuleCollider.Height,
|
||||
out Collider[] results, capsuleCollider.Orientation, uint.MaxValue, false);
|
||||
|
||||
if (traceInfo.startSolid)
|
||||
{
|
||||
foreach (Collider collider in results)
|
||||
{
|
||||
if (collider == colliderActor)
|
||||
continue;
|
||||
|
||||
colliderActor = colliderActor;
|
||||
}
|
||||
}
|
||||
else*/
|
||||
{
|
||||
collided = Physics.CapsuleCastAll(start,
|
||||
capsuleCollider.Radius, capsuleCollider.Height,
|
||||
direction, out traceInfo.hitInfos, capsuleCollider.Orientation, maxDistance,
|
||||
uint.MaxValue,
|
||||
false);
|
||||
}
|
||||
collided = Physics.CapsuleCastAll(start,
|
||||
capsuleCollider.Radius, capsuleCollider.Height,
|
||||
direction, out traceInfo.hitInfos, capsuleCollider.Orientation, maxDistance,
|
||||
uint.MaxValue,
|
||||
false);
|
||||
}
|
||||
else if (meshCollider && meshCollider.IsActive)
|
||||
{
|
||||
colliderActor = meshCollider;
|
||||
collided = Physics.ConvexCastAll(start,
|
||||
meshCollider.CollisionData,
|
||||
direction, out traceInfo.hitInfos, capsuleCollider.Orientation, maxDistance,
|
||||
uint.MaxValue,
|
||||
false);
|
||||
}
|
||||
else if (boxCollider && boxCollider.IsActive)
|
||||
{
|
||||
colliderActor = boxCollider;
|
||||
|
||||
//start.Y += boxCollider.Size.Y / 2;
|
||||
|
||||
/*traceInfo.startSolid = Physics.CheckBox(start, boxCollider.OrientedBox.Extents, boxCollider.Orientation,
|
||||
uint.MaxValue, false);
|
||||
|
||||
if (!traceInfo.startSolid)*/
|
||||
{
|
||||
collided = Physics.BoxCastAll(start,
|
||||
boxCollider.OrientedBox.Extents,
|
||||
direction, out traceInfo.hitInfos, boxCollider.Orientation, maxDistance, uint.MaxValue,
|
||||
false);
|
||||
}
|
||||
collided = Physics.BoxCastAll(start,
|
||||
boxCollider.OrientedBox.Extents,
|
||||
direction, out traceInfo.hitInfos, boxCollider.Orientation, maxDistance, uint.MaxValue,
|
||||
false);
|
||||
}
|
||||
|
||||
if (collided)
|
||||
{
|
||||
List<RayCastHit> hitInfosFiltered = new List<RayCastHit>();
|
||||
RayCastHit closest = new RayCastHit();
|
||||
RayCastHit furthest = new RayCastHit();
|
||||
closest.Distance = float.MaxValue;
|
||||
furthest.Distance = float.MinValue;
|
||||
foreach (var hitInfo in traceInfo.hitInfos)
|
||||
{
|
||||
if (hitInfo.Collider == colliderActor)
|
||||
@@ -147,53 +118,16 @@ namespace Game
|
||||
|
||||
if (hitInfo.Distance < closest.Distance)
|
||||
closest = hitInfo;
|
||||
if (hitInfo.Distance > furthest.Distance)
|
||||
furthest = hitInfo;
|
||||
}
|
||||
|
||||
traceInfo.hitInfos = hitInfosFiltered.ToArray();
|
||||
|
||||
//if (!closest.Normal.IsZero)
|
||||
if (closest.Distance > 0f)
|
||||
{
|
||||
//traceInfo.fraction = (closest.Point - start).Length / maxDistance;
|
||||
//traceInfo.maxFraction = (furthest.Point - start).Length / maxDistance;
|
||||
/*if (traceLength != 0f)
|
||||
{
|
||||
traceInfo.fraction = closest.Distance / traceLength;
|
||||
traceInfo.maxFraction = furthest.Distance / traceLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
traceInfo.fraction = 0f;
|
||||
traceInfo.maxFraction = 0f;
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
// convoluted shit incoming...
|
||||
// trace a ray back from the hitpoints to calculate the actual fraction travelled...
|
||||
Physics.RayCastAll(closest.Point, -direction, out RayCastHit[] rayHitInfos, maxDistance * 2f,
|
||||
uint.MaxValue, true);
|
||||
float altFraction = 0f;
|
||||
float oldFraction = closest.Distance / maxDistance;
|
||||
foreach (var hitInfo in rayHitInfos)
|
||||
{
|
||||
if (hitInfo.Collider != colliderActor)
|
||||
continue;
|
||||
|
||||
traceInfo.fraction = hitInfo.Distance / maxDistance;
|
||||
altFraction = (closest.Point - hitInfo.Point).Length / maxDistance;
|
||||
break;
|
||||
}
|
||||
|
||||
traceInfo.fraction = closest.Distance / maxDistance;
|
||||
traceInfo.hitNormal = closest.Normal;
|
||||
traceInfo.hitPosition = closest.Point;
|
||||
traceInfo.endPosition = start + (direction * traceInfo.fraction);
|
||||
|
||||
//traceInfo.maxHitNormal = furthest.Normal;
|
||||
//traceInfo.hitPosition = furthest.Point;
|
||||
//traceInfo.maxEndPosition = start + ((end - start).Normalized * traceInfo.maxFraction);
|
||||
traceInfo.endPosition = start + (delta * traceInfo.fraction);
|
||||
|
||||
if (traceInfo.fraction == 0f && maxDistance > 0f)
|
||||
traceInfo.startSolid = true;
|
||||
@@ -203,36 +137,11 @@ namespace Game
|
||||
traceInfo.startSolid = true;
|
||||
traceInfo.fraction = 0f;
|
||||
}
|
||||
|
||||
/*traceInfo.hitInfos = hitInfosFiltered.ToArray();
|
||||
|
||||
float travel = (end - start).Length;
|
||||
if (travel != 0f)
|
||||
{
|
||||
traceInfo.fraction = closest.Distance / travel;
|
||||
traceInfo.maxFraction = furthest.Distance / travel;
|
||||
}
|
||||
else
|
||||
{
|
||||
traceInfo.fraction = 0f;
|
||||
traceInfo.maxFraction = 0f;
|
||||
}
|
||||
|
||||
traceInfo.hitNormal = closest.Normal;
|
||||
traceInfo.hitPosition = closest.Point;
|
||||
traceInfo.endPosition = start + ((end - start).Normalized * traceInfo.fraction);
|
||||
|
||||
traceInfo.maxHitNormal = furthest.Normal;
|
||||
//traceInfo.hitPosition = furthest.Point;
|
||||
traceInfo.maxEndPosition = start + ((end - start).Normalized * traceInfo.maxFraction);
|
||||
|
||||
if (traceInfo.fraction == 0f && travel > 0f)
|
||||
traceInfo.startSolid = true;*/
|
||||
}
|
||||
else
|
||||
{
|
||||
traceInfo.hitInfos = new RayCastHit[0];
|
||||
traceInfo.fraction = traceInfo.startSolid ? 0f : 1f;
|
||||
traceInfo.fraction = 1f;
|
||||
traceInfo.endPosition = end;
|
||||
}
|
||||
|
||||
@@ -249,6 +158,11 @@ namespace Game
|
||||
if (capsuleCollider && capsuleCollider.IsActive)
|
||||
{
|
||||
Quaternion rotation = capsuleCollider.LocalOrientation * Quaternion.Euler(0f, 90f, 0f);
|
||||
if (cylinderShape)
|
||||
{
|
||||
DebugDraw.DrawWireBox(capsuleCollider.Box, Color.GreenYellow * 0.8f);
|
||||
}
|
||||
|
||||
DebugDraw.DrawWireTube(rigidBody.Position, rotation, capsuleCollider.Radius, capsuleCollider.Height, Color.GreenYellow * 0.8f);
|
||||
}
|
||||
else if (boxCollider && boxCollider.IsActive)
|
||||
@@ -269,49 +183,36 @@ namespace Game
|
||||
|
||||
List<Vector3> hitNormals = new List<Vector3>();
|
||||
|
||||
// margin in fte: 0.0312 away from the wall
|
||||
|
||||
Vector3 traceOffset = Vector3.Zero;
|
||||
for (int bump = 0; bump < 4; bump++)
|
||||
{
|
||||
Vector3 startPos = position;
|
||||
Vector3 endPos = position + (velocity * timeleft);
|
||||
float distt = (endPos - startPos).Length;
|
||||
|
||||
TraceInfo trace = TracePlayer(startPos + traceOffset, endPos);
|
||||
TraceInfo trace = TracePlayer(startPos, endPos);
|
||||
// TODO: handle portals here
|
||||
|
||||
float fraction = trace.fraction;
|
||||
Vector3 hitNormal = trace.hitNormal;
|
||||
Vector3 hitEndPosition = trace.endPosition;
|
||||
/*if (bump > 0 && trace.startSolid)
|
||||
{
|
||||
fraction = trace.maxFraction;
|
||||
hitNormal = trace.maxHitNormal;
|
||||
hitEndPosition = trace.maxEndPosition;
|
||||
}*/
|
||||
|
||||
if (fraction > 0f)
|
||||
{
|
||||
position = hitEndPosition;
|
||||
hitNormals.Clear();
|
||||
|
||||
/*if (position.Z > 368f)
|
||||
{
|
||||
position = position;
|
||||
TracePlayer(startPos + traceOffset, endPos);
|
||||
}*/
|
||||
}
|
||||
else if (trace.startSolid)
|
||||
if (trace.startSolid)
|
||||
{
|
||||
velocity = Vector3.Zero;
|
||||
break;
|
||||
}
|
||||
|
||||
if (fraction > 0f)
|
||||
{
|
||||
position = trace.endPosition;
|
||||
hitNormals.Clear(); // this is present in some forks, not in Q3
|
||||
}
|
||||
|
||||
if (fraction >= 1f)
|
||||
break;
|
||||
|
||||
timeleft -= timeleft * fraction;
|
||||
timeleft *= 1.0f - fraction;
|
||||
|
||||
// this doesn't seem to do anything, we never have any hitNormals stored here
|
||||
bool hitPreviousNormal = false;
|
||||
foreach (Vector3 normal in hitNormals)
|
||||
{
|
||||
@@ -327,58 +228,9 @@ namespace Game
|
||||
continue;
|
||||
|
||||
hitNormals.Add(hitNormal);
|
||||
if (hitNormals.Count != 1)
|
||||
Console.Print("hitNormals: " + hitNormals.Count);
|
||||
|
||||
/*
|
||||
bool stuck = true;
|
||||
foreach (Vector3 normal in hitNormals)
|
||||
{
|
||||
// clip velocity
|
||||
Vector3 velocityProjected = Vector3.ProjectOnPlane(velocity, normal);
|
||||
float backoff = Vector3.Dot(velocity, normal) * 1f;
|
||||
velocity -= normal * backoff;
|
||||
//velocity = velocityProjected;
|
||||
|
||||
bool parallel = true;
|
||||
foreach (Vector3 otherNormal in hitNormals)
|
||||
{
|
||||
if (otherNormal == normal)
|
||||
continue;
|
||||
|
||||
if (Vector3.Dot(velocity, otherNormal) < 0f)
|
||||
{
|
||||
parallel = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (parallel)
|
||||
{
|
||||
// moving away from all touched walls
|
||||
stuck = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (stuck)
|
||||
{
|
||||
if (hitNormals.Count == 2)
|
||||
{
|
||||
Vector3 dir = Vector3.Cross(hitNormals[0], hitNormals[1]);
|
||||
float dist = Vector3.Dot(dir, velocity);
|
||||
velocity = dist * dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity = Vector3.Zero;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// nudge very slightly away from the wall to avoid getting stuck
|
||||
//position += trace.hitNormal * 0.01f;
|
||||
}
|
||||
*/
|
||||
int plane;
|
||||
Vector3 normalMargin = Vector3.Zero;
|
||||
for (plane = 0; plane < hitNormals.Count; plane++)
|
||||
@@ -407,6 +259,7 @@ namespace Game
|
||||
break;
|
||||
}
|
||||
|
||||
// push off slightly away from the walls to not get stuck
|
||||
position += normalMargin.Normalized * 0.031f;
|
||||
|
||||
if (plane == hitNormals.Count)
|
||||
@@ -414,6 +267,7 @@ namespace Game
|
||||
if (hitNormals.Count == 2)
|
||||
{
|
||||
Vector3 dir = Vector3.Cross(hitNormals[0], hitNormals[1]);
|
||||
//dir.Normalize();
|
||||
float dist = Vector3.Dot(dir, velocity);
|
||||
velocity = dist * dir;
|
||||
}
|
||||
@@ -437,156 +291,6 @@ namespace Game
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!asdf)
|
||||
{
|
||||
/*var finalTrace = TracePlayer(position, position);
|
||||
if (finalTrace.startSolid)
|
||||
{
|
||||
position = originalPosition;
|
||||
velocity = originalVelocity;
|
||||
SlideMove(ref position, false, ref velocity, true);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
private bool SlideMove2(ref Vector3 position, bool stepUp, ref Vector3 velocity)
|
||||
{
|
||||
// PM_SlideMove
|
||||
/*
|
||||
|
||||
endpos = pos + velocity
|
||||
|
||||
for i=0, i<4, i++
|
||||
trace(pos, endpos)
|
||||
|
||||
if trace.dist >= 1
|
||||
break;
|
||||
|
||||
blocked =
|
||||
blocked_floor => trace.plane.normal[2] >= MIN_STEP_NORMAL
|
||||
blocked_step => trace.plane.normal[2] == 0
|
||||
blocked_other => .
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
if (velocity.IsZero)
|
||||
return false;
|
||||
|
||||
Vector3 originalVelocity = velocity;
|
||||
|
||||
Vector3 startPos = position;
|
||||
//float distanceLeft = (velocity * Time.DeltaTime).Length;
|
||||
float timeLeft = Time.DeltaTime;
|
||||
|
||||
|
||||
|
||||
bool anyCollisions = false;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
Vector3 normal = Vector3.Zero;
|
||||
//float distance = distanceLeft;
|
||||
Vector3 direction = velocity.Normalized;
|
||||
//Vector3 endPos = startPos + (direction * distanceLeft);
|
||||
Vector3 endPos = startPos + (velocity * timeLeft);
|
||||
float travel = (endPos - startPos).Length;
|
||||
|
||||
//if (endPos == startPos)
|
||||
// break;
|
||||
|
||||
TraceInfo trace = TracePlayer(startPos, endPos);
|
||||
bool collided = trace.hitInfos.Length > 0;
|
||||
float fraction = 1.0f;
|
||||
float distance = 0f;
|
||||
|
||||
if (trace.startSolid)
|
||||
{
|
||||
velocity = Vector3.Zero;
|
||||
anyCollisions = true;
|
||||
break;
|
||||
}
|
||||
if (collided)
|
||||
{
|
||||
collided = false;
|
||||
if (trace.hitInfos.Length > 1)
|
||||
Console.Print("fixme more traces!");
|
||||
|
||||
foreach (var hitInfo in trace.hitInfos)
|
||||
{
|
||||
//if (hitInfo.Distance < minDistanceFromFloor)
|
||||
// continue;
|
||||
|
||||
//if (hitInfo.Distance > distance)
|
||||
// Console.Print("hit distance is greater: " + hitInfo.Distance.ToString() + " > " + distance.ToString());
|
||||
|
||||
|
||||
fraction = hitInfo.Distance / travel;
|
||||
|
||||
collided = true;
|
||||
//if (hitInfo.Distance <= distance)
|
||||
{
|
||||
normal = hitInfo.Normal;
|
||||
distance = hitInfo.Distance;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
//Console.Print("collided " + hitInfo.Distance.ToString());
|
||||
//break;
|
||||
}
|
||||
}
|
||||
|
||||
if (collided)
|
||||
{
|
||||
|
||||
|
||||
// move to contact position, continue from here
|
||||
startPos += direction * distance; // what if this points to different direction?
|
||||
//distanceLeft -= distance;
|
||||
timeLeft *= fraction;
|
||||
|
||||
//Console.Print("timeleft: " + timeLeft.ToString() + ", frac:" + fraction.ToString());
|
||||
|
||||
//velocity = Vector3.ProjectOnPlane(velocity, normal);
|
||||
|
||||
Vector3 velocityProjected = Vector3.ProjectOnPlane(velocity, normal);
|
||||
float backoff = Vector3.Dot(velocity, normal) * 1f;
|
||||
velocity -= normal * backoff;
|
||||
velocity = velocityProjected;
|
||||
|
||||
//Console.Print((velocityProjected-velocity).Length.ToString());
|
||||
|
||||
|
||||
// prevents bouncing against the wall
|
||||
if (velocity.Length > 0f && Vector3.Dot(velocity, originalVelocity) <= 0f)
|
||||
{
|
||||
velocity = Vector3.Zero;
|
||||
//distanceLeft = 0f;
|
||||
timeLeft = 0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//distanceLeft = 0f;
|
||||
timeLeft = 0f;
|
||||
startPos = endPos;
|
||||
|
||||
}
|
||||
|
||||
anyCollisions |= collided;
|
||||
|
||||
if (timeLeft <= 0f)//if (distanceLeft <= 0f)
|
||||
break;
|
||||
}
|
||||
|
||||
position = startPos;
|
||||
|
||||
|
||||
return anyCollisions;
|
||||
}
|
||||
|
||||
//Vector3 wishVelocity = Vector3.Zero;
|
||||
@@ -609,6 +313,8 @@ namespace Game
|
||||
|
||||
private bool jumped = false;
|
||||
|
||||
private bool cylinderShape = true;
|
||||
|
||||
private Vector3 safePosition;
|
||||
|
||||
public override void OnFixedUpdate()
|
||||
@@ -625,9 +331,9 @@ namespace Game
|
||||
|
||||
// categorize position
|
||||
onGround = true;
|
||||
//TraceInfo traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + Physics.Gravity.Normalized);
|
||||
TraceInfo traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + Physics.Gravity.Normalized);
|
||||
|
||||
/*if (!traceGround.startSolid && traceGround.fraction < 1f &&
|
||||
if (!traceGround.startSolid && traceGround.fraction < 1f &&
|
||||
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < 0.7f)
|
||||
{
|
||||
// slope
|
||||
@@ -642,8 +348,7 @@ namespace Game
|
||||
(1f - traceGround.fraction) * bounce;
|
||||
|
||||
// retrace
|
||||
traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + point,
|
||||
false);
|
||||
traceGround = TracePlayer(rigidBody.Position, rigidBody.Position + point);
|
||||
}
|
||||
|
||||
if (!traceGround.startSolid && (traceGround.fraction >= 1f ||
|
||||
@@ -657,7 +362,7 @@ namespace Game
|
||||
onGround = !traceGround.startSolid;
|
||||
}
|
||||
|
||||
if (onGround)
|
||||
/*if (onGround)
|
||||
{
|
||||
// snap to ground
|
||||
if (!traceGround.startSolid)
|
||||
@@ -766,7 +471,7 @@ namespace Game
|
||||
|
||||
if (!onGround)
|
||||
{
|
||||
//currentVelocity += Physics.Gravity * Time.DeltaTime;
|
||||
currentVelocity += Physics.Gravity * Time.DeltaTime;
|
||||
//Console.Print("grav");
|
||||
}
|
||||
//else
|
||||
|
||||
Reference in New Issue
Block a user