{"action":"created","issue":{"url":"https://api.github.com/repos/schteppe/p2.js/issues/202","labels_url":"https://api.github.com/repos/schteppe/p2.js/issues/202/labels{/name}","comments_url":"https://api.github.com/repos/schteppe/p2.js/issues/202/comments","events_url":"https://api.github.com/repos/schteppe/p2.js/issues/202/events","html_url":"https://github.com/schteppe/p2.js/issues/202","id":128365206,"number":202,"title":"Objects overlap even when stiffness set to MAX_VALUE","user":{"login":"sudoman","id":342930,"avatar_url":"https://avatars.githubusercontent.com/u/342930?v=3","gravatar_id":"","url":"https://api.github.com/users/sudoman","html_url":"https://github.com/sudoman","followers_url":"https://api.github.com/users/sudoman/followers","following_url":"https://api.github.com/users/sudoman/following{/other_user}","gists_url":"https://api.github.com/users/sudoman/gists{/gist_id}","starred_url":"https://api.github.com/users/sudoman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sudoman/subscriptions","organizations_url":"https://api.github.com/users/sudoman/orgs","repos_url":"https://api.github.com/users/sudoman/repos","events_url":"https://api.github.com/users/sudoman/events{/privacy}","received_events_url":"https://api.github.com/users/sudoman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-24T01:24:25Z","updated_at":"2016-01-24T11:55:51Z","closed_at":null,"body":"When contact stiffness is set to `Number.MAX_VALUE` in the circles demo, there often are a few circles that overlap quite a bit, even after they've all settled down. In the restitution demo I noticed that the circle on the left is occasionally drawn intersecting with the block below it.\r\n\r\nMy hope is that these stiff objects might never overlap, overlap by very tiny amounts, or (at least) overlap for brief durations only.\r\n\r\n````diff\r\ndiff --git a/demos/circles.html b/demos/circles.html\r\nindex 10a0a59..bacef54 100644\r\n--- a/demos/circles.html\r\n+++ b/demos/circles.html\r\n@@ -47,6 +47,9 @@\r\n             // Enables sleeping of bodies\r\n             world.sleepMode = p2.World.BODY_SLEEPING;\r\n \r\n+            var circleMaterial = new p2.Material();\r\n+            world.addContactMaterial(new p2.ContactMaterial(circleMaterial, circleMaterial, {restitution: 0.1, stiffness: Number.MAX_VALUE}));\r\n+\r\n             // Create circle bodies\r\n             for(var i=0; i<N; i++){\r\n                 for(var j=M-1; j>=0; j--){\r\n@@ -56,7 +59,9 @@\r\n                         mass: 1,\r\n                         position: [x, y],\r\n                     });\r\n-                    p.addShape(new p2.Circle({ radius: r }));\r\n+                    var circle = new p2.Circle({ radius: r });\r\n+                    circle.material = circleMaterial;\r\n+                    p.addShape(circle);\r\n                     p.allowSleep = true;\r\n                     p.sleepSpeedLimit = 1;  // Body will feel sleepy if speed<1 (speed is the norm of velocity)\r\n                     p.sleepTimeLimit = 1;   // Body falls asleep after 1s of sleepiness\r\n````"},"comment":{"url":"https://api.github.com/repos/schteppe/p2.js/issues/comments/174288598","html_url":"https://github.com/schteppe/p2.js/issues/202#issuecomment-174288598","issue_url":"https://api.github.com/repos/schteppe/p2.js/issues/202","id":174288598,"user":{"login":"schteppe","id":1063152,"avatar_url":"https://avatars.githubusercontent.com/u/1063152?v=3","gravatar_id":"","url":"https://api.github.com/users/schteppe","html_url":"https://github.com/schteppe","followers_url":"https://api.github.com/users/schteppe/followers","following_url":"https://api.github.com/users/schteppe/following{/other_user}","gists_url":"https://api.github.com/users/schteppe/gists{/gist_id}","starred_url":"https://api.github.com/users/schteppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/schteppe/subscriptions","organizations_url":"https://api.github.com/users/schteppe/orgs","repos_url":"https://api.github.com/users/schteppe/repos","events_url":"https://api.github.com/users/schteppe/events{/privacy}","received_events_url":"https://api.github.com/users/schteppe/received_events","type":"User","site_admin":false},"created_at":"2016-01-24T11:55:51Z","updated_at":"2016-01-24T11:55:51Z","body":"Hi,\r\nWe all want the overlap to be as small as possible :) Even thought the stiffness has a role in this problem, the time step size is probably worse.\r\n\r\nTo move bodies forward in time, this is the formula used:\r\n\r\n```\r\nx += v * dt\r\n```\r\n\r\nLet's say you have a configuration where a body is very close to another body, but not overlapping. The body is moving towards the other body with a velocity ```v```. Next time step, the bodies will overlap by ```v * dt``` length units, no matter the value of ```stiffness```.\r\n\r\nIf you're lucky, your configuration will yield some smaller overlap. If you choose the time step parameter to be small, you can make the overlaps smaller.\r\n\r\nYou can also make sure to limit the velocities of things in the world (this is a common gamedev trick). For example, less gravity value will give you less speedy objects.\r\n\r\nRelated is the experimental CCD feature in p2. But this is only helps if your objects move VERY fast. What it does is choosing a time step so that the bodies move forward to the time of impact and not more. It's sequential and slow, and not energy preserving, so it's probably not a solution for you.\r\n\r\n```\r\n// CCD\r\nx += v * timeOfImpact\r\n```"}}