Jump to content

Short example of Math::ATan2


Vulcan
 Share

Recommended Posts

Today I got into little problem with how to get a entity to look at a specific point Vec3(x, 1, z).

I would suggest updating the Math::ATan2 documentation to include an example of how to do this with both C++ and Lua.

I found this explaination usefull.

 

Another suggestion could be to add a method for Entity class. Maybe something like:

Entity::LookAtXZ(..) for 2d look at XZ (still usefull for 3d)

Entity::LookAtXYZ(..) for 3d look at (ie: a players head looking at something, just like minecraft does)

Link to comment
Share on other sites

Put an entity there?

 

Long answer:

 

Create a box at the start of your program. Hide it.

 

Whenever you need to point something at a location:

  • Unhide box
  • Position it to location
  • Point entity at box
  • Hide box

Thats quite a creative solution I must say, but still I think a Entity::LookAt(..) would look cleaner way to do it.

This works and I think this is something that LE should have:

void LookAt(Vec3 lookAt)
{
// Calculate angle from point A towards point B
Vec3 tv = lookAt - obj->GetPosition();
float tRoty = Math::ATan2(tv.x, tv.z);
// Look at point B (height is excluded)
obj->SetRotation(Vec3(0.0f, tRoty, 0.0f));
}

 

EDIT: Physics must be set to rigid mode temporary when SetRotation

  • Upvote 2
Link to comment
Share on other sites

I would like an Entity:LookAt() function. Would save me some lines of messy math code.

Also you could use SetInput(rot.y) for a character controller instead of switching its physics mode temporarily to use SetRotation(0,rot.y,0). At least I think that should work.

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...