Exemplo n.º 1
0
        SCNNode CreateBanana()
        {
            //Create model
            if (bananaCollectable == null)
            {
                bananaCollectable       = GameSimulation.LoadNodeWithName("banana", GameSimulation.PathForArtResource("level/banana.dae"));
                bananaCollectable.Scale = new SCNVector3(0.5f, 0.5f, 0.5f);


                SCNSphere       sphereGeometry = SCNSphere.Create(40);
                SCNPhysicsShape physicsShape   = SCNPhysicsShape.Create(sphereGeometry, new SCNPhysicsShapeOptions());
                bananaCollectable.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, physicsShape);

                // Only collide with player and ground
                bananaCollectable.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground;
                // Declare self in the banana category
                bananaCollectable.PhysicsBody.CategoryBitMask = GameCollisionCategory.Banana;

                // Rotate and Hover forever.
                bananaCollectable.Rotation = new SCNVector4(0.5f, 1f, 0.5f, -(nfloat)Math.PI / 4);
                SCNAction idleHoverGroupAction = SCNAction.Group(new SCNAction[] { BananaIdleAction, HoverAction });
                SCNAction repeatForeverAction  = SCNAction.RepeatActionForever(idleHoverGroupAction);
                bananaCollectable.RunAction(repeatForeverAction);
            }

            return(bananaCollectable.Clone());
        }
        public SCNNode GetBox()
        {
            if (_prefab != null)
            {
                return(_prefab.Clone());
            }

            var g = new SCNBox {
                Height = .066f, Width = .066f, Length = .066f
            };

            g.FirstMaterial.Diffuse.Contents = UIImage.FromFile("xamagon-fill");

            var box = new SCNNode {
                Geometry = g
            };

            box.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, SCNPhysicsShape.Create(g, new SCNPhysicsShapeOptions {
                ShapeType = SCNPhysicsShapeType.BoundingBox
            }));
            box.PhysicsBody.ContinuousCollisionDetectionThreshold = g.Width * 2;

            _prefab = box;

            return(_prefab.Clone());
        }
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            var forcePower = 10;

            base.TouchesBegan(touches, evt);
            var pointOfView = this.SceneView.PointOfView;
            var transform   = pointOfView.Transform;
            var location    = new SCNVector3(transform.M41, transform.M42, transform.M43);
            var orientation = new SCNVector3(-transform.M31, -transform.M32, -transform.M33);
            var position    = location + orientation;
            var pokeball    = new SCNNode()
            {
                Geometry = SCNSphere.Create(0.15f),
            };

            pokeball.Geometry.FirstMaterial.Diffuse.ContentImage = UIImage.FromBundle("pokeball");
            pokeball.Position    = position;
            pokeball.PhysicsBody = SCNPhysicsBody.CreateDynamicBody();
            pokeball.PhysicsBody.PhysicsShape       = SCNPhysicsShape.Create(pokeball);
            pokeball.PhysicsBody.ContactTestBitMask = (int)BitMaskCategory.Pokemon;
            pokeball.PhysicsBody.CategoryBitMask    = (int)BitMaskCategory.Pokeball;

            pokeball.PhysicsBody.ApplyForce(new SCNVector3(orientation.X * forcePower, orientation.Y * forcePower, orientation.Z * forcePower), true);
            SceneView.Scene.RootNode.AddChildNode(pokeball);
        }
