Moving 3d object with camera

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
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Moving 3d object with camera

Post by kaka »

So in my game i have used the bullet physics engine and i'm using the wrapper irrbullet but thats not the problem. that works perfectly fine. What i want to be able to do is move those objects in the direction of my camera. This is what i've done so far

Code: Select all

 
void level::playerIneraction(myEventReceiver r, IKinematicCharacterController *character, irr::scene::ICameraSceneNode *camera){
 
        for(irr::core::list<IRigidBody *>::Iterator Iterator = objects.begin(); Iterator != objects.end(); Iterator++){
 
                IRigidBody *object = *Iterator;
 
                object->setGravity(irr::core::vector3df(0,-10,0));
                irr::core::vector3df pos = object->getWorldTransform().getTranslation();
                irr::core::vector3df posChar = character->getWorldTransform().getTranslation();
                irr::core::vector3df rotChar = object->getWorldTransform().getRotationDegrees();
 
                irr::f32 length = posChar.getDistanceFrom(pos);
                
                if(r.isKeyDown(irr::KEY_KEY_E)){
                        if(length <= 40.f){
                                std::cout<<"can pick up"<<std::endl;
                                object->setGravity(irr::core::vector3df(0));
                                irr::core::matrix4 mat;
                                mat.setTranslation(irr::core::vector3df(posChar.X + 30,posChar.Y,posChar.Z)); 
                                object->setWorldTransform(mat);
                        }
                        else if(length >= 40.f){
                                std::cout<<"can't pick up"<<std::endl;
                        }
                }
 
        }
}
 
i have a feeling that i'm doing this wrong and that i am supposed to use a mouse event receiver to move the objects?
CuteAlien
Admin
Posts: 9670
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Moving 3d object with camera

Post by CuteAlien »

Always tell as well what is actually happening and what you expect to happen.

But one hint - if you want to move in camera direction you should probably at least use the camera transformation. Or is character and camera already the same?

Anyway, before we can help you, please clarify what you mean with "move those objects in the direction of my camera". Is the camera at some other place and you want move objects toward it with your character? Or is the camera same as character and you want to pull objects toward you? Or do you mean you want to push objects in your current view direction? Or do you want to push them in the direction they have relative to the camera independent of where the camera is looking at?
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
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

oops my bad. ok i'll try to explain here goes.

my game is a first person shooter, so what i've done is created a IkinematicCharacterController and attached the camera to it and put the function in the game loop.

Code: Select all

 
void player::attachCameraToPlayer(){
        camera->setPosition(character->getWorldTransform().getTranslation()+irr::core::vector3df(0,20,0));
        irr::core::vector3df movement(directionX,0.0f,directionZ);
        irr::core::matrix4 m;
        m.setRotationDegrees(irr::core::vector3df(0,camera->getRotation().Y-180.0f,0));
        m.transformVect(movement);
 
        character->setPositionIncrementPerSimulatorStep(movement*1.f);
}
 
Yes i want to be able to move objects towards me and then be able to move the object in the direction of my camera. Just like how you do in the game portal. so for example i walk towards a box and when i press "e" depending on far i am from the box i can pick it up and move it about. i can get the distance from the object and my character perfectly fine. the code is in the first post

i hope that makes sense
CuteAlien
Admin
Posts: 9670
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Moving 3d object with camera

Post by CuteAlien »

*sigh* I'm probably the last person in the world who has never played Portal. But I guess it's about finding the direction vector between target and yourself. Just do "pos-posChar" and you have a vector pointing in the direction of posChar (or other way round and it will point from pos towards posChar). Normalize that vector (so it has lenght 1) and multiply it by the step-size you want to move your object.

To move it away like the character (or the camera) is looking start with a vector pointing in the direction in which your character originally shows when imported - basically the side where the nose is on start. For example (0,1,0) (this really depends on which direction you are using for "forward"). Then you get the transformation with getWorldTransform and use the rotateVect function of the matrix to rotate that vector in the same direction. Then add the rotated vector to your object to move it away (and again you can multiply it by some stepsize, usually also by a time-delta to ensure you have the same speed on all systems).
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
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

oh man i have no idea what you wrote in the second paragraph. So this is what i have done so far:

Code: Select all

 
irr::core::vector3df direction(pos-posChar);
direction.normalize() * 2;
 
so now i have the direction and the length.
To move it away like the character (or the camera) is looking start with a vector pointing in the direction in which your character originally shows when imported - basically the side where the nose is on start. For example (0,1,0) (this really depends on which direction you are using for "forward")
my character moves in the Z axis so does that mean its (1,0,0)? i output the rotation of my character on the command box and it says "0,-0,0" so do i need to rotate my character as well when i rotate my camera or should i just use the rotation of my camera?
Then you get the transformation with getWorldTransform and use the rotateVect function of the matrix to rotate that vector in the same direction. Then add the rotated vector to your object to move it away (and again you can multiply it by some stepsize, usually also by a time-delta to ensure you have the same speed on all systems).
I am so confused with what you have said but do you mean something like this

Code: Select all

 
irr::core::matrix4 mat;
                                
        irr::core::vector3df direction(pos-posChar);
        direction.normalize() * 2;
        mat.rotateVect((character->getWorldTransform().getRotationDegrees())*DeltaTime);
        mat.setTranslation(direction);
        object->setWorldTransform(mat);
 
i havent implemented that code yet. im travelling right now but when i get home i'll just mess about with it
CuteAlien
Admin
Posts: 9670
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Moving 3d object with camera

Post by CuteAlien »

I don't know how much you know about vectors - but for this case just think of them as arrows pointing from one point to another (that gives you a direction and a length). Getting your "direction" which is just an arrow from the object to your character (or camera) is easy (subtraction works as both points are given relative to (0,0,0) so if you subtract them you get just the small way between them).

