A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimBody.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using RobocraftX.Common;
  2. using RobocraftX.Physics;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. namespace GamecraftModdingAPI
  7. {
  8. /// <summary>
  9. /// A rigid body (like a cluster of connected blocks) during simulation.
  10. /// </summary>
  11. public class SimBody
  12. {
  13. public EGID Id { get; }
  14. public SimBody(EGID id)
  15. {
  16. Id = id;
  17. }
  18. public SimBody(uint id) : this(new EGID(id, CommonExclusiveGroups.SIMULATION_BODIES_GROUP))
  19. {
  20. }
  21. /// <summary>
  22. /// The position of this body. When setting the position, update the position of the connected bodies as well,
  23. /// otherwise unexpected forces may arise.
  24. /// </summary>
  25. public float3 Position
  26. {
  27. get => GetStruct().position;
  28. set => GetStruct().position = value;
  29. }
  30. public float3 Velocity
  31. {
  32. get => GetStruct().velocity;
  33. set => GetStruct().velocity = value;
  34. }
  35. public float3 AngularVelocity
  36. {
  37. get => GetStruct().angularVelocity;
  38. set => GetStruct().angularVelocity = value;
  39. } //Delta versions are used internally, can't be set or get
  40. public float3 Rotation
  41. {
  42. get => ((Quaternion) GetStruct().rotation).eulerAngles;
  43. set
  44. {
  45. ref var str = ref GetStruct();
  46. Quaternion quaternion = str.rotation;
  47. quaternion.eulerAngles = value;
  48. str.rotation = quaternion;
  49. }
  50. }
  51. public float Mass
  52. {
  53. get => math.rcp(GetStruct().physicsMass.InverseMass);
  54. //set => GetStruct().physicsMass.InverseMass = math.rcp(value);
  55. }
  56. public float3 CenterOfMass
  57. {
  58. get => GetStruct().physicsMass.CenterOfMass;
  59. //set => GetStruct().physicsMass.CenterOfMass = value;
  60. }
  61. /// <summary>
  62. /// Whether the body can be moved or static.
  63. /// </summary>
  64. public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic; //Setting it doesn't have any effect
  65. /// <summary>
  66. /// The rigid bodies connected to this one via functional joints (broken ones don't count).
  67. /// </summary>
  68. public SimBody[] GetConnectedBodies()
  69. {
  70. return Block.BlockEngine.GetConnectedSimBodies(Id.entityID);
  71. }
  72. private ref RigidBodyEntityStruct GetStruct()
  73. {
  74. return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id);
  75. }
  76. public override string ToString()
  77. {
  78. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Mass)}: {Mass}, {nameof(Static)}: {Static}";
  79. }
  80. /// <summary>
  81. /// Returns the object identified by the given ID (A-Z).
  82. /// This has the same result as calling ObjectIdentifier.GetByID(id) and then GetRigidBody() with the duplicates filtered out.
  83. /// </summary>
  84. /// <param name="id">The alphabetical ID</param>
  85. /// <returns>An array that may be empty</returns>
  86. public static SimBody[] GetFromObjectID(char id) => Block.BlockEngine.GetSimBodiesFromID((byte) (id - 'A'));
  87. }
  88. }