mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-06 17:58:05 +02:00
71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using System;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Physics;
|
|
|
|
namespace Robust.Shared.GameObjects;
|
|
|
|
public partial class SharedPhysicsSystem
|
|
{
|
|
public void SetLinearVelocity(PhysicsComponent body, Vector2 velocity)
|
|
{
|
|
if (body.BodyType == BodyType.Static ||
|
|
!body.CanCollide) return;
|
|
|
|
if (Vector2.Dot(velocity, velocity) > 0.0f)
|
|
body.Awake = true;
|
|
|
|
if (body._linearVelocity.EqualsApprox(velocity, 0.0001f))
|
|
return;
|
|
|
|
body._linearVelocity = velocity;
|
|
body.Dirty(EntityManager);
|
|
}
|
|
|
|
public Box2 GetWorldAABB(PhysicsComponent body, TransformComponent xform, EntityQuery<TransformComponent> xforms, EntityQuery<FixturesComponent> fixtures)
|
|
{
|
|
var (worldPos, worldRot) = xform.GetWorldPositionRotation(xforms);
|
|
|
|
var transform = new Transform(worldPos, (float) worldRot.Theta);
|
|
|
|
var bounds = new Box2(transform.Position, transform.Position);
|
|
|
|
foreach (var fixture in fixtures.GetComponent(body.Owner).Fixtures.Values)
|
|
{
|
|
for (var i = 0; i < fixture.Shape.ChildCount; i++)
|
|
{
|
|
var boundy = fixture.Shape.ComputeAABB(transform, i);
|
|
bounds = bounds.Union(boundy);
|
|
}
|
|
}
|
|
|
|
return bounds;
|
|
}
|
|
|
|
public Box2 GetHardAABB(PhysicsComponent body, TransformComponent? xform = null, FixturesComponent? fixtures = null)
|
|
{
|
|
if (!Resolve(body.Owner, ref xform, ref fixtures))
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
var (worldPos, worldRot) = xform.GetWorldPositionRotation();
|
|
|
|
var transform = new Transform(worldPos, (float) worldRot.Theta);
|
|
|
|
var bounds = new Box2(transform.Position, transform.Position);
|
|
|
|
foreach (var fixture in fixtures.Fixtures.Values)
|
|
{
|
|
if (!fixture.Hard) continue;
|
|
|
|
for (var i = 0; i < fixture.Shape.ChildCount; i++)
|
|
{
|
|
var boundy = fixture.Shape.ComputeAABB(transform, i);
|
|
bounds = bounds.Union(boundy);
|
|
}
|
|
}
|
|
|
|
return bounds;
|
|
}
|
|
}
|