Beam scenenode

A forum to store posts deemed exceptionally wise and useful
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Are you bleeping serious? You'll have to call setStartPoint() with your ships's position (from getAbsolutePosition()), and setEndPoint() with where you want it to end.

If you ask how you calculate the end point, I'm coming for you with a cheese grater and a jar of battery acid.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Post by torleif »

empirepro wrote:How can I get this to shoot from my ship instead of a set location?
instead of

Code: Select all

beam->SetStartPoint( core::vector3df( -500, 0, 0 ) ); 
use

Code: Select all

beam->SetStartPoint( shipMesh->getPosition()); 
phoboss
Posts: 4
Joined: Sat Aug 30, 2008 11:21 am

Post by phoboss »

When the camera is too near to the beam node, it disappears. Why is it like that?! I'm writing a space shooter and I don't see the lasers I'm shooting :?
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Here is my BeamSceneNode. Works better then the one showed at the beginning.

Header:

Code: Select all

#ifndef CBEAMSCENENODE_H
#define CBEAMSCENENODE_H
#include <irrlicht.h>
namespace irr
{
    namespace scene
    {
        class CBeamNode : public ISceneNode
        {
        public:
        private:
            // The beam material.
            video::SMaterial material;
            video::SMaterial material2;

            // Bounding Box
            core::aabbox3d<f32> Box;

            core::vector3df m_start;
            core::vector3df m_end;
            f32 m_thickness;

        public:

            CBeamNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id, char* szBeam , char* szBeamFront );

            ~CBeamNode(void);

            virtual void OnRegisterSceneNode(void);

            virtual void OnAnimate(u32 timeMs);

            virtual void render(void);

            virtual const core::aabbox3d<f32>& getBoundingBox() const;

            virtual u32 getMaterialCount();

            virtual video::SMaterial& getMaterial(u32 i);

            void setLine(core::vector3df start, core::vector3df end, f32 thickness);
        };
    }
}
#endif
Source:

Code: Select all

#include "CBeamSceneNode.h"
namespace irr
{
    namespace scene
    {
        CBeamNode::CBeamNode( ISceneNode* parent, ISceneManager *mgr, s32 id, char* szBeam , char* szBeamFront ) : ISceneNode( parent, mgr, id )
        {
            // Setup the beam material
            material.Wireframe = false;
            material.Lighting = false;
            //material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
            //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
            material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
            material.Textures[0] = mgr->getVideoDriver( )->getTexture( szBeam );
            //material.Textures[1] = mgr->getVideoDriver( )->getTexture( szBeam );

            material2.Wireframe = false;
            material2.Lighting = false;
            //material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
            //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
            material2.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
            material2.Textures[0] = mgr->getVideoDriver( )->getTexture( szBeamFront );

            m_thickness = 1.0f;
        }

        CBeamNode::~CBeamNode(void)
        {

        }

        void CBeamNode::OnRegisterSceneNode(void)
        {
            ISceneNode::OnRegisterSceneNode();
        }

        void CBeamNode::OnAnimate(u32 timeMs)
        {

            if ( IsVisible )
            {
                SceneManager->registerNodeForRendering( this );
            }

            ISceneNode::OnAnimate(timeMs);
        }

        void CBeamNode::setLine(core::vector3df start, core::vector3df end, f32 thickness)
        {
            m_start = start;
            m_end = end;
            m_thickness = thickness;
        }

        void CBeamNode::render(void)
        {
            video::IVideoDriver* driver = SceneManager->getVideoDriver();
            driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);

            core::vector3df direction = m_end-m_start;
            direction.normalize();
            core::vector3df savesatrt = m_start;
            AbsoluteTransformation.transformVect(savesatrt);
            core::vector3df lookdir = savesatrt-SceneManager->getActiveCamera()->getAbsolutePosition();
            if(lookdir.getLength() < 20)
                return;
            lookdir.normalize();

            core::vector3df view(SceneManager->getActiveCamera()->getTarget() - SceneManager->getActiveCamera()->getAbsolutePosition());
            view.normalize();

