private void markerHelper(GameTime gameTime)
        {
            if (rotationFlag)
            {
                if (rotationDirection == "Clockwise")
                {
                    if (label == "Humvee")
                    {
                        if (rotationAxis == "x-axis")
                            humveeX = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "y-axis")
                            humveeY = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "z-axis")
                            humveeZ = (float) rotationSpeed * (float)0.01;
                        humveeRotation = humveeRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), humveeX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), humveeY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), humveeZ);
                    }
                    else if (label == "Gears")
                    {
                        if (rotationAxis == "x-axis")
                            gearsX = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "y-axis")
                            gearsY = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "z-axis")
                            gearsZ = (float) rotationSpeed * (float)0.01;
                        gearsRotation = gearsRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), gearsX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), gearsY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), gearsZ);
                    }
                    else if (label == "Cup")
                    {
                        if (rotationAxis == "x-axis")
                            cupX = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "y-axis")
                            cupY = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "z-axis")
                            cupZ = (float) rotationSpeed * (float)0.01;
                        cupRotation = cupRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), cupX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), cupY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), cupZ);
                    }
                    else if (label == "G36C Gun")
                    {
                        if (rotationAxis == "x-axis")
                            g36cX = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "y-axis")
                            g36cY = (float) rotationSpeed * (float)0.01;
                        if (rotationAxis == "z-axis")
                            g36cZ = (float) rotationSpeed * (float)0.01;
                        g36cRotation = g36cRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), g36cX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), g36cY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), g36cZ);
                    }
                }
                if (rotationDirection == "Counterclockwise")
                {
                    if (label == "Humvee")
                    {
                        if (rotationAxis == "x-axis")
                            humveeX = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "y-axis")
                            humveeY = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "z-axis")
                            humveeZ = rotationSpeed * (float) -0.01;
                        humveeRotation = humveeRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), humveeX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), humveeY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), humveeZ);
                    }
                    else if (label == "Gears")
                    {
                        if (rotationAxis == "x-axis")
                            gearsX = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "y-axis")
                            gearsY = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "z-axis")
                            gearsZ = rotationSpeed * (float) -0.01;
                        gearsRotation = gearsRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), gearsX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), gearsY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), gearsZ);
                    }
                    else if (label == "Cup")
                    {
                        if (rotationAxis == "x-axis")
                            cupX = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "y-axis")
                            cupY = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "z-axis")
                            cupZ = rotationSpeed * (float) -0.01;
                        cupRotation = cupRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), cupX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), cupY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), cupZ);
                    }
                    else if (label == "G36C Gun")
                    {
                        if (rotationAxis == "x-axis")
                            g36cX = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "y-axis")
                            g36cY = rotationSpeed * (float) -0.01;
                        if (rotationAxis == "z-axis")
                            g36cZ = rotationSpeed * (float) -0.01;
                        g36cRotation = g36cRotation * Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), g36cX) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), g36cY) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), g36cZ);
                    }
                }
            }

            if (scaleFlag)
            {
                if (groundMarkerNode.MarkerFound)
                {
                    if (toolbarMarkerNode.MarkerFound)
                    {
                        if (label == "Humvee")
                        {
                            if (toolbarMarkerNode.WorldTransformation.Translation.Y > 0)
                                humveeSize = toolbarMarkerNode.WorldTransformation.Translation.Y * (float)0.01;
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(humvee.Physics, Matrix.CreateScale(humveeSize) * humveeRotation * humveeTrans * humveeMatrix);
                        }
                        if (label == "Gears")
                        {
                            if (toolbarMarkerNode.WorldTransformation.Translation.Y > 0)
                                gearsSize = toolbarMarkerNode.WorldTransformation.Translation.Y * (float)0.01;
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(gears.Physics, Matrix.CreateScale(gearsSize) * gearsRotation * gearsTrans * gearsMatrix);
                        }
                        if (label == "Cup")
                        {
                            if (toolbarMarkerNode.WorldTransformation.Translation.Y > 0)
                                cupSize = toolbarMarkerNode.WorldTransformation.Translation.Y * (float)0.01;
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(cup.Physics, Matrix.CreateScale(cupSize) * cupRotation * cupTrans * cupMatrix);
                        }
                        if (label == "G36C Gun")
                        {
                            if (toolbarMarkerNode.WorldTransformation.Translation.Y > 0)
                                g36cSize = toolbarMarkerNode.WorldTransformation.Translation.Y * (float)0.01;
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(g36c.Physics, Matrix.CreateScale(g36cSize) * g36cRotation * g36cTrans * g36cMatrix);
                        }
                    }
                }
            }

            if (rotationObjectFlag)
            {
                if (groundMarkerNode.MarkerFound)
                {
                    if (toolbarMarkerNode.MarkerFound)
                    {
                        if (label == "Humvee")
                        {
                            humveeRotation = Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Y * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Z * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.X * 6));
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(humvee.Physics, Matrix.CreateScale(humveeSize) * humveeRotation * humveeTrans * humveeMatrix);
                        }
                        if (label == "Gears")
                        {
                            gearsRotation = Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Y * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Z * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.X * 6));
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(gears.Physics, Matrix.CreateScale(gearsSize) * gearsRotation * gearsTrans * gearsMatrix);
                        }
                        if (label == "Cup")
                        {
                            cupRotation = Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Y * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Z * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.X * 6));
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(cup.Physics, Matrix.CreateScale(cupSize) * cupRotation * cupTrans * cupMatrix);
                        }
                        if (label == "G36C Gun")
                        {
                            g36cRotation = Matrix.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Y * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.Z * 6)) * Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), MathHelper.ToRadians(toolbarMarkerNode.WorldTransformation.Translation.X * 6));
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(g36c.Physics, Matrix.CreateScale(g36cSize) * g36cRotation * g36cTrans * g36cMatrix);
                        }
                    }
                }
            }

            if (translationModeFlag)
            {
                // If ground marker array is detected
                if (groundMarkerNode.MarkerFound)
                {
                    if (toolbarMarkerNode.MarkerFound)
                    {
                        if (label == "Humvee")
                        {
                            humveeMatrix = Matrix.CreateTranslation(toolbarMarkerNode.WorldTransformation.Translation) * Matrix.Invert(groundMarkerNode.WorldTransformation);
                            // Modify the transformation in the physics engine
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(humvee.Physics, Matrix.CreateScale(humveeSize) * humveeRotation * humveeTrans * humveeMatrix);
                        }
                        if (label == "Gears")
                        {
                            gearsMatrix = Matrix.CreateTranslation(toolbarMarkerNode.WorldTransformation.Translation) * Matrix.Invert(groundMarkerNode.WorldTransformation);
                            // Modify the transformation in the physics engine
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(gears.Physics, Matrix.CreateScale(gearsSize) * gearsRotation * gearsTrans * gearsMatrix);
                        }
                        if (label == "Cup")
                        {
                            cupMatrix = Matrix.CreateTranslation(toolbarMarkerNode.WorldTransformation.Translation) * Matrix.Invert(groundMarkerNode.WorldTransformation);
                            // Modify the transformation in the physics engine
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(cup.Physics, Matrix.CreateScale(cupSize) * cupRotation * cupTrans * cupMatrix);
                        }
                        if (label == "G36C Gun")
                        {
                            g36cMatrix = Matrix.CreateTranslation(toolbarMarkerNode.WorldTransformation.Translation) * Matrix.Invert(groundMarkerNode.WorldTransformation);
                            // Modify the transformation in the physics engine
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(g36c.Physics, Matrix.CreateScale(g36cSize) * g36cRotation * g36cTrans * g36cMatrix);
                        }
                    }
                }
            }
            else if (translationModeFlag == false || label == "Nothing is selected")
            {
                // If ground marker array is detected
                if (groundMarkerNode.MarkerFound)
                {
                    if (resetFlag == true)
                    {
                        UI2DRenderer.WriteText(Vector2.Zero, "System has been reset", Color.Red, textFont1, GoblinEnums.HorizontalAlignment.Right, GoblinEnums.VerticalAlignment.Top);
                    }
                    if (label == "Humvee" || label == "Gears" || label == "Cup" || label == "G36C Gun")
                        if (panelTrigger)
                            objectFrame.Visible = true;
                    // If the toolbar marker array is detected
                    if (toolbarMarkerNode.MarkerFound)
                    {
                        if (collusionflag == false)
                        {
                            // Create collision pair1 and add a collision callback function that will be called when the pair collides
                            NewtonPhysics.CollisionPair pair1 = new NewtonPhysics.CollisionPair(humvee.Physics, pointingMarker.Physics);
                            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair1, humveeCollision);
                            // Create collision pair2 and add a collision callback function that will be called when the pair collides
                            NewtonPhysics.CollisionPair pair2 = new NewtonPhysics.CollisionPair(gears.Physics, pointingMarker.Physics);
                            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair2, gearsCollision);
                            // Create collision pair3 and add a collision callback function that will be called when the pair collides
                            NewtonPhysics.CollisionPair pair3 = new NewtonPhysics.CollisionPair(cup.Physics, pointingMarker.Physics);
                            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair3, cupCollision);
                            // Create collision pair4 and add a collision callback function that will be called when the pair collides
                            NewtonPhysics.CollisionPair pair4 = new NewtonPhysics.CollisionPair(g36c.Physics, pointingMarker.Physics);
                            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair4, g36cCollision);
                            collusionflag = true;
                        }
                        if (rotationObjectFlag)
                        {
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(pointingMarker.Physics, Matrix.CreateTranslation(-200, -200, -200));
                            pointingMarker.Material.Diffuse = new Vector4(pointingMarker.Material.Diffuse.X, pointingMarker.Material.Diffuse.Y, pointingMarker.Material.Diffuse.Z, 0);
                        }
                        else if (scaleFlag)
                        {
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(pointingMarker.Physics, Matrix.CreateTranslation(-200, -200, -200));
                            pointingMarker.Material.Diffuse = new Vector4(pointingMarker.Material.Diffuse.X, pointingMarker.Material.Diffuse.Y, pointingMarker.Material.Diffuse.Z, 0);
                        }
                        else
                        {
                            pointingMarker.Material.Diffuse = new Vector4(pointingMarker.Material.Diffuse.X, pointingMarker.Material.Diffuse.Y, pointingMarker.Material.Diffuse.Z, 0.6f);
                            Matrix markerMatrix = Matrix.CreateScale((float)0.4) * toolbarMarkerNode.WorldTransformation * Matrix.Invert(groundMarkerNode.WorldTransformation);
                            // Modify the transformation in the physics engine
                            ((NewtonPhysics)scene.PhysicsEngine).SetTransform(pointingMarker.Physics, markerMatrix);
                        }
                    }
                    else
                    {
                        ((NewtonPhysics)scene.PhysicsEngine).SetTransform(pointingMarker.Physics, Matrix.CreateTranslation(-400, -400, -400));
                        pointingMarker.Material.Diffuse = new Vector4(pointingMarker.Material.Diffuse.X, pointingMarker.Material.Diffuse.Y, pointingMarker.Material.Diffuse.Z, 0);
                    }
                    ((NewtonPhysics)scene.PhysicsEngine).SetTransform(humvee.Physics, Matrix.CreateScale(humveeSize) * humveeRotation * humveeTrans * humveeMatrix);
                    ((NewtonPhysics)scene.PhysicsEngine).SetTransform(gears.Physics, Matrix.CreateScale(gearsSize) * gearsRotation * gearsTrans * gearsMatrix);
                    ((NewtonPhysics)scene.PhysicsEngine).SetTransform(cup.Physics, Matrix.CreateScale(cupSize) * cupRotation * cupTrans * cupMatrix);
                    ((NewtonPhysics)scene.PhysicsEngine).SetTransform(g36c.Physics, Matrix.CreateScale(g36cSize) * g36cRotation * g36cTrans * g36cMatrix);
                }
                else
                {
                    objectFrame.Visible = false;
                }
            }
            if (groundMarkerNode.MarkerFound == true && toolbarMarkerNode.MarkerFound == true && transferFlag == true)
            {
                pointingMarker.Material.Diffuse = new Vector4(pointingMarker.Material.Diffuse.X, pointingMarker.Material.Diffuse.Y, pointingMarker.Material.Diffuse.Z, 0);
                if (label == "Humvee")
                {
                    if (humveeflag == false)
                    {
                        groundMarkerNode.RemoveChild(humvee);
                        toolbarMarkerNode.AddChild(humvee);
                        humveeflag = true;
                    }
                }
                if (label == "Gears")
                {
                    if (gearsflag == false)
                    {
                        groundMarkerNode.RemoveChild(gears);
                        toolbarMarkerNode.AddChild(gears);
                        gearsflag = true;
                    }
                }
                if (label == "Cup")
                {
                    if (cupflag == false)
                    {
                        groundMarkerNode.RemoveChild(cup);
                        toolbarMarkerNode.AddChild(cup);
                        cupflag = true;
                    }
                }
                if (label == "G36C Gun")
                {
                    if (g36cflag == false)
                    {
                        groundMarkerNode.RemoveChild(g36c);
                        toolbarMarkerNode.AddChild(g36c);
                        g36cflag = true;
                    }
                }
            }
            if (groundMarkerNode.MarkerFound == true && toolbarMarkerNode.MarkerFound == false && transferFlag == true)
            {
                if (humveeflag == true)
                {
                    toolbarMarkerNode.RemoveChild(humvee);
                    // Create humvee model.
                    ModelLoader loader = new ModelLoader();
                    humvee = new GeometryNode("Humvee");
                    humvee.Model = (Model)loader.Load("", "humvee");
                    // Add this humvee model to the physics engine for collision detection
                    humvee.AddToPhysicsEngine = true;
                    humvee.Physics.Shape = ShapeType.ConvexHull;
                    // Set model materials.
                    ((Model)humvee.Model).UseInternalMaterials = true;
                    // Create collision pair1 and add a collision callback function that will be called when the pair collides
                    NewtonPhysics.CollisionPair pair1 = new NewtonPhysics.CollisionPair(humvee.Physics, pointingMarker.Physics);
                    ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair1, humveeCollision);
                    ((Model)humvee.Model).ShowBoundingBox = true;
                    groundMarkerNode.AddChild(humvee);
                    humveeflag = false;
                }
                if (gearsflag == true)
                {
                    toolbarMarkerNode.RemoveChild(gears);
                    // Create humvee model.
                    ModelLoader loader = new ModelLoader();
                    // Create gears model.
                    gears = new GeometryNode("Gears");
                    gears.Model = (Model)loader.Load("", "gears");
                    // Add this gears model to the physics engine for collision detection
                    gears.AddToPhysicsEngine = true;
                    gears.Physics.Shape = ShapeType.ConvexHull;
                    // Set model materials.
                    ((Model)gears.Model).UseInternalMaterials = true;
                    // Create collision pair2 and add a collision callback function that will be called when the pair collides
                    NewtonPhysics.CollisionPair pair2 = new NewtonPhysics.CollisionPair(gears.Physics, pointingMarker.Physics);
                    ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair2, gearsCollision);
                    ((Model)gears.Model).ShowBoundingBox = true;
                    groundMarkerNode.AddChild(gears);
                    gearsflag = false;
                }
                if (cupflag == true)
                {
                    toolbarMarkerNode.RemoveChild(cup);
                    // Create humvee model.
                    ModelLoader loader = new ModelLoader();
                    // Create cup model.
                    cup = new GeometryNode("Cup");
                    cup.Model = (Model)loader.Load("", "bardak");
                    // Add this cup model to the physics engine for collision detection
                    cup.AddToPhysicsEngine = true;
                    cup.Physics.Shape = ShapeType.ConvexHull;
                    // Set model materials.
                    ((Model)cup.Model).UseInternalMaterials = true;
                    // Create collision pair3 and add a collision callback function that will be called when the pair collides
                    NewtonPhysics.CollisionPair pair3 = new NewtonPhysics.CollisionPair(cup.Physics, pointingMarker.Physics);
                    ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair3, cupCollision);
                    ((Model)cup.Model).ShowBoundingBox = true;
                    groundMarkerNode.AddChild(cup);
                    cupflag = false;
                }
                if (g36cflag == true)
                {
                    toolbarMarkerNode.RemoveChild(g36c);
                    // Create humvee model.
                    ModelLoader loader = new ModelLoader();
                    // Create g36c model.
                    g36c = new GeometryNode("G36C Gun");
                    g36c.Model = (Model)loader.Load("", "g36c");
                    // Add this g36c model to the physics engine for collision detection
                    g36c.AddToPhysicsEngine = true;
                    g36c.Physics.Shape = ShapeType.ConvexHull;
                    // Set model materials.
                    ((Model)g36c.Model).UseInternalMaterials = true;
                    // Create collision pair4 and add a collision callback function that will be called when the pair collides
                    NewtonPhysics.CollisionPair pair4 = new NewtonPhysics.CollisionPair(g36c.Physics, pointingMarker.Physics);
                    ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair4, g36cCollision);
                    ((Model)g36c.Model).ShowBoundingBox = true;
                    groundMarkerNode.AddChild(g36c);
                    g36cflag = false;
                }
            }
        }
コード例 #2
0
ファイル: Tutorial8.cs プロジェクト: NinjaSteph/SureShot
        private void CreateObjects()
        {
            // Create a geometry node with a model of a sphere that will be overlaid on
            // top of the ground marker array
            GeometryNode sphereNode = new GeometryNode("Sphere");
            // We will use TexturedSphere instead of regular Box class since we will need the
            // texture coordinates elements for passing the vertices to the SimpleShadowShader
            // we will be using
            sphereNode.Model = new TexturedSphere(16, 20, 20);

            // Add this sphere model to the physics engine for collision detection
            sphereNode.AddToPhysicsEngine = true;
            sphereNode.Physics.Shape = ShapeType.Sphere;
            // Make this sphere model cast and receive shadows
            sphereNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast;
            // Assign a shadow shader for this model that uses the IShadowMap we assigned to the scene
            sphereNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap);

            // Create a marker node to track a ground marker array.
#if USE_NYARTOOLKIT
            groundMarkerNode = new MarkerNode(scene.MarkerTracker, "NyARToolkitGroundArray.xml",
                NyARToolkitTracker.ComputationMethod.Average);
#else
            groundMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARGroundArray.xml");
#endif

            TransformNode sphereTransNode = new TransformNode();
            sphereTransNode.Translation = new Vector3(0, 0, 50);

            // Create a material to apply to the sphere model
            Material sphereMaterial = new Material();
            sphereMaterial.Diffuse = new Vector4(0, 0.5f, 0, 1);
            sphereMaterial.Specular = Color.White.ToVector4();
            sphereMaterial.SpecularPower = 10;

            sphereNode.Material = sphereMaterial;

            // Now add the above nodes to the scene graph in the appropriate order.
            // Note that only the nodes added below the marker node are affected by 
            // the marker transformation.
            scene.RootNode.AddChild(groundMarkerNode);
            groundMarkerNode.AddChild(sphereTransNode);
            sphereTransNode.AddChild(sphereNode);

            // Create a geometry node with a model of a box that will be overlaid on
            // top of the ground marker array initially. (When the toolbar marker array is
            // detected, it will be overlaid on top of the toolbar marker array.)
            boxNode = new GeometryNode("Box");
            // We will use TexturedBox instead of regular Box class since we will need the
            // texture coordinates elements for passing the vertices to the SimpleShadowShader
            // we will be using
            boxNode.Model = new TexturedBox(32.4f);

            // Add this box model to the physics engine for collision detection
            boxNode.AddToPhysicsEngine = true;
            boxNode.Physics.Shape = ShapeType.Box;
            // Make this box model cast and receive shadows
            boxNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast;
            // Assign a shadow shader for this model that uses the IShadowMap we assigned to the scene
            boxNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap);

            // Create a marker node to track a toolbar marker array.
