Throw a object in direction of camera.. [Solved!]

Tano_ITA

02-05-2008 10:27:15

Hi,
i need to know how i throw an object that i've picked with mouse. Like Half Life 2. I've writed this code:

if((dragging)&&(mMouse->getMouseState().buttonDown(OIS::MB_Right)))
{
info.mBody->setCustomForceAndTorqueCallback<OgreNewtonFrameListener>(&OgreNewtonFrameListener::ThrowObjectCallBack, this);

}


So to determinate the direction:

void OgreNewtonFrameListener::ThrowObjectCallBack (OgreNewt::Body* me)
{

Ogre::Vector3 dir, vec;
Ogre::Quaternion camorient = msnCam->getWorldOrientation();
Ogre::Vector3 pos = msnCam->getWorldPosition();
vec = Ogre::Vector3(0,0,-1);
dir = camorient * vec;


Ogre::Real mass;
//Vector3 jumpForce(0, 15,-5), inertia;
Vector3 jumpForce(pos.x,pos.y,pos.z*(-1000)), inertia;
me->getMassMatrix(mass,inertia);
//jumpForce += Vector3(0.0,-9.8,0.0);
//jumpForce *= mass;

me->addForce(jumpForce);
}


But i can't understand how i can determinate the z directorion with position of mouse and camera.. Anyone can help me?

Thanks a lot and sorry for my bad english.. =)

pra

02-05-2008 17:38:28

you have to set the body's position, and then either add a force or set the velocity once.

i.e.
Ogre::Quaternion camorient = msnCam->getWorldOrientation();

Real factor= 100;

Vector3 force = ( camorient*Vector3::NEGATIVE_UNIT_Z ) *factor;
mBody->setVelocity(factor * mass);

this should be run only once, at the moment when the body should be thrown.
at least I shoot my fireballs like that^^

Tano_ITA

05-05-2008 10:26:13

I solved:

void OgreNewtonFrameListener::ThrowObjectCallBack (OgreNewt::Body* me)
{
Ogre::Quaternion camorient = msnCam->getWorldOrientation();
me->setStandardForceCallback();

Real factor= 100;

Vector3 force = ( camorient*Vector3::NEGATIVE_UNIT_Z ) *factor;
me->setVelocity(Vector3(force.x,force.y,force.z));

}


Thanks a lot! Now work fine.