bafflingly repositioned Pivot 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
warped_realities
Posts: 4
Joined: Wed Dec 08, 2010 10:50 am

bafflingly repositioned Pivot point

Post by warped_realities »

Edited OP: Original below the LINE.
I have uncovered an unusual problem with the irrlicht engine in my current game code.

I have a scenenode for the player's ship in the game world. In order to make the camera follow the player's ship i had the ship's scene node as the parent when i added the camera to the scene manager.

However this has resulted in the ship scene node's point of pivot for rotation being the camera's position, not the ship's position. Why does it do this and how can i make the ship's pivot point once again be its own position and not the position of the camera without de-parenting the camera from the ship?


LINE.
Arbitrarily it seems irrlicht is repositioning the pivot point for a particular scene node a LONG way behind said node which results in the rotation being screwed up.

The node in question is the node for the player ship, which is supposed to swoop up and down when it moves up and down in response to the up and down arrow keys being pressed. it does do this rotation but in a way that is hardly noticeable as its pivot point is miles behind it.
(logic for player is in Player.cpp, creation is in Game::startgame())


This is gonna be a tricky one to solve i think as my code is somewhat developed and this problem has cropped up amidst it and i'm not sure how why or what i did to cause it. So i will be posting download link for the code in its entirety.

I am using MSVC 2008.

link is
http://www.mediafire.com/?j70gw2s32r7pt4q

Thanks in advance, i have no clue why this is occuring.
Last edited by warped_realities on Thu Dec 09, 2010 3:37 pm, edited 2 times in total.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: bafflingly repositioned Pivot point

Post by randomMesh »

This are 31 uncommented headers and source files... :roll:
A minimal compilable snippet which reproduces the problem would be better.
"Whoops..."
warped_realities
Posts: 4
Joined: Wed Dec 08, 2010 10:50 am

Post by warped_realities »

Okay. Fun stuff. I've managed to isolate the problem. But it raises a question i would like answered.

