Add support for connected bodies

Added center of mass
Removed delta velocities
This commit is contained in:
Norbi Peti 2020-05-24 21:55:49 +02:00
parent ab7c5805fe
commit 269d30b0db
5 changed files with 47 additions and 49 deletions

View file

@ -4,6 +4,8 @@ using Gamecraft.Wires;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.GUI.Hotbar.Colours; using RobocraftX.GUI.Hotbar.Colours;
using RobocraftX.Physics;
using RobocraftX.Scene.Simulation;
using Svelto.DataStructures; using Svelto.DataStructures;
using Svelto.ECS; using Svelto.ECS;
@ -126,6 +128,20 @@ namespace GamecraftModdingAPI.Blocks
return ret.ToArray(); return ret.ToArray();
} }
public SimBody[] GetConnectedSimBodies(uint id)
{
var joints = entitiesDB.QueryEntities<JointEntityStruct>(MachineSimulationGroups.JOINTS_GROUP);
var list = new FasterList<SimBody>(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 #if DEBUG
public EntitiesDB GetEntitiesDB() public EntitiesDB GetEntitiesDB()
{ {

View file

@ -28,17 +28,6 @@ namespace GamecraftModdingAPI.Blocks
public void Ready() 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; } public EntitiesDB entitiesDB { get; set; }

View file

@ -249,21 +249,6 @@ namespace GamecraftModdingAPI.Players
: maxDistance; : maxDistance;
if (rayCast.hit && rayCast.distance <= distance) if (rayCast.hit && rayCast.distance <= distance)
return rayCast.hitEgid; return rayCast.hitEgid;
/*if (rayCast.hit)
{
*Logging.MetaDebugLog("RayCast EGID: " + rayCast.hitEgid);
var d = AccessTools.Field(typeof(ExclusiveGroup), "_knownGroups").GetValue(null) as
Dictionary<string, ExclusiveGroupStruct>;
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; return null;
} }

View file

@ -1,5 +1,4 @@
using Gamecraft.Wires; using RobocraftX.Common;
using RobocraftX.Common;
using RobocraftX.Physics; using RobocraftX.Physics;
using Svelto.ECS; using Svelto.ECS;
using Unity.Mathematics; using Unity.Mathematics;
@ -23,6 +22,10 @@ namespace GamecraftModdingAPI
{ {
} }
/// <summary>
/// The position of this body. When setting the position, update the position of the connected bodies as well,
/// otherwise unexpected forces may arise.
/// </summary>
public float3 Position public float3 Position
{ {
get => GetStruct().position; get => GetStruct().position;
@ -39,19 +42,7 @@ namespace GamecraftModdingAPI
{ {
get => GetStruct().angularVelocity; get => GetStruct().angularVelocity;
set => GetStruct().angularVelocity = value; set => GetStruct().angularVelocity = value;
} } //Delta versions are used internally, can't be set or get
public float3 DeltaVelocity
{
get => GetStruct().deltaVelocity;
set => GetStruct().deltaVelocity = value;
}
public float3 DeltaAngularVelocity
{
get => GetStruct().deltaAngularVelocity;
set => GetStruct().deltaAngularVelocity = value;
}
public float3 Rotation public float3 Rotation
{ {
@ -68,14 +59,27 @@ namespace GamecraftModdingAPI
public float Mass public float Mass
{ {
get => math.rcp(GetStruct().physicsMass.InverseMass); 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;
} }
/// <summary> /// <summary>
/// Whether the body can be moved or static /// Whether the body can be moved or static.
/// </summary> /// </summary>
public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic; public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic; //Setting it doesn't have any effect
//Setting it doesn't have any effect
/// <summary>
/// The rigid bodies connected to this one via functional joints (broken ones don't count).
/// </summary>
public SimBody[] GetConnectedBodies()
{
return Block.BlockEngine.GetConnectedSimBodies(Id.entityID);
}
private ref RigidBodyEntityStruct GetStruct() private ref RigidBodyEntityStruct GetStruct()
{ {

View file

@ -216,6 +216,11 @@ namespace GamecraftModdingAPI.Tests
{ {
Logging.CommandLog("SimBody: " + body); Logging.CommandLog("SimBody: " + body);
body.Position += new float3(0, 10, 0); 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(); }).Build();
@ -296,9 +301,8 @@ namespace GamecraftModdingAPI.Tests
if (body == null) return "Body: none"; if (body == null) return "Body: none";
return "Body: " + (body.Static ? "static" : "non-static") return "Body: " + (body.Static ? "static" : "non-static")
+ "\nAt: " + body.Position + " - rotated: " + body.Rotation + "\nAt: " + body.Position + " - rotated: " + body.Rotation
+ "\nWith mass: " + body.Mass + "\nWith mass: " + body.Mass + " - center: " + body.CenterOfMass
+ "\nVelocity: " + body.Velocity + " - angular: " + body.AngularVelocity + "\nVelocity: " + body.Velocity + " - angular: " + body.AngularVelocity;
+ "\nDelta velocity: " + body.DeltaVelocity + " - angular: " + body.DeltaAngularVelocity;
} }
return "Switching modes..."; return "Switching modes...";
@ -310,7 +314,7 @@ namespace GamecraftModdingAPI.Tests
if (modsString != null) return modsString; if (modsString != null) return modsString;
StringBuilder sb = new StringBuilder("Installed mods:"); StringBuilder sb = new StringBuilder("Installed mods:");
foreach (var plugin in PluginManager.Plugins) foreach (var plugin in PluginManager.Plugins)
sb.Append("\n" + plugin); sb.Append("\n" + plugin.Name + " - " + plugin.Version);
return modsString = sb.ToString(); return modsString = sb.ToString();
} }