gbisocoli
12-06-2009 03:25:54
Hi!
I'm working on a project and I want to apply a force to a body (a ball) picked by the mouse, the amount of force and the direction applied is given by the dragging of the mouse. The picking works ok but I can't make the ball go to the right direction.
I have this:
1. I pick the ball and set to true "mCalculatingBallPath":
2. I update current Ball Path with the point of the ground I clicked:
3. Finally, I apply the force:
4. The BallPath singleton have this methods, I think the problem(s) is(are) here on the calculations:
Maybe I should do all with NxOgre? Raycasting, use NxOgre::Vector3 instead of Ogre::Vector3, etc?
Oh, and I'm using NxOgre 1.0 '22T5.
Thanks!
I'm working on a project and I want to apply a force to a body (a ball) picked by the mouse, the amount of force and the direction applied is given by the dragging of the mouse. The picking works ok but I can't make the ball go to the right direction.
I have this:
1. I pick the ball and set to true "mCalculatingBallPath":
bool MatchInputHandler::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if (id == OIS::MB_Left)
{
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mMatchCamera->getCamera()->getCameraToViewportRay( mousePos.d_x/float(arg.state.width), mousePos.d_y/float(arg.state.height) );
mRaySceneQuery->setRay(mouseRay);
mRaySceneQuery->setSortByDistance(true);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr;
for ( itr = result.begin(); itr != result.end() && !( itr->movable && itr->movable->getParentSceneNode()->getName() == mBall->getSceneNode()->getName() ); itr++ )
; // for: while not end and i'm not hitting the ball
if ( itr != result.end() && (itr->movable && itr->movable->getParentSceneNode()->getName() == mBall->getSceneNode()->getName()) ) // I clicked the ball
{
mCalculatingBallPath= true;
BallPath::getInstance()->initialize( mBall->getWorldPosition() );
}
else mCalculatingBallPath= false;
...
return true;
}
2. I update current Ball Path with the point of the ground I clicked:
bool MatchInputHandler::mouseMoved(const OIS::MouseEvent &arg)
{
...
// If we are dragging the left mouse button.
if (mLMouseDown)
{
if (mCalculatingBallPath)
{
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mMatchCamera->getCamera()->getCameraToViewportRay(mousePos.d_x/float(arg.state.width),mousePos.d_y/float(arg.state.height));
mRaySceneQuery->setRay(mouseRay);
mRaySceneQuery->setSortByDistance(true);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr;
for ( itr = result.begin(); itr != result.end(); itr++ )
{
if (itr->movable && itr->movable->getName() == "GroundEntity")
{
BallPath::getInstance()->update( mouseRay.getPoint (itr->distance) ); // Vector3 getPoint (Real t) const :Gets the position of a point t units along the ray.
}
}
}
...
return true;
}
3. Finally, I apply the force:
bool MatchInputHandler::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if (id == OIS::MB_Left)
{
if (mCalculatingBallPath)
{
if ( mPlayer->isAbleToShoot(mBall) ) // Shoot
{
mBall->getBody()->addForce( (BallPath::getInstance()->getForce() * BallPath::getInstance()->getDirection()).normalisedCopy() );
}
}
mCalculatingBallPath = false;
BallPath::getInstance()->clear(); // clears path data.
mLMouseDown = false;
}
...
return true;
}
4. The BallPath singleton have this methods, I think the problem(s) is(are) here on the calculations:
Vector3 getDirection() { return mDirection; }
Vector3 getForce() { return mLenght * mForceByMeter; }
void initialize (Vector3 ballPosition)
{
mDirection= mLenght= Vector3::ZERO;
mDeviation= ballPosition;
}
void update (Vector3 pos)
{
mLenght = (pos - mDeviation).normalisedCopy();
mDirection = ( mDirection + (pos - mDeviation) / 2 ).normalisedCopy();
}
void clear()
{
mDirection= mDeviation= mLenght= Vector3::ZERO;
}
private:
static BallPath bp;
Vector3 mDirection;
Vector3 mDeviation;
Vector3 mLenght;
int mForceByMeter;
BallPath()
{
mDirection= mDeviation= mLenght= Vector3::ZERO;
mForceByMeter= 1; // Temporal value
}
...
Maybe I should do all with NxOgre? Raycasting, use NxOgre::Vector3 instead of Ogre::Vector3, etc?
Oh, and I'm using NxOgre 1.0 '22T5.
Thanks!