MogreNewt + Mogre for Ogre 1.4.0 : objects not moving

BenJ

30-03-2007 23:27:25

Hello,
I'm French and I want to do a small physic game in C# using Mogre and MogreNewt.
I've a problem compiling the sample application (after porting Axiom to MOIS), the objects are not moving :


They keep floating in the sky.

Here is the code of Basics.cs :
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using MOIS;
using MogreNewt;

namespace MogreNewt.Demo.Basics
{
// Pressing 'SPACE' shoots a box
// 'F3' Newton debug lines

class Basics : Mogre.Demo.ExampleApplication.Example
{
const int NEWTON_FRAMERATE = 60;

float m_update = 1.0f / (float)NEWTON_FRAMERATE;
float m_elapsed = 0;

int mEntityCount = 0;
World m_World;

float timer = 0;

public override void CreateFrameListener()
{
base.CreateFrameListener();

root.FrameStarted += new FrameListener.FrameStartedHandler(Scene_FrameStarted);
root.FrameStarted += new FrameListener.FrameStartedHandler(Newton_FrameStarted);
}

bool Scene_FrameStarted(FrameEvent evt)
{
// in this frame listener we allow the user to "shoot" boxes
// by pressing the space bar.

if(inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE))
{
if (timer <= 0.0f)
{
// now "shoot" an object!

// we get the position and direction from the camera...
Mogre.Vector3 dir, vec;
Quaternion camorient = camera.Orientation;
vec = new Mogre.Vector3(0, 0, -1);

dir = camorient * vec;

// then make the visual object (again a cylinder)
Mogre.Vector3 pos = camera.Position;

Body body = MakeSimpleBox(new Mogre.Vector3(2, 2, 2), pos, camorient);

// set the initial orientation and velocity!
body.setVelocity((dir * 30.0f));

timer = 0.2f;
}
}

timer -= evt.timeSinceLastFrame;

return true;
}


// The basic OgreNewt framelistener with time slicing
bool Newton_FrameStarted(FrameEvent evt)
{
m_elapsed += evt.timeSinceLastFrame;

if ((m_elapsed > m_update) && (m_elapsed < (1.0f)))
{
while (m_elapsed > m_update)
{
m_World.update(m_update);
m_elapsed -= m_update;
}
}
else
{
if (m_elapsed < (m_update))
{
// not enough time has passed this loop, so ignore for now.
}
else
{
m_World.update(m_elapsed);
m_elapsed = 0.0f; // reset the elapsed time so we don't become "eternally behind".
}
}

// For the debug lines
/*if (input.IsKeyPressed(Axiom.Input.KeyCodes.F3))
{
MogreNewt.Debugger.Instance.showLines(m_World);
}
else
{
MogreNewt.Debugger.Instance.hideLines();
}*/

return true;
}

public override void CreateScene()
{
// Newton initialization
m_World = new World();
MogreNewt.Debugger.Instance.init(sceneMgr);


// sky box.
sceneMgr.SetSkyBox(true, "Examples/CloudyNoonSkyBox");

// shadows on!
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;

// floor object!
Entity floor;
SceneNode floornode;
floor = sceneMgr.CreateEntity("Floor", "simple_terrain.mesh");
floornode = sceneMgr.RootSceneNode.CreateChildSceneNode("FloorNode");
floornode.AttachObject(floor);
floor.SetMaterialName("Simple/BeachStones");

floor.CastShadows = false;

//-------------------------------------------------------------
// add some other objects.
Entity floor2;
SceneNode floornode2;
floor2 = sceneMgr.CreateEntity("Floor2", "simple_terrain.mesh");
floornode2 = floornode.CreateChildSceneNode("FloorNode2");
floornode2.AttachObject(floor2);
floor2.SetMaterialName("Simple/BeachStones");
floor2.CastShadows = false;
floornode2.SetPosition(80.0f, 0.0f, 0.0f);

Entity floor3;
SceneNode floornode3;
floor3 = sceneMgr.CreateEntity("Floor3", "simple_terrain.mesh");
floornode3 = floornode.CreateChildSceneNode("FloorNode3");
floornode3.AttachObject(floor3);
floor3.SetMaterialName("Simple/BeachStones");
floor3.CastShadows = false;
floornode3.SetPosition(-80.0f, -5.0f, 0.0f);
floornode3.Orientation = new Quaternion(new Degree(15.0f), Mogre.Vector3.UNIT_Z);
//-------------------------------------------------------------

// using the new "SceneParser" TreeCollision primitive. this will automatically parse an entire tree of
// SceneNodes (parsing all children), and add collision for all meshes in the tree.
MogreNewt.CollisionPrimitives.TreeCollisionSceneParser stat_col = new MogreNewt.CollisionPrimitives.TreeCollisionSceneParser(m_World);
stat_col.parseScene(floornode, true);
MogreNewt.Body bod = new MogreNewt.Body(m_World, stat_col);
stat_col.Dispose();

bod.attachToNode(floornode);
bod.setPositionOrientation(new Mogre.Vector3(0.0f, -20.0f, 0.0f), Quaternion.IDENTITY);


// position camera
camera.SetPosition(0.0f, -3.0f, 20.0f);

//make a light
Light light;

light = sceneMgr.CreateLight("Light1");
light.Type = Light.LightTypes.LT_POINT;
light.SetPosition(0.0f, 100.0f, 100.0f);
}

public override void DestroyScene()
{
// Not absolutely necessary, it can get cleaned up at the finalizer
m_World.Dispose();
m_World = null;

MogreNewt.Debugger.Instance.deInit();
}

Body MakeSimpleBox(Mogre.Vector3 size, Mogre.Vector3 pos, Quaternion orient)
{
// base mass on the size of the object.
float mass = 10000f;

// calculate the inertia based on box formula and mass
Mogre.Vector3 inertia = MogreNewt.MomentOfInertia.CalcBoxSolid(mass, size);


Entity box1;
SceneNode box1node;

box1 = sceneMgr.CreateEntity("Entity" + (mEntityCount++), "box.mesh");
box1node = sceneMgr.RootSceneNode.CreateChildSceneNode();
box1node.AttachObject(box1);
box1node.SetScale(size);
box1.NormaliseNormals = true;

MogreNewt.Collision col = new MogreNewt.CollisionPrimitives.Box(m_World, size);
MogreNewt.Body bod = new MogreNewt.Body(m_World, col);
col.Dispose();

bod.attachToNode(box1node);
bod.setMassMatrix(mass, inertia);
bod.IsGravityEnabled = true;

box1.SetMaterialName("Examples/10PointBlock");


bod.setPositionOrientation(pos, orient);

return bod;
}
}
}


