Note that there are some explanatory texts on larger screens.

plurals
  1. POFollow object in 3D
    primarykey
    data
    text
    <p>I'm in a bit of a problem here and I would like some help. I'm trying to find a rotation that allows me to make an object follow the player ( face it at all times ). I first tried doing the cross product, the dot and rotating, but that just doesn't work correctly. I then proceeded to decompose it into two rotations ( yaw and pitch ) and locked-out roll. It now works, but just for some directions ( I believe a quarter of a circle, but could be less ). This is my current code:</p> <pre><code> XMVECTOR newDir = Vec3DtoDX((Player-&gt;Position - InternalEntity-&gt;Position).getNormalized()); XMVECTOR orig = XMVectorSet(0,0,1,0); XMVECTOR xNewDir,xOrigDir,yNewDir,yOrigDir; xOrigDir = XMVectorSet(0,1,0,0); // (y,z) yOrigDir = XMVectorSet(0,1,0,0); // (x,z) xNewDir = XMVectorSet(newDir.m128_f32[1],newDir.m128_f32[2],0,0); yNewDir = XMVectorSet(newDir.m128_f32[0],newDir.m128_f32[2],0,0); float xAngle = XMVector2AngleBetweenVectors(xNewDir,xOrigDir).m128_f32[0]; float yAngle = XMVector2AngleBetweenVectors(yNewDir,yOrigDir).m128_f32[0]; XMVECTOR rotDX = XMQuaternionRotationRollPitchYaw(xAngle,yAngle,0); PxQuat rot = VecDXto4D&lt;PxQuat&gt;(rotDX); </code></pre> <p>Here's working correctly, if I face the object close to the Z axis <a href="http://imgur.com/oNNNRXo" rel="nofollow">http://imgur.com/oNNNRXo</a></p> <p>And if I move more, it turns, something like this: <a href="http://imgur.com/xFIEzdg" rel="nofollow">http://imgur.com/xFIEzdg</a></p> <p>Any help will be appreciated.</p> <p>EDIT: I allready tried to create the quaternion from the cross and the angle from the acos of the dot. In fact, it was my first though. The problem is that it doesn't work correctly. Here's how I'm doing it:</p> <pre><code> PxVec3 newDir = (Player-&gt;Position - InternalEntity-&gt;Position).getNormalized(); PxVec3 orig(0,0,1); PxVec3 axis = newDir.cross(orig); float angle = XMVector3AngleBetweenVectors(Vec3DtoDX(newDir),Vec3DtoDX(orig)).m128_f32[0]; PxQuat rot(angle,axis); </code></pre> <p>where XMVector3AngleBetweenVectors is:</p> <pre><code> XMVECTOR L1 = XMVector3ReciprocalLength(V1); XMVECTOR L2 = XMVector3ReciprocalLength(V2); XMVECTOR Dot = XMVector3Dot(V1, V2); L1 = XMVectorMultiply(L1, L2); XMVECTOR CosAngle = XMVectorMultiply(Dot, L1); CosAngle = XMVectorClamp(CosAngle, g_XMNegativeOne.v, g_XMOne.v); return XMVectorACos(CosAngle); </code></pre> <p>This results in the following screens: <a href="http://imgur.com/vlMPAwG" rel="nofollow">http://imgur.com/vlMPAwG</a> And : <a href="http://imgur.com/PEz1aMc" rel="nofollow">http://imgur.com/PEz1aMc</a></p> <p>Any help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload