A couple of problems: camera control,torque property and friction property

Jan 16, 2009 at 1:10 PM
I've ran into a couple of problems that I can't seem to find an answer to:

Firstly and hopefully most easily I can't get a camera controll to follow an object. It zooms in on the general area but doesn't track it across the page:
<Spritehand_FarseerHelper:CameraController x:Name="cameraController1" Canvas.Left="16" Canvas.Top="49" Target="CarBody" ZoomSpeed="0.010" ZoomPercentage="50"/>

Car body is inside a usercontrol named Car1 and appears as such:

<Canvas Height="150" Width="320" x:Name="CarBody" Canvas.Left="2" Canvas.Top="3">
      <Path Data="M0.5,0.5 L17.499998,0.5 L17.499998,85.5 L185.5,85.5 L185.5,0.5 L239.5,0.5 L239.5,72.059212 L319.5,72.059212 L319.5,149.5 L0.5,149.5 L0.5,78.921051 L0.5,72.059212 z" Fill="#FFEE0A0A" Stretch="Fill" Stroke="#FF000000"/>
      <Rectangle Height="59" HorizontalAlignment="Right" VerticalAlignment="Top" Width="38" Stroke="#FF000000" Canvas.Left="192" Canvas.Top="10">
       <Rectangle.Fill>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="1.632,0.763">
         <GradientStop Color="#FF54A292" Offset="0.549"/>
         <GradientStop Color="#FFFFFFFF" Offset="1"/>
         <GradientStop Color="#FFFCFAFA" Offset="0"/>
        </LinearGradientBrush>
       </Rectangle.Fill>
      </Rectangle>
      <Rectangle Height="8" Width="17" Fill="#FF786C6C" Stroke="#FF000000" Canvas.Top="135" Canvas.Left="-16"/>
      <Path Height="52.035" Width="48.05" Fill="#FF786C6C" Stretch="None" Stroke="#FF000000" Canvas.Top="103.573" Canvas.Left="-59.999" x:Name="path" RenderTransformOrigin="0.5,0.5">
       <Path.RenderTransform>
        <TransformGroup>
         <ScaleTransform/>
         <SkewTransform/>
         <RotateTransform/>
         <TranslateTransform X="-1.7489999532699585" Y="-0.79199999570846558"/>
        </TransformGroup>
       </Path.RenderTransform>
       <Path.Data>
        <PathGeometry>
         <PathFigure IsClosed="True" StartPoint="44.917537689209,30.1317577362061">
          <LineSegment Point="43.1624374389648,24.0291938781738"/>
          <LineSegment Point="38.7746810913086,17.9266300201416"/>
          <LineSegment Point="32.6318283081055,15.3112468719482"/>
          <LineSegment Point="28.2440719604492,18.7984256744385"/>
          <LineSegment Point="22.1012153625488,26.6445789337158"/>
          <LineSegment Point="18.5910110473633,30.1317577362061"/>
          <LineSegment Point="15.0808067321777,35.3625297546387"/>
          <LineSegment Point="5.42774486541748,34.4907341003418"/>
          <LineSegment Point="7.18284702301025,43.208683013916"/>
          <LineSegment Point="15.9583568572998,47.5676574707031"/>
          <LineSegment Point="23.8563175201416,48.439453125"/>
          <LineSegment Point="27.8311805725098,39.6051216125488"/>
          <LineSegment Point="31.4262428283691,25.2552757263184"/>
          <LineSegment Point="37.5690994262695,30.4860439300537"/>
          <LineSegment Point="43.1624374389648,41.4650917053223"/>
          <LineSegment Point="47.5501937866211,37.977912902832"/>
         </PathFigure>
        </PathGeometry>
       </Path.Data>
      </Path>
     </Canvas>

I have tried focusing the camera on the Car1 object and the rear wheel but the same thing always happens.

Secondly I'm trying to slow the car down when it isn't accelerating. I figured I could either increase the friction on an object it is landing on (without upping the default friction) or applying torque counter to what torque is currently on the wheels.

In the source code for the helper on the default friction property it says I can apply friction to a specific sprite but I can't find the property...so I resorted to trying to counter the current torque on the wheels, however the torque property seems to be returning 0 no matter how much torque is applied. Are there any solutions to these problems?

As ever, hope this makes sense and any help would be greatly appreciated.

Cheers

Craig.
Coordinator
Jan 17, 2009 at 1:34 AM
Your CameraController is inside your main Canvas, right? (and not the User Control)

Your app sounds similar to "Demo6" except for the nested User Control, so I can't think of what else might be up. If you post your entire Page.xaml and UserControl.xaml I could try and take a look
Jan 19, 2009 at 10:51 AM
Edited Jan 19, 2009 at 10:56 AM
Hi

Yeah it is very similar to demo 6 - I'm just having a play around at the moment to see what I can do and get some sort of demo together. The camera control is in the main canvas.


