setTarget for a animetedmesh how to?
setTarget for a animetedmesh how to?
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?
Something like a setTarget for a cemera?
Any ideas?
-
- Posts: 1186
- Joined: Fri Dec 29, 2006 12:04 am
Re: setTarget for a animetedmesh how to?
Something like:
(untested)
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()));
"Whoops..."
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: setTarget for a animetedmesh how to?
I prefer this one.
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.
Code: Select all
ICameraSceneNode *dummyCamera = smgr->addCameraSceneNode();
dummyCamera->setPosition(animatedmesh->getAbsolutePosition());
dummyCamera->setTarget(targetMesh->getAbsolutePosition());
animatedmesh->setRotation(dummyCamera->getRotation());
dummyCamera->remove();
smgr->setActiveCamera(viewCamera);
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: setTarget for a animetedmesh how to?
Though my previous answer works, it's not efficient to insert a dummyCamera and remove it So ignore it..
You can use this function written by my friend.
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.
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;
}
Re: setTarget for a animetedmesh how to?
Hi,
Why is the condition below used?
dot = (dot < -1 || dot > 1) ? 0.0:dot;
Why is the condition below used?
dot = (dot < -1 || dot > 1) ? 0.0:dot;
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: setTarget for a animetedmesh how to?
acos(x) is defined only for -1<=x<=1
Re: setTarget for a animetedmesh how to?
acos(x) is not the dot product.
a.b = |a||b|cos(angle) is not equal to [-1,1] unless |a||b| = 1.
a.b = |a||b|cos(angle) is not equal to [-1,1] unless |a||b| = 1.
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: setTarget for a animetedmesh how to?
what are you trying to say? I never told that acos(x) = dot
I thought you would understand. acos(x) will return NaN for x<-1 and x>1
I thought you would understand. acos(x) will return NaN for x<-1 and x>1
Re: setTarget for a animetedmesh how to?
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
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
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: setTarget for a animetedmesh how to?
//This might help someone if someone requires the local position of the target
After this, could help to find the local rotation required.
Code: Select all
matrix4 axis = IdentityMatrix;
axis.setTranslation(source->getAbsolutePosition());
axis = source->getAbsoluteTransformation() * axis;
vector3df targetLocalPosition = axis * target->getAbsolutePosition();
Code: Select all
targetLocalPosition.getHorizontalAngle()