Thanks for your help !

Bekas

31-03-2007 10:43:09

Wierd, I tried your sample and the objects are moving fine. Maybe you should try a clean install of the latest SDK.
Anyone else having the same problem ?

BenJ

31-03-2007 12:56:32

After hours of headache, it finally works !

- I replaced the Newton.dll from NewtonSDK\sdk\dll_double to the one in NewtonSDK\sdk\dll, then the sample works, but not my game.
- I added newtWorld.setWorldSize(new Vector3(-100000, -100000, -100000), new Vector3(100000, 100000, 100000)); (Maybe too big, but I'll see after)
The world in my game was much bigger, so when a box goes out of the newton bounding box, no more physic.

CK_MACK

02-04-2007 05:41:55

if you get your example working could you repost the code?

I have been looking for more MogreNewt code samples.

Thanks.

Marc

Eldritch

02-04-2007 09:31:05

I had a similar problem, but it seemed like I had to use setVelocity() rather than setForce(). Now I have another problem though.. no entities will collide with each other.

BenJ

02-04-2007 17:23:22

if you get your example working could you repost the code?

I have been looking for more MogreNewt code samples.

Thanks.

Marc


The sample works itself, it was just a problem of DLL.
If you want, I've ported the Ogre DotScene Loader into C#, and it works perfectly.

Eldritch

02-04-2007 18:00:41

Interesting!! What can export to such a format?

BenJ

03-04-2007 17:33:45

In my project, I use Maya to make the scene, then an exporter to make the .scene

If someone can help me with the stencil shadows, I can't activate them.

Here is an example of what we've done :

Bekas

03-04-2007 21:29:32

If someone can help me with the stencil shadows, I can't activate them.
Do you activate them like this:
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
And make sure that the objects have CastShadows = true.

BenJ

03-04-2007 21:52:12

If someone can help me with the stencil shadows, I can't activate them.
Do you activate them like this:
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
And make sure that the objects have CastShadows = true.


Yes, I have done those two things, and it does nothing (as you can see on the screenshot)

Bekas

03-04-2007 22:28:43

Try
MeshPtr mesh = MeshManager.Singleton.Load("my.mesh");
ushort src, dest;
mesh.SuggestTangentVectorBuildParams(VertexElementSemantic.VES_TANGENT, out src, out dest);
mesh.BuildTangentVectors(VertexElementSemantic.VES_TANGENT, src, dest);

for all the meshes used.

BenJ

03-04-2007 22:42:37

I do :
MeshPtr mesh = MeshManager.Singleton.Load(meshFile, m_sGroupName);
ushort src, dest;
mesh.SuggestTangentVectorBuildParams(VertexElementSemantic.VES_TANGENT, out src, out dest);
mesh.BuildTangentVectors(VertexElementSemantic.VES_TANGENT, src, dest);

pEntity = mSceneMgr.CreateEntity(name, meshFile);
pEntity.CastShadows = castShadows;


for every mesh, but doesn't works.
I also get :
WARNING: **.mesh is an older format ([MeshSerializer_v1.30]); you should upgrade it as soon as possible using the OgreMeshUpgrade tool.
for every mesh, maybe it's linked ?

Bekas

03-04-2007 23:40:57

I also get :
WARNING: **.mesh is an older format ([MeshSerializer_v1.30]); you should upgrade it as soon as possible using the OgreMeshUpgrade tool.
for every mesh, maybe it's linked ?

Probably; try upgrading the meshes.

BenJ

04-04-2007 18:12:25

I've upgraded all my meshes (no more warnings) but there is no shadows.

Bekas

04-04-2007 18:46:56

Can you upload somewhere one of the meshes ?

BenJ

04-04-2007 19:03:03

Here is the main mesh (the brown ground on the screenshot above) :
http://gamomax.free.fr/Ground.mesh

Bekas

04-04-2007 19:39:59

With this code:
class Demo : Demo.ExampleApplication.Example
{
public override void CreateScene()
{
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;

// Set ambient light
sceneMgr.AmbientLight = new ColourValue(0.3f, 0.3f, 0.2f);

Light l = sceneMgr.CreateLight("Light2");
l.SetPosition(10, 200, 0);
Vector3 dir = new Vector3(-1, -1, 0);
dir.Normalise();
l.Type = Light.LightTypes.LT_DIRECTIONAL;
l.Direction = dir;
l.SetDiffuseColour(1, 1, 0.8f);
l.SetSpecularColour(1, 1, 1);


Entity pEnt;


pEnt = sceneMgr.CreateEntity("3", "Ground.mesh");
sceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(0, 0, 300)).AttachObject(pEnt);
pEnt.SetMaterialName("Examples/Rockwall");

sceneMgr.SetSkyBox(true, "Examples/MorningSkyBox");

camera.SetPosition(-400, 400, 900);
camera.LookAt(0, 80, 0);
}
}