Here is page.xaml
<UserControl x:Class="Physics2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="2657.5" Height="496" xmlns:Spritehand_FarseerHelper="clr-namespace:Spritehand.FarseerHelper;assembly=Spritehand.FarseerHelper" xmlns:Physics2="clr-namespace:Physics2">
    <Canvas x:Name="LayoutRoot" Background="White">
        <Spritehand_FarseerHelper:PhysicsStaticHolder Canvas.Left="260" Canvas.Top="420" Body="Floor"/>
        <Spritehand_FarseerHelper:PhysicsController MousePickEnabled="True" Canvas.Left="102" Canvas.Top="58.5" x:Name="PhysicsController1"/>
        <Physics2:Car Height="210" Width="331.5" Canvas.Left="0.25" Canvas.Top="257" x:Name="Car1"/>
        <Path Height="167.5" Width="706" Canvas.Left="20.5" Canvas.Top="317.5" Fill="#FF160202" Stretch="Fill" Stroke="#FF000000" Data="M21,473.99997 L463.50003,459 L720.00006,318.00027 L726.00006,479.99997 L34.500008,484.49997 z" x:Name="Floor"/>
        <Rectangle Height="42" Width="1655" Canvas.Left="902.5" Canvas.Top="280.5" Fill="#FF160202" Stroke="#FF000000" x:Name="Platform"/>
        <Spritehand_FarseerHelper:PhysicsStaticHolder Height="12" Width="13.5" Canvas.Left="975" Canvas.Top="285" Body="Platform"/>
        <Spritehand_FarseerHelper:CameraController x:Name="cameraController1" Canvas.Left="16" Canvas.Top="49" Target="CarBody" ZoomSpeed="0.010" ZoomPercentage="50"/>
        <Path Height="54.334" Width="59" Canvas.Left="1533.5" Canvas.Top="226.833" Fill="#FF160202" Stretch="Fill" Stroke="#FF000000" Data="M1536,264 L1564.6666,211.33334 L1594,264.66666 z" RenderTransformOrigin="0.43,0.689" x:Name="seesaw_base"/>
        <Path Height="105.621" Width="486.185" Canvas.Left="1294.167" Canvas.Top="175.004" Fill="#FF160202" Stretch="Fill" Stroke="#FF000000" Data="M1562.6666,222.66667 L1779.8517,175.71204 L1744.0187,174.63005 L1561.3329,214.49936 L1301.6156,275.53262 L1294.6664,277.16568 L1340.4994,279.25037 z" x:Name="seesaw_top"/>
        <Spritehand_FarseerHelper:PhysicsStaticHolder Canvas.Left="1550" Canvas.Top="253.334" RenderTransformOrigin="0.533,0.433" Body="seesaw_base"/>
        <Spritehand_FarseerHelper:PhysicsJoint Canvas.Left="1552.667" Canvas.Top="210" BodyOne="seesaw_top" BodyTwo="seesaw_base"/>
    </Canvas>
</UserControl>



and Car.xaml

