This project is read-only.

Physics Mass and Damping

Feb 4, 2013 at 5:41 PM
First off, what an amazing engine. The architecture is off the charts.

I was having some issues with RigidBody (Mass or Damping or a combination of the two). Using this xaml:

<Group Name="TankBody" Transform.Position="50, 100, 50">
    <Model Source="{ContentReference ../Models/Tank}">
    </Model>
    <RigidBody Mass="1" Damping="1">
        <ModelCollider Source="{ContentReference ../Models/Tank}" />
    </RigidBody>
</Group>
I get error:

Error 1 Building content threw TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Xaml.Schema.SafeReflectionInvoker.InvokeMethodCritical(MethodInfo method, Object instance, Object[] args)
at System.Xaml.Schema.SafeReflectionInvoker.InvokeMethod(MethodInfo method, Object instance, Object[] args)
at System.Xaml.Schema.XamlMemberInvoker.SetValueSafeCritical(Object instance, Object value)
at System.Xaml.Schema.XamlMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
at System.Xaml.XamlObjectWriter.SetValue(Object inst, XamlMember property, Object value)
at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndMember()
at Nine.Content.Pipeline.Xaml.ObjectWriter.WriteEndMember()
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Xaml.XamlServices.Transform(XamlReader xamlReader, XamlWriter xamlWriter, Boolean closeWriter)
at Nine.Content.Pipeline.Xaml.XamlSerializer.Load(Stream stream)
at Nine.Content.Pipeline.Xaml.XamlSerializer.Load(String filename)
at Nine.Content.Pipeline.Importers.XamlImporter.Import(String filename, ContentImporterContext context)
at Microsoft.Xna.Framework.Content.Pipeline.ContentImporter1.Microsoft.Xna.Framework.Content.Pipeline.IContentImporter.Import(String filename, ContentImporterContext context)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAssetDirectly(BuildItem item, String importerName)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAsset(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAssetWorker(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAsset(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.RunTheBuild()
at Microsoft.Xna.Framework.Content.Pipeline.Tasks.BuildContent.RemoteProxy.RunTheBuild(BuildCoordinatorSettings settings, TimestampCache timestampCache, ITaskItem[] sourceAssets, String[]& outputContent, String[]& rebuiltContent, String[]& intermediates, Dictionary
2& dependencyTimestamps, KeyValuePair`2[]& warnings)
Inner exception: NullReferenceException: Object reference not set to an instance of an object.
   at Nine.Physics.RigidBody.set_Mass(Single value) C:\Projects\Samples\Tutorial\TutorialContent\Tutorials\06. Terrain.xaml Tutorial
Jan 6, 2014 at 4:27 AM
I noticed I'm answering this message very late since its posted, but in an attempt to others that have run into this problem (like I have myself), I noticed that if you define either of these values in the XAML code, you'll have the problem. But if you use it in your controller class that you create to manipulate objects, it doesn't fail out and makes an impact.

To help understand (because it took me more than a couple days to figure this out), I made this PhsyicsController component that is identical to the one in the Engine Nine Samples.
namespace Controller
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using Nine;
    using Nine.Physics;

    public class PhysicsController : Component
    {
        public float Speed { get; set; }
        public Vector3 ResetPosition { get; set; }
        public Keys ResetKey { get; set; }

        protected override void Update(TimeSpan elapsedTime)
        {
            var Body = Parent.Find<RigidBody>();
            var keyboardState = Keyboard.GetState();

// Heres where I added either Mass or Dampening.

            Body.Mass=50;
 
            if (keyboardState.IsKeyDown(Keys.Left))
                Body.ApplyImpulse(Speed * Vector3.Left);
                
            if (keyboardState.IsKeyDown(Keys.Right))
                Body.ApplyImpulse(Speed * Vector3.Right);
            
            if (keyboardState.IsKeyDown(Keys.Down))
                Body.ApplyImpulse(Speed * Vector3.Backward);

            if (keyboardState.IsKeyDown(Keys.Up))
                Body.ApplyImpulse(Speed * Vector3.Forward);

            if (keyboardState.IsKeyDown(ResetKey))
            {
                var body = Parent.Find<RigidBody>();
                body.Position = ResetPosition;
                body.Velocity = Vector3.Zero;
                body.AngularVelocity = Vector3.Zero;
            }
        }
    }
}
Then I had to add a header to the top of scene.xaml because I made controller in a separate project, and this involved referencing that project. After make sure I had made included bunches of references to the project. To help:
<Scene xmlns="http://schemas.microsoft.com/nine/2011/xaml" xmlns:g="clr-namespace:Controller;assembly=Controller">
Then When I placed a model in the scene, I used this code, hope it makes it all come together.
    <Group Transform.Position="1,30,-29">
        <Model Source="{ContentReference Models/BaseFighter}" />
        <RigidBody>
            <BoxCollider  Size="2.75,2.75,2.75" />
        </RigidBody>  

        <g:PhysicsController Speed="2" ResetPosition="1,30,-29" ResetKey="Q" />,
    </Group
So I can't claim to fully understand what's going on. In attempt to try to explain it, I made a class to control a model, then I made my XAML be sure to reference that class, then I declared the values I could in XAML file, then declared the value I could in the Controller Class.

Good Luck Everyone.