Collider isn't moved to the position of the main limb when frozen, limb collisions are temporarily disabled if the ragdoll is too far from the collider, some cleanup

This commit is contained in:
Regalis
2016-10-19 16:21:54 +03:00
parent 51a2bf097d
commit b4389277aa
5 changed files with 89 additions and 113 deletions
@@ -15,8 +15,6 @@ namespace Barotrauma
private float waveAmplitude; private float waveAmplitude;
private float waveLength; private float waveLength;
private Limb mainLimb;
private bool rotateTowardsMovement; private bool rotateTowardsMovement;
private bool mirror, flip; private bool mirror, flip;
@@ -45,19 +43,16 @@ namespace Barotrauma
} }
rotateTowardsMovement = ToolBox.GetAttributeBool(element, "rotatetowardsmovement", true); rotateTowardsMovement = ToolBox.GetAttributeBool(element, "rotatetowardsmovement", true);
Limb torso = GetLimb(LimbType.Torso);
Limb head = GetLimb(LimbType.Head);
mainLimb = torso == null ? head : torso;
} }
public override void UpdateAnim(float deltaTime) public override void UpdateAnim(float deltaTime)
{ {
if (character.IsDead || Frozen || character.IsUnconscious || stunTimer > 0.0f) if (Frozen) return;
if (character.IsDead || character.IsUnconscious || stunTimer > 0.0f)
{ {
collider.PhysEnabled = false; collider.PhysEnabled = false;
collider.SetTransform(mainLimb.SimPosition, 0.0f); collider.SetTransform(MainLimb.SimPosition, 0.0f);
if (stunTimer > 0.0f) if (stunTimer > 0.0f)
{ {
@@ -169,32 +164,24 @@ namespace Barotrauma
Limb torso = GetLimb(LimbType.Torso); Limb torso = GetLimb(LimbType.Torso);
Limb head = GetLimb(LimbType.Head); Limb head = GetLimb(LimbType.Head);
mainLimb.pullJoint.Enabled = true; MainLimb.pullJoint.Enabled = true;
mainLimb.pullJoint.WorldAnchorB = collider.SimPosition; MainLimb.pullJoint.WorldAnchorB = collider.SimPosition;
if (movement.LengthSquared() < 0.00001f) return; if (movement.LengthSquared() < 0.00001f) return;
float movementAngle = MathUtils.VectorToAngle(movement) - MathHelper.PiOver2; float movementAngle = MathUtils.VectorToAngle(movement) - MathHelper.PiOver2;
if (rotateTowardsMovement) if (rotateTowardsMovement)
{ {
collider.SmoothRotate(movementAngle, 25.0f); collider.SmoothRotate(movementAngle, 25.0f);
mainLimb.body.SmoothRotate(movementAngle, 25.0f); MainLimb.body.SmoothRotate(movementAngle, 25.0f);
} }
else else
{ {
collider.SmoothRotate(HeadAngle * Dir, 25.0f); collider.SmoothRotate(HeadAngle * Dir, 25.0f);
mainLimb.body.SmoothRotate(HeadAngle * Dir, 25.0f); MainLimb.body.SmoothRotate(HeadAngle * Dir, 25.0f);
} }
//float angle = (rotateTowardsMovement) ?
// mainLimb.body.Rotation + MathUtils.GetShortestAngle(mainLimb.body.Rotation, movementAngle) :
// HeadAngle * Dir;
//collider.SmoothRotate(angle, 25.0f);
//mainLimb.body.SmoothRotate(angle, 25.0f);
Limb tail = GetLimb(LimbType.Tail); Limb tail = GetLimb(LimbType.Tail);
if (tail != null && waveAmplitude > 0.0f) if (tail != null && waveAmplitude > 0.0f)
{ {
@@ -211,11 +198,11 @@ namespace Barotrauma
Vector2 pullPos = Limbs[i].pullJoint == null ? Limbs[i].SimPosition : Limbs[i].pullJoint.WorldAnchorA; Vector2 pullPos = Limbs[i].pullJoint == null ? Limbs[i].SimPosition : Limbs[i].pullJoint.WorldAnchorA;
Limbs[i].body.ApplyForce(movement * Limbs[i].SteerForce * Limbs[i].Mass, pullPos); Limbs[i].body.ApplyForce(movement * Limbs[i].SteerForce * Limbs[i].Mass, pullPos);
if (Limbs[i] == mainLimb) continue; if (Limbs[i] == MainLimb) continue;
float dist = (mainLimb.SimPosition - Limbs[i].SimPosition).Length(); float dist = (MainLimb.SimPosition - Limbs[i].SimPosition).Length();
Vector2 limbPos = mainLimb.SimPosition - Vector2.Normalize(movement) * dist; Vector2 limbPos = MainLimb.SimPosition - Vector2.Normalize(movement) * dist;
Limbs[i].body.ApplyForce(((limbPos - Limbs[i].SimPosition) * 3.0f - Limbs[i].LinearVelocity * 3.0f) * Limbs[i].Mass); Limbs[i].body.ApplyForce(((limbPos - Limbs[i].SimPosition) * 3.0f - Limbs[i].LinearVelocity * 3.0f) * Limbs[i].Mass);
} }
@@ -233,7 +220,7 @@ namespace Barotrauma
IgnorePlatforms = (TargetMovement.Y < -Math.Abs(TargetMovement.X)); IgnorePlatforms = (TargetMovement.Y < -Math.Abs(TargetMovement.X));
float mainLimbHeight, mainLimbAngle; float mainLimbHeight, mainLimbAngle;
if (mainLimb.type == LimbType.Torso) if (MainLimb.type == LimbType.Torso)
{ {
mainLimbHeight = TorsoPosition; mainLimbHeight = TorsoPosition;
mainLimbAngle = torsoAngle; mainLimbAngle = torsoAngle;
@@ -244,18 +231,18 @@ namespace Barotrauma
mainLimbAngle = headAngle; mainLimbAngle = headAngle;
} }
mainLimb.body.SmoothRotate(mainLimbAngle * Dir, 50.0f); MainLimb.body.SmoothRotate(mainLimbAngle * Dir, 50.0f);
collider.LinearVelocity = new Vector2( collider.LinearVelocity = new Vector2(
movement.X, movement.X,
collider.LinearVelocity.Y > 0.0f ? collider.LinearVelocity.Y * 0.5f : collider.LinearVelocity.Y); collider.LinearVelocity.Y > 0.0f ? collider.LinearVelocity.Y * 0.5f : collider.LinearVelocity.Y);
mainLimb.MoveToPos(GetColliderBottom() + Vector2.UnitY * mainLimbHeight, 10.0f); MainLimb.MoveToPos(GetColliderBottom() + Vector2.UnitY * mainLimbHeight, 10.0f);
mainLimb.pullJoint.Enabled = true; MainLimb.pullJoint.Enabled = true;
mainLimb.pullJoint.WorldAnchorB = GetColliderBottom() + Vector2.UnitY * mainLimbHeight; MainLimb.pullJoint.WorldAnchorB = GetColliderBottom() + Vector2.UnitY * mainLimbHeight;
walkPos -= mainLimb.LinearVelocity.X * 0.05f; walkPos -= MainLimb.LinearVelocity.X * 0.05f;
Vector2 transformedStepSize = new Vector2( Vector2 transformedStepSize = new Vector2(
(float)Math.Cos(walkPos) * stepSize.X * 3.0f, (float)Math.Cos(walkPos) * stepSize.X * 3.0f,
@@ -267,7 +254,7 @@ namespace Barotrauma
{ {
case LimbType.LeftFoot: case LimbType.LeftFoot:
case LimbType.RightFoot: case LimbType.RightFoot:
Vector2 footPos = new Vector2(limb.SimPosition.X, mainLimb.SimPosition.Y - mainLimbHeight); Vector2 footPos = new Vector2(limb.SimPosition.X, MainLimb.SimPosition.Y - mainLimbHeight);
if (limb.RefJointIndex>-1) if (limb.RefJointIndex>-1)
{ {
@@ -61,10 +61,11 @@ namespace Barotrauma
public override void UpdateAnim(float deltaTime) public override void UpdateAnim(float deltaTime)
{ {
if (character.IsDead || Frozen || character.IsUnconscious || stunTimer > 0.0f) if (Frozen) return;
if (character.IsDead || character.IsUnconscious || stunTimer > 0.0f)
{ {
collider.PhysEnabled = false; collider.PhysEnabled = false;
collider.SetTransform(GetLimb(LimbType.Torso).SimPosition, 0.0f); collider.SetTransform(GetLimb(LimbType.Torso).SimPosition, 0.0f);
if (stunTimer > 0) if (stunTimer > 0)
@@ -97,9 +98,7 @@ namespace Barotrauma
0.0f); 0.0f);
collider.FarseerBody.Enabled = true; collider.FarseerBody.Enabled = true;
} }
if (swimming) if (swimming)
{ {
@@ -146,12 +145,6 @@ namespace Barotrauma
HandIK(rightHand, midPos); HandIK(rightHand, midPos);
HandIK(leftHand, midPos); HandIK(leftHand, midPos);
//rightHand.pullJoint.Enabled = true;
//rightHand.pullJoint.WorldAnchorB = midPos;
//rightHand.pullJoint.Enabled = true;
//rightHand.pullJoint.WorldAnchorB = midPos;
} }
else else
{ {
@@ -310,8 +303,6 @@ namespace Barotrauma
} }
} }
//if (LowestLimb == null) return;
if (onGround && (!character.IsRemotePlayer || GameMain.Server != null)) if (onGround && (!character.IsRemotePlayer || GameMain.Server != null))
{ {
collider.LinearVelocity = new Vector2( collider.LinearVelocity = new Vector2(
@@ -319,14 +310,6 @@ namespace Barotrauma
collider.LinearVelocity.Y > 0.0f ? collider.LinearVelocity.Y * 0.5f : collider.LinearVelocity.Y); collider.LinearVelocity.Y > 0.0f ? collider.LinearVelocity.Y * 0.5f : collider.LinearVelocity.Y);
} }
//float? ceilingY = null;
//if (Submarine.PickBody(head.SimPosition, head.SimPosition + Vector2.UnitY, null, Physics.CollisionWall)!=null)
//{
// ceilingY = Submarine.LastPickedPosition.Y;
// if (ceilingY - floorY < HeadPosition) Crouching = true;
//}
getUpSpeed = getUpSpeed * Math.Max(head.SimPosition.Y - colliderPos.Y, 0.5f); getUpSpeed = getUpSpeed * Math.Max(head.SimPosition.Y - colliderPos.Y, 0.5f);
torso.pullJoint.Enabled = true; torso.pullJoint.Enabled = true;
@@ -585,14 +568,7 @@ namespace Barotrauma
//pull head above water //pull head above water
head.body.SmoothRotate(0.0f, 5.0f); head.body.SmoothRotate(0.0f, 5.0f);
walkPos += 0.05f; walkPos += 0.05f;
//push feet downwards
//leftFoot.body.ApplyForce(leftFoot.Mass * new Vector2(0.0f, -50.0f));
// rightFoot.body.ApplyForce(rightFoot.Mass * new Vector2(0.0f, -50.0f));
} }
else else
{ {
@@ -98,6 +98,12 @@ namespace Barotrauma
private set; private set;
} }
public Limb MainLimb
{
get;
private set;
}
public Vector2 WorldPosition public Vector2 WorldPosition
{ {
get get
@@ -313,6 +319,11 @@ namespace Barotrauma
{ {
limb.sprite.Depth = startDepth + limb.sprite.Depth * 0.0001f; limb.sprite.Depth = startDepth + limb.sprite.Depth * 0.0001f;
} }
Limb torso = GetLimb(LimbType.Torso);
Limb head = GetLimb(LimbType.Head);
MainLimb = torso == null ? head : torso;
} }
public void AddJoint(XElement subElement, float scale = 1.0f) public void AddJoint(XElement subElement, float scale = 1.0f)
@@ -702,7 +713,7 @@ namespace Barotrauma
SetPosition(collider.SimPosition + moveAmount); SetPosition(collider.SimPosition + moveAmount);
character.CursorPosition += moveAmount; character.CursorPosition += moveAmount;
} }
private void UpdateCollisionCategories() private void UpdateCollisionCategories()
{ {
Category wall = currentHull == null ? Category wall = currentHull == null ?
@@ -732,11 +743,10 @@ namespace Barotrauma
public void Update(Camera cam, float deltaTime) public void Update(Camera cam, float deltaTime)
{ {
if (!character.Enabled) return; if (!character.Enabled || Frozen) return;
if (Frozen) return;
UpdateNetPlayerPosition(deltaTime); UpdateNetPlayerPosition(deltaTime);
CheckDistFromCollider();
Vector2 flowForce = Vector2.Zero; Vector2 flowForce = Vector2.Zero;
@@ -1025,25 +1035,30 @@ namespace Barotrauma
} }
} }
//public void SetRotation(float rotation)
//{
// float rotateAmount = rotation - refLimb.Rotation;
// Matrix rotationMatrix = Matrix.CreateRotationZ(rotateAmount); private bool collisionsDisabled;
// refLimb.body.SetTransform(refLimb.SimPosition, rotation); protected void CheckDistFromCollider()
{
float allowedDist = Math.Max(Math.Max(collider.radius, collider.width), collider.height);
// foreach (Limb limb in Limbs) //if the ragdoll is too far from the collider, disable collisions until it's close enough
// { //(in case the ragdoll has gotten stuck somewhere)
// if (limb == refLimb) continue; if (Vector2.Distance(collider.SimPosition, MainLimb.SimPosition) > allowedDist)
{
foreach (Limb limb in Limbs)
{
limb.body.CollidesWith = Physics.CollisionNone;
}
// Vector2 newPos = limb.SimPosition - refLimb.SimPosition; collisionsDisabled = true;
// newPos = Vector2.Transform(newPos, rotationMatrix); }
else if (collisionsDisabled)
// TrySetLimbPosition(limb, refLimb.SimPosition, refLimb.SimPosition + newPos); {
// limb.body.SetTransform(limb.SimPosition, limb.Rotation + rotateAmount); UpdateCollisionCategories();
// } collisionsDisabled = false;
//} }
}
float t = 0.0f; float t = 0.0f;
@@ -1068,41 +1083,41 @@ namespace Barotrauma
if (character.MemLocalPos.Count > 120) character.MemLocalPos.RemoveRange(0, character.MemLocalPos.Count - 120); if (character.MemLocalPos.Count > 120) character.MemLocalPos.RemoveRange(0, character.MemLocalPos.Count - 120);
character.MemPos.Clear(); character.MemPos.Clear();
return;
}
PosInfo prev = character.MemPos[0];
PosInfo next = character.MemPos[1];
Vector2 currPos = collider.SimPosition;
//interpolate the position of the collider from the first position in the buffer towards the second
if (prev.Timestamp < next.Timestamp)
{
//if there are more than 2 positions in the buffer,
//increase the interpolation speed to catch up with the server
float speedMultiplier = 1.0f + (float)Math.Pow((character.MemPos.Count - 2) / 2.0f, 2.0f);
t += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp);
currPos = Vector2.Lerp(prev.Position, next.Position, t);
//override the targetMovement to make the character play the walking/running animation
overrideTargetMovement = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp);
} }
else else
{ {
currPos = next.Position; PosInfo prev = character.MemPos[0];
t = 1.0f; PosInfo next = character.MemPos[1];
}
collider.SetTransform(currPos, collider.Rotation);
if (t >= 1.0f) Vector2 currPos = collider.SimPosition;
{
t = 0.0f; //interpolate the position of the collider from the first position in the buffer towards the second
character.MemPos.RemoveAt(0); if (prev.Timestamp < next.Timestamp)
} {
//if there are more than 2 positions in the buffer,
//increase the interpolation speed to catch up with the server
float speedMultiplier = 1.0f + (float)Math.Pow((character.MemPos.Count - 2) / 2.0f, 2.0f);
t += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp);
currPos = Vector2.Lerp(prev.Position, next.Position, t);
//override the targetMovement to make the character play the walking/running animation
overrideTargetMovement = (next.Position - prev.Position) / (next.Timestamp - prev.Timestamp);
}
else
{
currPos = next.Position;
t = 1.0f;
}
collider.SetTransform(currPos, collider.Rotation);
if (t >= 1.0f)
{
t = 0.0f;
character.MemPos.RemoveAt(0);
}
}
} }
public virtual Vector2 EstimateCurrPosition(Vector2 prevPosition, float timePassed) public virtual Vector2 EstimateCurrPosition(Vector2 prevPosition, float timePassed)
+1 -1
View File
@@ -475,7 +475,7 @@ namespace Barotrauma
public override Vector2 DrawPosition public override Vector2 DrawPosition
{ {
get { return AnimController.Collider.DrawPosition; } get { return AnimController.MainLimb.body.DrawPosition; }
} }
public delegate void OnDeathHandler(Character character, CauseOfDeath causeOfDeath); public delegate void OnDeathHandler(Character character, CauseOfDeath causeOfDeath);
-2
View File
@@ -341,8 +341,6 @@ namespace Barotrauma
} }
body.SetTransform(targetPosition, targetRotation == 0.0f ? body.Rotation : targetRotation); body.SetTransform(targetPosition, targetRotation == 0.0f ? body.Rotation : targetRotation);
//body.LinearVelocity = targetVelocity;
//body.AngularVelocity = targetAngularVelocity;
targetPosition = Vector2.Zero; targetPosition = Vector2.Zero;
} }