diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs index 0c7c2484..5608e561 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidEntity.cs @@ -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) { @@ -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; @@ -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;