public override void Register(PhysicsWorld physics) { if (pworld == physics) { return; } pworld = physics; Collider cld = null; if (SurPath == null) //sphere { cld = new SphereCollider(SphereRadius); } else { var mr = (ModelRenderer)Parent.RenderComponent; sur = new SurCollider(SurPath); cld = sur; if (Parent.RigidModel.From3db) { sur.AddPart(PlainCrc, Matrix4x4.Identity, null); } else { foreach (var part in Parent.RigidModel.AllParts) { var crc = CrcTool.FLModelCrc(part.Name); if (part.Construct == null) { sur.AddPart(crc, Matrix4x4.Identity, part); } else { sur.AddPart(crc, part.LocalTransform, part); } } } } if (Mass < float.Epsilon) { Body = physics.AddStaticObject(Parent.GetTransform(), cld); } else { Body = physics.AddDynamicObject(Mass, Parent.GetTransform(), cld, Inertia); } Body.Tag = Parent; collider = cld; }
public override void FixedUpdate(TimeSpan time) { var world = Parent.GetWorld(); var player = world.GetObject("player"); if (player == null) { return; } if (VectorMath.DistanceSquared(player.PhysicsComponent.Body.Position, Field.Zone.Position) > activateDist) { return; } var cds = (Field.CubeSize + COLLIDE_DISTANCE); cds *= cds; for (int i = bodies.Count - 1; i >= 0; i--) { var distance = VectorMath.DistanceSquared(player.PhysicsComponent.Body.Position, bodies[i].Position); if (distance > cds) { world.Physics.RemoveObject(bodies[i]); bodies.RemoveAt(i); } } var close = AsteroidFieldShared.GetCloseCube(player.PhysicsComponent.Body.Position, Field.CubeSize); var cubeRad = new Vector3(Field.CubeSize) * 0.5f; int amountCubes = (int)Math.Floor((COLLIDE_DISTANCE / Field.CubeSize)) + 1; for (int x = -amountCubes; x <= amountCubes; x++) { for (int y = -amountCubes; y <= amountCubes; y++) { for (int z = -amountCubes; z <= amountCubes; z++) { var center = close + new Vector3(x * Field.CubeSize, y * Field.CubeSize, z * Field.CubeSize); if (!Field.Zone.Shape.ContainsPoint(center)) { continue; } if (VectorMath.DistanceSquared(player.PhysicsComponent.Body.Position, center) > cds) { continue; } float tval; if (!AsteroidFieldShared.CubeExists(center, Field.EmptyCubeFrequency, out tval)) { continue; } if (GetExclusionZone(center) != null) { continue; } bool create = true; for (int i = 0; i < bodies.Count; i++) { if ((bodies[i].Position - center).LengthFast < 3) { create = false; break; } } if (create) { var transform = Field.CubeRotation.GetRotation(tval) * Matrix4.CreateTranslation(center); var body = phys.AddStaticObject(transform, shape); bodies.Add(body); } } } } }