I get working shadows:

BenJ

04-04-2007 22:47:22

Ok, it works, but only with LT_DIRECTIONAL lights, and I was in spotlights.

Eldritch

05-04-2007 07:14:30

I have the same problem, with Point Lights...

Bekas

05-04-2007 10:50:59

class Demo : Mogre.Demo.ExampleApplication.Example
{
public override void CreateScene()
{
sceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
// Set ambient light
sceneMgr.AmbientLight = new ColourValue(0.3f, 0.3f, 0.2f);

Light l = sceneMgr.CreateLight("Light2");
l.SetPosition(10, 400, 0);
Vector3 dir = new Vector3(0, 0, 300) - new Vector3(10, 400, 0);
dir.Normalise();
l.Type = Light.LightTypes.LT_SPOTLIGHT;
l.Direction = dir;
l.SetDiffuseColour(1, 1, 0.8f);
l.SetSpecularColour(1, 1, 1);



Entity pEnt;


pEnt = sceneMgr.CreateEntity("3", "Ground.mesh");
SceneNode sn = sceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(0, 0, 300));
sn.Scale(Vector3.UNIT_SCALE * 0.1f);
sn.AttachObject(pEnt);
pEnt.SetMaterialName("Examples/Rockwall");

Plane plane;
plane.normal = Vector3.UNIT_Y;
plane.d = 100;
MeshManager.Singleton.CreatePlane("Myplane",
ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane,
1500, 1500, 10, 10, true, 1, 5, 5, Vector3.UNIT_Z);
Entity pPlaneEnt = sceneMgr.CreateEntity("plane", "Myplane");
pPlaneEnt.SetMaterialName("Examples/Rockwall");
pPlaneEnt.CastShadows = false;
sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(pPlaneEnt);

sceneMgr.SetSkyBox(true, "Examples/MorningSkyBox");

camera.SetPosition(-400, 400, 900);
camera.LookAt(0, 80, 0);
}
}



LT_SPOTLIGHT:


And if you change the light to LT_POINT: