Aug 5, 2011 at 12:46 PM

Hi Nich

This may be a basic Farseer problem rather than XAML Physics...

If I drag a Physics item, and my touch moves over a static PolygonBody, I can actually make the object move through it. I can not do that on a RectangleBody. Could this be because of the many geometries used for collisions?

Below is using TouchDrag with FixedMouseJoint, as the RevoluteJoint was even more reluctant to accept static bodies. The high values in DampingRatio, Frequency and MaxForce achieves more or less the same, why I increased the mass for the static items.

 <Physics:TouchDrag Width="1024" Height="768" DampingRatio="100" Frequency="5000" MaxForce="5000">
<Path Data="M0,0 L164,0 164,10 161.38177,10 C80.867546,16.604429 16.604429,80.867546 10,161.38177 L10,164 0,164 z" Fill="Red" Height="164" Canvas.Left="0" Stretch="Fill" Canvas.Top="0" Width="164" >
                        <Physics:PolygonBody IgnoreGravity="True" IsStatic="True" Mass="10000">
                                <Physics:PolygonGeometry Points="0,0, 164,0, 164,10 148,12 0,12" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,12, 148,12, 133,15 115,21 0,21" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,21, 115,21, 101,27 90,33 0,33" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,33, 90,33, 78,41 69,48 0,48" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,48, 69,48, 58,58 48,69 0,69" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,69, 48,69, 41,78 33,90 0,90" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,90, 33,90, 27,101 21,115 0,115" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,115, 21,115, 15,133 12,148 0,148" RestitutionCoefficient="0.7" />
                                <Physics:PolygonGeometry Points="0,148, 12,148, 10,164 0,164" RestitutionCoefficient="0.7" />
 <Rectangle Canvas.Top="502" Canvas.Left="0" Width="1024" Height="51" Fill="Red">  
                        <Physics:RectangleBody IsStatic="True" IgnoreGravity="True" Mass="10000">
                                <Physics:RectangleGeometry RestitutionCoefficient="0.7"/>
<Ellipse Canvas.Left="512" Canvas.Top="219" Width="80" Height="80">
                            <Physics:CircleBody IgnoreGravity="True" >
                                    <Physics:CircleGeometry RestitutionCoefficient="0.7"/>

Aug 6, 2011 at 9:52 AM

This is a common problem with physics engines. I wasn't able to get the ellipse to pass though the path. Although most of those polygons are concave so that could be related. Here is what I changed the polygons to to make them all convex.

    <Physics:PolygonGeometry Points="0,0, 164,0, 164,10 148,12 0,12" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,12, 148,12, 133,15 0,15" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,15, 133,15 115,21 0,21" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,21, 115,21, 101,27 0,27" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,27, 101,27 90,33 0,33" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,33, 90,33, 78,41 0,41" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,41, 78,41 69,48 0,48" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,48, 69,48, 58,58 0,58" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,58, 58,58 48,69 0,69" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,69 48,69, 41,78 0,78" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,78 41,78 33,90 0,90" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,90, 33,90, 27,101 0,101" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,101, 27,101 21,115 0,115" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,115, 21,115, 15,133 0,133" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,133, 15,133 12,148 0,148" RestitutionCoefficient="0.7" />
    <Physics:PolygonGeometry Points="0,148, 12,148, 10,164 0,164" RestitutionCoefficient="0.7" />

But I didn't have any tunneling problems before making that change either. If you compile and run the Farseer Physics library as "Debug" then it will point these problem out to you. I assume you are using "Release" and are not seeing them.

There is a feature in the physics engine for this kind of situation called continuous collision. When I was developing Xaml Physics it was causing more harm then good for what I was doing so I turned it off. It also uses up some cpu so it's best to turn it off if you don't need it. Anyway it's still off. I added it as a property to PhysicalBox and defaulted it to on because that's what the Farseer Physics does.

I don't think the issue is having too many polygons.

Aug 8, 2011 at 2:36 PM

Wow, weird. I can make them tunnel so easy - mostly on polygon objects, but if I'm 'fast' enough (high velocity) actually on all types.I did try the new physical box with continous collision on, and although the experience is better, it does have a few other problems.

I uploaded 3 videos of my trouble here - I'm always building something 'real' when testing out new things, to keep it non-abstract: (with continuos collisions)

As you can see in ah1, I can easily move the pad to intersect with the corner object. At around 00:10 you can see that doesnt really happen with the rectangle object, even though the touchpoint is below the screen. And around 00:33 you can see me knock the ellipse right into the polygon object where it stays, if not passing straight through it.

