Skip to content

Commit

Permalink
Revert "disable physics and stop it from calling, as a test"
Browse files Browse the repository at this point in the history
This reverts commit b56b4ba.
  • Loading branch information
InvalidArgument3 committed Jun 19, 2024
1 parent b56b4ba commit bacc488
Showing 1 changed file with 30 additions and 46 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");

if (MyAPIGateway.Session.IsServer)
{
Log.Info("Creating physics");
CreatePhysics();
}
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}");

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

if (MyAPIGateway.Session.IsServer)
{
Expand All @@ -356,46 +356,6 @@ 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 @@ -561,6 +521,30 @@ 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 bacc488

Please sign in to comment.