Adding a new object to a fluid container programmatically

Jul 28, 2010 at 1:46 PM


I keep playing with these behaviors and now I ran into another trouble. Whenever I try to add a new object it fall apart from the fluid container influence... I tried to change de zindex and to insert the object in the 0 index canvas.children position but still not working properly.

Any ideas about how to acomplish this?


Jul 29, 2010 at 12:11 PM


I'll try to explain the problem because maybe I did not explain myself well enough.

I'm doing a simple application that inserts ellipses into a water pool whenever you press a button. Two ellipses are already created when the application starts and the others are created programmatically.

I've observed that you need to put the objects in a correct order while working with blend in order that the fluidcontainer can get what objects will be "inside of it". So, this way, you put object1,object2 and then water. If you change this order and put object1,water & object2, then object2 will not be affected by fluidcontainer.

My problem here is that whenever I create a programmatic ball it seems to get out of the fluidwater influence. No matter which zindex I put or if I use the Layroot.Children.Insert(0,ball) instead of Children.Add...

Besides that, and trying to find out a solution, I ran into another problems but i will create a new post for each of them for the sake of readibility.

Here is the maincontrol:

	<Canvas x:Name="screen" Margin="0" Background="#FFAFA6A6">
			<phb:PhysicsControllerBehavior MousePickEnabled="True"/>
		<local:ball x:Name="ball1" Height="100" Canvas.Left="308" Canvas.Top="118" Width="100"/>
		<local:ball x:Name="ball2" Height="100" Canvas.Left="583" Canvas.Top="160" Width="100"/>
		<Rectangle x:Name="water" Fill="#EA0E6E7A" Height="267" Stroke="Black" Canvas.Top="146" Width="640">
				<phb:PhysicsFluidContainerBehavior WaveGeneratorMin="6" RotationalDragCoefficient="0.5"/>
		<Path x:Name="ground" Data="M0.5,0.5 L0.52364284,0.5 L0.60435027,7.3271866 C4.9783115,192.12895 146.30214,340.5 320,340.5 C493.69788,340.5 635.02167,192.12895 639.39563,7.3271866 L639.47638,0.5 L639.5,0.5 L639.5,412.5 L0.5,412.5 z" Fill="#FF7A440E" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" Height="413" Canvas.Top="67" Width="640">
				<phb:PhysicsObjectBehavior IsStatic="True"/>
		<Button x:Name="btAdd" Content="Add ball" Height="33" Canvas.Left="480" Canvas.Top="8" Width="99"/>

and here you are the code in the btAdd handler:

        'reference to main controller
        Dim p = CType(screen.GetValue(PhysicsControllerMain.PhysicsControllerProperty), PhysicsControllerMain)
         'adding ball
        Dim b As New ball
        screen.Children.Insert(0, b)
        b.SetValue(Canvas.LeftProperty, Convert.ToDouble(New Random().Next(0, 500)))
        b.SetValue(Canvas.TopProperty, CDbl(0))
        Dim o = p.AddPhysicsBodyForCanvasWithBehaviors(b.LayoutRoot)
        'o(0).SetValue(Canvas.ZIndexProperty, 20)

As you can see I've tried to insert the control in the 0 position unsuccessfully... and I also tried to change the Zindex of the sprite but it didn' work too.

Some help? I just want that new balls got into Water control influence.