Physics (#3485)
* Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab * Fix dupe pulling * Zumzum's based fix * Don't run tile friction for non-predicted bodies * Experimental pulling improvement * Everything's a poly now * Optimise AI region debugging a bit Could still be better but should improve default performance a LOT * Mover no updater * Crazy kinematic body idea * Good collisions * KinematicController * Fix aghost * Throwing refactor * Pushing cleanup * Fix throwing and footstep sounds * Frametime in ICollideBehavior * Fix stuff * Actually fix weightlessness * Optimise collision behaviors a lot * Make open lockers still collide with walls * powwweeerrrrr * Merge master proper * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * Ch ch ch changesss * SHIP IT * Fix #if DEBUG * Fix vaulting and item locker collision * Fix throwing * Editing yaml by hand what can go wrong * on * Last yaml fixes * Okay now it's fixed * Linter Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com> Co-authored-by: Vera Aguilera Puerto <zddm@outlook.es>
This commit is contained in:
118
Content.Server/Physics/Controllers/ConveyorController.cs
Normal file
118
Content.Server/Physics/Controllers/ConveyorController.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Conveyor;
|
||||
using Content.Server.GameObjects.Components.Recycling;
|
||||
using Content.Shared.GameObjects.Components.Movement;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Controllers;
|
||||
|
||||
namespace Content.Server.Physics.Controllers
|
||||
{
|
||||
internal sealed class ConveyorController : VirtualController
|
||||
{
|
||||
public override List<Type> UpdatesAfter => new() {typeof(MoverController)};
|
||||
|
||||
public override void UpdateBeforeSolve(bool prediction, float frameTime)
|
||||
{
|
||||
base.UpdateBeforeSolve(prediction, frameTime);
|
||||
foreach (var comp in ComponentManager.EntityQuery<ConveyorComponent>())
|
||||
{
|
||||
Convey(comp, frameTime);
|
||||
}
|
||||
|
||||
// TODO: Uhh you can probably wrap the recycler's conveying properties into... conveyor
|
||||
foreach (var comp in ComponentManager.EntityQuery<RecyclerComponent>())
|
||||
{
|
||||
ConveyRecycler(comp, frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void Convey(ConveyorComponent comp, float frameTime)
|
||||
{
|
||||
// TODO: Use ICollideBehavior and cache intersecting
|
||||
// Use an event for conveyors to know what needs to run
|
||||
if (!comp.CanRun())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var intersecting = EntityManager.GetEntitiesIntersecting(comp.Owner, true);
|
||||
var direction = comp.GetAngle().ToVec();
|
||||
Vector2? ownerPos = null;
|
||||
|
||||
foreach (var entity in intersecting)
|
||||
{
|
||||
if (!comp.CanMove(entity)) continue;
|
||||
|
||||
if (!entity.TryGetComponent(out IPhysBody? physics) || physics.BodyStatus == BodyStatus.InAir ||
|
||||
entity.IsWeightless()) continue;
|
||||
|
||||
ownerPos ??= comp.Owner.Transform.WorldPosition;
|
||||
var itemRelativeToConveyor = entity.Transform.WorldPosition - ownerPos.Value;
|
||||
|
||||
physics.LinearVelocity += Convey(direction * comp.Speed, frameTime, itemRelativeToConveyor);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Uhhh I did a shit job plz fix smug
|
||||
private Vector2 Convey(Vector2 velocityDirection, float frameTime, Vector2 itemRelativeToConveyor)
|
||||
{
|
||||
//gravitating item towards center
|
||||
//http://csharphelper.com/blog/2016/09/find-the-shortest-distance-between-a-point-and-a-line-segment-in-c/
|
||||
Vector2 centerPoint;
|
||||
|
||||
var t = 0f;
|
||||
if (velocityDirection.Length > 0) // if velocitydirection is 0, this calculation will divide by 0
|
||||
{
|
||||
t = Vector2.Dot(itemRelativeToConveyor, velocityDirection) /
|
||||
Vector2.Dot(velocityDirection, velocityDirection);
|
||||
}
|
||||
|
||||
if (t < 0)
|
||||
{
|
||||
centerPoint = new Vector2();
|
||||
}
|
||||
else if (t > 1)
|
||||
{
|
||||
centerPoint = velocityDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
centerPoint = velocityDirection * t;
|
||||
}
|
||||
|
||||
var delta = centerPoint - itemRelativeToConveyor;
|
||||
return delta * (400 * delta.Length) * frameTime;
|
||||
}
|
||||
|
||||
private void ConveyRecycler(RecyclerComponent comp, float frameTime)
|
||||
{
|
||||
if (!comp.CanRun())
|
||||
{
|
||||
comp.Intersecting.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var direction = Vector2.UnitX;
|
||||
Vector2? ownerPos = null;
|
||||
|
||||
for (var i = comp.Intersecting.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var entity = comp.Intersecting[i];
|
||||
|
||||
if (entity.Deleted || !comp.CanMove(entity) || !EntityManager.IsIntersecting(comp.Owner, entity))
|
||||
{
|
||||
comp.Intersecting.RemoveAt(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entity.TryGetComponent(out IPhysBody? physics)) continue;
|
||||
ownerPos ??= comp.Owner.Transform.WorldPosition;
|
||||
physics.LinearVelocity += Convey(direction, frameTime, entity.Transform.WorldPosition - ownerPos.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
198
Content.Server/Physics/Controllers/MoverController.cs
Normal file
198
Content.Server/Physics/Controllers/MoverController.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.GameObjects.Components.Sound;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.GameObjects.Components.Inventory;
|
||||
using Content.Shared.GameObjects.Components.Movement;
|
||||
using Content.Shared.GameObjects.Components.Tag;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Physics.Controllers;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Physics.Controllers
|
||||
{
|
||||
public class MoverController : SharedMoverController
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
private AudioSystem _audioSystem = default!;
|
||||
|
||||
private const float StepSoundMoveDistanceRunning = 2;
|
||||
private const float StepSoundMoveDistanceWalking = 1.5f;
|
||||
|
||||
private HashSet<EntityUid> _excludedMobs = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_audioSystem = EntitySystem.Get<AudioSystem>();
|
||||
}
|
||||
|
||||
public override void UpdateBeforeSolve(bool prediction, float frameTime)
|
||||
{
|
||||
base.UpdateBeforeSolve(prediction, frameTime);
|
||||
_excludedMobs.Clear();
|
||||
|
||||
foreach (var (mobMover, mover, physics) in ComponentManager.EntityQuery<IMobMoverComponent, IMoverComponent, PhysicsComponent>())
|
||||
{
|
||||
_excludedMobs.Add(mover.Owner.Uid);
|
||||
HandleMobMovement(mover, physics, mobMover);
|
||||
}
|
||||
|
||||
foreach (var mover in ComponentManager.EntityQuery<ShuttleControllerComponent>())
|
||||
{
|
||||
_excludedMobs.Add(mover.Owner.Uid);
|
||||
HandleShuttleMovement(mover);
|
||||
}
|
||||
|
||||
foreach (var (mover, physics) in ComponentManager.EntityQuery<IMoverComponent, PhysicsComponent>(true))
|
||||
{
|
||||
if (_excludedMobs.Contains(mover.Owner.Uid)) continue;
|
||||
|
||||
HandleKinematicMovement(mover, physics);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Some thoughts:
|
||||
* Unreal actually doesn't predict vehicle movement at all, it's purely server-side which I thought was interesting
|
||||
* The reason for this is that vehicles change direction very slowly compared to players so you don't really have the requirement for quick movement anyway
|
||||
* As such could probably just look at applying a force / impulse to the shuttle server-side only so it controls like the titanic.
|
||||
*/
|
||||
private void HandleShuttleMovement(ShuttleControllerComponent mover)
|
||||
{
|
||||
var gridId = mover.Owner.Transform.GridID;
|
||||
|
||||
if (!_mapManager.TryGetGrid(gridId, out var grid) || !EntityManager.TryGetEntity(grid.GridEntityId, out var gridEntity)) return;
|
||||
|
||||
//TODO: Switch to shuttle component
|
||||
if (!gridEntity.TryGetComponent(out PhysicsComponent? physics))
|
||||
{
|
||||
physics = gridEntity.AddComponent<PhysicsComponent>();
|
||||
physics.BodyStatus = BodyStatus.InAir;
|
||||
physics.Mass = 1;
|
||||
physics.CanCollide = true;
|
||||
physics.AddFixture(new Fixture(physics, new PhysShapeGrid(grid)));
|
||||
}
|
||||
|
||||
// TODO: Uhh this probably doesn't work but I still need to rip out the entity tree and make RenderingTreeSystem use grids so I'm not overly concerned about breaking shuttles.
|
||||
physics.ApplyForce(mover.VelocityDir.walking + mover.VelocityDir.sprinting);
|
||||
mover.VelocityDir = (Vector2.Zero, Vector2.Zero);
|
||||
}
|
||||
|
||||
protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover)
|
||||
{
|
||||
var transform = mover.Owner.Transform;
|
||||
// Handle footsteps.
|
||||
if (_mapManager.GridExists(mobMover.LastPosition.GetGridId(EntityManager)))
|
||||
{
|
||||
// Can happen when teleporting between grids.
|
||||
if (!transform.Coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance))
|
||||
{
|
||||
mobMover.LastPosition = transform.Coordinates;
|
||||
return;
|
||||
}
|
||||
|
||||
mobMover.StepSoundDistance += distance;
|
||||
}
|
||||
|
||||
mobMover.LastPosition = transform.Coordinates;
|
||||
float distanceNeeded;
|
||||
if (mover.Sprinting)
|
||||
{
|
||||
distanceNeeded = StepSoundMoveDistanceRunning;
|
||||
}
|
||||
else
|
||||
{
|
||||
distanceNeeded = StepSoundMoveDistanceWalking;
|
||||
}
|
||||
|
||||
if (mobMover.StepSoundDistance > distanceNeeded)
|
||||
{
|
||||
mobMover.StepSoundDistance = 0;
|
||||
|
||||
if (!mover.Owner.HasTag("FootstepSound") || mover.Owner.Transform.GridID == GridId.Invalid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
|
||||
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
|
||||
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
|
||||
{
|
||||
modifier.PlayFootstep();
|
||||
}
|
||||
else
|
||||
{
|
||||
PlayFootstepSound(mover.Owner, mover.Sprinting);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayFootstepSound(IEntity mover, bool sprinting)
|
||||
{
|
||||
var coordinates = mover.Transform.Coordinates;
|
||||
// Step one: figure out sound collection prototype.
|
||||
var grid = _mapManager.GetGrid(coordinates.GetGridId(EntityManager));
|
||||
var tile = grid.GetTileRef(coordinates);
|
||||
|
||||
// If the coordinates have a FootstepModifier component
|
||||
// i.e. component that emit sound on footsteps emit that sound
|
||||
string? soundCollectionName = null;
|
||||
foreach (var maybeFootstep in grid.GetSnapGridCell(tile.GridIndices, SnapGridOffset.Center))
|
||||
{
|
||||
if (maybeFootstep.Owner.TryGetComponent(out FootstepModifierComponent? footstep))
|
||||
{
|
||||
soundCollectionName = footstep._soundCollectionName;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if there is no FootstepModifierComponent, determine sound based on tiles
|
||||
if (soundCollectionName == null)
|
||||
{
|
||||
// Walking on a tile.
|
||||
var def = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
|
||||
if (def.FootstepSounds == null)
|
||||
{
|
||||
// Nothing to play, oh well.
|
||||
return;
|
||||
}
|
||||
|
||||
soundCollectionName = def.FootstepSounds;
|
||||
}
|
||||
|
||||
// Ok well we know the position of the
|
||||
try
|
||||
{
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
|
||||
var file = _robustRandom.Pick(soundCollection.PickFiles);
|
||||
_audioSystem.PlayAtCoords(file, coordinates, sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
|
||||
}
|
||||
catch (UnknownPrototypeException)
|
||||
{
|
||||
// Shouldn't crash over a sound
|
||||
Logger.ErrorS("sound", $"Unable to find sound collection for {soundCollectionName}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
106
Content.Server/Physics/Controllers/SingularityController.cs
Normal file
106
Content.Server/Physics/Controllers/SingularityController.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.Observer;
|
||||
using Content.Server.GameObjects.Components.Singularity;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Broadphase;
|
||||
using Robust.Shared.Physics.Controllers;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Physics.Controllers
|
||||
{
|
||||
internal sealed class SingularityController : VirtualController
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
private float _pullAccumulator;
|
||||
private float _moveAccumulator;
|
||||
|
||||
public override void UpdateBeforeSolve(bool prediction, float frameTime)
|
||||
{
|
||||
base.UpdateBeforeSolve(prediction, frameTime);
|
||||
|
||||
_moveAccumulator += frameTime;
|
||||
_pullAccumulator += frameTime;
|
||||
|
||||
while (_pullAccumulator > 0.5f)
|
||||
{
|
||||
_pullAccumulator -= 0.5f;
|
||||
|
||||
foreach (var singularity in ComponentManager.EntityQuery<SingularityComponent>())
|
||||
{
|
||||
// TODO: Use colliders instead probably yada yada
|
||||
PullEntities(singularity);
|
||||
// Yeah look the collision with station wasn't working and I'm 15k lines in and not debugging this shit
|
||||
DestroyTiles(singularity);
|
||||
}
|
||||
}
|
||||
|
||||
while (_moveAccumulator > 1.0f)
|
||||
{
|
||||
_moveAccumulator -= 1.0f;
|
||||
|
||||
foreach (var (singularity, physics) in ComponentManager.EntityQuery<SingularityComponent, PhysicsComponent>())
|
||||
{
|
||||
if (singularity.Owner.HasComponent<BasicActorComponent>()) continue;
|
||||
|
||||
// TODO: Need to essentially use a push vector in a random direction for us PLUS
|
||||
// Any entity colliding with our larger circlebox needs to have an impulse applied to itself.
|
||||
physics.BodyStatus = BodyStatus.InAir;
|
||||
MoveSingulo(singularity, physics);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveSingulo(SingularityComponent singularity, PhysicsComponent physics)
|
||||
{
|
||||
if (singularity.Level <= 1) return;
|
||||
// TODO: Could try gradual changes instead but for now just try to replicate
|
||||
|
||||
var pushVector = new Vector2(_robustRandom.Next(-10, 10), _robustRandom.Next(-10, 10));
|
||||
|
||||
if (pushVector == Vector2.Zero) return;
|
||||
|
||||
physics.LinearVelocity = Vector2.Zero;
|
||||
physics.LinearVelocity = pushVector.Normalized * 2;
|
||||
}
|
||||
|
||||
private void PullEntities(SingularityComponent component)
|
||||
{
|
||||
var singularityCoords = component.Owner.Transform.Coordinates;
|
||||
// TODO: Maybe if we have named fixtures needs to pull out the outer circle collider (inner will be for deleting).
|
||||
var entitiesToPull = EntityManager.GetEntitiesInRange(singularityCoords, component.Level * 10);
|
||||
foreach (var entity in entitiesToPull)
|
||||
{
|
||||
if (!entity.TryGetComponent<PhysicsComponent>(out var collidableComponent) || collidableComponent.BodyType == BodyType.Static) continue;
|
||||
if (entity.HasComponent<GhostComponent>()) continue;
|
||||
if (singularityCoords.EntityId != entity.Transform.Coordinates.EntityId) continue;
|
||||
var vec = (singularityCoords - entity.Transform.Coordinates).Position;
|
||||
if (vec == Vector2.Zero) continue;
|
||||
|
||||
var speed = 10 / vec.Length * component.Level;
|
||||
|
||||
collidableComponent.ApplyLinearImpulse(vec.Normalized * speed);
|
||||
}
|
||||
}
|
||||
|
||||
private void DestroyTiles(SingularityComponent component)
|
||||
{
|
||||
if (!component.Owner.TryGetComponent(out PhysicsComponent? physicsComponent)) return;
|
||||
var worldBox = physicsComponent.GetWorldAABB();
|
||||
|
||||
foreach (var grid in _mapManager.FindGridsIntersecting(component.Owner.Transform.MapID, worldBox))
|
||||
{
|
||||
foreach (var tile in grid.GetTilesIntersecting(worldBox))
|
||||
{
|
||||
grid.SetTile(tile.GridIndices, Tile.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user