Fix (or a workaround) to characters teleporting inside/through colliders when leaving a sub (see #552). Gaps do a raycast out from the sub to see if there are obstacles outside, and if so, ragdolls place a collider at the corresponding position inside the sub. The collider is simply a straight axis-aligned line atm, so it doesn't work accurately with sloped ice/submarine walls, but does prevent the ragdoll from ending up inside the obstacles. TODO: use a few edges to approximate the shape of the obstacles more closely?

This commit is contained in:
Joonas Rikkonen
2018-08-02 16:48:51 +03:00
parent 708f72c883
commit 9099b191d0
4 changed files with 146 additions and 15 deletions
+56 -1
View File
@@ -1,4 +1,5 @@
using Barotrauma.Items.Components;
using FarseerPhysics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
@@ -13,6 +14,8 @@ namespace Barotrauma
public static bool ShowGaps = true;
const float OutsideColliderRaycastInterval = 0.1f;
public readonly bool IsHorizontal;
//a value between 0.0f-1.0f (0.0 = closed, 1.0f = open)
@@ -26,12 +29,18 @@ namespace Barotrauma
private float lowerSurface;
private Vector2 lerpedFlowForce;
//if set to true, hull connections of this gap won't be updated when changes are being done to hulls
public bool DisableHullRechecks;
//can ambient light get through the gap even if it's not open
public bool PassAmbientLight;
//position of a collider outside the gap (for example an ice wall next to the sub)
//used by ragdolls to prevent them from ending up inside colliders when teleporting out of the sub
private Vector2? outsideColliderPos;
private Vector2? outsideColliderNormal;
private float outsideColliderRaycastTimer;
public float Open
{
@@ -191,6 +200,8 @@ namespace Barotrauma
{
flowForce = Vector2.Zero;
outsideColliderRaycastTimer -= deltaTime;
if (open == 0.0f || linkedTo.Count == 0)
{
lerpedFlowForce = Vector2.Zero;
@@ -505,7 +516,51 @@ namespace Barotrauma
hull1.LethalPressure += (Submarine != null && Submarine.AtDamageDepth) ? 100.0f * deltaTime : 10.0f * deltaTime;
}
}
}
public bool GetOutsideCollider(out Vector2? simPosition, out Vector2? normal)
{
simPosition = null;
normal = null;
if (IsRoomToRoom || Submarine == null || open <= 0.0f) return false;
if (outsideColliderRaycastTimer <= 0.0f)
{
UpdateOutsideColliderPos((Hull)linkedTo[0]);
outsideColliderRaycastTimer = OutsideColliderRaycastInterval;
}
simPosition = outsideColliderPos;
normal = outsideColliderNormal;
return simPosition != null;
}
private void UpdateOutsideColliderPos(Hull hull)
{
outsideColliderNormal = null;
outsideColliderPos = null;
if (Submarine == null) return;
Vector2 rayDir;
if (IsHorizontal)
{
rayDir = new Vector2(Math.Sign(rect.Center.X - hull.Rect.Center.X), 0);
}
else
{
rayDir = new Vector2(0, Math.Sign((rect.Y - rect.Height / 2) - (hull.Rect.Y - hull.Rect.Height / 2)));
}
Vector2 rayStart = ConvertUnits.ToSimUnits(WorldPosition);
Vector2 rayEnd = rayStart + rayDir * 500.0f;
if (Submarine.CheckVisibility(rayStart, rayEnd) != null)
{
outsideColliderNormal = -rayDir;
outsideColliderPos = Submarine.LastPickedPosition;
}
}
private void UpdateOxygen()