Draw a line 3d beginning at a mesh point.

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
vayou
Posts: 1
Joined: Fri May 06, 2016 10:46 am

Draw a line 3d beginning at a mesh point.

Post by vayou »

Hello,

I have an FPS camera, who have a child, the gun scene.
So the gun position is relative to the camera position (with little offsets):

Code: Select all

 
m_nodeGun->setScale(irr::core::vector3df(20.0f, 20.0f, 10.f));
m_nodeGun->setPosition(irr::core::vector3df(1.2f, -0.8f, 1.6f));
 
Image

I want to draw a line3d from the gun barrel to the wall (or others elements).
The system must work with other smaller or bigger weapons.

For you, what is the most efficient way to do this ?

Thanks a lot !
Have a good day :)
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Draw a line 3d beginning at a mesh point.

Post by CuteAlien »

Figure out the offset for each weapon in the editor and just write it down (in an xml or textilfe for example). You can get the start position then by transforming that offset by the absolute transformation matrix of your gun node. For the end-position you have to do a line-collision (with ISceneCollisionManager::getSceneNodeAndCollisionPointFromRay) with the line starting at the offset and then probably going along the current rotation of your player node.

For drawing you have different options. I would probably use a plane (2 triangles) for drawing. But if you want it to look like in the image with the start and end having same width in 2d then there's some additional work. You could for example draw in 2d (ISceneCollisionManager has some functions to get from 3d points to 2d points on the screen). Or you could calculate the necessary width at both points. I did that recently, so maybe helps (it's not 10ß0% correct as I return a single value while width/depth should be separate and not sure if thera are better ways to calculate this):

Code: Select all

 
// Return how many units are needed at pos to draw a pixel in 2d (return max of width or height as I want to ensure a pixel is visible)
irr::f32 getPixelSizeAtWorldPos(const irr::core::vector3df& pos, const irr::scene::SViewFrustum* frustum, const irr::core::dimension2du& screenDim)
{
    using namespace irr;
    using namespace core;
    using namespace scene;
 
    vector3df nearestPointLeftPlane, nearestPointRightPlane;
    frustum->planes[SViewFrustum::VF_LEFT_PLANE].getIntersectionWithLine( pos, frustum->planes[SViewFrustum::VF_LEFT_PLANE].Normal, nearestPointLeftPlane );
    frustum->planes[SViewFrustum::VF_RIGHT_PLANE].getIntersectionWithLine( pos, frustum->planes[SViewFrustum::VF_RIGHT_PLANE].Normal, nearestPointRightPlane );
 
    vector3df nearestPointTopPlane, nearestPointBottomPlane;
    frustum->planes[SViewFrustum::VF_TOP_PLANE].getIntersectionWithLine( pos, frustum->planes[SViewFrustum::VF_TOP_PLANE].Normal, nearestPointTopPlane );
    frustum->planes[SViewFrustum::VF_BOTTOM_PLANE].getIntersectionWithLine( pos, frustum->planes[SViewFrustum::VF_BOTTOM_PLANE].Normal, nearestPointBottomPlane );
 
    // width of frustum at the depth of the point we're interested in
    f32 frustumWidthUnits = (nearestPointRightPlane-nearestPointLeftPlane).getLength();
    f32 frustumHeightUnits = (nearestPointTopPlane-nearestPointBottomPlane).getLength();
    f32 unitsPixelWidth = frustumWidthUnits / (f32)screenDim.Width;
    f32 unitsPixelHeight = frustumHeightUnits / (f32)screenDim.Height;
 
    return core::max_(unitsPixelWidth, unitsPixelHeight);
}
 
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Draw a line 3d beginning at a mesh point.

Post by hendu »

Why all the trouble? Use draw3DLine and set the desired pixel thickness in the material.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Draw a line 3d beginning at a mesh point.

Post by CuteAlien »

Pixel thickness only works with OpenGL, D3D does not support it. And using a mesh also offers more features for experiments (as you have material then). But yeah, I also usually start with draw3DLine until it works before I switch to using a mesh.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply