Code Snippet
#region Pendulum With Camera
public class PendulumWithCam : VisualEntity
{
private CameraEntity _camera = null;
private PhysicsJoint _joint = null;
private SingleShapeEntity _support;
private SingleShapeEntity _pend;
/*
* This is the Cam attached to the swinging part
*/
[DataMember]
public CameraEntity PendulumsCamera
{
get { return _camera; }
set { _camera = value; }
}
/*
* Joint connecting the static and moving part
*/
[DataMember]
public Joint Joint
{
get { return _joint; }
set { _joint = (PhysicsJoint)value; }
}
/*
* The static part of the pendulum
*/
[DataMember]
public SingleShapeEntity Support
{
get { return _support; }
set { _support = value; }
}
/*
* The swinging Part
*/
[DataMember]
public SingleShapeEntity Pendulum
{
get { return _pend; }
set { _pend = value; }
}
private const float radius = 0.05f;
private const float length = 0.50f;
private const float mass = 10.0f;
/*
* some basic Constructors
*/
public PendulumWithCam()
{
}
public PendulumWithCam(Vector3 initialPosition)
{
base.State.Pose.Position = initialPosition;
}
/*
* Create the joint and set propertys
*/
private void CreateJoint()
{
JointAngularProperties commonAngular = new JointAngularProperties();
//make twisting possible
commonAngular.TwistMode = JointDOFMode.Free;
_joint = PhysicsJoint.Create(new JointProperties(commonAngular, null, null));
_joint.State.Name = "PendulumJoint";
}
/*
* Create the static Part of the pendulum
* As a simple single Shape entity
*/
private void CreateSupport(Vector3 pos)
{
BoxShapeProperties prop = new BoxShapeProperties(
"supportbox",
mass,
new Pose(new Vector3(0, 0, 0)), //pose of shape in entitys coordframe
new Vector3(radius, radius, length)); //dimensions
Shape shape = new BoxShape(prop);
_support = new SingleShapeEntity(shape,
base.State.Pose.Position); //pose of support-Entity in world coordframe
_support.State.Name = "support";
Console.WriteLine(base.State.Pose.Position);
}
/*
* Create the swinging part
* also as single shape entity
*/
private void CreatePendulum(Vector3 pos)
{
BoxShapeProperties prop = new BoxShapeProperties(
"pendulumbox",
mass,
new Pose(new Vector3(0, 0, 0)), //pose of shape in entitys coordframe
new Vector3(radius, length, radius));
Shape shape = new BoxShape(prop);
_pend = new SingleShapeEntity(shape,
new Vector3(base.State.Pose.Position.X, //same as support
(base.State.Pose.Position.Y - 0.5f * length + 0.5f * radius),
base.State.Pose.Position.Z - 0.5f * length - 0.5f * radius));
//base.State.Pose.Position); //pose of support-Entity in world coordframe
_pend.State.Name = "pendulum";
}
public void CreateCamera(String camID, Vector3 position)
{
// low resolution, wide Field of View
_camera = new CameraEntity(320, 240);
//Set the name of the camera -> for the service
_camera.State.Name = camID;
//set position
_camera.State.Pose.Position = position;
_camera.IsRealTimeCamera = true;
}
public override void Initialize(xnagrfx.GraphicsDevice device, PhysicsEngine physicsEngine)
{
try
{
InitError = string.Empty;
// create joint
if (_joint == null)
{
CreateJoint();
}
else
{
_joint = PhysicsJoint.Create(_joint.State);
}
//create entitys and Cam
if (_support == null) CreateSupport(base.State.Pose.Position);
if (_pend == null) CreatePendulum(base.State.Pose.Position);
if (_camera == null) CreateCamera("PendCam", base.State.Pose.Position);
base.Initialize(device, physicsEngine);
_support.Initialize(device, physicsEngine);
_pend.Initialize(device, physicsEngine);
// Pose of the joint is important because non static objects positions will be forced to the joints attach point
_joint.State.Connectors[0] = new EntityJointConnector(
_support,
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
new Vector3(0f, 0f, - length / 2)
);
_joint.State.Connectors[1] = new EntityJointConnector(
_pend,
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
new Vector3(0f, length / 2 - radius / 2 , radius / 2)
);
// IMPORTANT: Insert Cam as child of pendulum -> cam pose relative to pendulum.
_pend.InsertEntity(_camera);
//make _support a fixed object
_support.PhysicsEntity.IsKinematic = true;
PhysicsEngine.InsertJoint(_joint);
Flags |= VisualEntityProperties.DoCompletePhysicsShapeUpdate;
}
catch (Exception ex)
{
HasBeenInitialized = false;
InitError = ex.ToString();
}
}
public override void Update(FrameUpdate update)
{
base.Update(update);
_support.Update(update);
_pend.Update(update);
}
public override void Render(RenderMode renderMode, MatrixTransforms transforms, CameraEntity currentCamera)
{
base.Render(renderMode, transforms, currentCamera);
_support.Render(renderMode, transforms, currentCamera);
_pend.Render(renderMode, transforms, currentCamera);
}
public override void Dispose()
{
base.Dispose();
_support.Dispose();
_pend.Dispose();
}
}
#endregion