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);
        }
        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());
        }
Exemple #3
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 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);
        }
Exemple #5
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();
        }
Exemple #6
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));
        }
        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);
                }
            }
        }
        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);
        }
        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);
        }
Exemple #10
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
                };
            }
        }
Exemple #11
0
        private void LoadCharacter()
        {
            // Load character from external file
            var scene = SCNScene.FromFile("art.scnassets/character/max.scn");

            this.model          = scene.RootNode.FindChildNode("Max_rootNode", true);
            this.model.Position = Character.ModelOffset;

            /* setup character hierarchy
             * character
             |_orientationNode
             |_model
             */

            this.characterNode = new SCNNode {
                Name = "character", Position = Character.InitialPosition
            };

            this.characterOrientation = new SCNNode();
            this.characterNode.AddChildNode(this.characterOrientation);
            this.characterOrientation.AddChildNode(this.model);

            var collider = this.model.FindChildNode("collider", true);

            if (collider?.PhysicsBody != null)
            {
                collider.PhysicsBody.CollisionBitMask = (int)(Bitmask.Enemy | Bitmask.Trigger | Bitmask.Collectable);
            }

            // Setup collision shape
            var min = new SCNVector3();
            var max = new SCNVector3();

            this.model.GetBoundingBox(ref min, ref max);
            nfloat collisionCapsuleRadius = (max.X - min.X) * 0.4f;
            nfloat collisionCapsuleHeight = max.Y - min.Y;

            var collisionGeometry = SCNCapsule.Create(collisionCapsuleRadius, collisionCapsuleHeight);

            this.characterCollisionShape = SCNPhysicsShape.Create(collisionGeometry,
                                                                  new NSMutableDictionary()
            {
                { SCNPhysicsShapeOptionsKeys.CollisionMargin, NSNumber.FromFloat(Character.CollisionMargin) }
            });
            this.collisionShapeOffsetFromModel = new SCNVector3(0f, (float)collisionCapsuleHeight * 0.51f, 0f);
        }
Exemple #12
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);
        }
        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);
            }
        }
Exemple #14
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);
        }
        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;
        }
    }
Exemple #16
0
        public static SCNNode ReplaceCatapultPlaceholder(SCNNode placeholder)
        {
            var node = SCNNodeExtensions.LoadSCNAsset("catapult");

            // somehow setting the world transform doesn't update the Euler angles (180, 0, 180) is decoded
            //  but need it to be 0, 180, 0
            node.Transform   = placeholder.Transform;
            node.EulerAngles = placeholder.EulerAngles;

            // Add physics body to it
            node.WorldPosition += new SCNVector3(0f, 0.2f, 0f);
            node.PhysicsBody?.ResetTransform();

            var baseGeomNode = node.FindChildNode("catapultBase", true);

            if (baseGeomNode == null)
            {
                throw new Exception("No catapultBase");
            }

            var prongGeomNode = node.FindChildNode("catapultProngs", true);

            if (prongGeomNode == null)
            {
                throw new Exception("No catapultProngs");
            }

            // shift center of mass of the prong from the bottom
            // the 0.55 value is from experimentation
            var prongPivotShiftUp = new SCNVector3(0f, 0.55f, 0f);

            prongGeomNode.Pivot     = SCNMatrix4.CreateTranslation(prongPivotShiftUp);
            prongGeomNode.Position += prongPivotShiftUp;

            var baseShape = SCNPhysicsShape.Create(baseGeomNode, new SCNPhysicsShapeOptions()
            {
                ShapeType = SCNPhysicsShapeType.ConvexHull
            });
            var prongShape = SCNPhysicsShape.Create(prongGeomNode, new SCNPhysicsShapeOptions()
            {
                ShapeType = SCNPhysicsShapeType.ConvexHull
            });

            var compoundShape = SCNPhysicsShape.Create(new SCNPhysicsShape[] { baseShape, prongShape },
                                                       new SCNMatrix4[] { SCNMatrix4.Identity, SCNMatrix4.Identity });

            if (node.PhysicsBody != null)
            {
                node.PhysicsBody.PhysicsShape = compoundShape;
            }

            // rename back to placeholder name must happen after gameObject is assigned
            // currently placeholders are all Catapult1 to Catapult6, they may be under a teamA, teamB parent
            // so stash the placeholder name for later
            if (!string.IsNullOrEmpty(placeholder.Name))
            {
                node.SetValueForKey(new NSString(placeholder.Name), new NSString("nameRestore"));
            }

            placeholder.ParentNode.ReplaceChildNode(placeholder, node);

            node.Name = "catapult";

            return(node);
        }
