public static Node Create(Vector3 pos, int collisionMask) { var cmp = new MyBlock (); var spr = new Sprite (128, 64); spr.AddTexture (Resource.GetDefaultTexture ()); spr.Color = Color.Cyan; var col = new CollisionObject (); col.Shape = new BoxShape (spr.Width/2, spr.Height/2, 1); col.SetOffset (spr.Width/2, spr.Height/2, 1); col.CollideWith = collisionMask; var label = new Label (); label.Text = "Mask = 0x" + collisionMask.ToString("x"); var node = new Node ("Block"); node.Attach (cmp); node.Attach (spr); node.Attach (col); node.Attach (label); node.Translation = pos; return node; }
public void Test_Overlap() { var node1 = new Node ("Node1"); var col1 = new CollisionObject (); col1.Shape = new BoxShape (1, 1, 1); node1.Attach (col1); var node2 = new Node ("Node1"); var col2 = new CollisionObject (); col2.Shape = new BoxShape (1, 1, 1); node2.Attach (col2); var wld = new World (); wld.AddChild (node1); wld.AddChild (node2); node1.Translate (0, 0, 0); node2.Translate (0, 0, 0); Assert.AreEqual (true, wld.Overlap (node1, node2)); node1.Translate (0, 0, 0); node2.Translate (10, 0, 0); Assert.AreEqual (false, wld.Overlap (node1, node2)); node1.Rotate (0, 0, 0, 0); node2.Rotate (45, 0, 0, 1); Assert.AreEqual (false, wld.Overlap (node1, node2)); node1.Detach (col1); node2.Detach (col2); Assert.AreEqual (false, wld.Overlap (node1, node2)); }
public static Node Create(Vector3 pos) { var cmp = new MySprite (); var spr = new Sprite (64, 64); spr.AddTexture (new Texture ("media/Box-64x64.png")); var col = new CollisionObject (); col.Shape = new BoxShape (spr.Width / 2, spr.Height / 2, 100); col.SetOffset (spr.Width / 2, spr.Height / 2, 0); var mbox1 = new MailBox ("MouseSelect"); var mbxo2 = new MailBox ("MouseDeselect"); var node = new Node ("MySprite"); node.Attach (cmp); node.Attach (spr); node.Attach (col); node.Attach (mbox1); node.Attach (mbxo2); node.Translation = pos; return node; }
public void Test_Distance_Box_to_Box() { var node1 = new Node ("Node1"); var col1 = new CollisionObject (); col1.Shape = new BoxShape (1, 1, 1); col1.SetOffset (0, 0, 0); node1.Attach (col1); var node2 = new Node ("Node2"); var col2 = new CollisionObject (); col2.Shape = new BoxShape (1,1,1); col2.SetOffset (0, 10, 0); node2.Attach (col2); node1.Translate (0, 0, 0); node2.Translate (10, 0, 0); // 10*√2 var wld = new World (); wld.AddChild (node1); wld.AddChild (node2); wld.CollisionUpdate (); Assert.AreEqual(10*1.4142f - 2*1.4142f, wld.Distance (node1, node2), 0.05f); wld.Destroy (); }
public void Test_Overlaps() { var node1 = new Node ("Node1"); var col1 = new CollisionObject (); col1.Shape = new BoxShape (1, 1, 1); node1.Attach (col1); var node2 = new Node ("Node2"); var col2 = new CollisionObject (); col2.Shape = new BoxShape (1, 1, 1); node2.Attach (col2); var wld = new World (); wld.AddChild (node1); wld.AddChild (node2); // コリジョン発生 wld.CollisionUpdate (); Assert.AreEqual (1, col1.OverlappingObjectCount); Assert.AreEqual (1, col2.OverlappingObjectCount); // コリジョン消失 node2.Translate (10, 0, 0); wld.CollisionUpdate (); Assert.AreEqual (0, col1.OverlappingObjectCount); Assert.AreEqual (0, col2.OverlappingObjectCount); }
public void Test2() { // Test following: A ray touches a plane at the ray-end. Then the plane moves so that // they are separated. PlaneRayAlgorithm algo = new PlaneRayAlgorithm(new CollisionDetection()); // Plane in xz plane. CollisionObject plane = new CollisionObject(new GeometricObject { Shape = new PlaneShape(Vector3F.UnitY, 1), Pose = new Pose(new Vector3F(0, -1, 0)), }); // Ray CollisionObject ray = new CollisionObject(new GeometricObject { Shape = new RayShape(new Vector3F(0, 0, 0), new Vector3F(0, -1, 0), 10), Pose = new Pose(new Vector3F(0, 10, 0)), }); ContactSet contacts = algo.GetContacts(plane, ray); Assert.AreEqual(true, contacts.HaveContact); Assert.AreEqual(1, contacts.Count); Assert.AreEqual(10, contacts[0].PenetrationDepth); // Move plane less than contact position tolerance, but into a separated state. ((GeometricObject)plane.GeometricObject).Pose = new Pose(new Vector3F(0, -1.001f, 0)); algo.UpdateClosestPoints(contacts, 0); Assert.AreEqual(false, contacts.HaveContact); Assert.AreEqual(1, contacts.Count); Assert.IsTrue(Numeric.AreEqual(-0.001f, contacts[0].PenetrationDepth)); }
public void TestMethods() { NoCollisionAlgorithm algo = new NoCollisionAlgorithm(new CollisionDetection()); CollisionObject a = new CollisionObject { GeometricObject = new GeometricObject { Shape = new SphereShape(1) } }; CollisionObject b = new CollisionObject { GeometricObject = new GeometricObject { Shape = new SphereShape(2) } }; Assert.AreEqual(a, algo.GetClosestPoints(a, b).ObjectA); Assert.AreEqual(b, algo.GetClosestPoints(a, b).ObjectB); Assert.AreEqual(0, algo.GetClosestPoints(a, b).Count); Assert.AreEqual(a, algo.GetContacts(a, b).ObjectA); Assert.AreEqual(b, algo.GetContacts(a, b).ObjectB); Assert.AreEqual(0, algo.GetContacts(a, b).Count); Assert.AreEqual(false, algo.HaveContact(a, b)); ContactSet cs = ContactSet.Create(a, b); algo.UpdateClosestPoints(cs, 0); Assert.AreEqual(a, cs.ObjectA); Assert.AreEqual(b, cs.ObjectB); Assert.AreEqual(0, cs.Count); cs = ContactSet.Create(a, b); cs.Add(Contact.Create()); algo.UpdateContacts(cs, 0); Assert.AreEqual(a, cs.ObjectA); Assert.AreEqual(b, cs.ObjectB); Assert.AreEqual(0, cs.Count); }
public static Node Create(Vector3 pos) { var cmp = new MyButton (); var btn = new Button (ButtonType.Push); btn.Normal = new Texture ("media/image128x128(Red).png"); btn.Pressed = new Texture ("media/image128x128(Green).png"); var label1 = new Label (); var label2 = new Label (); label2.SetOffset (0, 20); var col = new CollisionObject (); col.Shape = new BoxShape (64, 64, 100); col.SetOffset (64, 64, 100); var node = new Node ("Button"); node.Attach (btn); node.Attach (cmp); node.Attach (label1); node.Attach (label2); node.Attach (col); node.Translation = pos; return node; }
public void TestSeparated() { PlaneConvexAlgorithm algo = new PlaneConvexAlgorithm(new CollisionDetection()); CollisionObject a = new CollisionObject(new GeometricObject { Shape = new BoxShape(1, 2, 3), Pose = new Pose(new Vector3F(0, 2, 0)), }); CollisionObject b = new CollisionObject(new GeometricObject { Shape = new PlaneShape(new Vector3F(0, 1, 0), 0), }); Assert.AreEqual(false, algo.HaveContact(a, b)); Assert.AreEqual(1,algo.GetClosestPoints(a, b).Count); Assert.AreEqual(new Vector3F(0, -1, 0), algo.GetClosestPoints(a, b)[0].Normal); Assert.AreEqual(new Vector3F(0.5f, 0.5f, 1.5f), algo.GetClosestPoints(a, b)[0].Position); Assert.IsTrue(Numeric.AreEqual(-1, algo.GetClosestPoints(a, b)[0].PenetrationDepth)); // Test swapped. Assert.AreEqual(false, algo.HaveContact(b, a)); Assert.AreEqual(1, algo.GetClosestPoints(b, a).Count); Assert.AreEqual(new Vector3F(0, 1, 0), algo.GetClosestPoints(b, a)[0].Normal); Assert.AreEqual(new Vector3F(0.5f, 0.5f, 1.5f), algo.GetClosestPoints(b, a)[0].Position); Assert.IsTrue(Numeric.AreEqual(-1, algo.GetClosestPoints(b, a)[0].PenetrationDepth)); Assert.AreEqual(0, algo.GetContacts(a, b).Count); }
public void updateSingleAabb(CollisionObject colObj) { btVector3 minAabb, maxAabb; colObj.CollisionShape.getAabb(colObj.WorldTransform, out minAabb, out maxAabb); //need to increase the aabb for contact thresholds btVector3 contactThreshold = new btVector3(PersistentManifold.gContactBreakingThreshold, PersistentManifold.gContactBreakingThreshold, PersistentManifold.gContactBreakingThreshold); //minAabb -= contactThreshold; //maxAabb += contactThreshold; minAabb.Subtract(ref contactThreshold); maxAabb.Add(ref contactThreshold); //IBroadphaseInterface bp = m_broadphasePairCache; //moving objects should be moderately sized, probably something wrong if not if (colObj.isStaticObject || ((maxAabb - minAabb).Length2 < 1e12f)) { m_broadphasePairCache.setAabb(colObj.BroadphaseHandle,ref minAabb,ref maxAabb, m_dispatcher1); } else { //something went wrong, investigate //this assert is unwanted in 3D modelers (danger of loosing work) colObj.ActivationState = ActivationStateFlags.DISABLE_SIMULATION; if (reportMe && m_debugDrawer != null) { reportMe = false; m_debugDrawer.reportErrorWarning("Overflow in AABB, object removed from simulation"); m_debugDrawer.reportErrorWarning("If you can reproduce this, please email [email protected]\n"); m_debugDrawer.reportErrorWarning("Please include above information, your Platform, version of OS.\n"); m_debugDrawer.reportErrorWarning("Thanks.\n"); } } }
public void Test_Distance() { var node1 = new Node ("Node1"); var col1 = new CollisionObject (); col1.Shape = new BoxShape (1, 1, 1); node1.Attach (col1); var node2 = new Node ("Node2"); var col2 = new CollisionObject (); col2.Shape = new BoxShape (1, 1, 1); node2.Attach (col2); var wld = new World (); wld.AddChild (node1); wld.AddChild (node2); node1.Translate (0, 0, 0); node2.Translate (0, 0, 0); Assert.AreEqual (0, wld.Distance (node1, node2)); node1.Translate (0, 0, 0); node2.Translate (10, 0, 0); Assert.AreEqual (8, wld.Distance (node1, node2), 0.01f); node1.Rotate (0, 0, 0, 0); node2.Rotate (45, 0, 0, 1); Assert.AreEqual (7.6f, wld.Distance (node1, node2), 0.01f); node1.Detach (col1); node2.Detach (col2); Assert.AreEqual (Single.NaN, wld.Distance (node1, node2), 0.01f); }
public void ComputeCollision() { RayConvexAlgorithm algo = new RayConvexAlgorithm(new CollisionDetection()); CollisionObject ray = new CollisionObject(new GeometricObject { Shape = new RayShape(new Vector3F(0, 0, 0), new Vector3F(-1, 0, 0), 10), Pose = new Pose(new Vector3F(11, 0, 0)) }); CollisionObject triangle = new CollisionObject(new GeometricObject { Shape = new TriangleShape(new Vector3F(0, 0, 0), new Vector3F(0, 1, 0), new Vector3F(0, 0, 1)), Pose = Pose.Identity, }); ContactSet set; // Separated set = algo.GetClosestPoints(ray, triangle); Assert.AreEqual(new Vector3F(1, 0, 0), set[0].PositionAWorld); Assert.AreEqual(new Vector3F(0, 0, 0), set[0].PositionBWorld); Assert.AreEqual(-1, set[0].PenetrationDepth); Assert.AreEqual(false, algo.HaveContact(ray, triangle)); Assert.AreEqual(false, algo.HaveContact(triangle, ray)); Assert.AreEqual(0, algo.GetContacts(ray, triangle).Count); // Touching Pose newPose = ray.GeometricObject.Pose; newPose.Position = new Vector3F(5, 0, 0); ((GeometricObject)ray.GeometricObject).Pose = newPose; set = algo.GetClosestPoints(triangle, ray); Assert.AreEqual(new Vector3F(0, 0, 0), set[0].PositionBWorld); Assert.AreEqual(new Vector3F(0, 0, 0), set[0].PositionAWorld); Assert.AreEqual(5, set[0].PenetrationDepth); Assert.AreEqual(true, algo.HaveContact(ray, triangle)); Assert.AreEqual(true, algo.HaveContact(triangle, ray)); newPose = ray.GeometricObject.Pose; newPose.Position = new Vector3F(4, 0.1f, 0.1f); ((GeometricObject)ray.GeometricObject).Pose = newPose; algo.UpdateContacts(set, 0); Assert.AreEqual(new Vector3F(0, 0.1f, 0.1f), set[0].PositionBWorld); Assert.AreEqual(new Vector3F(0, 0.1f, 0.1f), set[0].PositionAWorld); Assert.AreEqual(new Vector3F(0, 0.1f, 0.1f), set[0].Position); Assert.AreEqual(4, set[0].PenetrationDepth); Assert.AreEqual(true, algo.HaveContact(ray, triangle)); Assert.AreEqual(true, algo.HaveContact(triangle, ray)); // Through triangle plane but separated. newPose = ray.GeometricObject.Pose; newPose.Position = new Vector3F(5, 1.1f, 0.1f); ((GeometricObject)ray.GeometricObject).Pose = newPose; algo.UpdateContacts(set, 0); Assert.AreEqual(0, set.Count); algo.UpdateClosestPoints(set, 0); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 1.1f, 0.1f), set[0].PositionBWorld)); Assert.AreEqual(false, algo.HaveContact(ray, triangle)); Assert.AreEqual(false, algo.HaveContact(triangle, ray)); }
public static Node Create(string name, string target, Vector3 pos) { var cmp = new MyButton (); cmp.target = target; var spr = new Sprite (64, 64); spr.AddTexture (new Texture("media/ButtonRed-Active-64x64.png")); spr.AddTexture (new Texture ("media/ButtonGreen-Active-64x64.png")); spr.AddTexture (new Texture ("media/ButtonBlue-Active-64x64.png")); switch (target) { case "A子": spr.ActiveTexture = 0; break; case "B子": spr.ActiveTexture = 1; break; case "C子": spr.ActiveTexture = 2; break; } var col = new CollisionObject (); col.Shape = new BoxShape (40, 25, 1); col.SetOffset (40, 25, 0); var node = new Node (name); node.Attach (cmp); node.Attach (spr); node.Attach (col); node.UserData.Add ("PinPon", new SoundEffectTrack ("media/PinPon.wav")); node.Translation = pos; return node; }
public void TestContainment() { PlaneSphereAlgorithm algo = new PlaneSphereAlgorithm(new CollisionDetection()); CollisionObject objectA = new CollisionObject(); ((GeometricObject)objectA.GeometricObject).Shape = new SphereShape(1); ((GeometricObject)objectA.GeometricObject).Pose = new Pose(new Vector3F(0, -2, 0)); CollisionObject objectB = new CollisionObject(); ((GeometricObject)objectB.GeometricObject).Shape = new PlaneShape(new Vector3F(0, 1, 0).Normalized, 0); ((GeometricObject)objectB.GeometricObject).Pose = new Pose(new Vector3F(0, 0, 0)); ContactSet cs = ContactSet.Create(objectA, objectB); algo.UpdateContacts(cs, 0); Assert.AreEqual(objectA, cs.ObjectA); Assert.AreEqual(objectB, cs.ObjectB); Assert.AreEqual(1, cs.Count); Assert.AreEqual(new Vector3F(0, -1.5f, 0), cs[0].Position); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, -1, 0), cs[0].Normal)); Assert.IsTrue(Numeric.AreEqual(3, cs[0].PenetrationDepth)); // Test swapped case: cs = ContactSet.Create(objectB, objectA); algo.UpdateContacts(cs, 0); Assert.AreEqual(objectB, cs.ObjectA); Assert.AreEqual(objectA, cs.ObjectB); Assert.AreEqual(1, cs.Count); Assert.AreEqual(new Vector3F(0, -1.5f, 0), cs[0].Position); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 1, 0), cs[0].Normal)); Assert.IsTrue(Numeric.AreEqual(3, cs[0].PenetrationDepth)); }
public void Containment() { MinkowskiPortalRefinement algo = new MinkowskiPortalRefinement(new CollisionDetection()); CollisionObject a = new CollisionObject(new GeometricObject { Shape = new SphereShape(2), Pose = new Pose(new Vector3F(1, 2, 3)), }); CollisionObject b = new CollisionObject(new GeometricObject { Shape = new SphereShape(1), Pose = new Pose(new Vector3F(1, 2, 3)), }); ContactSet set; set = algo.GetContacts(a, b); Assert.AreEqual(true, algo.HaveContact(a, b)); Assert.AreEqual(3, set[0].PenetrationDepth); ((GeometricObject)b.GeometricObject).Pose = new Pose(new Vector3F(2, 2, 3)); algo.UpdateContacts(set, 0); Assert.AreEqual(true, algo.HaveContact(a, b)); Assert.AreEqual(2, set[0].PenetrationDepth); }
public void ComputeCollision() { MinkowskiPortalRefinement algo = new MinkowskiPortalRefinement(new CollisionDetection()); CollisionObject a = new CollisionObject(new GeometricObject { Shape = new TriangleShape(new Vector3F(0, 0, 0), new Vector3F(0, 1, 0), new Vector3F(0, 0, 1)) }); CollisionObject b = new CollisionObject(new GeometricObject { Shape = new SphereShape(1) }); ContactSet set; set = algo.GetContacts(a, b); Assert.AreEqual(true, algo.HaveContact(a, b)); Assert.IsTrue(Numeric.AreEqual(1, set[0].PenetrationDepth)); ((GeometricObject)b.GeometricObject).Pose = new Pose(new Vector3F(2, 0.1f, 0.2f)); algo.UpdateContacts(set, 0); Assert.AreEqual(false, algo.HaveContact(a, b)); Assert.AreEqual(0, set.Count); ((GeometricObject)b.GeometricObject).Pose = new Pose(new Vector3F(0.9f, 0.1f, 0.2f)); algo.UpdateContacts(set, 0); Assert.AreEqual(true, algo.HaveContact(a, b)); Assert.AreEqual(1, set.Count); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0.1f, 0.2f), set[0].PositionAWorld, 0.02f)); }
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 static Node Create(string name, string texture, Vector3 pos) { var cmp = new MyTarget (); var spr = new Sprite (64, 128); spr.AddTexture (new Texture (texture)); var col = new CollisionObject (); col.Shape = new BoxShape (spr.Width/2, spr.Height/2, 100); col.SetOffset (spr.Width/2, spr.Height/2, 0); var mbox1 = new MailBox ("MouseSelect"); var mbox2 = new MailBox ("MouseDeselect"); var node = new Node (name); node.Attach (cmp); node.Attach (col); node.Attach (spr); node.Attach (mbox1); node.Attach (mbox2); node.Translation = pos; return node; }
public void ComputeCollision() { Gjk algo = new Gjk(new CollisionDetection()); CollisionObject a = new CollisionObject { GeometricObject = new GeometricObject(new TriangleShape(new Vector3F(0, 0, 0), new Vector3F(0, 1, 0), new Vector3F(0, 0, 1)), Pose.Identity), }; CollisionObject b = new CollisionObject { GeometricObject = new GeometricObject(new SphereShape(1), Pose.Identity), }; ContactSet set; set = algo.GetClosestPoints(a, b); Assert.AreEqual(true, algo.HaveContact(a, b)); Assert.AreEqual(0, set[0].PenetrationDepth); ((GeometricObject)b.GeometricObject).Pose = new Pose(new Vector3F(2, 0.1f, 0.2f)); algo.UpdateClosestPoints(set, 0); Assert.AreEqual(false, algo.HaveContact(a, b)); Assert.IsTrue(Numeric.AreEqual(-1, set[0].PenetrationDepth, 0.001f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0.1f, 0.2f), set[0].PositionAWorld, 0.01f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(1, 0.1f, 0.2f), set[0].PositionBWorld, 0.01f)); }
public void ComputeCollisionLineOther() { CollisionObject line0 = new CollisionObject(); //line0.Name = "line0"; ((GeometricObject)line0.GeometricObject).Shape = new LineShape(new Vector3F(0, 0, 1), new Vector3F(1, 0, 0)); ((GeometricObject)line0.GeometricObject).Pose = new Pose(new Vector3F(0, 0, 2)); CollisionObject sphere = new CollisionObject(); //sphere.Name = "sphere"; ((GeometricObject)sphere.GeometricObject).Shape = new SphereShape(1); ((GeometricObject)sphere.GeometricObject).Pose = Pose.Identity; LineAlgorithm algo = new LineAlgorithm(new CollisionDetection()); ContactSet set; set = algo.GetClosestPoints(line0, sphere); Assert.IsTrue(Numeric.AreEqual(-2, set[0].PenetrationDepth)); Assert.IsTrue(Vector3F.AreNumericallyEqual(-Vector3F.UnitZ, set[0].Normal)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 2), set[0].Position, 0.001f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 1), set[0].PositionALocal, 0.001f)); Assert.AreEqual(false, algo.HaveContact(line0, sphere)); set = set.Swapped; ((GeometricObject)sphere.GeometricObject).Pose = new Pose(new Vector3F(0, 0, 2.1f)); algo.UpdateContacts(set, 0); Assert.IsTrue(Numeric.AreEqual(0.1f, set[0].PenetrationDepth, 0.001f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(Vector3F.UnitZ, set[0].Normal, 0.1f)); // Large epsilon because MPR for spheres is not very accurate. Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 3), set[0].Position, 0.1f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 1), set[0].PositionALocal, 0.1f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 1), set[0].PositionBLocal, 0.1f)); Assert.AreEqual(true, algo.HaveContact(line0, sphere)); }
public void ComputeCollision() { CollisionDetection cd = new CollisionDetection(); CombinedCollisionAlgorithm cca = new CombinedCollisionAlgorithm(cd, new SphereSphereAlgorithm(cd), new SphereSphereAlgorithm(cd)); CollisionObject a = new CollisionObject(new GeometricObject { Shape = new SphereShape(1), }); CollisionObject b = new CollisionObject(new GeometricObject { Shape = new SphereShape(1), Pose = new Pose(new Vector3F(3, 0, 0)), }); ContactSet set = cca.GetClosestPoints(a, b); Assert.AreEqual(1, set.Count); set = cca.GetContacts(a, b); Assert.IsTrue(set == null || set.Count == 0); ((GeometricObject)b.GeometricObject).Pose = new Pose(new Vector3F(2, 0, 0)); set = cca.GetClosestPoints(a, b); Assert.AreEqual(1, set.Count); set = cca.GetContacts(a, b); Assert.AreEqual(1, set.Count); }
public void AxisAlignedBoundingBox() { CollisionObject obj = new CollisionObject(new GeometricObject(new SphereShape(0.3f), new Pose(new Vector3F(1, 2, 3)))); Assert.AreEqual(new Aabb(new Vector3F(0.7f, 1.7f, 2.7f), new Vector3F(1.3f, 2.3f, 3.3f)), obj.GeometricObject.Aabb); }
public static Node Create(string name, string address, Vector3 pos) { var cmp = new MyButton (); cmp.address = address; var spr = new Sprite (32, 32); spr.AddTexture (Resource.GetDefaultTexture ()); var label = new Label (); label.Text = name; label.Color = Color.Black; label.SetOffset (8, 8); var col = new CollisionObject (); col.Shape = new BoxShape(16, 16, 16); col.SetOffset (16, 16, 16); var node = new Node (name); node.Attach (cmp); node.Attach (spr); node.Attach (label); node.Attach (col); node.Translation = pos; return node; }
public static Node Create(Vector3 pos) { var cmp = new MyComponent (); var spr = new Sprite (480, 300); spr.AddTexture (new Texture ("media/Vanity.jpg")); spr.AddTexture (new Texture ("media/Tanks.png")); spr.AddTexture (new Texture ("media/TatamiRoom.png")); spr.AutoScale = true; Console.WriteLine ("tex = " + spr.GetTexture (0)); Console.WriteLine ("spr = " + spr); var col = new CollisionObject(); col.Shape = new BoxShape(spr.Width/2, spr.Height/2, 100); col.SetOffset (spr.Width/2, spr.Height/2, 0); var ctr = new AnimationController (); var node = new Node (); node.Attach (cmp); node.Attach (spr); node.Attach (col); node.Attach (ctr); node.Translation = pos; var clip = new SoundClip ("Sound"); clip.AddTrack (new SoundEffectTrack ("media/PinPon.wav")); node.UserData.Add (clip.Name, clip); return node; }
/// <summary> /// /// </summary> /// <param name="pos"></param> /// <param name="groupID"></param> /// <returns></returns> public static Node Create(Vector3 pos, int groupID) { var cmp = new MyCharacter (); var spr = new Sprite (64, 64); spr.AddTexture (Resource.GetDefaultTexture ()); spr.Color = Color.Red; spr.SetOffset (-32, -32); var col = new CollisionObject (); col.Shape = new BoxShape (spr.Width / 2, spr.Height / 2, 1); //col.SetOffset (spr.Width / 2, spr.Height / 2, 1); var label = new Label (); label.Text = "ID = 0x" + groupID.ToString ("x"); label.SetOffset (-spr.Width / 2, -spr.Height / 2); var node = new Node ("MyCharacter"); node.Attach (cmp); node.Attach (spr); node.Attach (col); node.Attach (label); node.Translation = pos; node.GroupID = groupID; return node; }
public static Node Create(string fileName) { var cmp = new MyTiledMap (); var tiledMap = new TiledMapComposer (); var node = new Node ("TiledMap"); node.Attach (tiledMap); node.Attach (cmp); tiledMap.LoadFromFile (fileName); var halfWidth = tiledMap.TileWidth / 2; var halfHeight = tiledMap.TileHeight / 2; // コリジョンなどのゲームロジックはすべて // 仮想の2D直交座標系で作成する // 表示だけアイソメトリック var colMap = node.Find("CollisionMap"); foreach (var tile in colMap.Downwards.Skip(1)) { var col = new CollisionObject(); col.Shape = new BoxShape (halfWidth, halfHeight, 1); col.SetOffset (halfWidth, halfHeight, 1); tile.Attach (col); } return node; }
public void Test_BoxShape() { var node = new Node (); var col = new CollisionObject (); col.Shape = new BoxShape (1, 1, 1); col.SetOffset (1, 0, 0); node.Attach (col); node.Translate (1, 0, 0); node.Rotate (45, 0, 0, 1); var wld = new World (); wld.AddChild (node); wld.CollisionUpdate (); // (1+1) - 1*√2 var result = wld.RayCast (new Vector3 (-10, 0, 0), new Vector3 (10, 0, 0)).First (); Assert.AreEqual (true, result.Hit); Assert.AreEqual (new Vector3 (0.5858f, 0, 0), result.Point); Assert.AreEqual (new Vector3 (-1, 0, 0), result.Normal); Assert.AreEqual (10.58579f, result.Distance, 0.01f); Assert.AreEqual (0.5245f, result.Fraction, 0.01f); // (1+1) + 1*√2 result = wld.RayCast (new Vector3 (10, 0, 0), new Vector3 (-10, 0, 0)).First (); Assert.AreEqual (true, result.Hit); Assert.AreEqual (new Vector3 (3.4142f, 0, 0), result.Point); Assert.AreEqual (new Vector3 (1, 0, 0), result.Normal); Assert.AreEqual (6.585786f, result.Distance, 0.01f); Assert.AreEqual (0.3225f, result.Fraction, 0.01f); wld.Destroy (); }
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 Test_SetIgnoreWith() { var col = new CollisionObject (); col.IgnoreWith = 255; Assert.AreEqual (255, col.IgnoreWith); }
public void Test_SetCollideWith() { var col = new CollisionObject (); col.CollideWith = 15; Assert.AreEqual (15, col.CollideWith); }
public byte DefaultCollisionValue(int x, int y, float BumpConstraint, float[,] GroundPlane, byte b, float[,] Heights, CollisionIndex[,] cI) { if (x < 1 || y < 1 || x >= MaxXPt || y >= MaxYPt) { return(SimPathStore.BLOCKED); } if (b == SimPathStore.STICKY_PASSABLE) { return(b); } float ZLevel = Heights[x, y]; float GLevel = GroundPlane[x, y]; if (ZLevel < GLevel) { ZLevel = GLevel; } var cci = cI[x, y]; CollisionObject collisionObject = null; if (cci != null) { collisionObject = cci.GetObjectAt(ZLevel); } int bumps; if (x == 270 && y == 1275) { collisionObject = collisionObject; } // fixed "contiguous objects need to be passable" if (collisionObject != null) { bumps = NeighborBump(x, y, ZLevel, MaxZ, ZLevel, 0.1f, Heights, cI, collisionObject, false, true); if (bumps >= 4) { return(SimPathStore.BRIDGY); } } // this tests for hieght differnces bumps = NeighborBump(x, y, ZLevel, MaxZ, ZLevel, BumpConstraint, Heights, cI, collisionObject, false, false); if (bumps > 0) { return(SimPathStore.BLOCKED); } if (bumps > 0) { // return SimPathStore.BLOCKED; } if (BumpConstraintPurple > CollisionIndex.MaxBumpInOpenPath) { // this looks for transitions between objects bumps = NeighborBump(x, y, ZLevel, MaxZ, ZLevel, BumpConstraintPurple, Heights, cI, collisionObject, true, false); if (bumps > 0) { return(SimPathStore.BLOCK_PURPLE); } } if (collisionObject == null) { float Water = PathStore.WaterHeight; if (DiffLessThan(Water, ZLevel, 0.1f)) { return(SimPathStore.WATER_Z); } if (DiffLessThan(Water, PathStore.GroundPlane[x, y], 0.1f)) { return(SimPathStore.WATER_G); } float MaxZLevel = MaxZ; if (MaxZLevel <= ZLevel - 2) { return(SimPathStore.TOO_HIGH); } if (ZLevel + 20 < MaxZLevel) // needs passable { return(SimPathStore.TOO_LOW); } } // this looks for transitions between objects bumps = NeighborBump(x, y, ZLevel, MaxZ, ZLevel, 0.1f, Heights, cI, collisionObject, true, false); if (bumps > 0) { if (collisionObject != null) { return(SimPathStore.MAYBE_BLOCKED); } return(SimPathStore.MAYBE_BLOCKED); } // this looks for little bumps bumps = NeighborBump(x, y, ZLevel, MaxZ, ZLevel, 0.1f, Heights, cI, null, false, false); if (bumps > 0) { return(SimPathStore.BLOCK_PURPLE); } if (b > 200) { return(--b); } CollisionIndex c = cI[x, y]; if (c != null) { return(c.GetOccupiedValue(GLevel, ZLevel)); } if (b > 10) { return(--b); } return(SimPathStore.INITIALLY); }
public Physics() { // collision configuration contains default setup for memory, collision setup collisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(collisionConf); Broadphase = new DbvtBroadphase(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, collisionConf); World.Gravity = new Vector3(0, -10, 0); CollisionShapes = new List <CollisionShape>(); // create the ground CollisionShape groundShape = new BoxShape(50, 1, 50); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies float mass = 1.0f; CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); float start_x = StartPosX - ArraySizeX / 2; float start_y = StartPosY; float start_z = StartPosZ - ArraySizeZ / 2; int k, i, j; for (k = 0; k < ArraySizeY; k++) { for (i = 0; i < ArraySizeX; i++) { for (j = 0; j < ArraySizeZ; j++) { Matrix startTransform = Matrix.CreateTranslation( new Vector3( 2 * i + start_x, 2 * k + start_y, 2 * j + start_z ) ); // using motionstate is recommended, it provides interpolation capabilities // and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); RigidBody body = new RigidBody(rbInfo); // make it drop from a height body.Translate(new Vector3(0, 20, 0)); World.AddRigidBody(body); } } } }
internal int NeighborLevelDifLessThan(float low, float high, float originZ, int PX, int PY, float[,] heights, CollisionIndex[,] cI, CollisionObject collisionObject, float mostDiff, bool objToNothingBump, bool objToSelfBump) { float O = NeighborLevel(low, high, originZ, PX, PY, heights); if (objToSelfBump) { if (collisionObject != null) { var FO = cI[PX, PY]; if (FO != null) { var fO = FO.GetObjectAt(O); if (fO == collisionObject) { return(1); } if (FO.Contains(collisionObject)) { return(1); } } } return(0); } if (objToNothingBump) { var FO = cI[PX, PY]; if (collisionObject != null) { if (FO == null) { return(1); } var fO = FO.GetObjectAt(O); if (fO != collisionObject) { return(1); } return(0); } else { if (FO == null) { return(0); } var fO = FO.GetObjectAt(O); if (fO != null) { return(1); } return(0); } return(0); } if (!DiffLessThan(O, originZ, mostDiff)) { if (collisionObject != null) { var FO = cI[PX, PY]; if (FO != null) { var fO = FO.GetObjectAt(O); if (fO == collisionObject) { return(0); } // experimental //if (FO.Contains(collisionObject)) return 0; } } return(1); } return(0); }