Cast a ray from the center of the screen

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!

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!

http://www.ogre3d.org/docs/api/html/cla ... 1Cameraa45


// 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

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?

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?

Hm. Sorry, I don't see anything in the above code that would cause that :|

majc

13-03-2008 00:28:39

Neither do i :s its a strange thing.

majc

13-03-2008 01:26:20

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.

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.

Yup, that's a possibility. Try to move the ray a bit away from the origin, like:

start += direction * 2;
end += direction * 2;

This way it won't start right where the camera is, but more in front of it.

majc

13-03-2008 13:17:36

I tryed that and didnt work :(

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;

You need to move the end position the same way as the origin.

Also, just move it until its out of your player/character that you don't want to be hit. And, one more thing - I don't use OgreNewt, but maybe its ray casting functionality allows some kind of "ignore" function? Or if you can override the (pre-)filter, just use that to ignore your main object.

majc

13-03-2008 20:27:32

Ok mate you are five stars :) I will try it.
The problem of the player walks with the same velocuty of the enemy when i fired the weapon was i was firing against the floor and that way i was giving velocity to the floor that is a static object.
The function you are talking about is this userPreFilterCallback().
By the way do you know how to turn my mesh in the same direction of my applied force?