LightningNode

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

LightningNode

Post by sudi »

Header:

Code: Select all

/*
  Copyright (C) 2010 Daniel Sudmann

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Daniel Sudmann suddani@googlemail.com
*/
#ifndef CBOLTSCENENODE_H
#define CBOLTSCENENODE_H

#include <irrlicht.h>

namespace irr
{
    namespace scene
    {
        class CBoltSceneNode : public ISceneNode
        {
        public:
            CBoltSceneNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id);

            virtual ~CBoltSceneNode(void);

            virtual void OnRegisterSceneNode(void);

            virtual void OnAnimate(u32 timeMs);

            virtual void render(void);

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

            virtual u32 getMaterialCount() const;

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

            void setLine(core::vector3df start, core::vector3df end, u32 updateTime = 300, u32 height = 10, u32 parts = 10, u32 bolts = 1, bool steddyend = true, video::SColor color = video::SColor(255,255,0,0));
        protected:
            struct Bolt
            {
                u32 m_height;
                u32 m_updateTime;
                bool m_end;
                Bolt(u32 height, u32 updateTime, bool end)
                {
                    m_height = height;
                    m_updateTime = updateTime;
                    time = 0;
                    startTime = 0;
                    m_end = end;
                }
                irr::core::vector3df normal;
                core::array<f32> delta;
                core::array<irr::core::vector3df> points;
                u32 time;
                u32 startTime;
                void Update(u32 timeMs)
                {
                    time = timeMs;
                    if (startTime == 0)
                        startTime = timeMs;
                }
                void draw(video::IVideoDriver* driver, video::SColor color)
                {
                	u32 h = m_height*2;

                    for (u32 i=0;i<points.size()-1;i++)
                    {
                        driver->draw3DLine(points[i]+normal*delta[i], points[i+1]+normal*delta[i+1], color);

                        if (time-startTime >= m_updateTime)
                        {
                            if (i == 0)
                            {
                                delta[i] = 0.0f;
                            }
                            else
                            {
                                delta[i] = rand()%h;
                                delta[i] -= (f32)m_height;
                            }
                        }
                    }

                    if (!m_end && time-startTime >= m_updateTime)
                    {
                    	delta[delta.size()-1] = rand()%h;
						delta[delta.size()-1] -= (f32)m_height;
                    }

                    if (time-startTime >= m_updateTime)
						startTime = 0;
                }
            };

            // The beam material.
            video::SMaterial material;

            // The bolts
            core::array<Bolt> boltarray;

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

            core::vector3df m_start;
            core::vector3df m_end;
            u32 m_parts;
        private:
        };
    }
}
#endif // CBOLTSCENENODE_H
Source:

Code: Select all

/*
  Copyright (C) 2010 Daniel Sudmann

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Daniel Sudmann suddani@googlemail.com
*/
#include "CBoltSceneNode.h"
namespace irr
{
    namespace scene
    {
        CBoltSceneNode::CBoltSceneNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id) : ISceneNode( parent, mgr, id )
        {
            //ctor
            material.Wireframe = false;
            material.Lighting = false;
            AutomaticCullingState = EAC_FRUSTUM_SPHERE;
        }

        CBoltSceneNode::~CBoltSceneNode()
        {
            //dtor
        }

        void CBoltSceneNode::OnRegisterSceneNode(void)
        {
            if ( IsVisible )
            {
                SceneManager->registerNodeForRendering( this );
            }
        }

        void CBoltSceneNode::OnAnimate(u32 timeMs)
        {

            if ( IsVisible )
            {
                for (u32 i=0;i<boltarray.size();i++)
                {
                    boltarray[i].Update(timeMs);
                }
            }

            ISceneNode::OnAnimate(timeMs);
        }

        void CBoltSceneNode::setLine(core::vector3df start, core::vector3df end, u32 updateTime, u32 height, u32 parts, u32 bolts, bool steddyend, video::SColor color)
        {
        	setPosition(start);

            srand(parts);
            boltarray.clear();

            m_start = core::vector3df(0,0,0);
            m_end = end-start;
            m_parts = parts;
            material.DiffuseColor = color;

            core::vector3df dir = end-start;
            dir/=(f32)parts;

            for (u32 i=0;i<bolts;i++)
            {
                //printf("add Bolt\n");
                boltarray.push_back(Bolt(height, updateTime, steddyend));

                for (u32 a=0;a<parts;a++)
                {
                    //printf("add Part[%i] ", a);
                    boltarray[i].points.push_back(m_start+dir*a);
                    if (a == parts-1 || a == 0)
                        boltarray[i].delta.push_back(0.0f);
                    else
                    {
                        u32 h = height*2;
                        f32 d = (rand()%h);
                        d-=(f32)height;
                        //printf("Delta: %f", d);
                        boltarray[i].delta.push_back(d);
                    }
                    //printf("\n");
                }

                u32 vec = rand()%2;
				//printf("vec: %i\n", vec);

				u32 cord = rand()%10+1;

                if (dir.X != 0)
                {
                    if (vec == 0)
                        boltarray[i].normal = core::vector3df(-dir.Y/dir.X*cord,cord,0);
                    else
                        boltarray[i].normal = core::vector3df(-dir.Z/dir.X*cord,0,cord);
                }
                else if (dir.Y != 0)
                {
                    if (vec == 0)
                        boltarray[i].normal = core::vector3df(cord,-dir.X/dir.Y*cord,0);
                    else
                        boltarray[i].normal = core::vector3df(0,-dir.Z/dir.Y*cord,cord);
                }
                else if (dir.Z != 0)
                {
                    if (vec == 0)
                        boltarray[i].normal = core::vector3df(cord,0,-dir.X/dir.Z*cord);
                    else
                        boltarray[i].normal = core::vector3df(0,cord,-dir.Y/dir.Z*cord);
                }
                boltarray[i].normal.normalize();
            }
        }

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

