Files
RobustToolbox/Robust.Shared/Physics/Dynamics/Shapes/EdgeShape.cs
metalgearsloth c17c8d7a11 Physics (#1605)
* Physics worlds

* Paul's a good boy

* Build working

* Ingame and not lagging to hell

* Why didn't you commit ahhhhh

* Hard collisions working

* Solver parity

* Decent broadphase work done

* BroadPhase outline done

* BroadPhase working

* waiting for pvs

* Fix static PVS AABB

* Stop static bodies from awakening

* Optimise a bunch of stuff

* Even more broadphase stuff

* I'm fucking stupid

* Optimise fixture updates

* Collision solver start

* Building

* A is for Argumentative

* Fix contact caching island flags

* Circle shapes actually workeded

* Damping

* DS2 consumables only

* Slightly more stable

* Even slightlier more stablier

* VV your heart out

* Initial joint support

* 90% of joints I just wanted to push as I'd scream if I lost progress

* JOINT PURGATORY

* Joints barely functional lmao

* Okay these joints slightly more functional

* Remove station FrictionJoint

* Also that

* Some Box2D ports

* Cleanup mass

* Edge shape

* Active contacts

* Fix active contacts

* Optimise active contacts even more

* Boxes be stacking

* I would die for smug oh my fucking god

* In which everything is fixed

* Distance joints working LETS GO

* Remove frequency on distancejoint

* Fix some stuff and break joints

* Crashing fixed mehbeh

* ICollideSpecial and more resilience

* auto-clear

* showbb vera

* Slap that TODO in there

* Fix restartround crash

* Random fixes

* Fix fixture networking

* Add intersection method for broadphase

* Fix contacts

* Licenses done

* Optimisations

* Fix wall clips

* Config caching for island

* allocations optimisations

* Optimise casts

* Optimise events queue for physics

* Contact manager optimisations

* Optimise controllers

* Sloth joint or something idk

* Controller graph

* Remove content cvar

* Random cleanup

* Finally remove VirtualController

* Manifold structs again

* Optimise this absolute retardation

* Optimise

* fix license

* Cleanup physics interface

* AHHHHHHHHHHHHH

* Fix collisions again

* snivybus

* Fix potential nasty manifold bug

* Tests go snivy

* Disable prediction for now

* Spans

* Fix ShapeTypes

* fixes

* ch ch changeesss

* Kinematic idea

* Prevent static bodies from waking

* Pass WorldAABB to MoveEvent

* Fix collisions

* manifold structs fucking WOOORRKKKINNGGG

* Better pushing

* Fix merge ickies

* Optimise MoveEvents

* Use event for collisions performance

* Fix content tests

* Do not research tests

* Fix most conflicts

* Paul's trying to kill me

* Maybe collisions work idk

* Make us whole again

* Smug is also trying to kill me

* nani

* shitty collisions

* Settling

* Do not research collisions

* SHIP IT

* Fix joints

* PVS moment

* Fix other assert

* Fix locker collisions

* serializable sleeptime

* Aether2D contacts

* Physics is no longer crashing (and burning)

* Add to the TODO list

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-08 03:19:01 +11:00

152 lines
4.9 KiB
C#

// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Robust.Shared.Physics.Dynamics.Shapes
{
[Serializable, NetSerializable]
[DataDefinition]
public sealed class EdgeShape : IPhysShape
{
/// <summary>
/// Edge start vertex
/// </summary>
internal Vector2 Vertex1;
/// <summary>
/// Edge end vertex
/// </summary>
internal Vector2 Vertex2;
/// <summary>
/// Is true if the edge is connected to an adjacent vertex before vertex 1.
/// </summary>
public bool HasVertex0 { get; set; }
/// <summary>
/// Is true if the edge is connected to an adjacent vertex after vertex2.
/// </summary>
public bool HasVertex3 { get; set; }
/// <summary>
/// Optional adjacent vertices. These are used for smooth collision.
/// </summary>
public Vector2 Vertex0 { get; set; }
/// <summary>
/// Optional adjacent vertices. These are used for smooth collision.
/// </summary>
public Vector2 Vertex3 { get; set; }
public int ChildCount => 1;
public bool OneSided => !(HasVertex0 && HasVertex3);
public float Radius
{
get => _radius;
set
{
if (MathHelper.CloseTo(_radius, value)) return;
_radius = value;
}
}
private float _radius;
public ShapeType ShapeType => ShapeType.Edge;
/// <summary>
/// Create a 1-sided edge.
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
public EdgeShape(Vector2 start, Vector2 end)
{
Set(start, end);
_radius = IoCManager.Resolve<IConfigurationManager>().GetCVar(CVars.PolygonRadius);
}
/// <summary>
/// Set this as an isolated edge.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
public void Set(Vector2 start, Vector2 end)
{
Vertex1 = start;
Vertex2 = end;
HasVertex0 = false;
HasVertex3 = false;
// TODO ComputeProperties();
}
public bool Equals(IPhysShape? other)
{
if (other is not EdgeShape edge) return false;
return (HasVertex0 == edge.HasVertex0 &&
HasVertex3 == edge.HasVertex3 &&
Vertex0 == edge.Vertex0 &&
Vertex1 == edge.Vertex1 &&
Vertex2 == edge.Vertex2 &&
Vertex3 == edge.Vertex3);
}
public Box2 CalculateLocalBounds(Angle rotation)
{
Vector2 lower = Vector2.ComponentMin(Vertex1, Vertex2);
Vector2 upper = Vector2.ComponentMax(Vertex1, Vertex2);
Vector2 r = new Vector2(Radius, Radius);
var aabb = new Box2
{
BottomLeft = lower - r,
TopRight = upper + r
};
return aabb;
}
public void ApplyState()
{
return;
}
public void DebugDraw(DebugDrawingHandle handle, in Matrix3 modelMatrix, in Box2 worldViewport, float sleepPercent)
{
var m = Matrix3.Identity;
m.R0C2 = modelMatrix.R0C2;
m.R1C2 = modelMatrix.R1C2;
handle.SetTransform(m);
handle.DrawLine(Vertex1, Vertex2, handle.CalcWakeColor(handle.RectFillColor, sleepPercent));
handle.SetTransform(Matrix3.Identity);
}
}
}