I don\'t know how to fix my coding problem. I know its something simple but I ca
ID: 3757291 • Letter: I
Question
I don't know how to fix my coding problem. I know its something simple but I can't really seem to get a hang of understanding it. The problem is in the UpdatWorld() function at the bottom with the line bolded and the problem in a comment.
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPI311.GameEngine
{
public class Transform
{
private Vector3 localPosition;
private Quaternion localRotation;
private Vector3 localScale;
private Matrix world;
private Transform parent;
private List<Transform> Children { get; }
//public Vector3 LocalPosition;
//public Vector3 LocalScale;
//public Quaternion LocalRotation;
//public List<Transform> Children { get { return children; } }
public Transform Parent
{
get { return parent; }
set
{
if(parent != null) parent.Children.Remove(this);
parent = value;
if(parent!=null) parent.Children.Add(this);
UpdateWorld();
}
}
public Transform()
{
localPosition = Vector3.Zero;
localRotation = Quaternion.Identity;
localScale = Vector3.One;
UpdateWorld();
//***Section C
parent = null;
Children = new List<Transform>();
}
public Vector3 LocalPosition
{
get { return localPosition; }
set { localPosition = value; UpdateWorld(); }
}
public Quaternion LocalRotation
{
get { return localRotation; }
set { localRotation = value; UpdateWorld(); }
}
public Vector3 LocalScale
{
get { return localScale; }
set { localScale = value; UpdateWorld(); }
}
public Vector3 Forward
{
get { return world.Forward; }
}
public Vector3 Backward
{
get { return world.Backward; }
}
public Vector3 Up
{
get { return world.Up; }
}
public Vector3 Down
{
get { return world.Down; }
}
public Vector3 Right
{
get { return world.Right; }
}
public Vector3 Left
{
get { return world.Left; }
}
public void Rotate(Vector3 axis, float angle)
{
LocalRotation *= Quaternion.CreateFromAxisAngle(axis, angle);
}
public Vector3 Position
{
get { return world.Translation; }
set
{
if (parent == null)
LocalPosition = value;
else
{
Matrix total = World;
total.Translation = value;
LocalPosition = (Matrix.Invert(Matrix.CreateScale(LocalScale) *
Matrix.CreateFromQuaternion(LocalRotation) *
total * Matrix.Invert(Parent.World)).Translation);
}
}
}
private void UpdateWorld()
{
world = Matrix.CreateScale(localScale) *
Matrix.CreateFromQuaternion(localRotation) *
Matrix.CreateTranslation(localPosition);
if (parent != null) world *= parent.World;
foreach (Transform child in Children) //'Object reference not set to an instance of an object.'
{
child.UpdateWorld();
}
}
public Matrix World
{
get { return world; }
}
}
}
Explanation / Answer
This is because in constructor public Transform() you are calling the method updateWorld() in which Children is getting used and you haven't initialized Children before its use.
public Transform()
{
localPosition = Vector3.Zero;
localRotation = Quaternion.Identity;
localScale = Vector3.One;
UpdateWorld(); //you have to initialize it first
//***Section C
parent = null;
Children = new List<Transform>();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.