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 :
![](http://gamomax.free.fr/screenshot1.jpg)
They keep floating in the sky.
Here is the code of Basics.cs :
Thanks for your help !
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 !