            if(lookdir.dotProduct(direction)/(lookdir.getLength()*direction.getLength()) < .98f && lookdir.dotProduct(direction)/(lookdir.getLength()*direction.getLength()) > -.98f)
            {
                core::vector3df updown = direction.crossProduct(lookdir);
                updown.normalize();
                core::vector3df normal = direction.crossProduct(updown);
                video::S3DVertex vertices[4];
                u16 indices[] = {0,1,2,1,3,2};

                vertices[0] = video::S3DVertex(m_end-updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(0,0));
                vertices[1] = video::S3DVertex(m_end+updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(0,1));
                vertices[2] = video::S3DVertex(m_start-updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(1,0));
                vertices[3] = video::S3DVertex(m_start+updown*m_thickness*0.5f, normal, video::SColor(255,255,255,255), core::vector2d<f32>(1,1));

                driver->setMaterial(material);
                driver->drawIndexedTriangleList(&vertices[0], 4, &indices[0], 2);
                //driver->draw3DLine(m_start, m_end, video::SColor(255,255,0,0));
            }
            else
            {
                //printf("daw dot\n");

                core::vector3df horizontal = SceneManager->getActiveCamera()->getUpVector().crossProduct(view);
                horizontal.normalize();
                horizontal *= 0.5f * m_thickness;

                core::vector3df vertical = horizontal.crossProduct(view);
                vertical.normalize();
                vertical *= 0.5f * m_thickness;
                view *= -1.0f;

                video::S3DVertex vertices[4];
                u16 indices[] = {0,1,2,1,3,2};

                vertices[0] = video::S3DVertex(m_end - horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,0));
                vertices[1] = video::S3DVertex(m_end + horizontal - vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(0,1));
                vertices[2] = video::S3DVertex(m_end - horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,0));
                vertices[3] = video::S3DVertex(m_end + horizontal + vertical, view, video::SColor(255,255,255,255), core::vector2d<f32>(1,1));

                driver->setMaterial(material2);
                driver->drawIndexedTriangleList(&vertices[0], 4, &indices[0], 2);
            }
        }

        const core::aabbox3d<f32>& CBeamNode::getBoundingBox() const
        {
            return Box;
        }

        u32 CBeamNode::getMaterialCount()
        {
            return 2;
        }

        video::SMaterial& CBeamNode::getMaterial(u32 i)
        {
            if(i==0)
                return material;
            else if(i==1)
                return material2;
        }
    }
}
Usage:

Code: Select all

scene::CBeamSceneNode* beam = new scene::CBeamNode(parent, smgr, -1, "The texture the beam should have when looking at it from the side , "The texture when looking at the cap" );

//now set the beam
beam->setLine(core::vector3df(0,0,0), core::vector3df(10,10,10), 5.0f);

beam->drop();
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
phoboss
Posts: 4
Joined: Sat Aug 30, 2008 11:21 am

Post by phoboss »

Thanks Sudi, it works better, but I still see the beams rarely.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

maybe u could show some code and tell us what exactly is not working.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Pennies to pounds he's shooting beams directly away from the camera position.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

ahh ok that should work with my beamscenenode when u set the start position a litle bit infront of the camera (farther then the near clipping plane) then u should see a dot bc u can only se th cap of the beam.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sudi wrote:ahh ok that should work with my beamscenenode when u set the start position a litle bit infront of the camera (farther then the near clipping plane) then u should see a dot bc u can only se th cap of the beam.
Oh, I've just taken a good look at your code. That's a nice way of doing it: much better than cross polys. Thanks for sharing.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

no problem thats what the forum is for :D
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
phoboss
Posts: 4
Joined: Sat Aug 30, 2008 11:21 am

Post by phoboss »

Okay, it works now. I had to exchange a custom scene node for a mesh scene node. :)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sudi wrote:no problem thats what the forum is for :D
Yes, but like a comic at a charity gig, you don't generally give away your A material. ;)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
nander
Posts: 6
Joined: Fri Feb 26, 2010 6:35 pm

Post by nander »

Hi,

It may be a noobish question, but I'm getting this error:

#LinkToDir#/untitled folder/beam.cpp|15|error: ‘class irr::video::SMaterial’ has no member named ‘Textures’|.
How do I solve this issue?
Is it caused by a change in syntax for 1.7?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, this was a change in Irrlicht 1.4.
freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Post by freetimecoder »

Use
SMaterial::TextureLayer[]::Texture instead.

e.g. myMaterial.TextureLayer[0].Texture

greetings
Post Reply