Jump to content

LookAt function (C++)


george68
 Share

Recommended Posts

Hi,

I need a function that rotates an entity in order to turn and look at another one.

So far I have calculated the vector from the source to the destination entity but I don't know how to calculate the pitch, yaw and the roll of the vector.

Any assistance will be appreciated.

Link to comment
Share on other sites

This is what I found when I was programming something. it also has resources and sources I used linked

 

function Script:LookAt(start,target)
   --http://www.leadwerks.com/werkspace/topic/10191-short-example-of-mathatan2/page__hl__lookat
   --http://stackoverflow.com/questions/1251828/calculate-rotations-to-look-at-a-3d-point
   --http://leadwerks.wikidot.com/wiki:face-entity

   --// Calculate angle from point A towards point B
   local tv = (target - start) 

   local tRoty = Math:ATan2(tv.x, tv.z)
   --local tRotx = 
   if tv.z >= 0 then    
       tRotx = -Math:ATan2(tv.y* Math:Cos(tRoty), tv.z)
       else    
       tRotx = Math:ATan2(tv.y* Math:Cos(tRoty), -tv.z)
   end
   tRotz = Math:ATan2( Math:Cos(tRotx), Math:Sin(tRotx) * Math:Sin(tRoty) )
   return Vec3(tRotx,tRoty,tRotz-90)
end

Link to comment
Share on other sites

Parameters

 

  • entity: the target entity to point towards.
  • axis: the entity's axis to orient. This may be 0, 1, or 2, for the X, Y, and Z axes, respectively.
  • rate: a multiplier to indicate how quickly the entity should orient itself.
  • roll: the rotation around the specified axis.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...