TechbloxModdingAPI/GamecraftModdingAPI/SimBody.cs
NorbiPeti 269d30b0db Add support for connected bodies
Added center of mass
Removed delta velocities
2020-05-24 21:55:49 +02:00

102 lines
No EOL
3.2 KiB
C#

using RobocraftX.Common;
using RobocraftX.Physics;
using Svelto.ECS;
using Unity.Mathematics;
using UnityEngine;
namespace GamecraftModdingAPI
{
/// <summary>
/// A rigid body (like a cluster of connected blocks) during simulation.
/// </summary>
public class SimBody
{
public EGID Id { get; }
public SimBody(EGID id)
{
Id = id;
}
public SimBody(uint id) : this(new EGID(id, CommonExclusiveGroups.SIMULATION_BODIES_GROUP))
{
}
/// <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
{
get => GetStruct().position;
set => GetStruct().position = value;
}
public float3 Velocity
{
get => GetStruct().velocity;
set => GetStruct().velocity = value;
}
public float3 AngularVelocity
{
get => GetStruct().angularVelocity;
set => GetStruct().angularVelocity = value;
} //Delta versions are used internally, can't be set or get
public float3 Rotation
{
get => ((Quaternion) GetStruct().rotation).eulerAngles;
set
{
ref var str = ref GetStruct();
Quaternion quaternion = str.rotation;
quaternion.eulerAngles = value;
str.rotation = quaternion;
}
}
public float Mass
{
get => math.rcp(GetStruct().physicsMass.InverseMass);
//set => GetStruct().physicsMass.InverseMass = math.rcp(value);
}
public float3 CenterOfMass
{
get => GetStruct().physicsMass.CenterOfMass;
//set => GetStruct().physicsMass.CenterOfMass = value;
}
/// <summary>
/// Whether the body can be moved or static.
/// </summary>
public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic; //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()
{
return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id);
}
public override string ToString()
{
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Mass)}: {Mass}, {nameof(Static)}: {Static}";
}
/// <summary>
/// Returns the object identified by the given ID (A-Z).
/// This has the same result as calling ObjectIdentifier.GetByID(id) and then GetRigidBody() with the duplicates filtered out.
/// </summary>
/// <param name="id">The alphabetical ID</param>
/// <returns>An array that may be empty</returns>
public static SimBody[] GetFromObjectID(char id) => Block.BlockEngine.GetSimBodiesFromID((byte) (id - 'A'));
}
}