Exemple #17
0
        public Character()
        {
            for (int i = 0; i < StepsSoundCount; i++)
            {
                steps [i, (int)FloorMaterial.Grass]        = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_grass_0{0}.mp3", i));
                steps [i, (int)FloorMaterial.Grass].Volume = 0.5f;
                steps [i, (int)FloorMaterial.Rock]         = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_rock_0{0}.mp3", i));
                if (i < StepsInWaterSoundCount)
                {
                    steps [i, (int)FloorMaterial.Water] = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_splash_0{0}.mp3", i));
                    steps [i, (int)FloorMaterial.Water].Load();
                }
                else
                {
                    steps [i, (int)FloorMaterial.Water] = steps [i % StepsInWaterSoundCount, (int)FloorMaterial.Water];
                }

                steps [i, (int)FloorMaterial.Rock].Load();
                steps [i, (int)FloorMaterial.Grass].Load();

                // Load the character.
                SCNScene characterScene        = SCNScene.FromFile("game.scnassets/panda.scn");
                SCNNode  characterTopLevelNode = characterScene.RootNode.ChildNodes [0];

                Node = SCNNode.Create();
                Node.AddChildNode(characterTopLevelNode);

                // Configure the "idle" animation to repeat forever
                foreach (var childNode in characterTopLevelNode.ChildNodes)
                {
                    foreach (var key in childNode.GetAnimationKeys())
                    {
                        CAAnimation animation = childNode.GetAnimation(key);
                        animation.UsesSceneTimeBase = false;
                        animation.RepeatCount       = float.PositiveInfinity;
                        childNode.AddAnimation(animation, key);
                    }
                }

                // retrieve some particle systems and save their birth rate
                fireEmitter   = characterTopLevelNode.FindChildNode("fire", true);
                fireBirthRate = fireEmitter.ParticleSystems [0].BirthRate;
                fireEmitter.ParticleSystems [0].BirthRate = 0;
                fireEmitter.Hidden = false;

                smokeEmitter   = characterTopLevelNode.FindChildNode("smoke", true);
                smokeBirthRate = smokeEmitter.ParticleSystems [0].BirthRate;
                smokeEmitter.ParticleSystems [0].BirthRate = 0;
                smokeEmitter.Hidden = false;

                whiteSmokeEmitter   = characterTopLevelNode.FindChildNode("whiteSmoke", true);
                whiteSmokeBirthRate = whiteSmokeEmitter.ParticleSystems [0].BirthRate;
                whiteSmokeEmitter.ParticleSystems [0].BirthRate = 0;
                whiteSmokeEmitter.Hidden = false;

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

                Node.GetBoundingBox(ref min, ref max);

                float radius = (max.X - min.X) * .4f;
                float height = (max.Y - min.Y);

                // Create a kinematic with capsule.
                SCNNode colliderNode = SCNNode.Create();
                colliderNode.Name        = "collider";
                colliderNode.Position    = new SCNVector3(0f, height * .51f, 0f);
                colliderNode.PhysicsBody = SCNPhysicsBody.CreateBody(
                    SCNPhysicsBodyType.Kinematic,
                    SCNPhysicsShape.Create(SCNCapsule.Create(radius, height))
                    );

                // We want contact notifications with the collectables, enemies and walls.
                colliderNode.PhysicsBody.ContactTestBitMask = (nuint)(int)(Bitmask.SuperCollectable | Bitmask.Collectable | Bitmask.Collision | Bitmask.Enemy);
                Node.AddChildNode(colliderNode);

                walkAnimation = LoadAnimationFromSceneNamed("game.scnassets/walk.scn");
                walkAnimation.UsesSceneTimeBase = false;
                walkAnimation.FadeInDuration    = .3f;
                walkAnimation.FadeOutDuration   = .3f;
                walkAnimation.RepeatCount       = float.PositiveInfinity;
                walkAnimation.Speed             = CharacterSpeedFactor;

                // Play foot steps at specific times in the animation
                walkAnimation.AnimationEvents = new [] {
                    SCNAnimationEvent.Create(.1f, (animation, animatedObject, playingBackward) => PlayFootStep()),
                    SCNAnimationEvent.Create(.6f, (animation, animatedObject, playingBackward) => PlayFootStep())
                };
            }
        }