Exemplo n.º 4
0
        private void PresentPrimitives(PresentationViewController presentationViewController)
        {
            var count  = 100;
            var spread = 0.0f;

            // create a cube with a sphere shape
            for (int i = 0; i < count; ++i)
            {
                var model = SCNNode.Create();
                model.Position    = GroundNode.ConvertPositionToNode(new SCNVector3(RandFloat(-1, 1), RandFloat(30, 50), RandFloat(-1, 1)), null);
                model.EulerAngles = new SCNVector3(RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2));

                var size          = new SCNVector3(RandFloat(1.0, 1.5), RandFloat(1.0, 1.5), RandFloat(1.0, 1.5));
                var random        = new Random((int)DateTime.Now.Ticks);
                int geometryIndex = random.Next(0, 7);
                switch (geometryIndex)
                {
                case 0:                 // Box
                    model.Geometry = SCNBox.Create(size.X, size.Y, size.Z, 0);
                    break;

                case 1:                 // Pyramid
                    model.Geometry = SCNPyramid.Create(size.X, size.Y, size.Z);
                    break;

                case 2:                 // Sphere
                    model.Geometry = SCNSphere.Create(size.X);
                    break;

                case 3:                 // Cylinder
                    model.Geometry = SCNCylinder.Create(size.X, size.Y);
                    break;

                case 4:                 // Tube
                    model.Geometry = SCNTube.Create(size.X, size.X + size.Z, size.Y);
                    break;

                case 5:                 // Capsule
                    model.Geometry = SCNCapsule.Create(size.X, size.Y + 2 * size.X);
                    break;

                case 6:                 // Torus
                    model.Geometry = SCNTorus.Create(size.X, NMath.Min(size.X, size.Y) / 2);
                    break;

                default:
                    break;
                }

                model.Geometry.FirstMaterial.Multiply.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/texture", "png"));

                model.PhysicsBody                 = SCNPhysicsBody.CreateDynamicBody();
                model.PhysicsBody.Velocity        = new SCNVector3(RandFloat(-spread, spread), -10, RandFloat(-spread, spread));
                model.PhysicsBody.AngularVelocity = new SCNVector4(RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-3, 3));

                Shapes.Add(model);

                ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(model);
            }
        }
        public SCNPhysicsBody CreatePlanePhysics(SCNGeometry geometry)
        {
            var body = SCNPhysicsBody.CreateStaticBody();

            body.PhysicsShape = SCNPhysicsShape.Create(geometry, new NSDictionary());
            body.Restitution  = 0.5f;
            body.Friction     = 0.5f;

            return(body);
        }
Exemplo n.º 6
0
        private void ApplyRandomTorque(SCNPhysicsBody physicsBody, float maxTorque)
        {
            var randomAxis = new SCNVector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());

            randomAxis = SCNVector3.Normalize(randomAxis);

            var randomTorque = new SCNVector4(randomAxis, (float)(random.NextDouble() * 2d - 1d) * maxTorque);

            physicsBody.ApplyTorque(randomTorque, true);
        }
Exemplo n.º 7
0
        private SCNNode CreatePoolBall(SCNVector3 position)
        {
            var model = SCNNode.Create();

            model.Position = position;
            model.Geometry = SCNSphere.Create(0.7f);
            model.Geometry.FirstMaterial.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Scenes.scnassets/pool/pool_8", "png"));
            model.PhysicsBody = SCNPhysicsBody.CreateDynamicBody();
            return(model);
        }
Exemplo n.º 8
0
        public PlayerCharacter(SCNNode characterNode) : base(characterNode)
        {
            CategoryBitMask   = NodeCategory.Lava;
            velocity          = SCNVector3.Zero;
            IsWalking         = false;
            changingDirection = false;
            baseWalkSpeed     = 0.0167f;
            JumpBoost         = 0.0f;

            WalkSpeed           = baseWalkSpeed * 2;
            Jumping             = false;
            groundPlaneHeight   = 0.0f;
            playerWalkDirection = WalkDirection.Right;

            cameraHelper = new SCNNode {
                Position = new SCNVector3(1000f, 200f, 0f)
            };

            AddChildNode(cameraHelper);

            CollideSphere = new SCNNode {
                Position = new SCNVector3(0f, 80f, 0f)
            };

            SCNGeometry     geo    = SCNCapsule.Create(90f, 160f);
            SCNPhysicsShape shape2 = SCNPhysicsShape.Create(geo, (NSDictionary)null);

            CollideSphere.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, shape2);

            CollideSphere.PhysicsBody.CollisionBitMask =
                GameCollisionCategory.Banana |
                GameCollisionCategory.Coin |
                GameCollisionCategory.Coconut |
                GameCollisionCategory.Lava;

            CollideSphere.PhysicsBody.CategoryBitMask = GameCollisionCategory.Player;
            AddChildNode(CollideSphere);

            DustPoof = GameSimulation.LoadParticleSystemWithName("dust");
            NSString artResourcePath = (NSString)GameSimulation.PathForArtResource("level/effects/effects_transparent.png");

            DustPoof.ParticleImage    = artResourcePath;
            DustWalking               = GameSimulation.LoadParticleSystemWithName("dustWalking");
            DustWalking.ParticleImage = artResourcePath;
            dustWalkingBirthRate      = DustWalking.BirthRate;

            // Load the animations and store via a lookup table.
            SetupIdleAnimation();
            SetupRunAnimation();
            SetupJumpAnimation();
            SetupBoredAnimation();
            SetupHitAnimation();

            PlayIdle();
        }
