Problem integrating OgreNewt with my engine [new problem]

3dmacuser2

12-09-2006 22:44:39

I'm on osx, an intel machine. I've been able to compile ogrenewt and build and all the demos, and they run fine.

However, I'm running into trouble while integrating with my engine.

As a test, I'm just trying to create a sphere, and watch it get moved by the physics engine.

So, I load the mesh as normal. I create a collisionObject as follows:

OgreNewt::Collision* collisionObject = new OgreNewt::CollisionPrimitives::Box( gcePhysicsWorld, Ogre::Vector3(1,1,1), Ogre::Quaternion(1,0,0,0), Ogre::Vector3(0, 0, 0) );

I make a body like this:
OgreNewt::Body *collisionBody = new OgreNewt::Body(gcePhysicsWorld, collisionObject);
and then the following code to setup the object:

Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcSphereSolid( 10.0, 1.0 );

collisionBody->setMassMatrix(10.0, inertia );
collisionBody->attachToNode( _mySceneNode);
collisionBody->setStandardForceCallback();
collisionBody->setVelocity(Ogre::Vector3(0,100,0));


Previously to this, I've created my physics world.

I have a timer that fires every 1/100 seconds, and runs the following code:


gcePhysicsWorld->update( 1/100);


but... when I run this code, it compiles without problems, but my sphere doesn't move!

Also, when I try to turn on debug lines, the program crashes, though without any error notice, though that happens with the ogrenewt demos too.

So, any ideas that I can try? Thanks!


edit: see below for my new problem!

HexiDave

13-09-2006 00:18:17

First, look at creating an Ellipsoid instead of a box for a sphere :P

Next, your world may not be defined - look at the example apps on how they create the world and set the boundaries properly. Finally, I suggest not updating manually like that until you get the hang of how/when OgreNewt needs updating - use the BasicFramelistener system that the demos use - add it as a regular framelistener like they do and it takes care of it.

I don't have OgreNewt on this machine at the moment, but try OgreNewt::Debugger->getSingleton().deInit() - I think that's what it is - before you delete the OgreNewt::World.

3dmacuser2

13-09-2006 01:47:13

Thanks for your reply!

Ooops, I actually did try an ellipsoid first! When that didn't work, I changed it to a box to see if it made any difference, and I forgot to change it back before posting the code here:).

As for creating the world, and setting the bounds, in the demos the only code I can find is:

m_World = new OgreNewt::World();


I can't find where it sets the bounds. I see void setWorldSize( const Ogre::Vector3& min, const Ogre::Vector3& max );

in OgreNewt_World.h, but I don't see that called anywhere from the demos.

Why do you not recommend using my own timer for OgreNewt? I'm using OS X's built in timer functions which work pretty well, and as long as ogre doesn't take more than 1/100 second to update (which it shouldn't with only two objects), it should call ogrenewt pretty reliable every 1/100 second. I'm going to make this more robust, but I thought it would work fine for a quick test to get it running.

The reason that I'm not using the framelistener is that my engine in general doesn't use framelisteners.

Thanks for your help so far!

HexiDave

13-09-2006 02:13:08

Well I don't know how big your world is, however as long as you aren't concerned about "culling" physics updates to objects that are really far away or something, just do something similar to this:
Real mSize = 10000;
mWorld->setWorldSize(Vector3(-mSize,-mSize,-mSize),Vector3(mSize,mSize,mSize));


That should update everything in a 20k x 20k x 20k world space - it shouldn't incur any overhead over a smaller world, it just checks per update to see that objects are within that space to recieve said update.

As for the update, the BasicFramelistener code is really reliable, so if you still have problems here I recommend looking at its code.

3dmacuser2

13-09-2006 03:27:52

Great, changing the bounds worked!

So I guess the default bounds are very small, so my object was outside the bounds and not getting updated?

Now the problem I have is that my object immediately jumps to 0,0,0, and that the standard force callback makes it look like it's underwater.... but I'm assuming I've probably got some scales and stuff set wrong, and I'm going to simplify my scene and keep working on it.

Thanks for you help again!

walaber

13-09-2006 04:09:24

if you have a project file you used to get OgreNewt to compile inder OSX, I would love to add it to the official OgreNewt library...did you have to make any changes to get it to compile?

3dmacuser2

13-09-2006 05:56:09

Yeah, I've got an XCode project that gets OgreNewt and the samples to compile under OSX. I didn't need to make any changes to OgreNewt, but I did have to make some changes to the samples. I'll try to package it up nicely and send it to you.


It works great on my MacBookPro!

Horizon

13-09-2006 16:04:48

A little off-topic, but how well does Ogre run on Mac these days? I'm considering buying a MacBook Pro too :).

3dmacuser2

13-09-2006 17:52:41

I've only tried the demos, but they worked pretty damn well on my MBP.

For the joints one, for example, I was able to keep shooting out spheres for as long as I wanted, with tons and tons of collisions going on, and I never saw the framerate drop below 60.

Horizon

15-09-2006 15:02:27

