public void Clone() { Pose poseA = new Pose(new Vector3F(1, 2, 3)); PointShape pointA = new PointShape(3, 4, 5); GeometricObject geometryA = new GeometricObject(pointA, poseA); Pose poseB = new Pose(new Vector3F(1, 2, 3)); PointShape pointB = new PointShape(3, 4, 5); GeometricObject geometryB = new GeometricObject(pointB, poseB); MinkowskiDifferenceShape minkowskiDifferenceShape = new MinkowskiDifferenceShape(geometryA, geometryB); MinkowskiDifferenceShape clone = minkowskiDifferenceShape.Clone() as MinkowskiDifferenceShape; Assert.IsNotNull(clone); Assert.IsNotNull(clone.ObjectA); Assert.IsNotNull(clone.ObjectB); Assert.AreNotSame(geometryA, clone.ObjectA); Assert.AreNotSame(geometryB, clone.ObjectB); Assert.IsTrue(clone.ObjectA is GeometricObject); Assert.IsTrue(clone.ObjectB is GeometricObject); Assert.AreEqual(poseA, clone.ObjectA.Pose); Assert.AreEqual(poseB, clone.ObjectB.Pose); Assert.IsNotNull(clone.ObjectA.Shape); Assert.IsNotNull(clone.ObjectB.Shape); Assert.AreNotSame(pointA, clone.ObjectA.Shape); Assert.AreNotSame(pointB, clone.ObjectB.Shape); Assert.IsTrue(clone.ObjectA.Shape is PointShape); Assert.IsTrue(clone.ObjectB.Shape is PointShape); Assert.AreEqual(pointA.Position, ((PointShape)clone.ObjectA.Shape).Position); Assert.AreEqual(pointB.Position, ((PointShape)clone.ObjectB.Shape).Position); Assert.AreEqual(minkowskiDifferenceShape.GetAabb(Pose.Identity).Minimum, clone.GetAabb(Pose.Identity).Minimum); Assert.AreEqual(minkowskiDifferenceShape.GetAabb(Pose.Identity).Maximum, clone.GetAabb(Pose.Identity).Maximum); }
public void GetScreenSizeException() { var viewport = new Viewport(10, 10, 200, 100); var geometricObject = new GeometricObject(new SphereShape()); GraphicsHelper.GetScreenSize(null, viewport, geometricObject); }
private void SynchronizeShape() { Debug.Assert(ParticleSystem != null); // Note: Scene does not allow TransformedShapes with infinite shapes. // --> Handle infinite shapes explicitly. (The code below only checks for InfiniteShape. // LineShape or PlaneShape will raise an exception in Scene!) if (ParticleSystem.ReferenceFrame == ParticleReferenceFrame.Local || ParticleSystem.Shape is InfiniteShape) { Shape = ParticleSystem.Shape; } else { // Particles are simulated in world space. ParticleSystem.Shape must not be scale by // SceneNode.ScaleWorld. --> Add a TransformedShape that negates the scale. var transformedShape = Shape as TransformedShape; if (transformedShape == null) { transformedShape = new TransformedShape(new GeometricObject()); Shape = transformedShape; } var geometricObject = transformedShape.Child as GeometricObject; if (geometricObject == null) { geometricObject = new GeometricObject(); transformedShape.Child = geometricObject; } geometricObject.Shape = ParticleSystem.Shape; geometricObject.Scale = Vector3F.One / ScaleWorld; } }
private bool HasLedgeContact() { // Here is a primitive way to detect ledges and edges where the character can climb: // We use a box that is ~10 cm high and wider than the character capsule. We check for // collisions in the top part of the character and a second time on a lower positions. // If the top part is free of collisions and the lower part collides with something, then // we have found a ledge and let the character hang onto it. // Note: This objects should not be allocated in each frame. Create them once, add them // to the collision domain and move them with the character controller - like the ray // of the DynamicCharacterController. BoxShape box = new BoxShape(CharacterController.Width + 0.2f, 0.1f, CharacterController.Width + 0.2f); GeometricObject geometricObject = new GeometricObject(box) { Pose = new Pose(CharacterController.Position + new Vector3(0, 1.6f, 0)) }; var collisionObject = new CollisionObject(geometricObject) { CollisionGroup = 4, // Should not collide with character. Type = CollisionObjectType.Trigger, // Use a trigger because we do not need to compute detailed }; // collision information. // First test: if (_simulation.CollisionDomain.HasContact(collisionObject)) return false; // Second test: geometricObject.Pose = new Pose(CharacterController.Position + new Vector3(0, 1.5f, 0)); if (_simulation.CollisionDomain.HasContact(collisionObject)) return true; return false; }
public void End3D() { var gobj = init_drawing(); if (gobj == null) { return; } gobj.End(); // recalculates the bound of collider var cl = gobj.gameObject.GetComponent <BoxCollider>(); if (cl != null) { var mr = gobj.gameObject.GetComponent <MeshRenderer>(); cl.center = mr.bounds.center; cl.size = mr.bounds.size; if (testmode) { Manager.DebugLog("CindyJS::End3D : collider size = " + cl.size.x + " / " + cl.size.y + " / " + cl.size.z); } } gobj_drawing = null; }
public void DelayedActivationOfLookupTable() { IGeometricObject geometricObjectA = new GeometricObject(); IGeometricObject geometricObjectB = new GeometricObject(); IGeometricObject geometricObjectC = new GeometricObject(); CollisionObject collisionObjectA = new CollisionObject(geometricObjectA); CollisionObject collisionObjectB = new CollisionObject(geometricObjectB); CollisionObject collisionObjectC = new CollisionObject(geometricObjectC); var collisionObjectCollection = new CollisionObjectCollection(); Assert.IsFalse(collisionObjectCollection.EnableLookupTable); collisionObjectCollection.EnableLookupTable = false; // Set to false again, just for code coverage. collisionObjectCollection.Add(collisionObjectA); collisionObjectCollection.EnableLookupTable = true; collisionObjectCollection.Add(collisionObjectB); collisionObjectCollection.Add(collisionObjectC); Assert.IsTrue(collisionObjectCollection.EnableLookupTable); Assert.AreEqual(collisionObjectA, collisionObjectCollection.Get(geometricObjectA)); Assert.AreEqual(collisionObjectB, collisionObjectCollection.Get(geometricObjectB)); Assert.AreEqual(collisionObjectC, collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.Remove(collisionObjectB); Assert.AreEqual(collisionObjectA, collisionObjectCollection.Get(geometricObjectA)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectB)); Assert.AreEqual(collisionObjectC, collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.EnableLookupTable = false; Assert.AreEqual(collisionObjectA, collisionObjectCollection.Get(geometricObjectA)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectB)); Assert.AreEqual(collisionObjectC, collisionObjectCollection.Get(geometricObjectC)); }
public void Clone() { Pose poseA = new Pose(new Vector3F(1, 2, 3)); PointShape pointA = new PointShape(3, 4, 5); GeometricObject geometryA = new GeometricObject(pointA, poseA); Pose poseB = new Pose(new Vector3F(1, 2, 3)); PointShape pointB = new PointShape(3, 4, 5); GeometricObject geometryB = new GeometricObject(pointB, poseB); MinkowskiSumShape minkowskiSumShape = new MinkowskiSumShape(geometryA, geometryB); MinkowskiSumShape clone = minkowskiSumShape.Clone() as MinkowskiSumShape; Assert.IsNotNull(clone); Assert.IsNotNull(clone.ObjectA); Assert.IsNotNull(clone.ObjectB); Assert.AreNotSame(geometryA, clone.ObjectA); Assert.AreNotSame(geometryB, clone.ObjectB); Assert.IsTrue(clone.ObjectA is GeometricObject); Assert.IsTrue(clone.ObjectB is GeometricObject); Assert.AreEqual(poseA, clone.ObjectA.Pose); Assert.AreEqual(poseB, clone.ObjectB.Pose); Assert.IsNotNull(clone.ObjectA.Shape); Assert.IsNotNull(clone.ObjectB.Shape); Assert.AreNotSame(pointA, clone.ObjectA.Shape); Assert.AreNotSame(pointB, clone.ObjectB.Shape); Assert.IsTrue(clone.ObjectA.Shape is PointShape); Assert.IsTrue(clone.ObjectB.Shape is PointShape); Assert.AreEqual(pointA.Position, ((PointShape)clone.ObjectA.Shape).Position); Assert.AreEqual(pointB.Position, ((PointShape)clone.ObjectB.Shape).Position); Assert.AreEqual(minkowskiSumShape.GetAabb(Pose.Identity).Minimum, clone.GetAabb(Pose.Identity).Minimum); Assert.AreEqual(minkowskiSumShape.GetAabb(Pose.Identity).Maximum, clone.GetAabb(Pose.Identity).Maximum); }
/// <summary> /// Initializes a new instance of the <see cref="Wheel"/> class. /// </summary> public Wheel() { Radius = 0.4f; SuspensionRestLength = 0.6f; MinSuspensionLength = float.NegativeInfinity; SuspensionLength = SuspensionRestLength; PreviousSuspensionLength = SuspensionRestLength; SuspensionStiffness = 20; SuspensionCompressionDamping = 4f; SuspensionRelaxationDamping = 3f; MaxSuspensionForce = 6000; RollingFrictionForce = 500; Friction = 0.9f; RollReduction = 0.3f; Vector3 rayOrigin = Vector3.Zero; Vector3 rayDirection = -Vector3.UnitY; float rayLength = Radius + SuspensionRestLength; Ray = new RayShape(rayOrigin, rayDirection, rayLength) { StopsAtFirstHit = true, }; GeometricObject = new GeometricObject(Ray); CollisionObject = new CollisionObject(GeometricObject); }
public void TestException() { var sphere0 = new SphereShape(1); var geo0 = new GeometricObject(sphere0); var geo1 = new GeometricObject(sphere0); var co0 = new CollisionObject(geo0); var co1 = new CollisionObject(geo1); geo0.Pose = Pose.Identity; geo1.Pose = new Pose(new Vector3(10, 0, 0)); float toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3(4, 0, 0)), co1, new Pose(new Vector3(6, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3(4, 0, 0)), co1, new Pose(new Vector3(6, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3(4, 0, 0)), co1, new Pose(new Vector3(6, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3(4f, 0, 0)), co1, new Pose(new Vector3(6f, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3(4f, 0, 0)), co1, new Pose(new Vector3(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3(4f, 0, 0)), co1, new Pose(new Vector3(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3(7f, 0, 0)), co1, new Pose(new Vector3(4f, 0, 0)), 0.01f); Assert.IsTrue(toi > 0 && toi < 1); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3(7f, 0, 0)), co1, new Pose(new Vector3(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.IsTrue(toi > 0 && toi < 1); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3(7f, 0, 0)), co1, new Pose(new Vector3(4f, 0, 0)), 0.01f, new CollisionDetection()); Assert.IsTrue(toi > 0 && toi < 1); // Moving away toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3(-1f, 0, 0)), co1, new Pose(new Vector3(11f, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3(-1f, 0, 0)), co1, new Pose(new Vector3(11f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3(-1f, 0, 0)), co1, new Pose(new Vector3(11f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); // Touching at start. => GetTimeOfImpact result is invalid when objects touch at the start. //geo0.Pose = new Pose(new Vector3(9, 0, 0)); //toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3(10f, 0, 0)), co1, new Pose(new Vector3(5f, 0, 0)), 0.01f); //Assert.AreEqual(0, toi); //toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3(10f, 0, 0)), co1, new Pose(new Vector3(5f, 0, 0)), 0.01f, new CollisionDetection()); //Assert.AreEqual(0, toi); //toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3(10f, 0, 0)), co1, new Pose(new Vector3(5f, 0, 0)), 0.01f, new CollisionDetection()); //Assert.AreEqual(0, toi); }
public void Clone() { CompositeShape compositeShape = new CompositeShape(); for (int i = 0; i < 10; i++) { Pose pose = new Pose(new Vector3F(i, i, i)); PointShape point = new PointShape(i, i, i); GeometricObject geometry = new GeometricObject(point, pose); compositeShape.Children.Add(geometry); } CompositeShape clone = compositeShape.Clone() as CompositeShape; Assert.IsNotNull(clone); Assert.AreEqual(10, clone.Children.Count); for (int i = 0; i < 10; i++) { Assert.IsNotNull(clone.Children[i]); Assert.AreNotSame(compositeShape.Children[i], clone.Children[i]); Assert.IsTrue(clone.Children[i] is GeometricObject); Assert.AreEqual(compositeShape.Children[i].Pose, clone.Children[i].Pose); Assert.IsNotNull(clone.Children[i].Shape); Assert.AreNotSame(compositeShape.Children[i].Shape, clone.Children[i].Shape); Assert.IsTrue(clone.Children[i].Shape is PointShape); Assert.AreEqual(((PointShape)compositeShape.Children[i].Shape).Position, ((PointShape)clone.Children[i].Shape).Position); } Assert.AreEqual(compositeShape.GetAabb(Pose.Identity).Minimum, clone.GetAabb(Pose.Identity).Minimum); Assert.AreEqual(compositeShape.GetAabb(Pose.Identity).Maximum, clone.GetAabb(Pose.Identity).Maximum); }
public void Begin3D(string name) { if (gobj_drawing != null) { End3D(); } gobj_drawing = gobjs.FirstOrDefault(item => item.Value.name == name).Value; if (gobj_drawing == null) { idx_uc3dBuffer = 0; var active = readBoolean(); var o = Instantiate(GeometricObjectPrefab); o.name = name; o.SetActive(active); gobj_drawing = registerGobj(o); if (testmode) { Manager.DebugLog("CindyJS::Begin3D : Instantiate " + o.name + " id = " + gobj_drawing.id); } } uc3dBuffer[0] = gobj_drawing.id; }
public void SerializationBinary() { Pose poseA = new Pose(new Vector3F(1, 2, 3)); PointShape pointA = new PointShape(3, 4, 5); GeometricObject geometryA = new GeometricObject(pointA, poseA); Pose poseB = new Pose(new Vector3F(11, 22, 33)); PointShape pointB = new PointShape(33, 44, 55); GeometricObject geometryB = new GeometricObject(pointB, poseB); var a = new MinkowskiDifferenceShape(geometryA, geometryB); // Serialize object. var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, a); // Deserialize object. stream.Position = 0; var deserializer = new BinaryFormatter(); var b = (MinkowskiDifferenceShape)deserializer.Deserialize(stream); Assert.AreEqual(a.ObjectA.Pose, b.ObjectA.Pose); Assert.AreEqual(a.ObjectB.Pose, b.ObjectB.Pose); Assert.AreEqual(((PointShape)a.ObjectA.Shape).Position, ((PointShape)b.ObjectA.Shape).Position); Assert.AreEqual(((PointShape)a.ObjectB.Shape).Position, ((PointShape)b.ObjectB.Shape).Position); }
public void Clone() { CompositeShape compositeShape = new CompositeShape(); for (int i = 0; i < 10; i++) { Pose pose = new Pose(new Vector3(i, i, i)); PointShape point = new PointShape(i, i, i); GeometricObject geometry = new GeometricObject(point, pose); compositeShape.Children.Add(geometry); } CompositeShape clone = compositeShape.Clone() as CompositeShape; Assert.IsNotNull(clone); Assert.AreEqual(10, clone.Children.Count); for (int i = 0; i < 10; i++) { Assert.IsNotNull(clone.Children[i]); Assert.AreNotSame(compositeShape.Children[i], clone.Children[i]); Assert.IsTrue(clone.Children[i] is GeometricObject); Assert.AreEqual(compositeShape.Children[i].Pose, clone.Children[i].Pose); Assert.IsNotNull(clone.Children[i].Shape); Assert.AreNotSame(compositeShape.Children[i].Shape, clone.Children[i].Shape); Assert.IsTrue(clone.Children[i].Shape is PointShape); Assert.AreEqual(((PointShape)compositeShape.Children[i].Shape).Position, ((PointShape)clone.Children[i].Shape).Position); } Assert.AreEqual(compositeShape.GetAabb(Pose.Identity).Minimum, clone.GetAabb(Pose.Identity).Minimum); Assert.AreEqual(compositeShape.GetAabb(Pose.Identity).Maximum, clone.GetAabb(Pose.Identity).Maximum); }
public void TestException() { var sphere0 = new SphereShape(1); var geo0 = new GeometricObject(sphere0); var geo1 = new GeometricObject(sphere0); var co0 = new CollisionObject(geo0); var co1 = new CollisionObject(geo1); geo0.Pose = Pose.Identity; geo1.Pose = new Pose(new Vector3F(10, 0, 0)); float toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3F(4, 0, 0)), co1, new Pose(new Vector3F(6, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3F(4, 0, 0)), co1, new Pose(new Vector3F(6, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3F(4, 0, 0)), co1, new Pose(new Vector3F(6, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3F(4f, 0, 0)), co1, new Pose(new Vector3F(6f, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3F(4f, 0, 0)), co1, new Pose(new Vector3F(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3F(4f, 0, 0)), co1, new Pose(new Vector3F(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3F(7f, 0, 0)), co1, new Pose(new Vector3F(4f, 0, 0)), 0.01f); Assert.IsTrue(toi > 0 && toi < 1); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3F(7f, 0, 0)), co1, new Pose(new Vector3F(6f, 0, 0)), 0.01f, new CollisionDetection()); Assert.IsTrue(toi > 0 && toi < 1); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3F(7f, 0, 0)), co1, new Pose(new Vector3F(4f, 0, 0)), 0.01f, new CollisionDetection()); Assert.IsTrue(toi > 0 && toi < 1); // Moving away toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3F(-1f, 0, 0)), co1, new Pose(new Vector3F(11f, 0, 0)), 0.01f); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3F(-1f, 0, 0)), co1, new Pose(new Vector3F(11f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3F(-1f, 0, 0)), co1, new Pose(new Vector3F(11f, 0, 0)), 0.01f, new CollisionDetection()); Assert.AreEqual(1, toi); // Touching at start. => GetTimeOfImpact result is invalid when objects touch at the start. //geo0.Pose = new Pose(new Vector3F(9, 0, 0)); //toi = CcdHelper.GetTimeOfImpactLinearSweep(co0, new Pose(new Vector3F(10f, 0, 0)), co1, new Pose(new Vector3F(5f, 0, 0)), 0.01f); //Assert.AreEqual(0, toi); //toi = CcdHelper.GetTimeOfImpactLinearCA(co0, new Pose(new Vector3F(10f, 0, 0)), co1, new Pose(new Vector3F(5f, 0, 0)), 0.01f, new CollisionDetection()); //Assert.AreEqual(0, toi); //toi = CcdHelper.GetTimeOfImpactCA(co0, new Pose(new Vector3F(10f, 0, 0)), co1, new Pose(new Vector3F(5f, 0, 0)), 0.01f, new CollisionDetection()); //Assert.AreEqual(0, toi); }
public void TestEnabledDisabledStochastic() { // Test random Enabled and Pose values. int numberOfObjects = 20; int numberOfSteps = 1000; Shape shape = new SphereShape(1); //var meshShape = new TriangleMeshShape(shape.GetMesh(0.01f, 4)); //meshShape.Partition = new AabbTree<int>(); //shape = meshShape; var geometricObjects = new GeometricObject[numberOfObjects]; var collisionObjects = new CollisionObject[numberOfObjects]; var domain = new CollisionDomain(new CollisionDetection()); for (int i = 0; i < numberOfObjects; i++) { geometricObjects[i] = new GeometricObject(shape); collisionObjects[i] = new CollisionObject(geometricObjects[i]); domain.CollisionObjects.Add(collisionObjects[i]); } for (int i = 0; i < numberOfSteps; i++) { for (int j = 0; j < numberOfObjects; j++) { collisionObjects[j].Enabled = RandomHelper.Random.NextBool(); domain.Update(0); if (RandomHelper.Random.NextFloat(0, 1) > 0.5f) { geometricObjects[j].Pose = new Pose(RandomHelper.Random.NextVector3(-2, 2)); } domain.Update(0); } domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); // Compare result with brute-force check. for (int j = 0; j < numberOfObjects; j++) { for (int k = j + 1; k < numberOfObjects; k++) { var haveContact = domain.CollisionDetection.HaveContact(collisionObjects[j], collisionObjects[k]); Assert.AreEqual(haveContact, domain.ContactSets.GetContacts(collisionObjects[j], collisionObjects[k]) != null); } } } }
public Instance(GeometricObject obj) : this(obj, obj.Material) { this.obj = obj; invMatrix = Mat4.NewIdentity(); transformTheTexture = true; forwardMatrix = Mat4.NewIdentity(); }
public void ICloneableClone() { var o = new GeometricObject(new BoxShape(1, 2, 3), new Pose(new Vector3(1, 2, 3))); var clone = (GeometricObject)((IGeometricObject)o).Clone(); Assert.AreEqual(o.Pose, clone.Pose); Assert.AreEqual(((BoxShape)o.Shape).Extent, ((BoxShape)clone.Shape).Extent); }
public void ICloneableClone() { var o = new GeometricObject(new BoxShape(1, 2, 3), new Pose(new Vector3F(1, 2, 3))); var clone = (GeometricObject)((IGeometricObject)o).Clone(); Assert.AreEqual(o.Pose, clone.Pose); Assert.AreEqual(((BoxShape)o.Shape).Extent, ((BoxShape)clone.Shape).Extent); }
public void CopyObjects(List <GeometricObject> objects) { for (int i = 0; i < Objects.Count; i++) { GeometricObject obj = Objects[i].Clone(); objects.Add(obj); } }
private void CreateCompositeShape() { // Convert islands into CompositeShape with convex children. _decomposition = new CompositeShape(); if (_islands == null) { return; } foreach (var island in _islands) { if (island.Vertices.Length <= 0) { continue; } // ReSharper disable EmptyGeneralCatchClause try { // ----- Get convex hull mesh. DcelMesh convexHullMesh; if (island.ConvexHullBuilder == null) { // Create convex hull from scratch. // Get all vertices of all island triangles. var points = island.Triangles.SelectMany(t => t.Vertices); // Create convex hull. convexHullMesh = GeometryHelper.CreateConvexHull(points, VertexLimit, SkinWidth); } else { // Use existing convex hull. convexHullMesh = island.ConvexHullBuilder.Mesh; if (convexHullMesh.Vertices.Count > VertexLimit || SkinWidth != 0) { convexHullMesh.ModifyConvex(VertexLimit, SkinWidth); } } // ----- Add a ConvexPolyhedron to CompositeShape. if (convexHullMesh.Vertices.Count > 0) { var convexHullPoints = convexHullMesh.Vertices.Select(v => v.Position); var convexPolyhedron = new ConvexPolyhedron(convexHullPoints); var geometricObject = new GeometricObject(convexPolyhedron); _decomposition.Children.Add(geometricObject); } } catch { // Could not generate convex hull. Ignore object. } // ReSharper restore EmptyGeneralCatchClause } }
public void SetUp() { child0 = new GeometricObject(new CircleShape(3), new Pose(new Vector3(0, 5, 0), Quaternion.CreateRotationX(ConstantsF.PiOver2))); child1 = new GeometricObject(new CircleShape(3), new Pose(new Vector3(0, -5, 0), Quaternion.CreateRotationX(ConstantsF.PiOver2))); cs = new CompositeShape(); cs.Children.Add(child0); cs.Children.Add(child1); }
public Instance(GeometricObject obj, IMaterial material) : base() { this.obj = obj; this.material = material; invMatrix = Mat4.NewIdentity(); transformTheTexture = true; forwardMatrix = Mat4.NewIdentity(); }
public void GetScreenSizeWithPerspective() { // Camera var projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(90), 2.0f / 1.0f, 1.0f, 100f); var camera = new Camera(projection); var cameraNode = new CameraNode(camera); cameraNode.PoseWorld = new Pose(new Vector3(123, 456, -789), Matrix.CreateRotation(new Vector3(1, -2, 3), MathHelper.ToRadians(75))); // 2:1 viewport var viewport = new Viewport(10, 10, 200, 100); // Test object var shape = new SphereShape(); var geometricObject = new GeometricObject(shape); // Empty sphere at camera position. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld; Vector2F screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Empty sphere centered at near plane. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Create sphere which as a bounding sphere of ~1 unit diameter: // Since the bounding sphere is based on the AABB, we need to make the // actual sphere a bit smaller. shape.Radius = 1 / (2 * (float)Math.Sqrt(3)) + Numeric.EpsilonF; // Sphere at camera position. geometricObject.Pose = cameraNode.PoseWorld; screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.Greater(screenSize.X, 200); Assert.Greater(screenSize.Y, 100); // Sphere at near plane. geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 50.0f, 10f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 50.0f, 10f)); // Double distance --> half size geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -2)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 25.0f, 5f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 25.0f, 5f)); }
public SpatialPartitionSample(Microsoft.Xna.Framework.Game game) : base(game) { SampleFramework.IsMouseVisible = false; GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.Gray; GraphicsScreen.DrawReticle = true; SetCamera(new Vector3(0, 1, 10), 0, 0); // Create a spatial partition. DigitalRune Geometry supports several types, see also // http://digitalrune.github.io/DigitalRune-Documentation/html/e32cab3b-cc7c-42ee-8ec9-23dd4467edd0.htm#WhichPartition // An AabbTree is useful for static objects. A DynamicAabbTree is good for moving objects. // The spatial partition can manage different types of items. In this case it manages // GeometricObjects. A delegate has to inform the spatial partition how to get the AABB // of an object. //_spatialPartition = new DynamicAabbTree<GeometricObject> _spatialPartition = new AabbTree <GeometricObject> { GetAabbForItem = geometricObject => geometricObject.Aabb, // Optional: The tree is automatically built using a mixed top-down/bottom-up approach. // Bottom-up building is slower but produces better trees. If the tree building takes too // long, we can lower the BottomUpBuildThreshold (default is 128). //BottomUpBuildThreshold = 0, // Optional: A filter can be set to disable certain kind of overlaps. //Filter = ... }; // Create a triangle mesh. var triangleMesh = new SphereShape(1).GetMesh(0.01f, 4); var triangleMeshShape = new TriangleMeshShape(triangleMesh) { // TriangleMeshShapes can also use a spatial partition to manage triangle. // The items in the spatial partition are the triangle indices. The GetAabbForItem // delegate is set automatically. Partition = new AabbTree <int>(), }; // Spatial partitions are built automatically when needed. However, it is still recommended // to call Update to initialize the spatial partition explicitly. triangleMeshShape.Partition.Update(false); // Add a lot of triangle mesh objects to _spatialPartition. var random = new Random(); for (int i = 0; i < 50; i++) { var randomPosition = new Vector3(random.NextFloat(-6, 6), random.NextFloat(-3, 3), random.NextFloat(-10, 0)); var geometricObject = new GeometricObject(triangleMeshShape, new Pose(randomPosition)); _spatialPartition.Add(geometricObject); } _spatialPartition.Update(false); }
public CollisionDetectionSample(Microsoft.Xna.Framework.Game game) : base(game) { SampleFramework.IsMouseVisible = false; GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.CornflowerBlue; SetCamera(new Vector3F(0, 1, 10), 0, 0); // ----- Initialize collision detection and create objects. // Create a geometric object with a box shape. // Position it on the left with an arbitrary rotation. var geometricObjectA = new GeometricObject( new BoxShape(1, 2, 3), new Pose(new Vector3F(-2, -1, 0), Matrix33F.CreateRotationZ(0.1f))); // Create a geometric object with a capsule shape. // Position it on the right with an arbitrary rotation. var geometricObjectB = new GeometricObject( new CapsuleShape(1, 3), new Pose(new Vector3F(2, -1, 0), Matrix33F.CreateRotationZ(-0.2f))); // Create a geometric object with a complex shape that is the convex hull of // a circle and a rectangle. Position it on the top with an arbitrary rotation. // (A ConvexHullOfShapes is a collection of different shapes with different // positions and orientations. The ConvexHullOfShapes combines these shapes // into a single shape by building their convex hull.) var complexShape = new ConvexHullOfShapes(); complexShape.Children.Add(new GeometricObject(new RectangleShape(1, 1), new Pose(new Vector3F(0, 0, 1)))); complexShape.Children.Add(new GeometricObject(new CircleShape(1), new Pose(new Vector3F(0, 0, -1)))); var geometricObjectC = new GeometricObject( complexShape, new Pose(new Vector3F(0, 2, 0), QuaternionF.CreateRotation(Vector3F.UnitZ, new Vector3F(1, 1, 1)))); // Create collision objects for the geometric objects. // (A collision object is just a wrapper around the geometric object that // stores additional information that is required by the collision detection.) _collisionObjectA = new CollisionObject(geometricObjectA); _collisionObjectB = new CollisionObject(geometricObjectB); _collisionObjectC = new CollisionObject(geometricObjectC); // Create a collision detection. // (The CollisionDetection stores general parameters and it can be used to // perform closest-point and contact queries.) _collisionDetection = new CollisionDetection(); // Create a new collision domain and add the collision objects. // (A CollisionDomain manages multiple collision objects. It improves the // performance of contact queries by reusing results of the last frame.) _domain = new CollisionDomain(_collisionDetection); _domain.CollisionObjects.Add(_collisionObjectA); _domain.CollisionObjects.Add(_collisionObjectB); _domain.CollisionObjects.Add(_collisionObjectC); }
void OnCollisionEnter(Collision col) { if (collisionEnterCallback != null) { GeometricObject obj = col.gameObject.GetComponent <GeometricObject>(); if (obj != null) { collisionEnterCallback(id, obj.id); } } }
public void SetUp() { child0 = new GeometricObject(new CircleShape(3), new Pose(new Vector3(), Quaternion.CreateRotationX(ConstantsF.PiOver2))); child1 = new GeometricObject(new LineSegmentShape(new Vector3(0, 5, 0), new Vector3(0, -5, 0)), Pose.Identity); cs = new MinkowskiSumShape { ObjectA = child0, ObjectB = child1 }; }
public void TestEnabledDisabled() { var shape = new SphereShape(1); var goA = new GeometricObject(shape, Pose.Identity); var coA = new CollisionObject(goA); var goB = new GeometricObject(shape, Pose.Identity); var coB = new CollisionObject(goB); var cd = new CollisionDomain(new CollisionDetection()); cd.CollisionObjects.Add(coA); cd.CollisionObjects.Add(coB); // not touching goB.Pose = new Pose(new Vector3(3, 3, 0)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // not touching, disabled coB.Enabled = false; cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, disabled goB.Pose = new Pose(new Vector3(1, 1, 1)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, enabled coB.Enabled = true; cd.Update(0); Assert.AreEqual(1, cd.ContactSets.Count); // not touching - but broadphase overlap, enabled goB.Pose = new Pose(new Vector3(1.8f, 1.8f, 0)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // not touching, disabled coB.Enabled = false; cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, disabled goB.Pose = new Pose(new Vector3(1, 1, 1)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, enabled coB.Enabled = true; cd.Update(0); Assert.AreEqual(1, cd.ContactSets.Count); }
public void ValidateNewObjectWithInvalidShape() { GlobalSettings.ValidationLevel = 0xff; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); shape.Radius = float.NaN; Assert.Throws <GeometryException>(() => cd.CollisionObjects.Add(co)); }
public void ValidateInvalidScaleChange() { GlobalSettings.ValidationLevel = 0xff; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); cd.CollisionObjects.Add(co); Assert.Throws <GeometryException>(() => geometricObject.Scale = new Vector3(1, 1, float.NaN)); }
static void removeObject(GeometricObject go) { if (go is Compound) { Compound com = (Compound)go; foreach (GeometricObject g in com.Objects) { removeObject(g); } } GeometricObject.Remove(go.Name); }
public SimpleFixture(GeometricObject geometricObject, List <UniformMaterial> materialCollection, UniformMaterial material, FixtureDescriptor descriptor, Matrix4x4 realParentPose) { _wrappedGeometricObject = geometricObject; _materialCollection = materialCollection; _material = material; UserData = descriptor.UserData; ShapeFactory = new SimpleFixtureShapeFactory(this); MaterialFactory = new SimpleFixtureMaterialFactory(this); _pose = descriptor.Pose; _root = false; _realParentPose = realParentPose; }
SimpleFixture(DR.RigidBody wrappedRigidBody, FixtureDescriptor descriptor) { _wrappedRigidBody = wrappedRigidBody; _wrappedGeometricObject = new GeometricObject(new EmptyShape(), descriptor.Pose.ToDigitalRune()); _wrappedRigidBody.Shape = new TransformedShape(_wrappedGeometricObject); _wrappedRigidBody.Material = new UniformMaterial(); UserData = descriptor.UserData; _pose = descriptor.Pose; ShapeFactory = new SimpleFixtureShapeFactory(this); _root = true; MaterialFactory = new SimpleFixtureMaterialFactory(this); }
//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- public CharacterController(CollisionDomain domain) { _collisionDomain = domain; // Create a game object for the character controller. GeometricObject = new GeometricObject( new CapsuleShape(Width / 2, Height), new Pose(new Vector3F(0, Height / 2, 0))); // Create a collision object for the game object and add it to the collision domain. CollisionObject = new CollisionObject(GeometricObject); _collisionDomain.CollisionObjects.Add(CollisionObject); }
public void NoValidation1() { GlobalSettings.ValidationLevel = 0; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); // No exception with validation level 0. shape.Radius = float.NaN; cd.CollisionObjects.Add(co); }
public void CloneWithPartition() { CompositeShape compositeShape = new CompositeShape(); for (int i = 0; i < 10; i++) { Pose pose = new Pose(new Vector3F(i, i, i)); PointShape point = new PointShape(i, i, i); GeometricObject geometry = new GeometricObject(point, pose); compositeShape.Children.Add(geometry); } CloneWithPartition(compositeShape, new AabbTree<int>()); CloneWithPartition(compositeShape, new AdaptiveAabbTree<int>()); CloneWithPartition(compositeShape, new CompressedAabbTree()); CloneWithPartition(compositeShape, new DynamicAabbTree<int>()); CloneWithPartition(compositeShape, new SweepAndPruneSpace<int>()); }
public void Clone() { Pose pose = new Pose(new Vector3F(1, 2, 3)); PointShape pointShape = new PointShape(3, 4, 5); GeometricObject geometry = new GeometricObject(pointShape, pose); TransformedShape transformedShape = new TransformedShape(geometry); TransformedShape clone = transformedShape.Clone() as TransformedShape; Assert.IsNotNull(clone); Assert.IsNotNull(clone.Child); Assert.AreNotSame(geometry, clone.Child); Assert.IsTrue(clone.Child is GeometricObject); Assert.AreEqual(pose, clone.Child.Pose); Assert.IsNotNull(clone.Child.Shape); Assert.AreNotSame(pointShape, clone.Child.Shape); Assert.IsTrue(clone.Child.Shape is PointShape); Assert.AreEqual(pointShape.Position, ((PointShape)clone.Child.Shape).Position); Assert.AreEqual(transformedShape.GetAabb(Pose.Identity).Minimum, clone.GetAabb(Pose.Identity).Minimum); Assert.AreEqual(transformedShape.GetAabb(Pose.Identity).Maximum, clone.GetAabb(Pose.Identity).Maximum); }
public void LookupWithoutTable() { IGeometricObject geometricObjectA = new GeometricObject(); IGeometricObject geometricObjectB = new GeometricObject(); IGeometricObject geometricObjectC = new GeometricObject(); CollisionObject collisionObjectA = new CollisionObject(geometricObjectA); CollisionObject collisionObjectB = new CollisionObject(geometricObjectB); CollisionObject collisionObjectC = new CollisionObject(geometricObjectC); var collisionObjectCollection = new CollisionObjectCollection(); Assert.IsFalse(collisionObjectCollection.EnableLookupTable); Assert.IsNull(collisionObjectCollection.Get(geometricObjectA)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectB)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.Add(collisionObjectA); collisionObjectCollection.Add(collisionObjectB); collisionObjectCollection.Add(collisionObjectC); Assert.AreEqual(collisionObjectA, collisionObjectCollection.Get(geometricObjectA)); Assert.AreEqual(collisionObjectB, collisionObjectCollection.Get(geometricObjectB)); Assert.AreEqual(collisionObjectC, collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.Remove(collisionObjectB); Assert.AreEqual(collisionObjectA, collisionObjectCollection.Get(geometricObjectA)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectB)); Assert.AreEqual(collisionObjectC, collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.Clear(); Assert.IsNull(collisionObjectCollection.Get(geometricObjectA)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectB)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectC)); collisionObjectCollection.Add(collisionObjectA); collisionObjectCollection[0] = collisionObjectB; Assert.IsNull(collisionObjectCollection.Get(geometricObjectA)); Assert.AreEqual(collisionObjectB, collisionObjectCollection.Get(geometricObjectB)); Assert.IsNull(collisionObjectCollection.Get(geometricObjectC)); }
public virtual void AddObject(GeometricObject o) { bbox = BBox.Join(bbox, o.GetBoundingBox()); Objects.Add(o); }
public void SetUp() { child0 = new GeometricObject(new CircleShape(3), new Pose(new Vector3F(), QuaternionF.CreateRotationX(ConstantsF.PiOver2))); child1 = new GeometricObject(new LineSegmentShape(new Vector3F(0, 5, 0), new Vector3F(0, -5, 0)), Pose.Identity); cs = new MinkowskiSumShape { ObjectA = child0, ObjectB = child1 }; }
public void SetUp() { child0 = new GeometricObject(new CircleShape(3), new Pose(new Vector3F(0, 5, 0), QuaternionF.CreateRotationX(ConstantsF.PiOver2))); child1 = new GeometricObject(new CircleShape(3), new Pose(new Vector3F(0, -5, 0), QuaternionF.CreateRotationX(ConstantsF.PiOver2))); cs = new CompositeShape(); cs.Children.Add(child0); cs.Children.Add(child1); }
public void ValidateNewObjectWithInvalidShape() { GlobalSettings.ValidationLevel = 0xff; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); shape.Radius = float.NaN; Assert.Throws<GeometryException>(() => cd.CollisionObjects.Add(co)); }
public void ValidateInvalidPoseChange() { GlobalSettings.ValidationLevel = 0xff; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); cd.CollisionObjects.Add(co); var matrix = Matrix33F.Identity; matrix.M11 = float.NaN; Assert.Throws<GeometryException>(() => geometricObject.Pose = new Pose(new Vector3F(), matrix)); }
public void ComputeCapsuleWithRandomPoints() { float height; float radius; Pose pose; const int numberOfTests = 100; RandomHelper.Random = new Random(377); for (int test = 0; test < numberOfTests; test++) { // Fill list with a random number of random points. int numberOfPoints = RandomHelper.Random.NextInteger(2, 100); List<Vector3F> points = new List<Vector3F>(); for (int i = 0; i < numberOfPoints; i++) points.Add(RandomHelper.Random.NextVector3F(-10, 100)); GeometryHelper.ComputeBoundingCapsule(points, out radius, out height, out pose); // Check if sphere can be valid. Assert.IsTrue(radius >= 0); Assert.IsTrue(height >= 2 * radius); Assert.IsTrue(!float.IsNaN(pose.Position.Length)); Assert.IsTrue(pose.Orientation.IsRotation); // Test if all points are in the shape. var cd = new CollisionDetection(); GeometricObject geometry = new GeometricObject(new CapsuleShape(radius, height), pose); CollisionObject boundingObject = new CollisionObject(geometry); // Test if all points are in the bounding shape. for (int i = 0; i < numberOfPoints; i++) { var point = points[i]; // Test against a sphere around the point. Some points are exactly on the surface // and are very sensitive to tiny numerical errors. var pointGeometry = new GeometricObject(new SphereShape(Numeric.EpsilonF * (height + 1)), new Pose(point)); Assert.IsTrue(cd.HaveContact(new CollisionObject(pointGeometry), boundingObject)); } } }
public void CreateBoundingShape() { int numberOfBoxes = 0; int numberOfSpheres = 0; const int numberOfTests = 100; RandomHelper.Random = new Random(727); for (int test = 0; test < numberOfTests; test++) { // Fill list with a random number of random points. int numberOfPoints = RandomHelper.Random.NextInteger(2, 100); List<Vector3F> points = new List<Vector3F>(); for (int i = 0; i < numberOfPoints; i++) points.Add(RandomHelper.Random.NextVector3F(-10, 100)); Shape shape = GeometryHelper.CreateBoundingShape(points); GeometricObject geometry = new GeometricObject(shape); CollisionObject boundingObject = new CollisionObject(geometry); if (shape is BoxShape) numberOfBoxes++; if (shape is SphereShape) numberOfSpheres++; if (((TransformedShape)shape).Child.Shape is BoxShape) numberOfBoxes++; else numberOfSpheres++; Assert.IsNotNull(shape); var cd = new CollisionDetection(); // Test if all points are in the bounding shape. for (int i = 0; i < numberOfPoints; i++) { var point = points[i]; // Test against a sphere around the point. Some points are exactly on the surface // and are very sensitive to tiny numerical errors. var pointGeometry = new GeometricObject(new SphereShape(Numeric.EpsilonF * 10), new Pose(point)); Assert.IsTrue(cd.HaveContact(new CollisionObject(pointGeometry), boundingObject)); } } Console.WriteLine("ShapeHelper.CreateBoundingShape: Number of Boxes : Number of Spheres = " + numberOfBoxes + " : " + numberOfSpheres); }
public void ComputeBoundingShapeCenteredSphere() { RandomHelper.Random = new Random(123); var radius = new Vector3F(3, 0, 0); var points = new List<Vector3F> { new Vector3F(3, 0, 0), new Vector3F(-3, 0, 0), new Vector3F(0, 3, 0), new Vector3F(0, -3, 0), new Vector3F(0, 0, 3), new Vector3F(0, 0, -3), }; for (int i = 0; i < 40; i++) points.Add(RandomHelper.Random.NextQuaternionF().Rotate(radius)); var shape = GeometryHelper.CreateBoundingShape(points); SphereShape s = (SphereShape)shape; Assert.IsTrue(Numeric.AreEqual(3, s.Radius)); var cd = new CollisionDetection(); GeometricObject geometry = new GeometricObject(shape); CollisionObject boundingObject = new CollisionObject(geometry); // Test if all points are in the bounding shape. for (int i = 0; i < points.Count; i++) { var point = points[i]; // Test against a sphere around the point. Some points are exactly on the surface // and are very sensitive to tiny numerical errors. var pointGeometry = new GeometricObject(new SphereShape(Numeric.EpsilonF * 10), new Pose(point)); Assert.IsTrue(cd.HaveContact(new CollisionObject(pointGeometry), boundingObject)); } }
public void TestEnabledDisabled() { var shape = new SphereShape(1); var goA = new GeometricObject(shape, Pose.Identity); var coA = new CollisionObject(goA); var goB = new GeometricObject(shape, Pose.Identity); var coB = new CollisionObject(goB); var cd = new CollisionDomain(new CollisionDetection()); cd.CollisionObjects.Add(coA); cd.CollisionObjects.Add(coB); // not touching goB.Pose = new Pose(new Vector3F(3, 3, 0)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // not touching, disabled coB.Enabled = false; cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, disabled goB.Pose = new Pose(new Vector3F(1, 1, 1)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, enabled coB.Enabled = true; cd.Update(0); Assert.AreEqual(1, cd.ContactSets.Count); // not touching - but broadphase overlap, enabled goB.Pose = new Pose(new Vector3F(1.8f, 1.8f, 0)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // not touching, disabled coB.Enabled = false; cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, disabled goB.Pose = new Pose(new Vector3F(1, 1, 1)); cd.Update(0); Assert.AreEqual(0, cd.ContactSets.Count); // touching, enabled coB.Enabled = true; cd.Update(0); Assert.AreEqual(1, cd.ContactSets.Count); }
public void TestEnabledDisabledStochastic() { // Test random Enabled and Pose values. int numberOfObjects = 20; int numberOfSteps = 1000; Shape shape = new SphereShape(1); //var meshShape = new TriangleMeshShape(shape.GetMesh(0.01f, 4)); //meshShape.Partition = new AabbTree<int>(); //shape = meshShape; var geometricObjects = new GeometricObject[numberOfObjects]; var collisionObjects = new CollisionObject[numberOfObjects]; var domain = new CollisionDomain(new CollisionDetection()); for (int i = 0; i < numberOfObjects; i++) { geometricObjects[i] = new GeometricObject(shape); collisionObjects[i] = new CollisionObject(geometricObjects[i]); domain.CollisionObjects.Add(collisionObjects[i]); } for (int i = 0; i < numberOfSteps; i++) { for (int j = 0; j < numberOfObjects; j++) { collisionObjects[j].Enabled = RandomHelper.Random.NextBool(); domain.Update(0); if (RandomHelper.Random.NextFloat(0, 1) > 0.5f) geometricObjects[j].Pose = new Pose(RandomHelper.Random.NextVector3F(-2, 2)); domain.Update(0); } domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); domain.Update(0); // Compare result with brute-force check. for (int j = 0; j < numberOfObjects; j++) { for (int k = j + 1; k < numberOfObjects; k++) { var haveContact = domain.CollisionDetection.HaveContact(collisionObjects[j], collisionObjects[k]); Assert.AreEqual(haveContact, domain.ContactSets.GetContacts(collisionObjects[j], collisionObjects[k]) != null); } } } }
public void SerializationBinary() { Pose poseA = new Pose(new Vector3F(1, 2, 3)); PointShape pointA = new PointShape(3, 4, 5); GeometricObject geometryA = new GeometricObject(pointA, poseA); Pose poseB = new Pose(new Vector3F(11, 22, 33)); PointShape pointB = new PointShape(33, 44, 55); GeometricObject geometryB = new GeometricObject(pointB, poseB); var a = new MinkowskiSumShape(geometryA, geometryB); // Serialize object. var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, a); // Deserialize object. stream.Position = 0; var deserializer = new BinaryFormatter(); var b = (MinkowskiSumShape)deserializer.Deserialize(stream); Assert.AreEqual(a.ObjectA.Pose, b.ObjectA.Pose); Assert.AreEqual(a.ObjectB.Pose, b.ObjectB.Pose); Assert.AreEqual(((PointShape)a.ObjectA.Shape).Position, ((PointShape)b.ObjectA.Shape).Position); Assert.AreEqual(((PointShape)a.ObjectB.Shape).Position, ((PointShape)b.ObjectB.Shape).Position); }
private void CreateCompositeShape() { // Convert islands into CompositeShape with convex children. _decomposition = new CompositeShape(); if (_islands == null) return; foreach (var island in _islands) { if (island.Vertices.Length <= 0) continue; // ReSharper disable EmptyGeneralCatchClause try { // ----- Get convex hull mesh. DcelMesh convexHullMesh; if (island.ConvexHullBuilder == null) { // Create convex hull from scratch. // Get all vertices of all island triangles. var points = island.Triangles.SelectMany(t => t.Vertices); // Create convex hull. convexHullMesh = GeometryHelper.CreateConvexHull(points, VertexLimit, SkinWidth); } else { // Use existing convex hull. convexHullMesh = island.ConvexHullBuilder.Mesh; if (convexHullMesh.Vertices.Count > VertexLimit || SkinWidth != 0) convexHullMesh.ModifyConvex(VertexLimit, SkinWidth); } // ----- Add a ConvexPolyhedron to CompositeShape. if (convexHullMesh.Vertices.Count > 0) { var convexHullPoints = convexHullMesh.Vertices.Select(v => v.Position); var convexPolyhedron = new ConvexPolyhedron(convexHullPoints); var geometricObject = new GeometricObject(convexPolyhedron); _decomposition.Children.Add(geometricObject); } } catch { // Could not generate convex hull. Ignore object. } // ReSharper restore EmptyGeneralCatchClause } }
public void ValidateInvalidScaleChange() { GlobalSettings.ValidationLevel = 0xff; CollisionDomain cd = new CollisionDomain(new CollisionDetection()); var shape = new SphereShape(1); var geometricObject = new GeometricObject(shape, Pose.Identity); var co = new CollisionObject(geometricObject); cd.CollisionObjects.Add(co); Assert.Throws<GeometryException>(() => geometricObject.Scale = new Vector3F(1, 1, float.NaN)); }
public void Grow() { var a = new Aabb(new Vector3F(1, 2, 3), new Vector3F(3, 4, 5)); a.Grow(new Aabb(new Vector3F(1, 2, 3), new Vector3F(3, 4, 5))); Assert.AreEqual(new Aabb(new Vector3F(1, 2, 3), new Vector3F(3, 4, 5)), a); a.Grow(new Aabb(new Vector3F(-1, 2, 3), new Vector3F(3, 4, 5))); Assert.AreEqual(new Aabb(new Vector3F(-1, 2, 3), new Vector3F(3, 4, 5)), a); a.Grow(new Aabb(new Vector3F(1, 2, 3), new Vector3F(3, 5, 5))); Assert.AreEqual(new Aabb(new Vector3F(-1, 2, 3), new Vector3F(3, 5, 5)), a); var geo = new GeometricObject(new SphereShape(3), new Pose(new Vector3F(1, 0, 0))); a.Grow(geo); Assert.AreEqual(new Aabb(new Vector3F(-2, -3, -3), new Vector3F(4, 5, 5)), a); }
public override void AddObject(GeometricObject o) { bbox.Union(o.GetBoundingBox()); Objects.Add(o); }