In ah2 I changed the polygons on the top left corner as you did above, and no effect - the pad goes straight in it. At 00.15 you can also see the circlebodies intersecting while hitting the ellipse. At 00.19 you can see the ellipse penetrate the downright corner, although bouncing back. that can also be seen with the rectangle body at 00.23.

In ah3 I used your new physical box with continuous collisions. Notice the effect at around 00.05. If the touchpoint is moved out on the corner, it actually offsets the ellipse from the polygonbody with more or less the same distance. When I move the touchpoint back (00.06) the pad also moves back towards the polygonobject. At 00.11 you can see that is not the case with the rectangle body. I do the same at 00.16. At 00.30 you can see me move the pad straight through the ellipse. At 00.34 you can see the ellipse intersect with the pad.

Overall it seems to me there is some issues with the polygon body. The rest may be some misunderstanding on my side about mass/velocity. I set the sides mass to 10.000 and the ellipse mass to 0.1, and can still hit it so it tunnels through the sides. Maybe the velocity calculated when moving the touchpoint miss something - like if the touchpoint is'nt calculated as fast as a mouse, or with a lower resolution - and just use start and endpoint. But you said you did'nt experience this...

Aug 10, 2011 at 3:44 AM

I don't know what else to tell you. There must be something different between your code and my code. If you send me your project I should be able to reproduce it and maybe figure it out. Or you can try asking in the Farseer Physics forums.

Aug 10, 2011 at 8:18 AM

I think I've found out! I had the gameClock interval set at 0:0:0.040, to get a smoother debug-view. I changed that to 0:0:0.020, and now, at least I can't knock the ellipse into objects - so far. I guess when hitting objects at high velocity, the calculations indicates positions at 0:0:0.000 and 0:0:0.040, where it would be out of bounds, and all inbetween disregarded. That's also explain intersections. So lowering the interval also heightens the velocity resolution, so to speak. If I'm quick I can still move a pad straight through the ellipse, but I think (hope) this is a simulator-illusion. I can snap-move faster over larger distances with a mouse than with a touch. Would be nice with a validation-technique, when it renders the last position though - if objects intersect trace last movement and place object on movement path when no intersections is registered - may be costful in performance.

But I still have the problem with the polygon objects offsetting the colliding object, when the touch-point moves into it. Therefore I have uploaded my project here - I hope you will take a look at it:

It happens every time I move an object down to the corners, and then continue to move the touchpoint out on the very corner itself, leaving the ellipse bouncing outside. Then it offsets the ellipse more or less the same distance the touchpoint is from the edge.

Aug 10, 2011 at 12:27 PM

I haven't looked at this much yet but my first thought is an interval of 0:0:0.040 is very high. 0.020 is high too. The physics engine can do a much better job when the frames are shorter. When I converted my game Snow Spill to run on Windows Phone 7 I used 0.005. Obviously that's on much more limited hardware and the physics is a bit more complicated. There is another property called UISteps that you can use to help prevent a low interval from using too much cpu. Essentially the clock raises 2 events. One just meant for physics calculations and another to update the UI. By default UISteps is 1 so that means every physics step come with a UI step. If UISteps is 2 then only every other physics step will have a UI step and so on. In Snow Spill I have UISteps set to 5. I change the interval in your project to 0.005 and that seams to have fixed the strange offsetting.

I would still like to know what causes it so I'll look at it some more later. This might be something for the Farseer Physics discussions.

Still I can't get anything to tunnel through the static ploygons, not even with an interval of 0.040. I can get the ellipses to tunnel each other at that interval sometimes, but that's hardly surprising,

Aug 11, 2011 at 10:36 AM

I think what is happening is the mouse joint is pulling the ellipse so hard that it is bouncing back all in one frame. I changed the RestitutionCoefficient of everything to 0 and it stopped. It also stops if the ellipse is already sitting next to the polygon when you grab it.

Looking at it closer I think the Frequency of TouchDrag is to blame. 5000 is very high. The default is 5. The DampingRatio is very high too (it shouldn't even be above 1) but I think that's just because it needs to be with the Frequency so high. I changed the values back to their defaults and the problem goes away. With a Frequency of 10 and DampingRatio of 0.7 the problem is still gone and the handling is very similar. I didn't change MaxForce.

I was finally able to reproduce the tunneling problems by turning ContinuousPhysics off and using an interval of 0.04. But having either a low enough interval or leaving ContinuousPhysics on prevents this problem.