skuleguy

Hi,

I'm trying to put in a customized mesh for my wheel, but for some reason, the default wheel Mesh (when no Mesh is specified) is also rendered at the same time.

Please take a look at the Picture below:

http://farm2.static.flickr.com/1305/1010843235_3ac1cb05c8_o.jpg

(the little facing-up dish is my mesh)

Here's the wireframe:

http://farm2.static.flickr.com/1434/1010843289_9cd88c7d96_o.jpg

Here's what I wrote:

Code Snippet

[DataContract]

public class RobotEntity : VisualEntity

{

private SingleShapeEntity _swingBarL;

[DataMember]

public SingleShapeEntity SwingBarL

{

get { return _steerBarL; }

set { _steerBarL = value; }

}

private void CreateSwingBarL(Vector3 pos)

{

BoxShapeProperties prop = new BoxShapeProperties(1,

new Pose(new Vector3()),

new Vector3(SWING_BAR_X, SWING_BAR_Y, SWING_BAR_Z)

);

Shape swingBarLShape = new BoxShape(prop);

_swingBarL = new SingleShapeEntity(swingBarLShape, base.State.Pose.Position);

_swingBarL.State.Name = "SwingBarL";

}

private WheelEntity _wheelL;

public WheelEntity WheelL

{

get { return _wheelL; }

set { _wheelL = value; }

}

private void CreateWheelL(SingleShapeEntity parent)

{

// declaring wheel properties

WheelShapeProperties w = new WheelShapeProperties("SingleWheelL", WHEEL_MASS, WHEEL_RADIUS);

w.LocalPose = new Pose(new Vector3(SWING_BAR_X / 2 + ARM_BAR_X + STEER_BAR_X / 2, -V_WHEEL_OFFSET, 0f));

w.Flags = WheelShapeBehavior.OverrideAxleSpeed;

_wheelL = new WheelEntity(w);

_wheelL.State.Assets.Mesh = "wheelOne.bos";

_wheelL.State.Name = "WheelL";

_wheelL.Parent = parent;

SimulationEngine.GlobalInstancePort.Insert(_wheelL);

}

public override void Initialize(xnagrfx.GraphicsDevice device,

PhysicsEngine physicsEngine)

{

try

{

//Implementation of Initialization

InitError = string.Empty;

if (_swingBarL == null)

CreateSwingBarL(base.State.Pose.Position);

CreateWheelL(_swingBarL);

_swingBarL.Initialize(device, physicsEngine);

Flags |= VisualEntityProperties.DoCompletePhysicsShapeUpdate;

}

catch (Exception ex)

{

HasBeenInitialized = false;

InitError = ex.ToString();

}

}

public override void Update(FrameUpdate update)

{

base.Update(update);

_swingBarL.Update(update);

_wheelL.Update(update);

}

public override void Render(VisualEntity.RenderMode renderMode, MatrixTransforms transforms, CameraEntity currentCamera)

{

base.Render(renderMode, transforms, currentCamera);

_swingBarL.Render(renderMode, transforms, currentCamera);

}

public override void Dispose()

{

base.Dispose();

_swingBarL.Dispose();

}

}

The _swingBarL (a SingleShapeEntity guy)'s mesh was loaded just fine when I tried:

_swingBarL.State.Assets.Mesh = "somemesh.bos";

Please show me how to solve this problem.

Thanks so much !



Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

bkloster

Hello,
try removing the call to Insert on the SimulationEngine's port.I dimly remember a similar problem where the parent (that you have set) handles the insertion of the entity. So the parent inserts it, and you insert it.

Ben




Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

skuleguy

Hi Ben,

Thanks for your reply,

I deleted the SimulationPort insertion.

And in Initialized(), I did

Code Snippet
_swingBar1.InsertEntity(_wheel!)