Exemplo n.º 9
0
        void SetupCollisionNodes(SCNNode node)
        {
            if (node.Geometry != null)
            {
                Console.WriteLine(node.Name);
                node.PhysicsBody = SCNPhysicsBody.CreateStaticBody();
                node.PhysicsBody.CategoryBitMask = (nuint)(int)Bitmask.Collision;

                var options = new SCNPhysicsShapeOptions {
                    ShapeType = SCNPhysicsShapeType.ConcavePolyhedron
                };
                node.PhysicsBody.PhysicsShape = SCNPhysicsShape.Create(node, options);

                // Get grass area to play the right sound steps
                if (node.Geometry.FirstMaterial.Name == "grass-area")
                {
                    if (grassArea != null)
                    {
                        node.Geometry.FirstMaterial = grassArea;
                    }
                    else
                    {
                        grassArea = node.Geometry.FirstMaterial;
                    }
                }

                // Get the water area
                if (node.Geometry.FirstMaterial.Name == "water")
                {
                    waterArea = node.Geometry.FirstMaterial;
                }

                // Temporary workaround because concave shape created from geometry instead of node fails
                SCNNode child = SCNNode.Create();
                node.AddChildNode(child);
                child.Hidden   = true;
                child.Geometry = node.Geometry;
                node.Geometry  = null;
                node.Hidden    = false;

                if (node.Name == "water")
                {
                    node.PhysicsBody.CategoryBitMask = (nuint)(int)Bitmask.Water;
                }
            }

            foreach (SCNNode child in node.ChildNodes)
            {
                if (!child.Hidden)
                {
                    SetupCollisionNodes(child);
                }
            }
        }
Exemplo n.º 10
0
        private void CreateHingeJoint(SCNPhysicsBody source,
                                      SCNVector3 sourceAxis,
                                      SCNVector3 sourceAnchor,
                                      SCNPhysicsBody dest,
                                      SCNVector3 destAxis,
                                      SCNVector3 destAnchor)
        {
            var joint = SCNPhysicsHingeJoint.Create(source, sourceAxis, sourceAnchor, dest, destAxis, destAnchor);

            this.joints.Add(joint);
        }
Exemplo n.º 11
0
        public SCNPhysicsBody MakePhysicsBody(SCNGeometry geometry)
        {
            var option = new SCNPhysicsShapeOptions();

            option.KeepAsCompound = true;
            option.ShapeType      = SCNPhysicsShapeType.ConcavePolyhedron;

            var shape = SCNPhysicsShape.Create(geometry, option);

            return(SCNPhysicsBody.CreateBody(type: SCNPhysicsBodyType.Static, shape));
        }
Exemplo n.º 12
0
        public CubeNode(float size, UIColor color)
        {
            var rootNode = new SCNNode
            {
                Geometry    = CreateGeometry(size, color),
                Position    = new SCNVector3(0, size / 2, 0),
                PhysicsBody = SCNPhysicsBody.CreateDynamicBody()
            };

            AddChildNode(rootNode);
        }
        public override SCNPhysicsBody CreatePlanePhysics(SCNGeometry geometry)
        {
            var body = SCNPhysicsBody.CreateStaticBody();

            body.PhysicsShape = SCNPhysicsShape.Create(geometry, new SCNPhysicsShapeOptions {
                ShapeType = SCNPhysicsShapeType.BoundingBox
            });
            body.Restitution = 0.5f;
            body.Friction    = 0.5f;

            return(body);
        }
