Stupid code that not works :S

vilgeits

20-02-2006 10:27:16

i've use ogrenewt long ago when it was not an official addon, my code works fine but i try to update it and nothing works :cry:

i try to put a simply code in my app and.... noway

if (iInputDevice->isKeyDown(Ogre::KC_9))
{

// we get the position and direction from the camera...
Ogre::Vector3 dir, vec;
Ogre::Quaternion camorient = iCamera->getOrientation();
vec = Ogre::Vector3(0,0,-1);

dir = camorient * vec;

// then make the visual object (again a cylinder)
Entity* ent;
SceneNode* node;
Ogre::String name;
Ogre::Vector3 pos = iCamera->getPosition();

name = "Body "+Ogre::StringConverter::toString( count++ );

ent = iSceneManager->createEntity( name, "cylinder.mesh" );
node = iSceneManager->getRootSceneNode()->createChildSceneNode( name );
node->attachObject( ent );
node->setScale(10,10,10);

ent->setMaterialName( "Simple/dirt01" );
ent->setNormaliseNormals(true);

// again, make the collision shape.
OgreNewt::Collision* col2 = new OgreNewt::CollisionPrimitives::Cylinder(NF_World, 10, 10);

// then make the rigid body.
OgreNewt::Body* body = new OgreNewt::Body( NF_World, col2 );

//no longer need the collision shape object
delete col2;

// something new: moment of inertia for the body. this describes how much the body "resists"
// rotation on each axis. realistic values here make for MUCH more realistic results. luckily
// OgreNewt has some helper functions for calculating these values for many primitive shapes!
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcSphereSolid( 10.0, 1.0 );
body->setMassMatrix( 10.0, inertia );

// attach to the scene node.
body->attachToNode( node );

// this is a standard callback that simply add a gravitational force (-9.8*mass) to the body.
body->setStandardForceCallback();

// set the initial orientation and velocity!
body->setPositionOrientation( pos, camorient );
body->setVelocity( (dir * 50.0) );


}


The cylinders were created but it didn't move :cry:

thanks in advance

HexiDave

20-02-2006 11:09:59

Are they sticking exactly where you hit the key? If so, I had the same problem - you are out of the physic's world's bounds. Go to the constructor or wherever you created your NF_World variable (should look like
NF_World = new OgreNewt::World();

Change it to this:
NF_World = new OgreNewt::World();
NF_World->setWorldSize(Ogre::AxisAlignedBox(-1000,-1000,-1000,1000,1000,1000));


Set those boundaries to whatever you want to be (-x,-y,-z,+x,+y,+z) or use two Vector3's. I had success setting them to -100,000 - +100,000 because my world is pretty huge (no slowdowns.)

vilgeits

20-02-2006 13:01:51

WOP thanks but now i've another problem... :S ... setVelocity works ok but setForce doesn't made nothing :S

walaber

20-02-2006 18:53:58

what do you mean? you can throw the objects but theydon't fall to the ground?

vilgeits

20-02-2006 21:18:05

no, that problem was fixed using the HexiDave advice. But now i'm triying to move my node using forces instead velocity because if i put velocity the body has vibrations when it collide with a wall. I know the aswer is use forces but my body doesn't move with forces :( (with velocities works fine)

walaber

20-02-2006 21:38:54

you are applying the forces in a ForceAndtorqueCallback, right?

the only place you can add external forces in inside a callback.

vilgeits

20-02-2006 22:01:25

you mean that my body->addForce(Ogre::Vector3(A,B,C)); is a not good way? heheheh

Sorry i haven't saw any example doing this :oops:

walaber

21-02-2006 00:26:02

yes that is the right way, but it must be done in a registered callback, so it can be applied at the proper time in the physics process.

have a look at any demo that supports dragging objects around for an example callback.

vilgeits

21-02-2006 01:11:26

yes that is the right way, but it must be done in a registered callback, so it can be applied at the proper time in the physics process.

have a look at any demo that supports dragging objects around for an example callback.


What do you mean with registered callback

walaber

21-02-2006 04:40:00

I mean you need to make a callback function that accepts a single pointer to an OgreNewt::Body* as its parameter. if this is a member of a class, it needs to be a static function.

for example
static void SomeClass::myForceCallback( OgreNewt::Body* body )
{
// do your adding of forces on the body here
}


then, any body that you want to use this, you do:
body->setCustomForceAndTorqueCallback( SomeClass::myForceCallback );

vilgeits

21-02-2006 15:00:16

thanks

there is my last wall:
static void myForceCallback( OgreNewt::Body* currentBody)
{
currentBody->setForce(forces);
}


error C2597: referencia no vĂ¡lida a miembro no static 'iTechPhysicsFrameListener::forces'

something like:

error C2597: not valid reference to a no static member 'iTechPhysicsFrameListener::forces'

where forces is an Ogre::Vector3 protected into the class that contains this call.

walaber

21-02-2006 17:12:08

thats it because this is a static function, you cannot access members so easily. I believe you can access static memebers though, so if you change it to static it will probably work.

otherwise the preferred way is to use the userData() function of rigid bodies. for example you make a simple class or all your physics bodies... something like this

Class PhysicsObject
{
public:
PhysicsObject();
~PhysicsObject();

void setForce( Ogre::Vector3 force ) { mForce = force; }
Ogre::Vector3 getForce() { return mForce; }

private:

OgreNewt::Body mBody;
Ogre::Vector3 mForce;
};


then in the initation of this class (when you create the body) you do this:
mBody->setUserData(this)

finally, back in that callback, you can do this:

PhysicsObject* obj = (PhysicsObject*)currentBody->getUserData();
currentBody->setForce( obj->getForce() );

vilgeits

23-02-2006 14:57:47

thanks walaber :D you're the man ...