Exemple #18
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;
                    }
                }
            }
        }
Exemple #19
0
        private void AddTrainToScene(SCNScene scene, SCNVector3 position)
        {
            var max        = SCNVector3.Zero;
            var min        = SCNVector3.Zero;
            var trainScene = SCNScene.FromFile("train_flat", ResourceManager.ResourceFolder, (NSDictionary)null);

            foreach (var node in trainScene.RootNode.ChildNodes)
            {
                if (node.Geometry != null)
                {
                    node.Position = new SCNVector3(
                        node.Position.X + position.X,
                        node.Position.Y + position.Y,
                        node.Position.Z + position.Z
                        );

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

                    node.GetBoundingBox(ref min, ref max);

                    var body     = SCNPhysicsBody.CreateDynamicBody();
                    var boxShape = new SCNBox {
                        Width         = max.X - min.X,
                        Height        = max.Y - min.Y,
                        Length        = max.Z - min.Z,
                        ChamferRadius = 0f
                    };

                    body.PhysicsShape = SCNPhysicsShape.Create(boxShape, (NSDictionary)null);
                    node.Pivot        = SCNMatrix4.CreateTranslation(0f, -min.Y, 0f);
                    node.PhysicsBody  = body;
                    scene.RootNode.AddChildNode(node);
                }
            }

            var smokeHandle         = scene.RootNode.FindChildNode("Smoke", true);
            var smokeParticleSystem = SCNParticleSystem.Create("smoke", ResourceManager.ResourceFolder);

            smokeParticleSystem.ParticleImage = ResourceManager.GetResourcePath("smoke.png");
            smokeHandle.AddParticleSystem(smokeParticleSystem);

            var engineCar = scene.RootNode.FindChildNode("EngineCar", false);
            var wagon1    = scene.RootNode.FindChildNode("Wagon1", false);
            var wagon2    = scene.RootNode.FindChildNode("Wagon2", false);

            max = SCNVector3.Zero;
            min = SCNVector3.Zero;
            engineCar.GetBoundingBox(ref min, ref max);

            var wmax = SCNVector3.Zero;
            var wmin = SCNVector3.Zero;

            wagon1.GetBoundingBox(ref wmin, ref wmax);

            var anchorA = new SCNVector3(max.X, min.Y, 0f);
            var anchorB = new SCNVector3(wmin.X, wmin.Y, 0f);

            var joint = SCNPhysicsBallSocketJoint.Create(engineCar.PhysicsBody, anchorA, wagon1.PhysicsBody, anchorB);

            scene.PhysicsWorld.AddBehavior(joint);

            joint = SCNPhysicsBallSocketJoint.Create(wagon1.PhysicsBody,
                                                     new SCNVector3(wmax.X + 0.1f, wmin.Y, 0f),
                                                     wagon2.PhysicsBody,
                                                     new SCNVector3(wmin.X - 0.1f, wmin.Y, 0f)
                                                     );

            scene.PhysicsWorld.AddBehavior(joint);
        }
Exemple #20
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);
        }