How to implement picking and dragging object in physicsworld

qi-an

04-07-2008 10:36:03

I' m new for OgreNewt ,I want to implement picking and dragging object in physics world .

In my code,I create a floor and a box ,when I run it,evrything looks well,the box fall in the floor.But when I picking and dragging the box ,the collision detected loods as if it lose its effectiveness!

These are my code snippets:

public void CreateInputHandler()
{
mRoot.FrameStarted += new FrameListener.FrameStartedHandler(Newton_FrameStarted);
this.MouseDown += new MouseEventHandler(OgreForm_MouseDown);
this.MouseUp += new MouseEventHandler(OgreForm_MouseUp);
this.MouseMove += new MouseEventHandler(OgreForm_MouseMove);
}



void OgreForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mRaySceneQuery = mgr.CreateRayQuery(MouseRay);
mRaySceneQuery.SetSortByDistance(true);
RaySceneQueryResult mresult = mRaySceneQuery.Execute();
foreach ( RaySceneQueryResultEntry result in mresult)
{
mCurrentNode = result.movable.ParentSceneNode;
mCurrentNode .ShowBoundingBox = true ;
mRotating = true;

break;

}

}

}




bool Newton_FrameStarted(FrameEvent evt)
{

m_elapsed += evt.timeSinceLastFrame*3;

if ((m_elapsed > m_update) && (m_elapsed < (1.0f)))
{
while (m_elapsed > m_update)
{
myworld.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
{
myworld.Update(m_elapsed);
m_elapsed = 0.0f; // reset the elapsed time so we don't become "eternally behind".
}
}
return true;
}



public void SceneCreator()
{

// Create a Simple Scene
mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
cam = mgr.CreateCamera("Camera");
cam.SetPosition(60, 60, 60);
cam.LookAt(new Vector3(0, 0, 0));
cam.AutoAspectRatio = true;
cam.NearClipDistance = 5;
mviewport = mWindow.AddViewport(cam);
mviewport.BackgroundColour = new ColourValue(0.7f, 0.7f, 0.7f);

myworld.SetWorldSize(new Vector3 ( -1000, -1000, -1000),new Vector3 (1000,1000,1000));
MogreNewt.Debugger.Instance.Init(mgr);
Vector3 inertia = MomentOfInertia.CalcBoxSolid(100, new Vector3(100, 100, 100));
Vector3 size = new Vector3(20, 2, 20);
Entity floor = mgr.CreateEntity("floor", "Box.mesh");
SceneNode node1 = mgr.RootSceneNode.CreateChildSceneNode();
node1.AttachObject(floor);
floor.NormaliseNormals = true;
node1.Scale(size);

Collision col = new MogreNewt.CollisionPrimitives.TreeCollision (myworld,floor,true );
Body bod = new Body(myworld, col);
col.Dispose();

bod.AttachToNode(node1);
bod.SetMassMatrix(100, inertia);
bod.SetPositionOrientation(new Vector3(0, -30, 0), Quaternion.IDENTITY);


Entity ent2 = mgr.CreateEntity("ninja2", "box01.mesh");
SceneNode node2 = mgr.RootSceneNode.CreateChildSceneNode();
node2.AttachObject(ent2);
node2.Scale(0.1f, 0.1f, 0.1f);

Collision col1 = new MogreNewt .CollisionPrimitives .Box (myworld, new Vector3 (10,10,10) );
Body bod2 = new Body(myworld, col1);


Vector3 force = new Vector3(0, -980, 0);
bod2.AttachToNode(node2);
bod2.SetMassMatrix(100, inertia);
bod2.IsGravityEnabled = true;
bod2.SetPositionOrientation(new Vector3(0, 30, 0), Quaternion.IDENTITY);
bod2.Force = force;
}


what's wrong with me ?

I need any help and appreciate !

(I'am so sorry about my poor English!)

Thanks,again!

walaber

04-07-2008 14:05:47

when using physics, you cannot move an object by directly setting its position... you need to use forces to push or pull the object around.

please look at the OgreNewt demos, they show how to implement dragging of objects through physics.

qi-an

05-07-2008 09:06:00

Thank you,Walaber!
That's mean I need to add a Callback funtion of forces?But I lack a clear understanding of Callback()!Can you explain more?or give me some codes about it ?e.g I want to pull a box toward Z axis negative when I press key "W",how can I do that?
Thanks again!

albino

06-07-2008 15:22:55

there are examples of dragging bodies , which come with ogrenewt

then just edit the force callback like this


vectror3 force = someAccelerationValue * mass;
force = force * <0,0,1>; //to drag it only one direction

qi-an

09-07-2008 09:31:24

Thank you ,albino!
The object have be moved and dragged well ,now I also have two problems about it,first , I don't know how to stop the callback fuction,so that the boject move automaticly and I can't stop it.Second,the similar problem is that I dragging object with mouse,when I release the mouse ,the object don't fall the floor but fly.
I use MogreNewt ,there isn't setStandarForce() function ,Is there anyway to solve it?

Thanks ,again!

(I'm so sorry about my poor English!)