#if USE_NYARTOOLKIT
            toolbarMarkerNode = new MarkerNode(scene.MarkerTracker, "ToolbarNyARToolkit.xml",
                NyARToolkitTracker.ComputationMethod.Average);
#else
            toolbarMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARToolbar.xml");
#endif

            scene.RootNode.AddChild(toolbarMarkerNode);

            // Create a material to apply to the box model
            Material boxMaterial = new Material();
            boxMaterial.Diffuse = new Vector4(0.5f, 0, 0, 1);
            boxMaterial.Specular = Color.White.ToVector4();
            boxMaterial.SpecularPower = 10;

            boxNode.Material = boxMaterial;

            // Add this box model node to the ground marker node
            groundMarkerNode.AddChild(boxNode);

            // Create a collision pair and add a collision callback function that will be
            // called when the pair collides
            NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(boxNode.Physics, sphereNode.Physics);
            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, BoxSphereCollision);
        }
コード例 #3
0
ファイル: Game1.cs プロジェクト: RobinLieson/ar-battle-boats
        private void CreateObjects()
        {
            allShapesNode = new GeometryNode();
            allShapesMat = new Material();
            allShapesTransNode = new TransformNode();

            // Create a geometry node with a model of a sphere that will be overlaid on
            // top of the ground marker array
            sphereNode = new GeometryNode("Sphere");
            sphereNode.Model = new Sphere(3, 20, 20);

            // Add this sphere model to the physics engine for collision detection
            sphereNode.AddToPhysicsEngine = true;
            sphereNode.Physics.Shape = ShapeType.Sphere;
            // Make this sphere model cast and receive shadows
            sphereNode.Model.CastShadows = true;
            sphereNode.Model.ReceiveShadows = true;

            // Create a marker node to track a ground marker array.
            groundMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARGroundArray.xml");

            // Since the ground marker's size is 80x52 ARTag units, in order to move the sphere model
            // to the center of the ground marker, we shift it by 40x26 units and also make it
            // float from the ground marker's center
            TransformNode sphereTransNode = new TransformNode();
            sphereTransNode.Translation = new Vector3(40, 26, 10);

            // Create a material to apply to the sphere model
            Material sphereMaterial = new Material();
            sphereMaterial.Diffuse = new Vector4(0, 0.5f, 0, 1);
            sphereMaterial.Specular = Color.White.ToVector4();
            sphereMaterial.SpecularPower = 10;

            sphereNode.Material = sphereMaterial;

            // Now add the above nodes to the scene graph in the appropriate order.
            // Note that only the nodes added below the marker node are affected by
            // the marker transformation.
            scene.RootNode.AddChild(groundMarkerNode);
            groundMarkerNode.AddChild(sphereTransNode);
            sphereTransNode.AddChild(sphereNode);

            // Create a geometry node with a model of a box that will be overlaid on
            // top of the ground marker array initially. (When the toolbar marker array is
            // detected, it will be overlaid on top of the toolbar marker array.)
            boxNode = new GeometryNode("Box");
            boxNode.Model = new Box(8);

            // Add this box model to the physics engine for collision detection
            boxNode.AddToPhysicsEngine = true;
            boxNode.Physics.Shape = ShapeType.Box;
            // Make this box model cast and receive shadows
            boxNode.Model.CastShadows = true;
            boxNode.Model.ReceiveShadows = true;

            // Create a marker node to track a toolbar marker array.
            toolbarMarkerNode = new MarkerNode(scene.MarkerTracker, "Toolbar.txt");

            scene.RootNode.AddChild(toolbarMarkerNode);

            // Create a material to apply to the box model
            Material boxMaterial = new Material();
            boxMaterial.Diffuse = new Vector4(0.5f, 0, 0, 1);
            boxMaterial.Specular = Color.White.ToVector4();
            boxMaterial.SpecularPower = 10;

            boxNode.Material = boxMaterial;

            // Add this box model node to the ground marker node
            groundMarkerNode.AddChild(boxNode);

            // Create a collision pair and add a collision callback function that will be
            // called when the pair collides
            NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(boxNode.Physics, sphereNode.Physics);
            ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, BoxSphereCollision);

            //NewtonPhysics.CollisionPair tmp = new NewtonPhysics.CollisionPair

               // NewtonMaterial.ContactBegin startWar = new NewtonMaterial.ContactBegin(boxNode.Physics, sphereNode.Physics);

            ids1 = new int[4];
            ids1[0] = 70;
            ids1[1] = 71;
            ids1[2] = 72;
            ids1[3] = 73;
            MarkerNode1 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML1.xml", ids1);
            cylinderNode1 = new GeometryNode("ENEMY's SHIP");
            cylinderNode1.Model = new Cylinder(3, 3, 6, 10);
            cylinderNode1.Material = sphereMaterial;
            TransformNode cylinderTransNode = new TransformNode();
            cylinderTransNode.Translation = new Vector3(0, 0, 3);
            MarkerNode1.AddChild(cylinderTransNode);
            cylinderTransNode.AddChild(cylinderNode1);
            scene.RootNode.AddChild(MarkerNode1);

            ids2 = new int[4];
            ids2[0] = 80;
            ids2[1] = 81;
            ids2[2] = 82;
            ids2[3] = 83;

            MarkerNode2 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML2.xml", ids2);

            cylinderNode2 = new GeometryNode("PLAYER's SHIP EAST");
            cylinderNode2.Model = new Cylinder(3, 3, 6, 10);
            cylinderNode2.Material = boxMaterial;
            TransformNode cylinderTransNode2 = new TransformNode();
            cylinderTransNode2.Translation = new Vector3(20, 5, 10);
            MarkerNode2.AddChild(cylinderTransNode2);
            cylinderTransNode2.AddChild(cylinderNode2);
            scene.RootNode.AddChild(MarkerNode2);

            ids3 = new int[4];
            ids3[0] = 90;
            ids3[1] = 91;
            ids3[2] = 92;
            ids3[3] = 93;

            MarkerNode3 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML3.xml", ids3);

            cylinderNode3 = new GeometryNode("PLAYER's SHIP WEST");
            cylinderNode3.Model = new Cylinder(3, 3, 6, 10);
            cylinderNode3.Material = sphereMaterial;
            TransformNode cylinderTransNode3 = new TransformNode();
            cylinderTransNode3.Translation = new Vector3(0, 5, 0);
            MarkerNode3.AddChild(cylinderTransNode3);
            cylinderTransNode3.AddChild(cylinderNode3);
            scene.RootNode.AddChild(MarkerNode3);

            ids4 = new int[4];
            ids4[0] = 100;
            ids4[1] = 101;
            ids4[2] = 102;
            ids4[3] = 103;

            MarkerNode4 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML4.xml", ids4);

            cylinderNode4 = new GeometryNode("PLAYER's NORTH");
            cylinderNode4.Model = new Cylinder(3, 3, 6, 10);
            cylinderNode4.Material = boxMaterial;
            TransformNode cylinderTransNode4 = new TransformNode();
            cylinderTransNode4.Translation = new Vector3(0, 0, 0);
            MarkerNode4.AddChild(cylinderTransNode4);
            cylinderTransNode4.AddChild(cylinderNode4);
            scene.RootNode.AddChild(MarkerNode4);

            ids5 = new int[4];
            ids5[0] = 110;
            ids5[1] = 111;
            ids5[2] = 112;
            ids5[3] = 113;
            MarkerNode5 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML5.xml", ids5);

            ids6 = new int[4];
            ids6[0] = 120;
            ids6[1] = 121;
            ids6[2] = 122;
            ids6[3] = 123;
            MarkerNode6 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML6.xml", ids6);

            ids7 = new int[4];
            ids7[0] = 130;
            ids7[1] = 131;
            ids7[2] = 132;
            ids7[3] = 133;
            MarkerNode7 = new MarkerNode(scene.MarkerTracker, "ALVARConfigFromXML7.xml", ids7);

            scene.RootNode.AddChild(MarkerNode5);
            scene.RootNode.AddChild(MarkerNode6);
            scene.RootNode.AddChild(MarkerNode7);
        }