3D mouse position [solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

3D mouse position [solved]

Post 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.
Last edited by Malebolge on Sun Sep 28, 2008 2:54 pm, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

Post 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).
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

Post 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.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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)... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Malebolge
Posts: 19
Joined: Sun Aug 31, 2008 8:24 pm

Post 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.
Post Reply