Ah yes, the graphics card in the MBP is pretty sick, isn't it?

Good to hear Ogre works well though.

3dmacuser2

21-09-2006 21:36:42

Ok, I'm still having some problems integrating with my engine.


I can create an object with physics, and if I give it the StandardForceCallback it drops correctly, and even collides and bounces correctly with a plane.

However, I'm having trouble with a custom ForceAndTorqueCallback.

I have a ball that starts around 25 units above the plane. By default it has the standardforcecallback. When I press a key, I set the custom callback, and in the callback I use setForce(Ogre::Vector(0,10,)). When I let go of the key, it resets the standard force callback.

If my understanding is correct, this should make it move upwards.

Here's what 's weird:

If I press the key while the ball is falling, it slows the fall, but doesn't stop. If I press the key while the ball is rebounding upwards, it slowly moves up, but eventually stops.

If I press the key while the ball is unmoving on the plane, then nothing happens.

If I don't have any standardforcecallback (so it just sits at 25 units above the ground), then it never moves when I apply the force.


If I use a vector with only an x component, like (1,0,0), then it starts moving, but instead of just moving in the x direction, it moves diagonally in the x and y direction.

Putting anything in the z component behaves like in the y component.


I know my callback is being called, because I have some debug text that prints correctly.

Any idea what could be causing this? It's very strange.

HexiDave

21-09-2006 22:00:12

Again, I'm out of touch with OgreNewt for the moment, but you're using setForce - the velocity isn't being adjusted properly as the force is constantly +10 of what the velocity is, so try addForce() instead. That'll add (0,10,0) force per update, which will increase the speed instead of just slowing decent.

3dmacuser2

21-09-2006 22:17:04

Ok, I tried addForce, and it behaves exactly the same.

I'm a bit confused though... should this behave like real world physics?

If I use setForce, it sets the force to that value, right? So it should be under constant acceleration, A=F/M.

If I use add Force, it will be increasing acceleration. Even with constant acceleration, it should still stop a drop if the force is big enough. (And changing the magnitude of the force didn't change anything.)

The velocity it's currently moving at shouldn't affect the force, right?

Or is the terminology in newton different from "real" physics?

HexiDave

21-09-2006 23:20:44

No, I think I'm just being stupid - pretty sure I was thinking of setVelocity...

3dmacuser2

23-09-2006 18:29:48

Anyone else have any ideas what might be causing this strange problem?

walaber

23-09-2006 20:25:06

make it so that when holding down the key, an empty forcecallback is called (applies no forces at all).

then confirm that when you hold down that key, the object behaves as if it is in zero gravity.

also don't forget that if a body comes to rest (on the ground, etc), it is removed from the "active" objects lists, and it's force callback is no longer called. you can force an object back onto the list by calling unFreeze().

3dmacuser2

25-09-2006 06:48:27

I tried using an empty forcecallback.

It *mostly* behaves as if it is in zero gravity when I press the key.
Here's what I mean by mostly...

If I hit the key as soon as it loads, the ball doesn't move at all, which should be correct, since there are no forces on it.

If I let it drop a bit, and then hit the key, it stops accelerating downward, and drifts down at a constant velocity, which is also correct.

However, after it rebounds off the plane, or if I hit the key after it rebounds, it keeps moving up (as I would expect), but it eventually slows down and stops (which I wouldn't expect if there's no forces on it!) It almost behaves as if it has some sort of air resistance slowing it down.

I know about the freezing, and my method that sets the callback also calls unfreeze(). The object will move if I give it a force in the x direction (even though it moves in the x and y direction!). But if I give it a force in the y direction, it won't move.

In fact, giving it a force in the y direction, even 100, it behaves identical to the empty callback.

Thanks for the suggestions so far. Any other ideas?

walaber

25-09-2006 07:33:19

a few ideas.

1. have you set your world size properly? if the body leaves the world limits (default to -100,-100,-100 to 100,100,100), it will be removed from the active list as well.

the body stopping on it's own is because of "damping". Newton applies a damping factor to both the velocity and omega eash update.

it defaults to a high value of 0.1, so if you call setLinearDamping() and set it to a lower value (like 0.001), this effect will be reduced greatly.

3dmacuser2

25-09-2006 07:55:54

I'm setting the bounds to (-1000, -1000, -1000) to (1000, 1000, 1000), and I am confident that the object is not leaving the bounds. (I've placed some objects at +50 in x, y, and z, so I can get an idea of where my objects are.)

The damping explains why the object eventually slows.

It seems that the body is staying active, and the callback is being called correctly, since it acts as if it has no forces on it when I press a key.

and.... oh wow. I just looked at my code again, and I found my problem.

I feel very very stupid now.

I was making the force vector by pulling the x, y, and z components from a dictionary... and I copy and pasted the code, and forgot to change the second two components to 'y' and 'z'...

So, it was only moving when I gave it something in the x component.

Geez I feel dumb.

Anyway, thanks for helping me with this problem! I'll try to package of an osx project for you.

walaber

25-09-2006 17:11:24

no problem. glad you got it working.