Exemplo n.º 14
0
        SCNNode CreatePokemonNodeFromFile(string filePath, string nodeName, SCNVector3 vector)
        {
            var pScene  = SCNScene.FromFile(filePath);
            var pokemon = pScene.RootNode.FindChildNode(nodeName, true);

            pokemon.Position    = vector;
            pokemon.PhysicsBody = SCNPhysicsBody.CreateStaticBody();
            pokemon.PhysicsBody.PhysicsShape       = SCNPhysicsShape.Create(pokemon);
            pokemon.PhysicsBody.ContactTestBitMask = (int)BitMaskCategory.Pokeball;
            pokemon.PhysicsBody.CategoryBitMask    = (int)BitMaskCategory.Pokemon;
            return(pokemon);
        }
Exemplo n.º 15
0
        private void SetupInvironment(SCNScene scene)
        {
            var ambientLight = SCNNode.Create();

            ambientLight.Light = new SCNLight {
                LightType = SCNLightType.Ambient,
                Color     = UIColor.FromWhiteAlpha(0.3f, 1f)
            };
            scene.RootNode.AddChildNode(ambientLight);

            var lightNode = new SCNNode {
                Position = new SCNVector3(0f, 80f, 30f),
                Rotation = new SCNVector4(1f, 0f, 0f, (float)(-Math.PI / 2.8))
            };

            lightNode.Light = new SCNLight {
                LightType      = SCNLightType.Spot,
                Color          = UIColor.FromWhiteAlpha(0.8f, 1f),
                SpotInnerAngle = 0f,
                SpotOuterAngle = 50f,
                ShadowColor    = UIColor.Black,
                ZFar           = 500f,
                ZNear          = 50f
            };

            if (IsHighEndDevice)
            {
                ambientLight.Light.CastsShadow = true;
            }

            scene.RootNode.AddChildNode(lightNode);

            spotLightNode = lightNode;

            var floor = SCNNode.Create();

            floor.Geometry = new SCNFloor();
            floor.Geometry.FirstMaterial.Diffuse.Contents          = ResourceManager.GetResourcePath("wood.png");
            floor.Geometry.FirstMaterial.Diffuse.ContentsTransform = SCNMatrix4.Scale(2f, 2f, 1f);
            floor.Geometry.FirstMaterial.LocksAmbientWithDiffuse   = true;

            if (IsHighEndDevice)
            {
                ((SCNFloor)floor.Geometry).ReflectionFalloffEnd = 10f;
            }

            var staticBody = SCNPhysicsBody.CreateStaticBody();

            floor.PhysicsBody = staticBody;
            scene.RootNode.AddChildNode(floor);
        }
Exemplo n.º 16
0
        void CreateLavaAnimation()
        {
            var dummyFront = RootNode.FindChildNode("dummy_front", true);
            var lavaNodes  = new List <SCNNode> ();

            foreach (var dummyFrontNode in dummyFront.ChildNodes)
            {
                foreach (var lavaNode in dummyFrontNode.ChildNodes)
                {
                    if (!string.IsNullOrEmpty(lavaNode.Name) && lavaNode.Name.Contains("lava_0"))
                    {
                        lavaNodes.Add(lavaNode);
                    }
                }
            }

            foreach (SCNNode lava in lavaNodes)
            {
                var childrenWithGeometry = new List <SCNNode> ();

                foreach (var child in lava.ChildNodes)
                {
                    if (child.Geometry != null)
                    {
                        childrenWithGeometry.Add(child);
                    }
                }

                if (childrenWithGeometry.Count == 0)
                {
                    continue;
                }

                SCNNode lavaGeometry = childrenWithGeometry [0];
                lavaGeometry.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Static,
                                                                     SCNPhysicsShape.Create(lavaGeometry.Geometry, new SCNPhysicsShapeOptions {
                    ShapeType = SCNPhysicsShapeType.ConcavePolyhedron
                }));
                lavaGeometry.PhysicsBody.CategoryBitMask = GameCollisionCategory.Lava;
                lavaGeometry.CategoryBitMask             = NodeCategory.Lava;

                string shaderCode = "uniform float speed;\n" +
                                    "#pragma body\n" +
                                    "_geometry.texcoords[0] += vec2(sin(_geometry.position.z*0.1 + u_time * 0.1) * 0.1, -1.0* 0.05 * u_time);\n";

                lavaGeometry.Geometry.ShaderModifiers = new SCNShaderModifiers {
                    EntryPointGeometry = shaderCode
                };
            }
        }
