Echo Mesh

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
Helderash
Posts: 49
Joined: Sun Mar 25, 2007 8:41 pm

Echo Mesh

Post by Helderash »

Hey folks!

I'm currently working on some code to simulate the throwing of a ball when the left mouse is clicked. At the moment, when the left mouse button is clicked a sphere mesh is loaded, and attached to a node positioned at the camera position. It then moves off in the direction the camera is pointing as required. However, a second sphere mesh is also being created and remains stationary at the point the sphere was fired. My code dealing with this is as follows:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
	if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
	{
	    if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
		{
            IAnimatedMesh* sphereMesh = smgr->getMesh("sphere.3ds");
            sphereNode = smgr->addAnimatedMeshSceneNode(sphereMesh);
            sphereNode->setPosition(m_cam->getPosition());
            sphereNode->setMaterialFlag(EMF_LIGHTING, false);
            disp = m_cam->getTarget() - m_cam->getPosition();
        }
	}
	return false;
}
};
..and then each frame the following function is called:

Code: Select all

void updatePos()
{
    disp.setLength(initVel/driver->getFPS());
    spherePos = spherePos + disp;
    sphereNode->setPosition(spherePos);
}
Any idea what could be causing this? Any help is very much appreciated!
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

Try putting a time limit between fire times. My guess is you're making tons of balls, not just one extra one... one ball each frame.. and only the most recently created is moved.

Another way would be to have a boolean value and use it so that only one ball can be created at a time.
vi-wer
Posts: 93
Joined: Sun May 20, 2007 7:15 pm
Location: Germany
Contact:

Post by vi-wer »

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver() : bPressed(false) {};

bool bPressed;

virtual bool OnEvent(SEvent event)
{
   if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
   {
       if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN && !bPressed)
        {
            bPressed = true;
            IAnimatedMesh* sphereMesh = smgr->getMesh("sphere.3ds");
            sphereNode = smgr->addAnimatedMeshSceneNode(sphereMesh);
            sphereNode->setPosition(m_cam->getPosition());
            sphereNode->setMaterialFlag(EMF_LIGHTING, false);
            disp = m_cam->getTarget() - m_cam->getPosition();
        }
        if( event.MouseInput.Event ==  EMIE_LMOUSE_LEFT_UP)
        {
            bPressed = false;
        }
   }
   return false;
}
};
Helderash
Posts: 49
Joined: Sun Mar 25, 2007 8:41 pm

Post by Helderash »

That worked perfectly, thankyou very much!

The only other problem I'm noticing is that if I fire a ball soon after the application starts (within the first second or so) it seems to fly about randomly at extremely high speeds before dissapearing. If I wait a few moments though (or fire a second ball off) it acts as expected.

I think I'll probably just ensure a ball can't be fired for a couple of seconds after application start to solve this issue, rather than go hunting for the problem.
Post Reply