-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mob collisions #34580
Open
metalgearsloth
wants to merge
18
commits into
space-wizards:master
Choose a base branch
from
metalgearsloth:2025-01-23-mob-collisions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+421
−15
Open
Mob collisions #34580
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
784fb3e
Mob collisions
metalgearsloth 6a33f4e
Merge remote-tracking branch 'upstream/master' into 2025-01-23-mob-co…
metalgearsloth 849d685
impulses
metalgearsloth 57bbb05
Collision smoothing
metalgearsloth dae893a
Locked in
metalgearsloth 6465b2d
30tps working
metalgearsloth 4da36ba
r
metalgearsloth 4b20683
fixes
metalgearsloth 6a382fe
Best
metalgearsloth b73e0a2
Fixes + CVars
metalgearsloth 56c0fc3
CVars in place
metalgearsloth 7c68886
Pushies
metalgearsloth 5ccd72d
Opt attempt 1
metalgearsloth f06a00d
Revert "Opt attempt 1"
metalgearsloth 7884d27
Fix mispredicts
metalgearsloth f32607e
Ready-ish
metalgearsloth 2b8ce10
better
metalgearsloth cb267d7
Cleanup
metalgearsloth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Numerics; | ||
using Content.Shared.Movement.Components; | ||
using Content.Shared.Movement.Systems; | ||
using Robust.Client.Player; | ||
using Robust.Shared.Physics; | ||
using Robust.Shared.Physics.Components; | ||
using Robust.Shared.Physics.Systems; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Client.Movement.Systems; | ||
|
||
public sealed class MobCollisionSystem : SharedMobCollisionSystem | ||
{ | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly IPlayerManager _player = default!; | ||
[Dependency] private readonly SharedPhysicsSystem _physics = default!; | ||
|
||
public override void Update(float frameTime) | ||
{ | ||
base.Update(frameTime); | ||
|
||
if (!_timing.IsFirstTimePredicted) | ||
return; | ||
|
||
var player = _player.LocalEntity; | ||
|
||
if (!HasComp<MobCollisionComponent>(player) || !TryComp(player, out PhysicsComponent? physics)) | ||
return; | ||
|
||
if (physics.ContactCount == 0) | ||
return; | ||
|
||
var ourTransform = _physics.GetPhysicsTransform(player.Value); | ||
var contacts = _physics.GetContacts(player.Value); | ||
var direction = Vector2.Zero; | ||
|
||
while (contacts.MoveNext(out var contact)) | ||
{ | ||
if (!contact.IsTouching) | ||
continue; | ||
|
||
var other = contact.OtherEnt(player.Value); | ||
|
||
if (!HasComp<MobCollisionComponent>(other)) | ||
continue; | ||
|
||
// TODO: Get overlap amount | ||
var otherTransform = _physics.GetRelativePhysicsTransform(ourTransform, other); | ||
|
||
var diff = otherTransform.Position; | ||
|
||
var penDepth = MathF.Max(0f, 0.7f - diff.LengthSquared()); | ||
|
||
// Need the push strength proportional to penetration depth. | ||
direction += penDepth * diff.Normalized() * 1f * frameTime; | ||
} | ||
|
||
if (direction == Vector2.Zero) | ||
return; | ||
|
||
RaisePredictiveEvent(new MobCollisionMessage() | ||
{ | ||
Direction = direction, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Content.Shared.Movement.Systems; | ||
|
||
namespace Content.Server.Movement.Systems; | ||
|
||
public sealed class MobCollisionSystem : SharedMobCollisionSystem | ||
{ | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
Content.Shared/Movement/Components/MobCollisionComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Robust.Shared.GameStates; | ||
using Robust.Shared.Physics.Collision.Shapes; | ||
|
||
namespace Content.Shared.Movement.Components; | ||
|
||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] | ||
public sealed partial class MobCollisionComponent : Component | ||
{ | ||
/// <summary> | ||
/// Shape to give this entity for mob collisions. | ||
/// </summary> | ||
[DataField, AutoNetworkedField] | ||
public IPhysShape Shape = new PhysShapeCircle(radius: 0.35f); | ||
} |
48 changes: 48 additions & 0 deletions
48
Content.Shared/Movement/Systems/SharedMobCollisionSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Numerics; | ||
using Content.Shared.Movement.Components; | ||
using Content.Shared.Physics; | ||
using Robust.Shared.Physics.Systems; | ||
using Robust.Shared.Serialization; | ||
|
||
namespace Content.Shared.Movement.Systems; | ||
|
||
public abstract class SharedMobCollisionSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly FixtureSystem _fixtures = default!; | ||
[Dependency] private readonly SharedPhysicsSystem _physics = default!; | ||
[Dependency] private readonly SharedTransformSystem _xformSystem = default!; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
SubscribeAllEvent<MobCollisionMessage>(OnCollision); | ||
SubscribeLocalEvent<MobCollisionComponent, ComponentStartup>(OnCollisionStartup); | ||
} | ||
|
||
private void OnCollisionStartup(Entity<MobCollisionComponent> ent, ref ComponentStartup args) | ||
{ | ||
_fixtures.TryCreateFixture(ent.Owner, | ||
ent.Comp.Shape, | ||
"mob_collision", | ||
hard: false, | ||
collisionLayer: (int) CollisionGroup.MidImpassable, | ||
collisionMask: (int) CollisionGroup.MidImpassable); | ||
} | ||
|
||
private void OnCollision(MobCollisionMessage msg, EntitySessionEventArgs args) | ||
{ | ||
var player = args.SenderSession.AttachedEntity; | ||
|
||
if (!HasComp<MobCollisionComponent>(player) || !TryComp(player.Value, out TransformComponent? xform)) | ||
return; | ||
|
||
// TODO: Validation | ||
_xformSystem.SetLocalPosition(player.Value, xform.LocalPosition + msg.Direction); | ||
} | ||
|
||
[Serializable, NetSerializable] | ||
protected sealed class MobCollisionMessage : EntityEventArgs | ||
{ | ||
public Vector2 Direction; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should layer/mask be configurable on the component, so flying mobs could ignore ground mobs but still collide with other flying mobs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up using flammable as it's supposed to be a generic non-hard event subscriber though still not entirely happy with it.
If I ever get around to my planned collision layer refactor I might revisit it or if we have mobs that don't have flammable that also need pushing.