Exemple #1
0
        public static CGSize Size(this SCNNode self)
        {
            SCNVector3 min = SCNVector3.Zero, max = SCNVector3.Zero;

            self.GetBoundingBox(ref min, ref max);
            return(new CGSize(max.X - min.X, max.Y - min.Y));
        }
Exemple #2
0
        public static void Center(this SCNNode self)
        {
            SCNVector3 min = SCNVector3.Zero, max = SCNVector3.Zero;

            self.GetBoundingBox(ref min, ref max);
            self.Pivot = SCNMatrix4.CreateTranslation(min.X + (self.Width() * 0.5F), min.Y + (self.Height() * 0.5F), 0);
        }
Exemple #3
0
        public static SCNVector3 GetExtents(this SCNNode self)
        {
            var min = default(SCNVector3);
            var max = default(SCNVector3);

            self.GetBoundingBox(ref min, ref max);

            return(max - min);
        }
Exemple #4
0
        private void CenterPivot(SCNNode node)
        {
            var min = new SCNVector3();
            var max = new SCNVector3();

            if (node.GetBoundingBox(ref min, ref max))
            {
                node.Pivot = new SCNMatrix4(CATransform3D.MakeTranslation(
                                                min.X + (max.X - min.X) / 2,
                                                min.Y + (max.Y - min.Y) / 2,
                                                min.Z + (max.Z - min.Z) / 2));
            }
        }
Exemple #5
0
        // Returns the size of the horizontal parts of the node's bounding box.
        // x is the width, y is the depth.
        public static OpenTK.Vector2 GetHorizontalSize(this SCNNode node)
        {
            var minBox = SCNVector3.Zero;
            var maxBox = SCNVector3.Zero;

            node.GetBoundingBox(ref minBox, ref maxBox);

            // Scene is y-up, horizontal extent is calculated on x and z
            var sceneWidth  = Math.Abs(maxBox.X - minBox.X);
            var sceneLength = Math.Abs(maxBox.Z - minBox.Z);

            return(new OpenTK.Vector2(sceneWidth, sceneLength));
        }
Exemple #6
0
 public static void CalculateMassFromDensity(this SCNNode node, string name, float density)
 {
     if (node.PhysicsBody != null)
     {
         // our naming convention lets us parse the shape geometry
         var boundingBoxMin = SCNVector3.Zero;
         var boundingBoxMax = SCNVector3.Zero;
         node.GetBoundingBox(ref boundingBoxMin, ref boundingBoxMax);
         var bounds = (boundingBoxMax - boundingBoxMin);
         // calculate as a cylinder going up
         if (node.Name.StartsWith("block_cylinder", StringComparison.Ordinal))
         {
             var radius = bounds.X / 2f;
             var mass   = (float)Math.PI * radius * radius * bounds.Y;
             node.PhysicsBody.Mass = density * mass;
         }
         else if (node.Name.StartsWith("block_halfCylinder", StringComparison.Ordinal))
         {
             // half cylinder going up
             var radius = Math.Min(bounds.X, bounds.Z);
             var mass   = (float)Math.PI * radius * radius * bounds.Y / 2f;
             node.PhysicsBody.Mass = density * mass;
         }
         else if (node.Name.StartsWith("block_quarterCylinder", StringComparison.Ordinal))
         {
             // this is a cylinder lying sideways
             var radius = Math.Min(bounds.Y, bounds.Z);
             var mass   = (float)Math.PI * radius * radius * bounds.X / 4f;
             node.PhysicsBody.Mass = density * mass;
         }
         else
         {
             // for now, treat as box shape
             node.PhysicsBody.Mass = density * bounds.X * bounds.Y * bounds.Z;
         }
     }
 }
Exemple #7
0
        private SCNNode SetupVehicle(SCNScene scene)
        {
            var carScene    = SCNScene.FromFile("rc_car", ResourceManager.ResourceFolder, (NSDictionary)null);
            var chassisNode = carScene.RootNode.FindChildNode("rccarBody", false);

            chassisNode.Position = new SCNVector3(0f, 10f, 30f);
            chassisNode.Rotation = new SCNVector4(0f, 1f, 0f, (float)Math.PI);

            var body = SCNPhysicsBody.CreateDynamicBody();

            body.AllowsResting   = false;
            body.Mass            = 80f;
            body.Restitution     = 0.1f;
            body.Friction        = 0.5f;
            body.RollingFriction = 0f;

            chassisNode.PhysicsBody = body;

            var frontCameraNode = SCNNode.Create();

            frontCameraNode.Position    = new SCNVector3(0f, 3.5f, 2.5f);
            frontCameraNode.Rotation    = new SCNVector4(0f, 1f, 0f, (float)Math.PI);
            frontCameraNode.Camera      = SCNCamera.Create();
            frontCameraNode.Camera.XFov = 75f;
            frontCameraNode.Camera.ZFar = 500f;

            chassisNode.AddChildNode(frontCameraNode);

            scene.RootNode.AddChildNode(chassisNode);

            var pipeNode = chassisNode.FindChildNode("pipe", true);

            reactor = SCNParticleSystem.Create("reactor", ResourceManager.ResourceFolder);
            reactor.ParticleImage   = ResourceManager.GetResourcePath("spark.png");
            reactorDefaultBirthRate = reactor.BirthRate;
            reactor.BirthRate       = 0;
            pipeNode.AddParticleSystem(reactor);

            SCNNode wheel0Node = chassisNode.FindChildNode("wheelLocator_FL", true);
            SCNNode wheel1Node = chassisNode.FindChildNode("wheelLocator_FR", true);
            SCNNode wheel2Node = chassisNode.FindChildNode("wheelLocator_RL", true);
            SCNNode wheel3Node = chassisNode.FindChildNode("wheelLocator_RR", true);

            var wheel0 = SCNPhysicsVehicleWheel.Create(wheel0Node);
            var wheel1 = SCNPhysicsVehicleWheel.Create(wheel1Node);
            var wheel2 = SCNPhysicsVehicleWheel.Create(wheel2Node);
            var wheel3 = SCNPhysicsVehicleWheel.Create(wheel3Node);

            var min = SCNVector3.Zero;
            var max = SCNVector3.Zero;

            wheel0Node.GetBoundingBox(ref min, ref max);
            float wheelHalfWidth = 0.5f * (max.X - min.X);

            wheel0.ConnectionPosition = SCNVector3.Add(wheel0Node.ConvertPositionToNode(SCNVector3.Zero, chassisNode), new SCNVector3(wheelHalfWidth, 0f, 0f));
            wheel1.ConnectionPosition = SCNVector3.Subtract(wheel1Node.ConvertPositionToNode(SCNVector3.Zero, chassisNode), new SCNVector3(wheelHalfWidth, 0f, 0f));
            wheel2.ConnectionPosition = SCNVector3.Add(wheel2Node.ConvertPositionToNode(SCNVector3.Zero, chassisNode), new SCNVector3(wheelHalfWidth, 0f, 0f));
            wheel3.ConnectionPosition = SCNVector3.Subtract(wheel3Node.ConvertPositionToNode(SCNVector3.Zero, chassisNode), new SCNVector3(wheelHalfWidth, 0f, 0f));

            var vehicle = SCNPhysicsVehicle.Create(chassisNode.PhysicsBody,
                                                   new SCNPhysicsVehicleWheel[] { wheel0, wheel1, wheel2, wheel3 });

            scene.PhysicsWorld.AddBehavior(vehicle);
            this.vehicle = vehicle;

            return(chassisNode);
        }