<UserControl
 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"
 mc:Ignorable="d"
 x:Class="Physics2.Car"
 Height="223" Width="332" xmlns:Spritehand_FarseerHelper="clr-namespace:Spritehand.FarseerHelper;assembly=Spritehand.FarseerHelper">
 <UserControl.Resources>
  <Storyboard x:Name="exhaust" RepeatBehavior="Forever">
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1.905"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.002"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1.383"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1.905"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1.708"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.001"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1.251"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1.708"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-0.348"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="13.632"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="3.696"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-0.348"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-0.363"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="25.096"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="16.929"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-0.363"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy1" Storyboard.TargetProperty="(UIElement.Opacity)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0.5"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0.6"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-6.719"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="-16.56"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="12.994"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-7.07"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="2.979"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="-3.405"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="7.867"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-1.319"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy" Storyboard.TargetProperty="(UIElement.Opacity)">
    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0.5"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0.5"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1.857"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1.047"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2_Copy" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1.5"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="0.958"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="-8.661"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="-15.315"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="-28.94"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="-9.99"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="-3.337"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="2.538"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(UIElement.Opacity)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0.5"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="0.8"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1.443"/>
   </DoubleAnimationUsingKeyFrames>
   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
    <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1.293"/>
   </DoubleAnimationUsingKeyFrames>
  </Storyboard>
 </UserControl.Resources>
    <Canvas x:Name="LayoutRoot" Width="355" Height="234">
        <Rectangle Height="66" x:Name="box1" Width="73" Canvas.Left="27" Canvas.Top="6" Fill="#FF840A82" Stroke="#FF000000"/>
        <Rectangle Height="57" x:Name="box2" Width="58" Canvas.Left="121" Canvas.Top="19" Fill="#FF840A82" Stroke="#FF000000"/>
        <Rectangle Height="39" x:Name="box3" Width="66" Canvas.Left="82" Canvas.Top="-36" Fill="#FF840A82" Stroke="#FF000000"/>
     <Canvas Height="150" Width="320" x:Name="CarBody" Canvas.Left="2" Canvas.Top="3">
      <Path Data="M0.5,0.5 L17.499998,0.5 L17.499998,85.5 L185.5,85.5 L185.5,0.5 L239.5,0.5 L239.5,72.059212 L319.5,72.059212 L319.5,149.5 L0.5,149.5 L0.5,78.921051 L0.5,72.059212 z" Fill="#FFEE0A0A" Stretch="Fill" Stroke="#FF000000"/>
      <Rectangle Height="59" HorizontalAlignment="Right" VerticalAlignment="Top" Width="38" Stroke="#FF000000" Canvas.Left="192" Canvas.Top="10">
       <Rectangle.Fill>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="1.632,0.763">
         <GradientStop Color="#FF54A292" Offset="0.549"/>
         <GradientStop Color="#FFFFFFFF" Offset="1"/>
         <GradientStop Color="#FFFCFAFA" Offset="0"/>
        </LinearGradientBrush>
       </Rectangle.Fill>
      </Rectangle>
      <Ellipse Height="8" x:Name="ellipse2_Copy1" Width="7" Opacity="0.5" RenderTransformOrigin="0.524,0.583" Fill="#FF786C6C" Stroke="#FF000000" Canvas.Top="111.333" Canvas.Left="-33.333">
       <Ellipse.RenderTransform>
        <TransformGroup>
         <ScaleTransform/>
         <SkewTransform/>
         <RotateTransform/>
         <TranslateTransform/>
        </TransformGroup>
       </Ellipse.RenderTransform>
      </Ellipse>
      <Ellipse Height="8" x:Name="ellipse2_Copy" Width="7" Opacity="0.5" RenderTransformOrigin="0.929,1.125" Fill="#FF786C6C" Stroke="#FF000000" Canvas.Top="123.343" Canvas.Left="-26.998">
       <Ellipse.RenderTransform>
        <TransformGroup>
         <ScaleTransform/>
         <SkewTransform/>
         <RotateTransform/>
         <TranslateTransform/>
        </TransformGroup>
       </Ellipse.RenderTransform>
      </Ellipse>
      <Ellipse Height="7.667" Width="7.333" Fill="#FF786C6C" Stroke="#FF000000" Canvas.Top="135" Canvas.Left="-18.667" Opacity="0.5" x:Name="ellipse2" RenderTransformOrigin="0.5,0.5">
       <Ellipse.RenderTransform>
        <TransformGroup>
         <ScaleTransform/>
         <SkewTransform/>
         <RotateTransform/>
         <TranslateTransform/>
        </TransformGroup>
       </Ellipse.RenderTransform>
      </Ellipse>
      <Rectangle Height="8" Width="17" Canvas.Left="-16" Canvas.Top="135" Fill="#FF786C6C" Stroke="#FF000000"/>
     </Canvas>
        <Ellipse HorizontalAlignment="Left" Width="104" Fill="#FF7E7373" Stroke="#FF000000" StrokeThickness="15" x:Name="RearWheel" Height="104" VerticalAlignment="Bottom" Canvas.Top="104" Canvas.Left="4"/>
        <Ellipse Fill="#FF7E7373" Stroke="#FF000000" StrokeThickness="15" x:Name="FrontWheel" Height="104" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="104" Canvas.Top="104" Canvas.Left="212"/>
        <Spritehand_FarseerHelper:PhysicsJoint HorizontalAlignment="Left" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.4" BodyOne="RearWheel" BodyTwo="CarBody" CollisionGroup="1" d:LayoutOverrides="Height" x:Name="joinRear" Canvas.Top="145" Canvas.Left="45"/>
        <Spritehand_FarseerHelper:PhysicsJoint RenderTransformOrigin="0.5,0.4" HorizontalAlignment="Right" VerticalAlignment="Bottom" BodyOne="FrontWheel" BodyTwo="CarBody" CollisionGroup="1" d:LayoutOverrides="Width, Height" x:Name="JoinFront" Canvas.Top="146" Canvas.Left="254"/>
    </Canvas>
</UserControl>

Cheers


Craig
Apr 10, 2009 at 6:14 PM
Edited Apr 10, 2009 at 6:15 PM

I believe I have the solutions.

I was having the same problem with the camera, where it would zoom in, but not pan with the object.

After a little debugging, I found that my _parentCanvas.Width and Height were 'NaN' at 

 

double currentScreenCenterX = (_parentCanvas.Width / 2) / _scaleTransform.ScaleX;
double currentScreenCenterY = (_parentCanvas.Height / 2) / _scaleTransform.ScaleY;

in the CompositionTarget_Rendering Method in the CameraController class.

Lo and behold there was no height or width on my main Canvas (as it seems is the case for you). Added them and works fine now.

Also Andy, thanks for presenting in Albany the other day. Great stuff.

Zac