Search code examples
c#wpfphysicsgame-physics

Help with physics simulation in WPF


I'm studying simple physics for 2d games and I would like a help with a simulation I'm trying to do.

I have a bar that will fall over a fixed ball in the ground, xaml below:

<Canvas Background="CornflowerBlue">
    <Rectangle Name="bar" Width="200" Height="20" Fill="Cornsilk" Canvas.Left="250" Canvas.Top="50" />
    <Ellipse Name="ball" Height="50" Width="50" Fill="PaleGreen" Canvas.Left="380" Canvas.Top="250" />
</Canvas>

Before bar starts falling

I have all the calculations done to simulate the bar falling due to gravity but what I really need help with is, once the bar hits the ball, it's gonna have one side in the ground and the other side over the ball, which could be pretty much represented with the xaml:

<Canvas Background="CornflowerBlue">
    <Rectangle Name="bar" Width="200" Height="20" Fill="Cornsilk" Canvas.Left="250" Canvas.Top="246.675" RenderTransformOrigin="0.5,0.5" >
        <Rectangle.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="-18.897"/>
                <TranslateTransform/>
            </TransformGroup>
        </Rectangle.RenderTransform>
    </Rectangle>
    <Ellipse Name="ball" Height="50" Width="50" Fill="PaleGreen" Canvas.Left="380" Canvas.Top="250" />
</Canvas>

Bar is on the ground

And I don't know how to make the left side of the bar hit the ground while the left side will be over the ball in phisics terms.

I know there are awesome physics engine available like Farseen but I just want to understand how it would work in this case.

I know how to get the bar's Y based on time, so I keep checking it until it colides with the ball, but then what after that?

Thanks!


Solution

  • Rigid Body physical simulation is not a simple task Unless you are very interested in the topic I would recommend using a physics library. But if you are interested in doing this I would recommend looking at the Physically Based Modeling SIGGRAPH course notes, Brian Mirtich's thesis on impulse based dynamics and perhaps also Kenny Erleben's thesis on multibody dynamics.

    These provide a very in depth description of how rigid body dynamics can be implemented.