Page 1 of 1

3D mouse position [solved]

Posted: Sun Sep 28, 2008 2:16 pm
by Malebolge
Hello again.

I need to shoot a proyectile from the player´s weapon towards the mouse. The game´s view is top-down and the player is always on the center of the screen and is always "looking" towards the mouse also.
I need that the player can shoot towards the aim (mouse).

I first thought in select a triangle from the ground as a reference, but they are bigger that the mouse and could affect accuracy.

I need a clue in how to get the mouse position in 3D, or other idea in how to face the problem from another angle, that im probably missing.

Posted: Sun Sep 28, 2008 2:26 pm
by Acki
if you look at tutorial 07.Collision you'll se it not only gets the triangle of the collision, but also the collision point, that would be more accurate then... ;)

but if your player is always in the center of the screen you also can use the screen coordinate instead, I think... ;)

Posted: Sun Sep 28, 2008 2:33 pm
by rogerborg
The general idea is to cast a ray through the mouse co-ordinates, and then collide it against something.

The "something" that you collide it with depends on your problem domain. It can be a plane, or a triangle selector. It sounds like you'll want to collide with the terrain, using a triangle selector, as shown here.

Code: Select all

if(eventReceiver.ButtonClicked)
        {
            const position2di clickPosition = device->getCursorControl()->getPosition();
            const line3df ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(clickPosition, smgr->getActiveCamera());

            vector3df desiredPosition;
            triangle3df outTriangle;

            if(smgr->getSceneCollisionManager()->getCollisionPoint(ray, terrainSelector, desiredPosition, outTriangle))
            {
                formation->DesiredPosition = desiredPosition;
            }
        } 
That will get you the position on the terrain. However, since it's unlikely that you actually want to shoot at the ground, I'd expect that you'll want to (e.g.) shift the aim point straight up, by (again, e.g.) the same height as the gun.

Posted: Sun Sep 28, 2008 2:39 pm
by Malebolge
Acki wrote:if you look at tutorial 07.Collision you'll se it not only gets the triangle of the collision, but also the collision point, that would be more accurate then... ;)

but if your player is always in the center of the screen you also can use the screen coordinate instead, I think... ;)
yes, i saw that but the example use 3d coords, the camera position and the camera target position.

Code: Select all

core::line3d<f32> line;
                line.start = camera->getPosition();
                line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

                core::vector3df intersection;
                core::triangle3df tri;
i thought too using the relative position of the mouse, but the player is in the middle, not the gun, which is going to be attach to the bone on the player´s hand(sorry, i should explain better the problem).

Posted: Sun Sep 28, 2008 2:42 pm
by Malebolge
rogerborg wrote:The general idea is to cast a ray through the mouse co-ordinates, and then collide it against something.

The "something" that you collide it with depends on your problem domain. It can be a plane, or a triangle selector. It sounds like you'll want to collide with the terrain, using a triangle selector, as shown here.

Code: Select all

if(eventReceiver.ButtonClicked)
        {
            const position2di clickPosition = device->getCursorControl()->getPosition();
            const line3df ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(clickPosition, smgr->getActiveCamera());

            vector3df desiredPosition;
            triangle3df outTriangle;

            if(smgr->getSceneCollisionManager()->getCollisionPoint(ray, terrainSelector, desiredPosition, outTriangle))
            {
                formation->DesiredPosition = desiredPosition;
            }
        } 
That will get you the position on the terrain. However, since it's unlikely that you actually want to shoot at the ground, I'd expect that you'll want to (e.g.) shift the aim point straight up, by (again, e.g.) the same height as the gun.
Thanks again, i´ll give it a try.

Posted: Sun Sep 28, 2008 2:48 pm
by Acki
Malebolge wrote:yes, i saw that but the example use 3d coords, the camera position and the camera target position.
but you need the 3d coords, don't you ???
also you need a camera, or from where you should get any coords otherwise ???
that the example only uses the cams target is just for easyness, it so just uses the center of the screen this way, but you can use any screen coordinate you want (like rogerborg showed)... ;)

Posted: Sun Sep 28, 2008 2:53 pm
by Malebolge
Acki wrote:
Malebolge wrote:yes, i saw that but the example use 3d coords, the camera position and the camera target position.
but you need the 3d coords, don't you ???
also you need a camera, or from where you should get any coords otherwise ???
that the example only uses the cams target is just for easyness, it so just uses the center of the screen this way, but you can use any screen coordinate you want (like rogerborg showed)... ;)
Yes, i just post that at the same time as rogerborg jeje. I see the answer clear now. Thanks to both.