Exemplo n.º 17
0
        private SCNNode CreateBlock(SCNVector3 position, SCNVector3 size)
        {
            if (DiceMesh == null)
            {
                DiceMesh = CreateBlockMesh(size);
            }

            var model = SCNNode.Create();

            model.Position    = position;
            model.Geometry    = DiceMesh;
            model.PhysicsBody = SCNPhysicsBody.CreateDynamicBody();

            return(model);
        }
Exemplo n.º 18
0
        public void SCNNode_SetPhysicsBodyTest()
        {
            Asserts.EnsureYosemite();

            if (IntPtr.Size == 8)
            {
                // Create a new empty scene
                var Scene = new SCNScene();

                var floorNode = SCNNode.Create();
                Scene.RootNode.AddChildNode(floorNode);

                floorNode.PhysicsBody    = SCNPhysicsBody.CreateStaticBody();
                Scene.PhysicsWorld.Speed = 0;
            }
        }
Exemplo n.º 19
0
        private SCNNode CreateConcreteNode(ARPlaneAnchor planeAnchor)
        {
            SCNNode concreteNode = new SCNNode();

            concreteNode.Geometry = SCNPlane.Create(planeAnchor.Extent.X, planeAnchor.Extent.Z);
            concreteNode.Geometry.FirstMaterial.Diffuse.Contents = new UIImage("Concrete.png");
            concreteNode.Geometry.FirstMaterial.DoubleSided      = true;
            concreteNode.Position    = new SCNVector3(planeAnchor.Center.X, planeAnchor.Center.Y, planeAnchor.Center.Z);
            concreteNode.EulerAngles = new SCNVector3(ConvertDegreesToRadians(90), 0, 0);

            //TODO 4.1 Creando superficie
            SCNPhysicsBody staticBody = SCNPhysicsBody.CreateStaticBody();

            concreteNode.PhysicsBody = staticBody;

            return(concreteNode);
        }
Exemplo n.º 20
0
        private void AddWoodenBlockToScene(SCNScene scene, NSString imageName, SCNVector3 position)
        {
            var block = SCNNode.Create();

            block.Position = position;
            block.Geometry = new SCNBox {
                Width         = 5f,
                Height        = 5f,
                Length        = 5f,
                ChamferRadius = 0f
            };

            block.Geometry.FirstMaterial.Diffuse.Contents  = imageName;
            block.Geometry.FirstMaterial.Diffuse.MipFilter = SCNFilterMode.Linear;
            block.PhysicsBody = SCNPhysicsBody.CreateDynamicBody();
            scene.RootNode.AddChildNode(block);
        }
Exemplo n.º 21
0
        SCNNode MakeBallNode()
        {
            var sphere     = SCNSphere.Create((nfloat)0.05);
            var sphereNode = SCNNode.Create();
            var material   = new SCNMaterial();

            sphereNode.Geometry       = sphere;
            material.Diffuse.Contents = MakeRandomColor();
            sphere.FirstMaterial      = material;
            var shape = SCNPhysicsShape.Create(sphere);
            var body  = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, shape);

            body.Restitution       = (nfloat)0.1;
            body.Friction          = (nfloat)0.9;
            body.Mass              = (nfloat)0.1;
            sphereNode.PhysicsBody = body;

            return(sphereNode);
        }