            for (u32 i=0;i<boltarray.size();i++)
            {
                boltarray[i].draw(driver, material.DiffuseColor);
            }
        }

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

        u32 CBoltSceneNode::getMaterialCount() const
        {
            return 1;
        }

        video::SMaterial& CBoltSceneNode::getMaterial(u32 i)
        {
            return material;
        }
    }
}

Usage:

Code: Select all

irr::scene::CBoltSceneNode* beam = new irr::scene::CBoltSceneNode(smgr->getRootSceneNode(), smgr, -1);
	beam->setLine(irr::core::vector3df(0,0,0), irr::core::vector3df(100,100,100), 50, 10, 10, 3, false, irr::video::SColor(255,255,0,0));
	beam->drop();
Pic:
Image

PS: btw looks way better/cooler in action
Last edited by sudi on Sat Jun 26, 2010 3:18 pm, edited 3 times in total.
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.
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

Great! I'll try it. :D
There's something is fantastic, there's nothing is absolute.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

I think I am recalling this correctly, but don't scene nodes stay in the scene tree even after you drop them. If that is the case, then why would a person want a beam scene node to stay in there the whole time. For example, maybe someone just shot a laser gun or something.

At any rate, I don't think this should really be a scene node, but rather just some utility functions to aid in the drawing cool beams, and allows for the timed erasure, etc.

But it looks cool, so good job so far.
TheQuestion = 2B || !2B
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Halifax wrote:I think I am recalling this correctly, but don't scene nodes stay in the scene tree even after you drop them.
No, calling remove() will remove them from the scene graph and deallocate them. If you just drop() them then the node is not removed from its parent, even if it is deleted.
Halifax wrote:At any rate, I don't think this should really be a scene node, but rather just some utility functions to aid in the drawing cool beams, and allows for the timed erasure, etc.
Scene nodes have a relative position, relative rotation, relative scale, a bounding box, and are placed in a scene graph. Whatever it is, if it has these properties, then it should be a scene node.

BTW, timed erasure is handled by the delete animator.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Am i getting something wrong here?
It is a SceneNode isn't it??
Well ok the boundingBox is not calculated because i didn't it. But can be added without a problem.
Sry Halifax but could u explain why u r saying what u r saying^^
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.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

I played with some post processing but i kinda can't get it to really glow...

Image
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.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Ok some update:
Image
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.
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

my art lecturer told me the best way to create something realistic is by observing the same type of object in real world.

Let's take a look at light beam in our real world.

Image

Then we look at how other people did it.

Image

As you can see, the inner part of the beam is white/almost white in color, which makes it looks really bright if compare to the color toning of the surrounding. Hope this would help you to achieve better beam effect.

Keep the good work!
My company: https://kloena.com
My profile: https://zhieng.com
My co-working space: https://deskspace.info
My game engine: https://kemena3d.com
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

Thanks Sudi. Finally I've made some examples for them (Lighting and Beam)
Here's screeenshot :
Image
And this is a simple example :lol:
http://www.4shared.com/file/69998220/dc ... eNode.html
and this is a simple examples for BeamSceneNode :
http://www.4shared.com/file/69998405/9a ... eNode.html
There's something is fantastic, there's nothing is absolute.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Nice.
Could you also add a binary demo showing it in action please?

P.S
Looks pretty cool even as a screen-shot :wink: .
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

Sorry MasterGod ! I did'nt check my Zip file. I've reupload it (only BoltSceneNode, include source and Bin with Irrlicht-1.4)
Here the link :
http://www.4shared.com/file/69998220/dc ... eNode.html
There's something is fantastic, there's nothing is absolute.
Aelis440
Posts: 52
Joined: Sun Oct 05, 2008 8:45 pm

Post by Aelis440 »

Hello, I've been using your Scene Node in a project I'm working on and while it works quite nicely if I need a static lightning animation between two points, it does very little for instances in which I'm using it between moving targets. Something as simple as updateEndPoints(core::vector3df start, core::vector3df end) would be fantastic. I tried calling SetLine(), which moves it quite nicely, but causes me to lose the animations. If you have any suggestions or additions to the code, I'd be happy to hear them.

Thanks!
Scarabol
Posts: 167
Joined: Sat Jan 03, 2009 5:26 pm
Location: Aachen, Germany

Post by Scarabol »

Hey guys,

this SceneNode looks amazing, but i thing there is some space to improve this, but before doing this, I want to ask if someone did before and feels like sharing this result with me...

How can i manage to use this between moving SceneNodes?

Does anybody know where those fraktums come from (red circle)?
Image

Greetings
Scarabol
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

Hi Scarabol! This result because it is a collections of triangle, Lighting is made by many segments ( 4 point) , and connected them.(you can see in source :lol: )
I'm improving it! but not sure in recent.(I'm developing my game using it as Thunder Lighting)
There's something is fantastic, there's nothing is absolute.
Scarabol
Posts: 167
Joined: Sat Jan 03, 2009 5:26 pm
Location: Aachen, Germany

Post by Scarabol »

Hi,

could you give me a short advice in how you want to improve it? Maybe i could do?

How do i have to change code, to make it work with moving objekts?

Greetings
Scarabol
Post Reply