setTarget for a animetedmesh how to?

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
smajla
Posts: 6
Joined: Mon Jan 18, 2010 2:47 pm

setTarget for a animetedmesh how to?

Post by smajla »

I'm thinkink how to do somthing like that: i have an animated mesh and it is a pointer(model) and i have another animated mesh. How to do that all the time the pointer model will be looking at the model?

Something like a setTarget for a cemera?
Any ideas?
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: setTarget for a animetedmesh how to?

Post by randomMesh »

Something like:

Code: Select all

irr::core::vector3df turn_to_target(const irr::core::vector3df& pos, const irr::core::vector3df& target)
{
	core::matrix4 m, m2;
	m.buildCameraLookAtMatrixLH(pos, target, irr::core::vector3df(0.0f, 1.0f, 0.0f));

	if (m.getInverse(m2))
		return m2.getRotationDegrees();
	else
	{
		if ((target - pos).Y < 0.0f)
			return core::vector3df(90.0f, 0.0f, 0.0f);
		else
			return core::vector3df(-90.0f, 0.0f, 0.0f);
	}
}


//....


pointer->setRotation(turn_to_target(pointer->getAbsolutePosition(), model->getAbsolutePosition()));
(untested)
"Whoops..."
cegprakash
Posts: 41
Joined: Thu Feb 27, 2014 10:55 am

Re: setTarget for a animetedmesh how to?

Post by cegprakash »

I prefer this one.

Code: Select all

    ICameraSceneNode *dummyCamera = smgr->addCameraSceneNode();
    dummyCamera->setPosition(animatedmesh->getAbsolutePosition());
    dummyCamera->setTarget(targetMesh->getAbsolutePosition());
    animatedmesh->setRotation(dummyCamera->getRotation());
    dummyCamera->remove();
    smgr->setActiveCamera(viewCamera);
 
Because the the animatedmesh could be a child of some other mesh which in-turn could be a child of some other mesh and each of them could have their individual local rotations.
cegprakash
Posts: 41
Joined: Thu Feb 27, 2014 10:55 am

Re: setTarget for a animetedmesh how to?

Post by cegprakash »

Though my previous answer works, it's not efficient to insert a dummyCamera and remove it :P So ignore it..

You can use this function written by my friend.

Code: Select all

quaternion rotationBetweenVectors(vector3df targetDirection,vector3df initialDirection)
{
    //Gets the initialDirectionVector and targetDirectionVector and finds the rotation that is needed to rotatate initialDirection to targetDirection.
    //If initialDirection points the baseRotation(when the angle with all the axis is 0) then you can apply the return value directly.
    //else if the initialDirection points the currentRotation, you have to multiply the return value with the currentRotation. (Note that multiplication of two quaternions mean that you are adding two angles.)
    
    float dot = initialDirection.dotProduct(targetDirection);
    vector3df cross = initialDirection.crossProduct(targetDirection);
    cross.normalize();
    dot = (dot < -1 || dot > 1) ? 0.0:dot;
    float turnAngle = acos(dot);
    quaternion delta;
    delta.fromAngleAxis(turnAngle, cross);
    delta.normalize();
    return delta;
}
Note that you should pass both the directions with respect to the same world (should be either local or global). You can change the quaternion into Euler radians just by using toEuler function.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: setTarget for a animetedmesh how to?

Post by thanhle »

Hi,
Why is the condition below used?
dot = (dot < -1 || dot > 1) ? 0.0:dot;
cegprakash
Posts: 41
Joined: Thu Feb 27, 2014 10:55 am

Re: setTarget for a animetedmesh how to?

Post by cegprakash »

acos(x) is defined only for -1<=x<=1
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: setTarget for a animetedmesh how to?

Post by thanhle »

acos(x) is not the dot product.

a.b = |a||b|cos(angle) is not equal to [-1,1] unless |a||b| = 1.
cegprakash
Posts: 41
Joined: Thu Feb 27, 2014 10:55 am

Re: setTarget for a animetedmesh how to?

Post by cegprakash »

what are you trying to say? I never told that acos(x) = dot :roll:

I thought you would understand. acos(x) will return NaN for x<-1 and x>1
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: setTarget for a animetedmesh how to?

Post by thanhle »

Yes I know.
But the function above assumed vector a and vector b already normalized.

My hint was hopefully the author mention it in the text of why he used dot = (dot < -1 || dot > 1) ? 0.0:dot;.

Regards
Thanh
cegprakash
Posts: 41
Joined: Thu Feb 27, 2014 10:55 am

Re: setTarget for a animetedmesh how to?

Post by cegprakash »

//This might help someone if someone requires the local position of the target

Code: Select all

matrix4 axis = IdentityMatrix;
axis.setTranslation(source->getAbsolutePosition());
axis = source->getAbsoluteTransformation() * axis;
vector3df targetLocalPosition = axis * target->getAbsolutePosition();
 
After this,

Code: Select all

 targetLocalPosition.getHorizontalAngle()
could help to find the local rotation required.
Post Reply