Moving some meshes...

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
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Moving some meshes...

Post by theduck »

Well, I've got this little problem that I've had for a week at least trying to solve... And I've searched and asked and emailed and IRC'ed and its still not solved.

I got this FPSCamera (the player) and this animated MD2 mesh (the enemy). I want the enemy to rotate toward the player, then move forward towards it (Mesh move towards camera).


This is the main loop code (By the way, I'm using my own little interface to Irrlicht, but it shouldn't be too hard to figure out ;)):

Code: Select all

//main loop
        while(Running())
        {
                vector3df v = getTargetAngle(aiObj.node->getPosition(),camera.camera->getPosition());  //get angle between camera and mesh
                vector3df aiv = aiObj.node->getPosition();
                                vector3df civ = camera.camera->getPosition();
                                //aiObj.node->setRotation(v);
                //printf("aiobjxyz = %f,%f,%f\n",aiv.X,aiv.Y,aiv.Z);
                //printf("camxyz = %f,%f,%f\n",civ.X,civ.Y,civ.Z);
 
                vector3df v2 = vector3df(0.3,0.0,0.0);
                v2.rotateXZBy(0.1,v);
                aiObj.node->setPosition(aiObj.node->getPosition()+v2);
 
                //then setPosition(getPosition+ newvector)
 
                render();
        }
        //quit and close down
        end();

I'm not quite sure on the logic I need to do this. So once again, I just gotta get the MD2 model to rotate towards me, the FPSCamera, and move towards it.

Thanks in advance. :)
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

I really love polar coordinates so I can suggest a polar coordinate related suggestion for ya :)

Pretty much for your enemy you need to define some point to represent the point at which your enemy is looking when his rotation (theta) is 0. R can represent the distance to that point.

Now suppose you get a different position to look at like your character. You get a vector to the character from the enemy and normalize() it. Then multiply this by the R (distance that your character usually looks at). Using sin and cosine you can get what angle your new target (character) is compared to your 0 rotation.

Then you can simply rotate your character and add some multiple of your target (character's) position to your enemy position.

Hope this makes sense :)
Guest

Post by Guest »

Hmmmm...

Well, unfortunatly for me, I realllly suck at 3D math.. :S
So, I'm not quite sure what you're saying..

Think you could put a 'lil bit of example code for me? 'Cos I'm really confused...
Thanks in advance. :)
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Post by theduck »

[Forgot to login... Oops...]
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Post by theduck »

*Bump!* Anyone?
/me is still confused... :cry:
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Post by theduck »

*Bump!* Anyone?
/me is still confused... :cry:
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

Well I suggest just googling polar coordinates...its very very simple :)

Tell me if you need any more help. I dont really have any example code that I am using in my program right now (and its kinda late for me to write some more :( Sorry!).
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Post by theduck »

Okee dokee, I shall look it up.
theduck
Posts: 29
Joined: Thu Mar 03, 2005 4:59 am

Post by theduck »

Arrrgh, I'm really confused by this... I researched and read, but no joy. :(

My 3D maths skills are well... Lets just say a tad bit over par, maybe like a bogey. Or a double bogey. :S Less than stellar. ;)


I can get the mesh to rotate towards the camera, but now all I need to do is actually move it... When I try to move it, I have no luck. :(



Also:
Do I want to rotate the mesh, then move it by a vector, or rotate the vector and then move the mesh with the vector? 0.o
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

Well lemme give you a quickie tutorial on polar coordinates.

Pretty much you start at the origin and the coordinate system is defined in terms of (r, theta) instead of (x,y).

Suppose you choose a point away from the origin. Draw a vector to it.

r would be the length of the vector and theta would be the angle between the vector and the x axis.

Converting from (x,y) to (r,theta) is quite easy.

x = r cos theta
y = r sin theta
r = sqrt(x^2 + y^2)
theta = arctan(y/x)

=============================================
So now to see how this fits your scenario.

Damn, I just thought up an easier way to do it.

Well why not just get a vector pointing to the enemy from your character. Then taking the negative of that vector. Normalize it and add some multiple of it (speed) to your enemy's position? I think that should work.

If you already got the enemy to face the camera, then you are already set :)
Post Reply