(where _swingBar1 is wheel1's parent)

But then how do I insert it myself, could you please show me

Thanks,





Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

KyleJ - MSFT

The way the wheel entities work is they create a wheel shape and then insert that shape into their parent's PhysicsEntity along with a mesh that represents that wheel shape. When the parent is initialized, it creates meshes for every other shape.

These meshes are rendered for the parent if no custom mesh was specified, even if a custom mesh was specified for the child. So you are getting the default mesh for the wheel rendered as part of the parent and the custom wheel mesh rendered for the wheel entity.

One way to fix this is to create a new class that inherits from WheelEntity and override the initialize method. Remove the call to AddShapetoPhysicsEntity and replace it with a call to PHysicsEntity.InsertShape. That way no mesh will be inserted into the parent entity and you will only render the custom mesh for the wheel. Here is what your overridden initialize method looks like:

Code Snippet

public override void Initialize(xnagrfx.GraphicsDevice device, PhysicsEngine physicsEngine)

{

try

{

InitError = string.Empty;

// we don't support rendering. The parent can decide to render us as part of its geometry

base.Initialize(device, physicsEngine);

if (Parent == null)

throw new Exception("This entity must be a child of another entity.");

// add to parent

//Parent.AddShapeToPhysicsEntity(Wheel, null);

// new code

Parent.PhysicsEntity.InsertShape(shape);

shape.Parent = Parent;

}

catch (Exception ex)

{

HasBeenInitialized = false;

InitError = ex.ToString();

}

}

-Kyle





Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

skuleguy

Hi,

I rewrote the code for that entity, but it ask me for shapes. I looked up in the WheelEntity class code, there's no "shape" object in the Initialize function.

Code Snippet

///Class RobotWheelEntity

[DataContract]

public class RobotWheelEntity : WheelEntity

{

public RobotWheelEntity(WheelShapeProperties shape)

: base( shape)

{

}

/// <summary>

/// Initialization

/// </summary>

/// <param name="device"></param>

/// <param name="physicsEngine"></param>

public override void Initialize(xnagrfx.GraphicsDevice device, PhysicsEngine physicsEngine)

{

try

{

InitError = string.Empty;

// we don't support rendering. The parent can decide to render us as part of its geometry

base.Initialize(device, physicsEngine);

if (Parent == null)

throw new Exception("This entity must be a child of another entity.");

// add to parent

//Parent.AddShapeToPhysicsEntity(Wheel, null);

// new code

Parent.PhysicsEntity.InsertShape(shape);

shape.Parent = Parent;

}

catch (Exception ex)

{

HasBeenInitialized = false;

InitError = ex.ToString();

}

}

}

Thanks





Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

KyleJ - MSFT

The shape you are inserting into the physics entity is the wheel shape. Replace the call to Parent.AddShapeToPhysicsEntity(Wheel, null); with Parent.PhysicsEntity.InsertShape(Wheel); Wheel.Parent = Parent.

-Kyle





Re: Microsoft Robotics - Simulation Double Wheel Mesh issue

KyleJ - MSFT

Well, I got around to implementing the initialize override and it doesn't work because the parent entity adds the wheel meshes when it calls its base.Initialize() method.

There are two ways to eliminate the wheel meshes from your model:

1. Specify a custom mesh for the parent entity. In this case, the wheel meshes will not be generated and the simulator will use the custom mesh to render the entity. You can then specify a custom mesh for each wheel entity and they will be rendered as well with not default wheel meshes.

2. Delete the meshes from the parent entity once they have been generated. The following code shows how to delete meshes from the Meshes collection in the parent entity:

Code Snippet

public override void Initialize(xnagrfx.GraphicsDevice device, PhysicsEngine physicsEngine)
{
...
_wheelR = new WheelEntity(wheelRprop);
_wheelR.State.Name = base.State.Name + " RightWheel";
_wheelR.Parent = this;
_wheelR.Initialize(device, physicsEngine);

base.Initialize(device, physicsEngine);
Meshes.RemoveRange(2, 2);
...
}

Of course,you will need to adjust the beginning index and the number of meshes to delete according to your own parent entity. In the case of the code above, the parent entity has two shapes of its own so it has two meshes and it deletes the two meshes (index 2 and 3 in the collection) built from the wheels.

-Kyle