Cannot instantiate CameraControllerMain object

Dec 10, 2010 at 2:38 PM

Hi there,

We're attempting to use the PhysicsHelper library to develop a simple sidescrolling game for the WP7. We've got a simple Ellipse with a rectangular ground underneath it that it falls down onto. Everything seems quite trivial to figure out - except for the Camera.

We have attached a PhysicsCameraBehavior to the ellipse, and have been unsuccessful at trying to get an instance of a CameraControllerMain (attached to this?) - that we can then manipulate. We've tried grabbing it the same way the PhysicsControllerMain object is retrieved, and also tried the Initialize(Canvas,PhysicsControllerMain) method of the CameraControllerMain class. In both cases however, our instantiated objects remains null, and we're unable to get the camera to scroll dynamically.

Any help regarding this (especially sample code) would be greatly appreciated as we've searched everywhere and came up with no results.

Thanks in advance.

Dec 11, 2010 at 12:22 AM

Hi Arkuana,

 

Hope this helps. You can initialize the CameraControllerMain object in the physics controller Initialized event. The following code enables zoom and scrolling, it will follow the "Hero" object around the physics world. See below.

void _physicsController_Initialized(object source)
        {
            cameraControllerMain = new CameraControllerMain();
            cameraControllerMain.ZoomEnabled = true;
            cameraControllerMain.ZoomSpeed = 0; // Instant Zoom (low resource utilization)
            cameraControllerMain.ZoomPercentage = 30;
            cameraControllerMain.ScrollEnabled = true;
            cameraControllerMain.ScrollSpeed = 0; // Smooth Scrolling
            cameraControllerMain.Initialize(PhysicsControllerMain.ParentCanvas, physicsController);
            cameraControllerMain.Target = "Hero"; // Object to follow
        }
You can hook the Physics Controller Initialized event after getting a reference to the Physics Controller. See bellow. The following code retrieves a reference to the physicsController inside of a user control aattached to a canvas named "BoardRoot".
 
physicsController = ((Canvas)((UserControl)board).FindName("BoardRoot")).GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
Thanks,
Matt
Dec 11, 2010 at 2:50 PM

Hi Matt,

Thanks a lot for the quick reply! I was ecstatic to try it out, but got another NullReferenceException the Call Stack? I'm posting up the gist of my .xaml and .xaml.cs files, and hopefully you can tell me where I'm going wrong.

Here's the XAML code:

 

	<Canvas x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
        <Canvas.Clip>
            <RectangleGeometry Rect="0 0 480 800" />
        </Canvas.Clip>
        <Custom:Interaction.Behaviors>
            <pb:PhysicsControllerBehavior DebugMode="True" GravityVertical="10" MousePickEnabled="True"/>
        </Custom:Interaction.Behaviors>
        <Path Data="M-79,455.5 L-29.5,824.5 L528.5,824.5 L527,686.5 L531.5,602.5 z" Fill="#FFC2FD04" Height="373" Canvas.Left="-81" StrokeStartLineCap="Flat" Stretch="Fill" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Canvas.Top="453.5" UseLayoutRounding="False" Width="614.5">
            <Custom:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior IsStatic="True"/>
            </Custom:Interaction.Behaviors>
        </Path>
        <Ellipse x:Name="ball" Fill="#FFFD8104" Height="116" Canvas.Left="114" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Canvas.Top="64" Width="116">
        	<Custom:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior/>
                <pb:PhysicsCameraBehavior/>  
            </Custom:Interaction.Behaviors>
        </Ellipse>
        <Rectangle x:Name="wall" Fill="#FFC2FD04" Height="875" Canvas.Left="-80" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Canvas.Top="-51" Width="80">
            <Custom:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior IsStatic="True"/>
            </Custom:Interaction.Behaviors>
        </Rectangle>
        <Rectangle Fill="#FFC2FD04" Height="875" Canvas.Left="480" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Canvas.Top="-48" Width="80">
            <Custom:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior IsStatic="True"/>
            </Custom:Interaction.Behaviors>
        </Rectangle>
        <Rectangle Fill="#FFC2FD04" Height="150" Canvas.Left="-43" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Canvas.Top="-150" Width="603">
            <Custom:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior IsStatic="True"/>
            </Custom:Interaction.Behaviors>
        </Rectangle>

    </Canvas>

And here's the simple code-behind with your suggestions:

    public partial class MainPage : PhoneApplicationPage
    {
        PhysicsControllerMain pc;
        CameraControllerMain cc;

        public MainPage()
        {
            InitializeComponent();
            pc = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
            pc.Initialized += new PhysicsControllerMain.InitializedHandler(pc_Initialized);
        }

        void pc_Initialized(object source)
        {
            cc = new CameraControllerMain();

            cc.ZoomEnabled = true;
            cc.ZoomSpeed = 0;
            cc.ZoomPercentage = 30;

            cc.ScrollEnabled = true;
            cc.ScrollSpeed = 0;

            cc.Initialize(PhysicsControllerMain.ParentCanvas, pc);
            cc.Target = "ball";

        }
    }

I was initially under the impression that I was using the cameraControllerMain.Initalize() method incorrectly, but it seems like the problem lies elsewhere.

If you're unable to see what I'm doing wrong, I'd greatly appreciate it if you could show me a simple usage of a CameraControllerMain object that works for you.

Either way, I really appreciate the time you're putting in for this. You've been a great help :)

Dec 11, 2010 at 3:18 PM

By the way, the error I keep getting is as follows:

System.NullReferenceException was unhandled
  Message=NullReferenceException
  StackTrace:
       at Spritehand.FarseerHelper.CameraControllerMain.CompositionTarget_Rendering(Object sender, EventArgs e)
       at System.Windows.Media.CompositionTarget.OnRendering(Object sender, RenderingEventArgs e)
       at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)

Thanks

Jan 15, 2011 at 9:32 PM

   I've no idea if this will help but I had that exception when I had these two lines in this order, and it got fixed by switching them.

            cameraControllerMain.Target = "Hero"; // Object to follow
cameraControllerMain.Initialize(PhysicsControllerMain.ParentCanvas, physicsController);

Cheers,
Jan 28, 2011 at 6:25 PM

I found the problem to be a couple of lines that grabbed the incorrect position values for the target element. Have a look at my blogpost for details of how I did this.

Mar 10, 2011 at 4:26 PM

Hey guys,

 

I'm having a problem getting a reference to my camera.  

public PhysicsControllerMain _physicController;
        private CameraControllerMain _cameraControllerMain;
   

        public MainPage()
        {
            InitializeComponent();
            PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
         
            _physicsController.Initialized += new PhysicsControllerMain.InitializedHandler(_physicsController_Initialized);

          
        }

        void _physicsController_Initialized(object source)
        {

            _cameraControllerMain = new CameraControllerMain();
            

                _cameraControllerMain.ZoomEnabled = false;
                _cameraControllerMain.ZoomSpeed = 0;
                _cameraControllerMain.ZoomPercentage = 130;

                _cameraControllerMain.ScrollEnabled = true;
                _cameraControllerMain.ScrollSpeed = 0;

		_cameraControllerMain.Target = "body";

		_cameraControllerMain.Initialize(PhysicsControllerMain.ParentCanvas, _physicController);


        }

No matter what I do I keep getting the Null Reference Exception.

Any ideas?