This project is read-only.

Adding controls programmatically

Nov 3, 2013 at 7:41 PM
I may be doing this all wrong, so forgive me, but I'm trying to use this library to make a labyrinth. I found a maze engine adaptation here. This is also on WP8, as I managed to get PHZ to work. I'm adding lines programmatically using the maze engine. There's a draw line method that uses something that only works on WP7. Instead I'm doing using the UCBLock control and doing it the way the advanced demo does. The maze engine just dictates how to position the block, (which ends up being thin like a line).
  double thickness = 4.0;

            double width = x2 - x1 + thickness;
            if (width == 0.0) width = thickness;

            double height = y2 - y1 + thickness;
            if (height == 0.0) height = thickness;
                    
            ucBlock block = new ucBlock();
            block.Width = width;
            block.Height = height;
            block.blockInnerRectangle.Height = height;
            block.blockInnerRectangle.Width = width;
           
            cnvGame.AddPhysicsUserControl(block, x1, y1, 0);
This works very well and renders a nice maze consisting of physics user controls that are static. Next, I add a ball by instantiating a user control ball. Here's the ball markup
<UserControl x:Class="Demo.Simple.Ball"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ph="clr-namespace:Spritehand.FarseerHelper;assembly=Spritehand.FarseerHelper"
    mc:Ignorable="d"
    d:DesignHeight="60" d:DesignWidth="60">
    <Canvas x:Name="LayoutRoot" Width="55" Height="55">
        <ph:PhysicsSprite x:Name="ball"  RestitutionCoefficient=".4" CollisionGroup="1" 
              Width="55" Height="55" ShapeType="Ellipse">
            <Ellipse  Width="55" Height="55" Fill="LightYellow"/>
        </ph:PhysicsSprite>
    </Canvas>
</UserControl>
Again, the maze engine dictates where the ball will go
    Ball ball = new Ball();
            ball.Name = "ball";
            cnvGame.AddPhysicsUserControl(ball, ((cols / 2) + 0.5) * cellWidth, 0, 1);
I plugged in the Accelerometer and update the world when it changes. So far so good, everything works, with a couple exceptions. Then the game first starts, and the ball drops, sometimes it hits the "lines" and sticks. It doesn't bounce off or roll. It seems if I keep turning the accelerometer and never allow the ball to stop being in motion, it doesn't stick. When it sticks, the initial contact fires the collision event, but after that there are none. The ball has a collision group of 1 and the blocks 0. I know this is pretty far out there but any help would be appreciated. Thanks!
Nov 8, 2013 at 2:52 PM
Can you try putting DebugMode="true" on the PhysicsCanvas?

That will show you the boundaries that were determined by the control.
Nov 8, 2013 at 7:11 PM
Edited Nov 8, 2013 at 7:17 PM
Ahh! That helps a lot thank you. So its after the ball hits a corner and bounces between the two for a while that it sticks. In debug mode, the blocks turn red when the ball sticks. Maybe I can detect this and manipulate the collisions?



Here's a screenshot:

http://sdrv.ms/1etLdXo


Edit: After looking at it again while not in debug mode, the "blocks" appear as lines, so it looks like its getting stuck in the middle of a line, when actually its hit the underlying "corner" of two blocks. I guess I need to figure out how to make the underlying objects thin like the line that actually shows. But then the ball would be stuck in the new corner?
Nov 8, 2013 at 7:29 PM
When DebugMode is on, the colors mean:

Green: the object is active and is involved in the simulation.
Red: the object is "resting" and is NOT involved in the simulation. Farseer supports resting bodies, which greatly improve performance by ignoring objects in the simulation that become resting.

So, from your image there, it looks like the ball has become resting and you just need to wake it up. Are you adding Torque or Force to the ball at any time? That should wake it up, but if you need to explicitly wake it up you can set sprite.BodyObject.Awake = true on the ball.
Nov 8, 2013 at 8:48 PM
Edited Nov 8, 2013 at 8:49 PM
Thank you very much again for your reply. I haven't added torque or force to the ball. I will try that as well as sprite.BodyObject.Awake = true. Do you have an idea on how to make the maze which is constructed oc block controls which I've set the height and width, to have the underlying objects not be much bigger than what is rendered? In non debug view they are thin lines, in debug view, they are bigger blocks like the picture. I guess its my lack of knowing how to make a user control "line" that is like the block user control, except thin.
Nov 9, 2013 at 2:53 PM
What you are seeing in debug mode is just the stroke and fill that are applied when DebugMode is turned on. So the size of the blocks should be the size of your block control (same height and width) - its just showing a bit thicker stroke with the debug objects shown.
Nov 12, 2013 at 7:43 PM
Thank you again for your reply. I ended up getting to work by setting the width and height for all the user control elements I could think of.
    ucBlock block = new ucBlock();
            block.cnvBlock.Width = width;
            block.cnvBlock.Height = height;
            block.Width = width;
            block.Height = height;
            block.blockInnerRectangle.Height = height;
            block.blockInnerRectangle.Width = width;
            block.LayoutRoot.Width = width;
            block.LayoutRoot.Height = height;
the blocks are truly thin now, and the ball stops in corners like it should, not before them