For the other case - moving stuff away from the character or camera you need again one arrow pointing away from you and then you have to rotate it so it's identical to the camera-direction. The first part is probably what confuses you - which vector to use before the rotation. That vector is not your "direction" vector (that one is already rotated). But it's your base vector which is just 1,0,0 or 0,1,0 or 0,0,1. Mathematically it does not matter - you can use any of them. But you want probably to use the one that ensures the nose of your model is in the front and the ass in the back :-) If you don't know how your models are exported (depends entirely on your artist and modeling tool) just try all 3 (well 0,1,0 is unlikely, so probably one of the other 2).
So it's for example (not tested):

Code: Select all

 
 irr::core::vector3df base(0,0,1); // or (1,0,0)
// don't know your character-class, but you probably have an irrlicht-node in there
// You can also use the camera instead, but make sure bindTargetAndRotation is set to true then
 irr::core::matrix4 mat = character->node->getAbsoluteTransformation(); // this matrix has the rotation of your node (player or cam)
 mat.rotateVect(base); // rotate your base vector by the same rotation as the camera was rotated
irr::core::vector3df moveStep =  base * stepSize * deltaTime; // give it a nice length
object->node->setPosition( object->node->getPosition() + moveStep ); // move it by one step
 

edit: Hm, wait a second... for your case you can have it way easier. No matrices needed. You can get the direction vector for camera simply by:

Code: Select all

 
cam->getTarget() - cam->getAbsolutePosition();
 
You can then use this vector - normalize it (normalizing just sets the length to 1 so you know how long your step will be) and multiply by some stepsize and deltaTime. And then add it to your object position.
Although it won't hurt learning about matrices as well, sooner or later you will also need them :-)
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
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

i know only the basics so not a lot lol

ok so tried what you said. we are close but i get really weird results.
so first i did it your way

Code: Select all

 
if(r.isKeyDown(irr::KEY_KEY_E)){
                        if(length <= 40.f){
                                std::cout<<"can pick up"<<std::endl;
                                object->setGravity(irr::core::vector3df(0));
 
                                irr::core::matrix4 mat;
                                irr::core::vector3df base(0,0,1);
                                irr::core::vector3df direction(posChar-pos);
                                
                                camera->bindTargetAndRotation(true);
 
                                mat = camera->getAbsoluteTransformation();
                                mat.rotateVect(base);
 
                                irr::core::vector3df moveStep = base * (direction.normalize() *80) * DeltaTime;
                                
                                object->getCollisionShape()->getSceneNode()->setPosition(object->getCollisionShape()->getSceneNode()->getPosition() + moveStep);
 
                                
                                object->setWorldTransform(mat);
 
                        }
 
this gave me a very weird outcome. i could control the box however. moving the mouse left moved the node right and vice versa same with moving it up and down. so then i changed the base vector to (0,0,-1) that fixed with moving the node up and down but moving it left and right were still mixed up. the other problems were that if i moved it up and down the node went over my head and behind me. the other problem is the node seemed to be attracted to my camera. it would try to come towards me but quickly go back to where its supposed to be. it made it look like the node was on speed and was very jittery and when i let it go it would fly/appear towards me

so then i started messing about with the code and it worked perfectly! i could control the box exactly how i wanted. it would follow my mouse cursor perfectly! all i did was not use the normalising for the move step and changed it to this

Code: Select all

 
irr::core::vector3df moveStep = base * 2 * DeltaTime;
 
of course my problem wasn't solved that easily. the same problem occurred as before. the scene node i was controlling was attracted to me. it was all jittery and trying to escape and appear where i was. do you know why it was doing that?
CuteAlien
Admin
Posts: 9670
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Moving 3d object with camera

Post by CuteAlien »

Yeah - you multiply base*direction. That makes not much sense unfortunately. I'm not even sure what you are trying there - pulling towards you and pushing away at the same time? I thought that's 2 different modes which have nothing to do with each other.

direction: Vector from object to you.
base: Vector away from you in the direction in which you are looking.

Also note that my solution is basically for a still-standing camera. While the camera itself moves around... hm, could be this is getting more complicated. Would have to code that myself to figure that out (and sorry, I don't have that much spare-time).
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
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

Thanks anyway. i sort of managed to make it work. I can move objects..sort of. its good enough so its okay. Thanks for your help mate

BTW please go and play portal :lol:
Rusk
Posts: 7
Joined: Fri Feb 03, 2012 1:51 am

Re: Moving 3d object with camera

Post by Rusk »

It sounds like all you want to do is to have the object float in front of you, and move along as you move? Like you are carrying it?
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

Rusk wrote:It sounds like all you want to do is to have the object float in front of you, and move along as you move? Like you are carrying it?
yes!!!!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Moving 3d object with camera

Post by hybrid »

Such as positioning the object as a child and having it automatically moved along? Please check the examples (the q3 viewer example with the gun), if that's the right one.
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

hybrid wrote:Such as positioning the object as a child and having it automatically moved along? Please check the examples (the q3 viewer example with the gun), if that's the right one.
sorry but what example are you talking about?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Moving 3d object with camera

Post by hybrid »

The quake explorer has a weapon attached to the player.
kaka
Posts: 12
Joined: Thu Mar 15, 2012 9:08 pm

Re: Moving 3d object with camera

Post by kaka »

i've tried that and what happens is the box always goes to the left hand side of the camera and stays there even if i try to change the position of the box. i also cant let go of the box when i release the "e" button. i put in this if statement

Code: Select all

else if(!r.isKeyDown(irr::KEY_KEY_E)){
                camera->removeChild(object->getCollisionShape()->getSceneNode());
Post Reply