2

I am creating a spaceship game in which you will be able to fire a laser from their ship. Basically I want to create a ray from the players ship to the cursor position. The player can move around but the camera is static. So far I've tried using:

Ray laser = mCamera->getCameraToViewportRay(mMouse->getMouseState().X.abs, mMouse->getMouseState().Y.abs);

and setting:

laser.setOrigin->(mPlayer->getPosition);

However every time I execute the ray scene query, it fires towards the top left corner of my screen. I am using the code here as a reference to how to derive the screen co-ordinates: http://www.ogre3d.org/forums/viewtopic.php?f=5&t=49132

A quick side question for extra credit: Is there a way of only drawing ManualObject for a small amount of time to simulate a shot from a laser gun? I've already tried to draw a small portion of the ray using the following snippet:

Ogre::ManualObject* lazor = mSceneMgr->createManualObject("lazor");
lazor->begin("HiliteYellow", Ogre::RenderOperation::OT_LINE_LIST);
// define start and end point
for (int i = 0; i< 20000;i++)
{
    lazor->position(laser.getPoint(30+i));
    lazor->position(laser.getPoint(300+i));
}
lazor->end();

mSceneMgr->getRootSceneNode()->attachObject(lazor);

Thanks!

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
Seán
  • 21
  • 1

2 Answers2

0

If you've installed from source, or have the SDK, I'd recommend checking out SdkTrays.h - specifically, screenToScene, sceneToScreen, and getCursorRay.

HTH

Jeremy Sandell
  • 435
  • 4
  • 5
0

The camera to viewport ray starts at your camera's position and goes through the location you clicked in your world.

If one of the three axis coordinates are the same for all your objects (all on a same plain, 2d), you can use the camera to viewport ray to determine the point where the ray intersects the plain. Then you can draw the laser from your ship to that point.

You could also use the ray to get the intersection point of the object you targeted with your cursor. That would work with a 2d and a 3d representation. Again you would draw the laser from your ship to that point.

How to use such a ray query is explained in detail here: http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Intermediate+Tutorial+3