Exemplo n.º 22
0
        private void PresentHinge(PresentationViewController presentationViewController)
        {
            var count = 10.0f;

            var material = SCNMaterial.Create();

            material.Diffuse.Contents        = NSColor.White;
            material.Specular.Contents       = NSColor.White;
            material.LocksAmbientWithDiffuse = true;

            var cubeWidth  = 10.0f / count;
            var cubeHeight = 0.2f;
            var cubeLength = 5.0f;
            var offset     = 0;
            var height     = 5 + count * cubeWidth;

            SCNNode oldModel = null;

            for (int i = 0; i < count; ++i)
            {
                var model = SCNNode.Create();

                var worldtr = GroundNode.ConvertTransformToNode(SCNMatrix4.CreateTranslation(-offset + cubeWidth * i, height, 5), null);

                model.Transform = worldtr;

                model.Geometry = SCNBox.Create(cubeWidth, cubeHeight, cubeLength, 0);
                model.Geometry.FirstMaterial = material;

                var body = SCNPhysicsBody.CreateDynamicBody();
                body.Restitution  = 0.6f;
                model.PhysicsBody = body;

                ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(model);

                var joint = SCNPhysicsHingeJoint.Create(model.PhysicsBody, new SCNVector3(0, 0, 1), new SCNVector3(-cubeWidth * 0.5f, 0, 0), (oldModel != null ? oldModel.PhysicsBody : null), new SCNVector3(0, 0, 1), new SCNVector3(cubeWidth * 0.5f, 0, 0));
                ((SCNView)presentationViewController.View).Scene.PhysicsWorld.AddBehavior(joint);

                Hinges.Add(model);

                oldModel = model;
            }
        }
Exemplo n.º 23
0
        private void PresentWalls(PresentationViewController presentationViewController)
        {
            //add spheres and container
            var height = 2;
            var width  = 1;

            var count  = 3;
            var margin = 2;

            var totalWidth = count * (margin + width);

            var blockMesh = CreateBlockMesh(new SCNVector3(width, height, width));

            for (int i = 0; i < count; i++)
            {
                //create a static block
                var wall = SCNNode.Create();
                wall.Position    = new SCNVector3((i - (count / 2)) * (width + margin), -height / 2, totalWidth / 2);
                wall.Geometry    = blockMesh;
                wall.Name        = "container-wall";
                wall.PhysicsBody = SCNPhysicsBody.CreateStaticBody();

                GroundNode.AddChildNode(wall);
                wall.RunAction(SCNAction.MoveBy(new SCNVector3(0, height, 0), 0.5f));

                //one more
                wall          = (SCNNode)wall.Copy();
                wall.Position = new SCNVector3((i - (count / 2)) * (width + margin), -height / 2, -totalWidth / 2);
                GroundNode.AddChildNode(wall);

                // one more
                wall          = (SCNNode)wall.Copy();
                wall.Position = new SCNVector3(totalWidth / 2, -height / 2, (i - (count / 2)) * (width + margin));
                GroundNode.AddChildNode(wall);

                //one more
                wall          = (SCNNode)wall.Copy();
                wall.Position = new SCNVector3(-totalWidth / 2, -height / 2, (i - (count / 2)) * (width + margin));
                GroundNode.AddChildNode(wall);
            }
        }
Exemplo n.º 24
0
        private void PresentMeshes(PresentationViewController presentationViewController)
        {
            // add meshes
            var container = SCNNode.Create();
            var black     = Utils.SCAddChildNode(container, "teapot", "Scenes.scnassets/lod/midResTeapot.dae", 5);

            int count = 100;

            for (int i = 0; i < count; ++i)
            {
                var worldPos = GroundNode.ConvertPositionToNode(new SCNVector3(RandFloat(-1, 1), RandFloat(30, 50), RandFloat(-1, 1)), null);

                var node = (SCNNode)black.Copy();
                node.Position             = worldPos;
                node.PhysicsBody          = SCNPhysicsBody.CreateDynamicBody();
                node.PhysicsBody.Friction = 0.5f;

                ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(node);
                Meshes.Add(node);
            }
        }
        public override void TouchesEnded(NSSet touches, UIEvent evt)
        {
            base.TouchesEnded(touches, evt);
            if (!SessionStarted || PlaneTrackingEnabled)
            {
                return;
            }

            var frame = SCNView.Session.CurrentFrame;

            if (frame is null)
            {
                return;
            }

            using (frame)
            {
                var hits   = frame.HitTest(new CoreGraphics.CGPoint(0.5, 0.5), ARHitTestResultType.ExistingPlaneUsingExtent);
                var target = hits.FirstOrDefault(x => x.Anchor as ARPlaneAnchor != null);
                if (target == null)
                {
                    return;
                }

                var wt = target.WorldTransform.ToSCNMatrix4();

                var next = Prefabs.Random().Clone();
                next.Scale    = new SCNVector3(.15f, .15f, .0005f);
                next.Position = new SCNVector3(wt.Column3.X, wt.Column3.Y + .5f, wt.Column3.Z);
                next.Look(SCNView.PointOfView.WorldPosition);

                var body = SCNPhysicsBody.CreateDynamicBody();
                body.PhysicsShape = SCNPhysicsShape.Create(next, new NSDictionary());
                body.Restitution  = 0.5f;
                body.Friction     = 0.5f;
                next.PhysicsBody  = body;

                SCNView.Scene.RootNode.AddChildNode(next);
            }
        }
