Skip to content

Commit

Permalink
disable physics and stop it from calling, as a test
Browse files Browse the repository at this point in the history
physics separation and binding attempt
  • Loading branch information
InvalidArgument3 committed Jun 19, 2024
1 parent dab3fa0 commit b56b4ba
Showing 1 changed file with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,13 @@ private void Init(Vector3D position, float size, Vector3D initialVelocity, Aster
MyEntities.Add(this);
Log.Info($"{(MyAPIGateway.Session.IsServer ? "Server" : "Client")}: Added asteroid entity with ID {EntityId} to MyEntities");

Log.Info("Creating physics");
CreatePhysics();
Physics.LinearVelocity = initialVelocity + RandVector() * AsteroidSettings.VelocityVariability;
Physics.AngularVelocity = RandVector() * AsteroidSettings.GetRandomAngularVelocity(MainSession.I.Rand);
Log.Info($"Initial LinearVelocity: {Physics.LinearVelocity}, Initial AngularVelocity: {Physics.AngularVelocity}");
if (MyAPIGateway.Session.IsServer)
{
Log.Info("Creating physics");
CreatePhysics();
}

Log.Info($"Asteroid model {ModelString} loaded successfully with initial angular velocity: {Physics.AngularVelocity}");
BindPhysics(initialVelocity);

if (MyAPIGateway.Session.IsServer)
{
Expand All @@ -356,6 +356,46 @@ private void Init(Vector3D position, float size, Vector3D initialVelocity, Aster
}
}

private void BindPhysics(Vector3D initialVelocity)
{
if (Physics == null)
{
Log.Warning("Physics object is not created, binding failed.");
return;
}

Physics.Enabled = true;
Physics.LinearVelocity = initialVelocity + RandVector() * AsteroidSettings.VelocityVariability;
Physics.AngularVelocity = RandVector() * AsteroidSettings.GetRandomAngularVelocity(MainSession.I.Rand);

Log.Info($"Initial LinearVelocity: {Physics.LinearVelocity}, Initial AngularVelocity: {Physics.AngularVelocity}");
Log.Info($"Asteroid model {ModelString} loaded successfully with initial angular velocity: {Physics.AngularVelocity}");
}

private void CreatePhysics()
{
float mass = 10000 * Size * Size * Size;
float radius = Size / 2; // Assuming Size represents the diameter

PhysicsSettings settings = MyAPIGateway.Physics.CreateSettingsForPhysics(
this,
WorldMatrix,
Vector3.Zero,
linearDamping: 0f, // Remove damping
angularDamping: 0f, // Remove damping
rigidBodyFlags: RigidBodyFlag.RBF_DEFAULT,
collisionLayer: CollisionLayers.NoVoxelCollisionLayer,
isPhantom: false,
mass: new ModAPIMass(PositionComp.LocalAABB.Volume(), mass, Vector3.Zero, mass * PositionComp.LocalAABB.Height * PositionComp.LocalAABB.Height / 6 * Matrix.Identity)
);

MyAPIGateway.Physics.CreateSpherePhysics(settings, radius);
Physics.Enabled = true;
Physics.Activate();

Log.ServerInfo($"Physics created for asteroid with ID {EntityId}, Mass: {mass}, Radius: {radius}");
}

public float Size;
public string ModelString = "";
public AsteroidType Type;
Expand Down Expand Up @@ -521,30 +561,6 @@ public bool DoDamage(float damage, MyStringHash damageSource, bool sync, MyHitIn

public bool UseDamageSystem => true;

private void CreatePhysics()
{
float mass = 10000 * Size * Size * Size;
float radius = Size / 2; // Assuming Size represents the diameter

PhysicsSettings settings = MyAPIGateway.Physics.CreateSettingsForPhysics(
this,
WorldMatrix,
Vector3.Zero,
linearDamping: 0f, // Remove damping
angularDamping: 0f, // Remove damping
rigidBodyFlags: RigidBodyFlag.RBF_DEFAULT,
collisionLayer: CollisionLayers.NoVoxelCollisionLayer,
isPhantom: false,
mass: new ModAPIMass(PositionComp.LocalAABB.Volume(), mass, Vector3.Zero, mass * PositionComp.LocalAABB.Height * PositionComp.LocalAABB.Height / 6 * Matrix.Identity)
);

MyAPIGateway.Physics.CreateSpherePhysics(settings, radius);
Physics.Enabled = true;
Physics.Activate();

Log.ServerInfo($"Physics created for asteroid with ID {EntityId}, Mass: {mass}, Radius: {radius}");
}

private Vector3D RandVector()
{
var theta = MainSession.I.Rand.NextDouble() * 2.0 * Math.PI;
Expand Down

0 comments on commit b56b4ba

Please sign in to comment.