This project is read-only.

Problem of Collision Detection in AR

Aug 17, 2010 at 2:28 PM

I have added one ground on Marker detection.. Code Below..

 

            GeometryNode groundNode = new GeometryNode("Ground");


            ModelLoader aloader = new ModelLoader();
            GoblinXNA.Graphics.Model animatedModel = (GoblinXNA.Graphics.Model)aloader.Load("", "SeaFresh");
            animatedModel.UseInternalMaterials = true;

            groundNode.Model = animatedModel;
            //animatedModel.ShaderTechnique = "SkinnedModelTechnique";
            //animatedModel.LoadAnimationClip("Take 001");


            // Set this ground model to act as an occluder so that it appears transparent
            groundNode.IsOccluder = false;

            // Make the ground model to receive shadow casted by other objects with
            // CastShadows set to true
            groundNode.Model.ReceiveShadows = true;

            Material groundMaterial = new Material();

            //groundMaterial.Diffuse = Color.Gray.ToVector4();
            //groundMaterial.Specular = Color.White.ToVector4();
            groundMaterial.SpecularPower = 20;

            //groundNode.Physics.Interactable = false;
            groundNode.Physics.Collidable = true;
            //groundNode.Physics.Shape = GoblinXNA.Physics.ShapeType.Box;
            groundNode.AddToPhysicsEngine = true;
            groundNode.Physics.Mass = 10000;

            // Create a parent transformation for both the ground and the sphere models
            parentTransNode = new TransformNode();
            parentTransNode.Translation = new Vector3(0, 0, 0);

            // Create a scale transformation for the ground to make it bigger
            TransformNode groundScaleNode = new TransformNode();
            groundScaleNode.Scale = new Vector3(0.5f, 0.5f, 0.5f);
            groundScaleNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(20, 0, 0), MathHelper.ToRadians(3));
            groundScaleNode.Translation = new Vector3(0.1f, -15f, -1f);


            int[] ids = new int[28];
            for (int i = 0; i < ids.Length; i++)
                ids[i] = i;

            groundMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARGroundArray.txt", ids);

           

            groundNode.Material = groundMaterial;
            groundNode.Physics.MaterialName = "Ground";
            scene.RootNode.AddChild(groundMarkerNode);

            groundMarkerNode.AddChild(parentTransNode);

            parentTransNode.AddChild(groundScaleNode);
            groundScaleNode.AddChild(groundNode);

 

I am trying to shoot a Ball on it... Code Below

Material shooterMat = new Material();
            shooterMat.Diffuse = Color.Black.ToVector4();
            shooterMat.Specular = Color.Yellow.ToVector4();
            //shooterMat.SpecularPower = 10;
            Sphere sphereModel = new Sphere(3f, 20, 20);
          

            shootBox = new GeometryNode("ShooterBox" + shooterID++);
            shootBox.Model = sphereModel;
            shootBox.Material = shooterMat;
            shootBox.Physics.Interactable = true;
            shootBox.Physics.MaterialName = "ShooterBox";
            shootBox.Physics.Collidable = true;
            shootBox.Physics.Shape = GoblinXNA.Physics.ShapeType.Sphere;
            shootBox.Physics.Mass = 100f;
            shootBox.AddToPhysicsEngine = true;

            // Calculate the direction to shoot the box based on the near and far point
            Vector3 linVel = far - near;
            linVel.Normalize();
            // Multiply the direction with the velocity of 20
            linVel *= 200f;

            // Assign the initial velocity to this shooting box
            shootBox.Physics.InitialLinearVelocity = linVel;
            //shootBox.Physics.InitialAngularVelocity = new Vector3(0.1f, 0.1f, 100);
            shootBox.Physics.InitialWorldTransform = Matrix.CreateTranslation(near);
            scene.RootNode.AddChild(shootBox);

But the ball passes through.. Collision is not happening...  Trying to detect collision from the below code..

NewtonMaterial physMat = new NewtonMaterial();
            physMat.MaterialName1 = "ShooterBox";
            physMat.MaterialName2 = "Ground";
            physMat.Elasticity = 0.7f;
            physMat.StaticFriction = 0.8f;
            physMat.KineticFriction = 0.2f;
          
            physMat.ContactProcessCallback = delegate(Vector3 contactPosition, Vector3 contactNormal,
                float contactSpeed, float colObj1ContactTangentSpeed, float colObj2ContactTangentSpeed,
                Vector3 colObj1ContactTangentDirection, Vector3 colObj2ContactTangentDirection)
            {

               
                if (contactSpeed > 2)
                    collisionCount++;
              
                if (collisionCount >= 4)
                {
                 //  Code After collision..
                }
            };

 

Coordinator
Aug 24, 2010 at 12:31 AM

The collision won't happen because the marker transformation will not be applied to the position and orientation in the physics world by design. 

To correlate the physics world with marker transformation, you will need to set the transform of the tracked object by marker (in this case, the ground) directly in the physics engine like what I do in Tutorial 8. Also, moving a physics object based on marker transformation means that you'll be "transporting" the object, and you will see sometimes that the ball will go through the ground because it is not a desirable behavior. Unfortunately, Newton 1.53 does not support a way to correctly move objects in the physics simulation by telling it the new transformation of an object, but Havok physics can perform this kind of operation correctly. Havok physics will be supported in the next release, which will be when XNA 4 official release comes out.

Ohan

Dec 8, 2010 at 12:56 PM

Hello ohanoda

I've seen your Marble-Game (http://www.youtube.com/watch?v=6AKgH4On65A) and plan to create a similar game. But as mentioned in this thread, the marble tends to fall through the ground plane while moving (especially while rotating) the marker. The transformation of the ground is done directly in the physics engine, as you recommended.

I gave the Havok Wrapper in Goblin 3.5 a try, but since this wrapper is very limited and no interaction between marble and ground takes place at all when using it, I hope to solve this with Newton Physics.

your marble game uses the newton engine without this problem, maybe you have an idea for this?

thanks in advance for your reply

Coordinator
Dec 9, 2010 at 5:08 PM

It is likely that your ground is too "thin" compared to the size of the ball. I'm using Newton for the marble game, but since Newton is using discrete simulation, there is a possibility that the ball misses the collision if the physics update interval is somewhat large, so you want to make the ground thick enough that it will have less chance of missing the collision.

Ohan

Dec 9, 2010 at 8:22 PM
ohanoda wrote:

It is likely that your ground is too "thin" compared to the size of the ball. I'm using Newton for the marble game, but since Newton is using discrete simulation, there is a possibility that the ball misses the collision if the physics update interval is somewhat large, so you want to make the ground thick enough that it will have less chance of missing the collision.

Ohan

thank you for your fast reply! If I increase the ground thickness, the problem still exists but occurs much more seldom.

And is there any possibility to make the physics update interval smaller?

Coordinator
Dec 9, 2010 at 10:48 PM

There is a property called NewtonPhysics.SimulationTimeStep, which controls the update interval. If you change this, you may also want to change MaxSimulationSubSteps property so that the update won't look slow. Please see the API doc for details.

Ohan