majc
12-03-2008 23:08:15
Im doing a fps and i need to cast a ray from the center of the screen or from this point (385,285) where i have the crosshair, how i do that?
Thanks in advance!
Thanks in advance!
majc
12-03-2008 23:08:15
nullsquared
12-03-2008 23:32:33
Im doing a fps and i need to cast a ray from the center of the screen or from this point (385,285) where i have the crosshair, how i do that?
Thanks in advance!
// normalized screen coords in [0,1]
float x = 0.5, y = 0.5; // center of screen
Ogre::Ray ray = camera->getCameraToViewportRay(x, y);
// then just make your ogrenewt ray from
Ogre::Vector3 direction = ray.getDirection();
Ogre::Vector3 origin = ray.getOrigin();
// or if you need 2 points
Ogre::Vector3 point0 = origin;
Ogre::Vector3 point1 = origin + direction * lengthOfRay;
majc
12-03-2008 23:41:53
float x = 0.5, y = 0.5, rayDistance = 3000.0; // center of screen
Ogre::Ray ray = mCamera->getCameraToViewportRay(x, y);
Ogre::Vector3 direction = ray.getDirection();
Ogre::Vector3 origin = ray.getOrigin();
Ogre::Vector3 start = origin;
Ogre::Vector3 end = origin + direction * rayDistance;
if (mInputDevice->getMouseButton(0))
{
Vector3 pos;
Ogre::Quaternion orientation;
mPlayer->getBody()->getPositionOrientation(pos, orientation);
// normalized screen coords in [0,1]
OgreNewt::BasicRaycast* verticalRay = new OgreNewt::BasicRaycast(m_World,start,end);
OgreNewt::BasicRaycast::BasicRaycastInfo info;
info = verticalRay->getFirstHit();
if(info.mBody)
{
mEnemy->setHP(mEnemy->getHP()-10);
info.mBody->setVelocity(Vector3(50,0,50));
}
nullsquared
13-03-2008 00:26:55
Thx mate you are the best its working but something strange is happening, after i shoot some times the player body seems start walking with the same velocity the enemy im shooting even if i walk backwards the player body keeps walking int the enemy direction :s
float x = 0.5, y = 0.5, rayDistance = 3000.0; // center of screen
Ogre::Ray ray = mCamera->getCameraToViewportRay(x, y);
Ogre::Vector3 direction = ray.getDirection();
Ogre::Vector3 origin = ray.getOrigin();
Ogre::Vector3 start = origin;
Ogre::Vector3 end = origin + direction * rayDistance;
if (mInputDevice->getMouseButton(0))
{
Vector3 pos;
Ogre::Quaternion orientation;
mPlayer->getBody()->getPositionOrientation(pos, orientation);
// normalized screen coords in [0,1]
OgreNewt::BasicRaycast* verticalRay = new OgreNewt::BasicRaycast(m_World,start,end);
OgreNewt::BasicRaycast::BasicRaycastInfo info;
info = verticalRay->getFirstHit();
if(info.mBody)
{
mEnemy->setHP(mEnemy->getHP()-10);
info.mBody->setVelocity(Vector3(50,0,50));
}
Any clue about the problem?
majc
13-03-2008 00:28:39
majc
13-03-2008 01:26:20
nullsquared
13-03-2008 02:28:37
I think i found the problem im hitting my own body.
For some reason if i fire the ray below the middle of the screen it hits my own body but when i press f3 the player body it isnt in the ray direction, i dont know why it hits it anyway.
start += direction * 2;
end += direction * 2;
majc
13-03-2008 13:17:36
Ogre::Vector3 start = origin + direction * 10;
Ogre::Vector3 end = origin + direction * rayDistance;
nullsquared
13-03-2008 19:18:37
I tryed that and didnt work
Ogre::Vector3 start = origin + direction * 10;
Ogre::Vector3 end = origin + direction * rayDistance;
majc
13-03-2008 20:27:32