From 269d30b0db52e8e2cc06ae87ef82b206c6f82fbd Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 24 May 2020 21:55:49 +0200 Subject: [PATCH] Add support for connected bodies Added center of mass Removed delta velocities --- GamecraftModdingAPI/Blocks/BlockEngine.cs | 16 +++++++ GamecraftModdingAPI/Blocks/RemovalEngine.cs | 11 ----- GamecraftModdingAPI/Players/PlayerEngine.cs | 15 ------- GamecraftModdingAPI/SimBody.cs | 42 ++++++++++--------- .../Tests/GamecraftModdingAPIPluginTest.cs | 12 ++++-- 5 files changed, 47 insertions(+), 49 deletions(-) diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index eecebc2..eb462f9 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -4,6 +4,8 @@ using Gamecraft.Wires; using RobocraftX.Blocks; using RobocraftX.Common; using RobocraftX.GUI.Hotbar.Colours; +using RobocraftX.Physics; +using RobocraftX.Scene.Simulation; using Svelto.DataStructures; using Svelto.ECS; @@ -126,6 +128,20 @@ namespace GamecraftModdingAPI.Blocks return ret.ToArray(); } + public SimBody[] GetConnectedSimBodies(uint id) + { + var joints = entitiesDB.QueryEntities(MachineSimulationGroups.JOINTS_GROUP); + var list = new FasterList(4); + foreach (var joint in joints) + { + if (joint.jointState == JointState.Broken) continue; + if (joint.connectedEntityA == id) list.Add(new SimBody(joint.connectedEntityB)); + else if (joint.connectedEntityB == id) list.Add(new SimBody(joint.connectedEntityA)); + } + + return list.ToArray(); + } + #if DEBUG public EntitiesDB GetEntitiesDB() { diff --git a/GamecraftModdingAPI/Blocks/RemovalEngine.cs b/GamecraftModdingAPI/Blocks/RemovalEngine.cs index 05ad7c9..2aadaaa 100644 --- a/GamecraftModdingAPI/Blocks/RemovalEngine.cs +++ b/GamecraftModdingAPI/Blocks/RemovalEngine.cs @@ -28,17 +28,6 @@ namespace GamecraftModdingAPI.Blocks public void Ready() { - /*CommandManager.AddCommand(new SimpleCustomCommandEngine(() => - { - var block = BlockUtility.GetBlockLookedAt(LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB), entitiesDB); - if (block.HasValue) - { - RemoveBlock(block.Value); - Log.Output("Removed block."); - } - else - Log.Output("No block found where you're looking at."); - }, "removeCube", "Removes the cube you're looking at."));*/ } public EntitiesDB entitiesDB { get; set; } diff --git a/GamecraftModdingAPI/Players/PlayerEngine.cs b/GamecraftModdingAPI/Players/PlayerEngine.cs index d2c8fa3..1fa1296 100644 --- a/GamecraftModdingAPI/Players/PlayerEngine.cs +++ b/GamecraftModdingAPI/Players/PlayerEngine.cs @@ -249,21 +249,6 @@ namespace GamecraftModdingAPI.Players : maxDistance; if (rayCast.hit && rayCast.distance <= distance) return rayCast.hitEgid; - /*if (rayCast.hit) - { - *Logging.MetaDebugLog("RayCast EGID: " + rayCast.hitEgid); - var d = AccessTools.Field(typeof(ExclusiveGroup), "_knownGroups").GetValue(null) as - Dictionary; - foreach (var groupStruct in d) - { - if (groupStruct.Value == rayCast.hitEgid.groupID) - { - Logging.MetaDebugLog("Group name: " + groupStruct.Key); - break; //SIMULATION_BODIES_GROUP - } - }* - //Logging.MetaDebugLog("Position: " + Block.GetBlockPositionTest(rayCast.hitEgid)); - }*/ return null; } diff --git a/GamecraftModdingAPI/SimBody.cs b/GamecraftModdingAPI/SimBody.cs index f38df01..d9ba0c2 100644 --- a/GamecraftModdingAPI/SimBody.cs +++ b/GamecraftModdingAPI/SimBody.cs @@ -1,5 +1,4 @@ -using Gamecraft.Wires; -using RobocraftX.Common; +using RobocraftX.Common; using RobocraftX.Physics; using Svelto.ECS; using Unity.Mathematics; @@ -23,6 +22,10 @@ namespace GamecraftModdingAPI { } + /// + /// The position of this body. When setting the position, update the position of the connected bodies as well, + /// otherwise unexpected forces may arise. + /// public float3 Position { get => GetStruct().position; @@ -39,19 +42,7 @@ namespace GamecraftModdingAPI { get => GetStruct().angularVelocity; set => GetStruct().angularVelocity = value; - } - - public float3 DeltaVelocity - { - get => GetStruct().deltaVelocity; - set => GetStruct().deltaVelocity = value; - } - - public float3 DeltaAngularVelocity - { - get => GetStruct().deltaAngularVelocity; - set => GetStruct().deltaAngularVelocity = value; - } + } //Delta versions are used internally, can't be set or get public float3 Rotation { @@ -68,14 +59,27 @@ namespace GamecraftModdingAPI public float Mass { get => math.rcp(GetStruct().physicsMass.InverseMass); - set => GetStruct().physicsMass.InverseMass = math.rcp(value); + //set => GetStruct().physicsMass.InverseMass = math.rcp(value); + } + + public float3 CenterOfMass + { + get => GetStruct().physicsMass.CenterOfMass; + //set => GetStruct().physicsMass.CenterOfMass = value; } /// - /// Whether the body can be moved or static + /// Whether the body can be moved or static. /// - public bool Static => Block.BlockEngine.GetBlockInfo(Id).isStatic; - //Setting it doesn't have any effect + public bool Static => Block.BlockEngine.GetBlockInfo(Id).isStatic; //Setting it doesn't have any effect + + /// + /// The rigid bodies connected to this one via functional joints (broken ones don't count). + /// + public SimBody[] GetConnectedBodies() + { + return Block.BlockEngine.GetConnectedSimBodies(Id.entityID); + } private ref RigidBodyEntityStruct GetStruct() { diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 1342eae..783a33d 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -216,6 +216,11 @@ namespace GamecraftModdingAPI.Tests { Logging.CommandLog("SimBody: " + body); body.Position += new float3(0, 10, 0); + foreach (var bodyConnectedBody in body.GetConnectedBodies()) + { + Logging.CommandLog("Moving " + bodyConnectedBody); + bodyConnectedBody.Position += new float3(0, 10, 0); + } } }).Build(); @@ -296,9 +301,8 @@ namespace GamecraftModdingAPI.Tests if (body == null) return "Body: none"; return "Body: " + (body.Static ? "static" : "non-static") + "\nAt: " + body.Position + " - rotated: " + body.Rotation - + "\nWith mass: " + body.Mass - + "\nVelocity: " + body.Velocity + " - angular: " + body.AngularVelocity - + "\nDelta velocity: " + body.DeltaVelocity + " - angular: " + body.DeltaAngularVelocity; + + "\nWith mass: " + body.Mass + " - center: " + body.CenterOfMass + + "\nVelocity: " + body.Velocity + " - angular: " + body.AngularVelocity; } return "Switching modes..."; @@ -310,7 +314,7 @@ namespace GamecraftModdingAPI.Tests if (modsString != null) return modsString; StringBuilder sb = new StringBuilder("Installed mods:"); foreach (var plugin in PluginManager.Plugins) - sb.Append("\n" + plugin); + sb.Append("\n" + plugin.Name + " - " + plugin.Version); return modsString = sb.ToString(); }