Why does adding a camera to the scene with a node as its parent (we'll call this node player) result in the player's pivot point being moved to the camera?
warped_realities
Posts: 4
Joined: Wed Dec 08, 2010 10:50 am

Post by warped_realities »

bump due to OP now being edited for coherency and asking a specific question.

Sorry for earlier (was stressed and not thinking right).
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Post by blAaarg »

If the camera is a child of the ship node, it's not just going to follow the ship node's position, but will also pick up the ship's rotation and therefore rotate around the ship, which would look a lot like it's the ship rotating around the camera, when in fact the reverse is happening. If the camera's target is set to the position of the ship node and/or the camera's rotation and target are bound, the camera can rotate around the ship all day long seeing the ship from the same angle while the ship's position stays the same and it's the camera does the orbiting.

There are several ways to make the camera (position) follow the ship (position) independent of rotation but I'm not sure which fits in best with your code. If the camera is not a child of the ship node, one way would be to take any change in ship position as a vector and add that vector to the camera's position each time. Another simple way would be to have a constant vector which--each frame--would be added to the ship's absolute position and then set as the camera position. (Again, no parenting, and you may need to call updateAbsolutePosition() on the camera, depending on your other code.)
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
warped_realities
Posts: 4
Joined: Wed Dec 08, 2010 10:50 am

Post by warped_realities »

BL, ordinarily you're right. Hell that's how stacks and scenegraphs are supposed to work unless i'm going mental. Hell even the 'this is a small piece of code to illustrate my problem' had sanity prevail there and acted like it was supposed to with the camera obeying the laws of 'how stuff is supposed to go'.

Sadly i'm going mental or my skylancer project is possessed by evil forces cause the ship in that is NOT rotating around its own pivot point but rotating around the camera. And yes i have verified this. i have a debug version that just spawns the ship model and rotates it.

Here is is a somewhat gutted version of skylancer that demonstrates the problem.
http://www.mediafire.com/?kk71d8yc07jxvym

And here just for kicks, dont bother downloading unless for comparison.
Is the completely sane code that i used to try and reproduce the error smallscale but instead it decided to embrace sanity.

http://www.mediafire.com/?trwe3zn4fs2ya44
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Post by blAaarg »

You're right. This is fun stuff! :wink:
I had a chance to look at your gutted version this evening and I have to say, I was half-right.
If the camera's target is set to the position of the ship node and/or the camera's rotation and target are bound, the camera can rotate around the ship all day long seeing the ship from the same angle
Don't listen to that blAaarg guy! He doesn't know what he's talking about. :D
While that's correct, neither of those things are happening in your code, now that I look at it. :oops:

Still though, some observations first:

There are only 2 places that you add a camera scene node. One is in startgame() and if you set a break point there, you'll see that that code does not get executed. The other is testship() and that's where all (most) of the action is. The "swordsman" ship gets set at the origin (absolute vector(0,0,0))

Code: Select all

//generate player
irr::scene::IMesh *mesh= m_smgr->getMesh("Mesh/SWORDSMAN.obj");
m_node = m_smgr->addMeshSceneNode(mesh);
and the camera gets set "forward" of the origin by a little bit while looking backward at a target way, way off in the distance.

Code: Select all

m_pcamera= m_smgr->addCameraSceneNode(m_node, core::vector3df(0,2,18), core::vector3df(0,-40,-300));  
(Interesting to note here, if it's not confusing, that if you change the parent [m_node] at the above line of code to NULL, you'll see the ship rotate around itself--completely independently--though that might not be the way in which you want it to.)

Sooo...the analogy I can best come up with to illustrate what's happening hopefully will be helpful.

You take a couple steps up and 18 steps forward until you reach a seat on a ferris wheel, then you turn around, sit down and train a camera you brought with you on a far off distant point 40 units to your right and 300 units away (let's just say, you're focused on the horizen, it's so far away).

They start up the ferris wheel and you notice the center axle (or fulcrum) of the ferris wheel is in your viewport with a model of something facing you attached to it,(a spaceship, maybe?). :) and it's visible in the viewport of your camera But you keep your camera diligently facing it's target on the horizen. So as the ferris wheel spins you under the center-point of it's rotation, the model rises up in your viewport, all the while facing you. Eventually, the ferris wheel carries you up the other side, over the top (going backwards) and back down towards the place you started--the whole time, with your camera pointed towards that horizen) and the ferris wheel axle (and its model) reappear in your viewport (from the bottom this time) and still facing you, it would seem, as if it had been doing so the whole time.
Your play back the video of this exciting scene and you can't help but think the center of the ferris wheel (and the model attached to it) was revolving around you the whole time. :?

If that doesn't illustrate the phenomenon enough, try adding these two lines to the constructor of Game()::Game()

Code: Select all

scene::IMeshSceneNode* cube = m_smgr->addCubeSceneNode(10,0,-1,core::vector3df(0,-3,-30) );	
cube->setMaterialTexture(0,m_backdrop);
It shows a quick and hacky cube (just beyond the swordsman ship) that doesn't have a parent and therefore isn't rotating around anything, though it looks like it's going in a circle. This just shows how the change of position of the camera makes it look like other things are doing the rotating, while the camera position is rotating around a (different) center point (Remember: the camera is a child of the ship so it rotates around its parent.) and the camera's set target point (off in the distance) is confounding the rotations of the camera we would expect by not moving.

I can think of a few solutions depending on the effect that you want but off-hand I'd say start with explicitly setting the camera target to the ship's absolute position so it will point towards it regardless of its parent's transformation. That should keep the camera pointed at the tail of the ship the whole time. But, since your backdrop is drawn 2-dimensionally it wouldn't look like anything's rotating until you add back other things from the scene.

Schwew! I spoke too much. :roll: Sorry
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
Post Reply