Rotating IMeshSceneNode wrapping createPlaneMesh() output

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
etp4eva
Posts: 2
Joined: Sun Feb 11, 2018 5:27 pm

Rotating IMeshSceneNode wrapping createPlaneMesh() output

Post by etp4eva »

Hi there, I've been struggling with this problem all afternoon and need advice or a second pair of eyes to see where I'm going wrong.

I'm attempting to generate and render a forest of trees. Each trees have branches that radiate from the center of the tree at various angles and heights. But rotating (using setRotation()) either the IMeshSceneNode representing the branches or it's parents yields strange results.

To represent the branches I create an IMesh and a texture re-use like so:

Code: Select all

 
    f32 branch_length = 14.9f; // Height of the texture image but also length of the branch                             
 
    scene::IMesh* branch_plane = geometryCreator->createPlaneMesh(
        core::dimension2df(10.f, branch_length), // 10 is the width of the texture image and width of the branch         
        core::dimension2du(1, 1)                 // I only need 1x1 flat                                                 
        );
 
    irr::video::ITexture* branchTexture =
        driver->getTexture((exe_folder + "../media/branch.png").c_str());
 
I do a similar process to make a tree prototype. I then use the IMesh and ITexture to create a tree as an IMeshSceneNode.

To "attach" the branch to the tree I create an empty ISceneNode in the center of the trunk at the height that I want the branch to be. This branch node has the tree as its parent and would act as a pivot to simplify the future rotations I want to do. I then create an IMeshSceneNode for the branch in a similar way to how I created the tree. This branch mesh node has the branch node as its parent. I use setPosition() to the end of the branch coincide with the position of its parent branch node. Finally I try to rotate the parent node so that the branch bends downward... but nothing happens. See below for the important code doing what I describe above:

Code: Select all

 
        scene::ISceneNode* bnode = SceneManager->addEmptySceneNode(tree); // Create branch node to rotate branch around
 
        bnode->setPosition(
            core::vector3df(0, 1000, 0) // Move branch to desired height on tree in the center of the trunk
            );
 
        // Add branch branch node
        scene::IMeshSceneNode* branch = SceneManager->addMeshSceneNode(
            branch_plane,
            bnode
            );
 
        // Add texture, and set material properties etc... I've left this out because it isn't relevant
 
        branch->setPosition(
            core::vector3df(0.f, 0.f, branch_length/2.f) // Move branch out from the center of the tree
            );
 
        bnode->setRotation(core::vector3df(45.f, 0.f, 0.f)); // Rotate branch 45 degrees around the x-axis... or not
 
The result of the code above is that the branch moves out of the tree as expected but their is no rotational change in the orientation. If I rotate around the Z axis like this:

Code: Select all

 
bnode->setRotation(core::vector3df(0.f 0.f, 45.f)); 
 
The branch stretches! Rotating the tree node does produce the desired affect but it also rotates the entire tree. Rotating just the branch node has the same behaviour as rotating bnode.

Clearly I have missed something along the way. I've spent hours searching the forum but came up with nothing that addressed my issue. If anybody can provide advice to lead me on the right path I would be greatly appreciative.

Thanks very much!
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Rotating IMeshSceneNode wrapping createPlaneMesh() outpu

Post by CuteAlien »

Can you maybe post some quick example to reproduce it? I'm not seeing the problem from the code above, so only way would be to write an example myself. But then I'd probably miss the part that goes wrong.
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
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: Rotating IMeshSceneNode wrapping createPlaneMesh() outpu

Post by chronologicaldot »

Sounds like a camera issue to me. Based on your setup, it looks like rotating around the Z-axis is what you want to do. However, if your camera is in the wrong position (in this case, not directly overhead), you're going to end up with a weird stretching effect. The node is technically rotated correctly, but you're looking at it from an angle.

Try creating a static camera and setting it directly overhead of the tree. (Edit: By "overhead" I mean in the negative z position.)
Edit: Sorry I don't have an example for you. There are some examples for static cams in the examples folder of Irrlicht.
etp4eva
Posts: 2
Joined: Sun Feb 11, 2018 5:27 pm

Re: Rotating IMeshSceneNode wrapping createPlaneMesh() outpu

Post by etp4eva »

I figured out what was causing my issue. I had given the cylendir IMesh used for the tree trunk a width and depth of 1 and then scaled up the IMeshSceneNode using SetScale. The idea was to use this to give variety to the tree sizes but I think I will have to find a better way because when the scaling propgates to the child nodes (i.e. the branch joint node and the branch itself) it caused the strange rotation behaviour.

I'm not fully certain why it happened the way it did but I've now changed the size of the trunk IMesh and left the IMeshNode scale at its default and the problem is now solved. I think what I will do eventually is have either a custom or empty ISceneNode as the parent for the branch nodes and the trunk seperately so that they can all be moved/processed together but the scaling of the trunk won't effect the branches.

Thanks to the folks above who provided advice. Apologies for the delay in my response I only have time once or twice a week to work on this thing hence the frustration in spending a whole day fixing one bug!
Post Reply