Exemplo n.º 26
0
        private void AddCar()
        {
            SCNNode pointOfView = sceneView.PointOfView;

            if (pointOfView == null)
            {
                return;
            }

            SCNMatrix4 transform               = pointOfView.Transform;
            SCNVector3 orientation             = new SCNVector3(-transform.M31, -transform.M32, -transform.M33);
            SCNVector3 location                = new SCNVector3(transform.M41, transform.M42, transform.M43);
            SCNVector3 currentPositionOfCamera = orientation + location;

            //TODO 4.2 Creando el coche
            SCNScene carScene = SCNScene.FromFile("art.scnassets/CarScene.scn");
            SCNNode  carNode  = carScene.RootNode.FindChildNode("frame", false);

            SCNNode frontLeftWheel = carNode.FindChildNode("frontLeftParent", false);
            SCNPhysicsVehicleWheel v_frontLeftWheel = SCNPhysicsVehicleWheel.Create(frontLeftWheel);

            SCNNode frontRightWheel = carNode.FindChildNode("frontRightParent", false);
            SCNPhysicsVehicleWheel v_frontRightWheel = SCNPhysicsVehicleWheel.Create(frontRightWheel);

            SCNNode rearLeftWheel = carNode.FindChildNode("rearLeftParent", false);
            SCNPhysicsVehicleWheel v_rearLeftWheel = SCNPhysicsVehicleWheel.Create(rearLeftWheel);

            SCNNode rearRightWheel = carNode.FindChildNode("rearRightParent", false);
            SCNPhysicsVehicleWheel v_rearRightWheel = SCNPhysicsVehicleWheel.Create(rearRightWheel);

            carNode.Position = currentPositionOfCamera;

            SCNPhysicsBody body = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, SCNPhysicsShape.Create(carNode, keepAsCompound: true));

            carNode.PhysicsBody = body;
            PhysicsVehicle      = SCNPhysicsVehicle.Create(carNode.PhysicsBody, new SCNPhysicsVehicleWheel[] { v_frontLeftWheel, v_frontRightWheel, v_rearLeftWheel, v_rearRightWheel });

            sceneView.Scene.PhysicsWorld.AddBehavior(PhysicsVehicle);
            sceneView.Scene.RootNode.AddChildNode(carNode);
        }
Exemplo n.º 27
0
        SCNNode CreateLargeBanana()
        {
            //Create model
            if (largeBananaCollectable == null)
            {
                var   node      = GameSimulation.LoadNodeWithName("banana", GameSimulation.PathForArtResource("level/banana.dae"));
                float scaleMode = 0.5f * 10 / 4;
                node.Scale = new SCNVector3(scaleMode, scaleMode, scaleMode);


                SCNSphere       sphereGeometry = SCNSphere.Create(100);
                SCNPhysicsShape physicsShape   = SCNPhysicsShape.Create(sphereGeometry, new SCNPhysicsShapeOptions());
                node.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, physicsShape);

                // Only collide with player and ground
                node.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground;
                // Declare self in the banana category
                node.PhysicsBody.CategoryBitMask = GameCollisionCategory.Coin;

                // Rotate forever.
                SCNAction rotateCoin = SCNAction.RotateBy(0f, 8f, 0f, 2f);
                SCNAction repeat     = SCNAction.RepeatActionForever(rotateCoin);

                node.Rotation = new SCNVector4(0f, 1f, 0f, (nfloat)Math.PI / 2);
                node.RunAction(repeat);

                largeBananaCollectable = node;
            }

            SCNNode nodeSparkle = largeBananaCollectable.Clone();

            SCNParticleSystem newSystem = GameSimulation.LoadParticleSystemWithName("sparkle");

            nodeSparkle.AddParticleSystem(newSystem);

            return(nodeSparkle);
        }
Exemplo n.º 28
0
        void SetupPathColliders()
        {
            // Collect all the nodes that start with path_ under the dummy_front object.
            // Set those objects as Physics category ground and create a static concave mesh collider.
            // The simulation will use these as the ground to walk on.
            SCNNode front = RootNode.FindChildNode("dummy_front", true);

            foreach (var fronChild in front.ChildNodes)
            {
                foreach (var child in fronChild.ChildNodes)
                {
                    if (child.Name.Contains("path_"))
                    {
                        //the geometry is attached to the first child node of the node named path
                        SCNNode path    = child.ChildNodes [0];
                        var     options = new SCNPhysicsShapeOptions {
                            ShapeType = SCNPhysicsShapeType.ConcavePolyhedron
                        };
                        path.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Static, SCNPhysicsShape.Create(path.Geometry, options));
                        path.PhysicsBody.CategoryBitMask = GameCollisionCategory.Ground;
                    }
                }
            }
        }
Exemplo n.º 29
0
        void SetupGetCoconutAnimation()
        {
            SCNAnimationEventHandler pickupEventBlock = (CAAnimation animation, NSObject animatedObject, bool playingBackward) => {
                if (coconutInHand != null)
                {
                    coconutInHand.RemoveFromParentNode();
                }

                coconutInHand = Coconut.CoconutProtoObject;
                rightHand.AddChildNode(coconutInHand);
                hasCoconut = true;
            };

            CAAnimation getAnimation = LoadAndCacheAnimation(GameSimulation.PathForArtResource("characters/monkey/monkey_get_coconut"), "monkey_get_coconut-1");

            if (getAnimation.AnimationEvents == null)
            {
                getAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.4f, pickupEventBlock) }
            }
            ;

            getAnimation.RepeatCount = 1;
        }

        void SetupThrowAnimation()
        {
            CAAnimation throwAnimation = LoadAndCacheAnimation(GameSimulation.PathForArtResource("characters/monkey/monkey_throw_coconut"), "monkey_throw_coconut-1");

            throwAnimation.Speed = 1.5f;

            if (throwAnimation.AnimationEvents == null || throwAnimation.AnimationEvents.Length == 0)
            {
                SCNAnimationEventHandler throwEventBlock = ThrowCoconut;

                throwAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.35f, throwEventBlock) };
            }

            throwAnimation.RepeatCount = 0;
        }

        void ThrowCoconut(CAAnimation animation, NSObject animatedObject, bool playingBackward)
        {
            if (!hasCoconut)
            {
                return;
            }

            SCNMatrix4 worldMtx = coconutInHand.PresentationNode.WorldTransform;

            coconutInHand.RemoveFromParentNode();

            Coconut         node = Coconut.CoconutThrowProtoObject;
            SCNPhysicsShape coconutPhysicsShape = Coconut.CoconutPhysicsShape;

            node.PhysicsBody                  = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, coconutPhysicsShape);
            node.PhysicsBody.Restitution      = 0.9f;
            node.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground;
            node.PhysicsBody.CategoryBitMask  = GameCollisionCategory.Coconut;

            node.Transform = worldMtx;
            GameSimulation.Sim.RootNode.AddChildNode(node);
            GameSimulation.Sim.GameLevel.Coconuts.Add(node);
            node.PhysicsBody.ApplyForce(new SCNVector3(-200, 500, 300), true);
            hasCoconut = false;
            isIdle     = true;
        }
    }
Exemplo n.º 30
0
 public PlaneNode(ARPlaneAnchor planeAnchor)
 {
     Geometry    = (planeGeometry = CreateGeometry(planeAnchor));
     Position    = new SCNVector3(0, -PlaneHeight / 2, 0);
     PhysicsBody = SCNPhysicsBody.CreateKinematicBody();
 }