Page 1 of 1
Simple question about parenting an object to a camera?
Posted: Mon Mar 11, 2024 8:07 pm
by wizard4
Hopefully CuteAlien is so busy that he/she won't be able to answer this. I'm worried that something so simple has to be asked.
Here is some code...
Code: Select all
IAnimatedMesh* mesh (...)
IAnimatedSceneNode* mesh_node = smgr->addAnimatedMeshSceneNode (...)
ICameraSceneNode* camera = addCameraSceneNode (...)
If I give positions in the arguments, I can place the camera looking at the mesh no problem. When I try to parent the camera to the mesh with
Code: Select all
camera->setParent(mesh);
camera->setPosition(vector3df(0.f, 500.f, 0.f));
Shouldn't the position of the camera be relative to the parent?
Wouldn't a positive Y axis make the camera 500.f above the mesh?
So far trying this the camera gets wedged into the mesh in the middle (from what I can tell) and points randomly outside it.
It's not doing what I expected.
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 12:26 am
by wizard4
Well this code works (in a new file)
Code: Select all
irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
camera->setPosition(irr::core::vector3df(200.f, 200.f, -200.f));
irr::scene::ISceneNode* empty_node = smgr->addEmptySceneNode(0, -1);
empty_node->setPosition(irr::core::vector3df(0.f, 0.f, 0.f));
camera->setParent(empty_node);
irr::scene::ISceneNodeAnimator* anim_cam = smgr->createRotationAnimator(irr::core::vector3df(0.f, 1.f, 0.f));
empty_node->addAnimator(anim_cam);
anim_cam->drop();
Even if I set the camera position before or after it works.
For the fuller code
Code: Select all
irr::s32 id_player = 11;
irr::scene::IAnimatedMesh* blitz_mesh = makeObject(smgr, "media/weights_bone_rot_only.b3d");
if (!blitz_mesh) { std::cout << "WARNING: Blitz model not loaded. Quitting... "; return 1; }
irr::scene::IAnimatedMeshSceneNode* blitz_mesh_node = smgr->addAnimatedMeshSceneNode(blitz_mesh, 0, id_player);
if (blitz_mesh_node)
{
blitz_mesh_node->setPosition(irr::core::vector3df(0.f, 50.f, 0.f));
blitz_mesh_node->setScale(irr::core::vector3df(100.f, 100.f, 100.f));
blitz_mesh_node->setAnimationSpeed(24.f);
blitz_mesh_node->setFrameLoop(0, 24);
blitz_mesh_node->setName("Player.name");
}
irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(blitz_mesh_node);
camera->setPosition(irr::core::vector3df(0.f, 500.f, 0.f));
I'll keep trying.
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 2:01 am
by Noiecity
Something similar happened to me, using addCameraSceneNode, I do not use parent, but calculate the position of the model directly in each frame and pass it to the camera, and when rotating up or down, the speed is apparently slowed down
I give the executable, it is a bit slow, but it is for the resolution of the textures and mainly because of the lighting (when using 3 lights and having addShadowVolumeSceneNode()), without addShadowVolumeSceneNode() and the 3 lights in my old laptop goes to 250 - 450 fps, and with 3 lights and addShadowVolumeSceneNode() goes to 30-60 fps
(I would give the code, but I hope to publish it once clean the spaghetti code)
https://www.mediafire.com/file/vdhe3mx2 ... e.rar/file
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 5:42 am
by Noiecity
I had come up with a solution using the square root to calculate the distance traveled between two points to calculate the angle, irrlicht had something called "reciprocal_squareroot" that calculated the square root less accurately but faster, ironically, I didn't use any of these in the end. I solved it by using getRotation().rotationToDirection() and getting the absolute value to always get the same movement speed, without using square root, I got a better performance, I hope to post the results soon, but for now it looks something like this:
Code: Select all
if (EventReceiver.IsKeyDown(KEY_KEY_W)) {
core::vector3df forward = entities[0].node->getRotation().rotationToDirection();
f32 forwardXY = abs_(0 - forward.X) + abs_(0 - forward.Z);
forward.X = ((MoveSpeed * deltaTime) / (forwardXY))*forward.X;
forward.Z = ((MoveSpeed * deltaTime) / (forwardXY))*forward.Z;
forward.Y = zeroF32;
entities[0].node->setPosition(entities[0].node->getPosition() + forward);
}
Months of struggling with the math to get smooth and fast responsive movement without having to use the damn slow square root
it is likely that >getRotation().rotationToDirection() uses square root (I haven't seen the source code, so I don't know), but adding more square roots in a nested way probably generated the bottleneck.
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 10:56 am
by CuteAlien
I wouldn't worry too much about speed of camera calculations. You don't run many of those, so it's unlikely to ever be a bottleneck.
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 3:41 pm
by Noiecity
CuteAlien wrote: Wed Mar 13, 2024 10:56 am
I wouldn't worry too much about speed of camera calculations. You don't run many of those, so it's unlikely to ever be a bottleneck.
I did it to understand how cameraNodeFPS works, at least I wanted to achieve something similar, and as I'm new to c++, understanding the irrlicht source code still seems too advanced.
Well, I did what I wanted anyway, I made more than 300 models of heads while I was thinking about a solution, now my next obstacle is to create an algorithm to calculate the speed of movement of the character, so that when the foot collides it moves forward, so that it looks a natural animation of movement, and not so linear, I still need to understand the acceleration and collisions.
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 4:24 pm
by CuteAlien
Ah, yes - mixture of animation and movement. Always a challenge. And can get super complicated once you have crazy stuff like walk into water and start swimming ;-)
Re: Simple question about parenting an object to a camera?
Posted: Wed Mar 13, 2024 8:29 pm
by kh_
FYI, in the first post you set Parent to a mesh, not the node as in second post. Something to look at.