# Problem making a character rotate towards a clicked point?

This topic is 3614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having trouble trying to make a game character rotate towards a point where the user clicks. The character rotates but it doesnt face the correct direction. What I did is to find the character's facing vector and to find the vector from the player's position to the clicked point. Then both of them are normalised and the dot product between these two vectors are taken. Then I apply arc cosine to the angle and set the z axis of the SetRotation method to the angle. (The z axis is the vertical axis) I have included the code snippet of the rotation calculation below.  void Rotate(NiPoint3 targetPos) //NiPoint3 is a 3D vector type { NiPoint3 playerpos = character->GetTranslate(); //get the current position //The code below calculates the facing vector NiPoint3 kFacingDir; character->GetWorldRotate().GetCol(1, kFacingDir.y, kFacingDir.x,kFacingDir.z); kFacingDir.y += kFacingDir.x; kFacingDir.x = kFacingDir.y - kFacingDir.x; kFacingDir.y -= kFacingDir.x; kFacingDir.y *= -1.0f; kFacingDir.x *= -1.0f; kFacingDir.Unitize(); //Find the vector from the player to the clicked point NiPoint3 target(targetPos.x,targetPos.y); NiPoint3 current(playerpos.x,playerpos.y); NiPoint3 targetDir = target - current; targetDir.Unitize(); //Dot product and set the rotation double angle = NiACos(kFacingDir * targetDir); character->SetRotate(angle,0,0,1); //apply the rotation angle to the z axis //(the z axis is the vertical axis) } [\CODE] The method is run when the mouse is right clicked. The part where the facing vector is calculated is obtained from a code sample which can correctly rotate the character towards the clicked point. What that code does is to calculate the rotation matrix. What is the problem in the rotation angle calculated above and using it to perform the rotation? 
 0 
 Share this post Link to post Share on other sites 
 
 
 Advertisement someboddy    100 someboddy Member 100 Posted January 19, 2009 Quote:Original post by Weng kFacingDir.y += kFacingDir.x; kFacingDir.x = kFacingDir.y - kFacingDir.x; kFacingDir.y -= kFacingDir.x; kFacingDir.y *= -1.0f; kFacingDir.x *= -1.0f; kFacingDir.Unitize();[\CODE]I assume this code suppose to calculate the perpendicular of the facing direction - 'cause that's what it looks like. The formula for perpendicular to a 2D vector is (-y,x) or (y,-x), depends of your axis system and the direction you want to rotate the vector. What you did here is (-y,-x) - and that's one of the reasons you fail. Delete either "kFacingDir.y *= -1.0f;" or "kFacingDir.x *= -1.0f;".Your second problem is that cross product can be used to calculate the angle required to rotate from one vector to another - but it can't tell the direction. Since you need the direction, you have two options:1) Use atan2. I'm assuming you are using C++, but many other languages also have that(or a similar) function. Use it to calculate the angle of targetDir, and subtract the angle of kFacingDir from it.2) Use the formula (kFacingDir.x*targetDir.y-kFacingDir.y*targetDir.x). If it's positive, rotate one way. If it's negative, rotate the other way. If it's zero, than the angle is either 0 or 180 degrees, so it doesn't really matter which way you'll rotate... 0 Share this post Link to post Share on other sites Weng123    122 Weng123 Member 122 Posted January 20, 2009 Quote: character->GetWorldRotate().GetCol(1,kFacingDir.y,kFacingDir.x, kFacingDir.z); kFacingDir.y += kFacingDir.x; kFacingDir.x = kFacingDir.y - kFacingDir.x; kFacingDir.y -= kFacingDir.x; kFacingDir.y *= -1.0f; kFacingDir.x *= -1.0f; kFacingDir.Unitize();[\CODE]Thanks for the explanation :)For the facing vector above, I'm still not sure how it works and am not sure if it is the actual facing vector or is it the perpendicular of the facing vector.For the lines below,kFacingDir.y += kFacingDir.x;kFacingDir.x = kFacingDir.y - kFacingDir.x;kFacingDir.y -= kFacingDir.x;What do these calculations represent?Also, why does the x and y components need to be multiplied by -1?For the above calculations, the z axis is the vertical axis. (corresponding to the usual y axis ). The x and y axis correspond to the usual x and z axis respectively. 0 Share this post Link to post Share on other sites someboddy    100 someboddy Member 100 Posted January 20, 2009 Quote:Original post by Weng123For the lines below,kFacingDir.y += kFacingDir.x;kFacingDir.x = kFacingDir.y - kFacingDir.x;kFacingDir.y -= kFacingDir.x;What do these calculations represent?Well, this is just a fancy way to swap between kFacingDir.x and kFacingDir.y. It's supposed to save some memory allocation because you don't need a temporary swap variable, but it's less readable and slower.Anyways, I've just noticed that an much easier and efficient way to do this swap is to simply writing:character->GetWorldRotate().GetCol(1, kFacingDir.x, kFacingDir.y,kFacingDir.z);instead ofcharacter->GetWorldRotate().GetCol(1, kFacingDir.y, kFacingDir.x,kFacingDir.z);OK, this code makes less and less sense the more I read it. First they read a x value to y and a y value to x(I assume the GetCol function uses the xyz order), and than use some fancy code to swap?OK, forget what I said about making it perpendicular - it is not needed here. The problem is only the lack of code to determine the direction of the rotation.Quote:Original post by Weng123Also, why does the x and y components need to be multiplied by -1?That's just for turning the vector 180 degrees. The same effect could be achieved by changing the lineNiPoint3 targetDir = target - current;toNiPoint3 targetDir = current - target; 0 Share this post Link to post Share on other sites MightyDog    151 MightyDog Member 151 Posted January 20, 2009 Since you are using Gamebryo, check out the Metal Wars demo. They have code that basically accomplishes this. I modified it and put in head tracking for following the mouse pointer. I know this doesn't explain anything, but maybe you can get some insights looking at the sample code (they even have dampening included). 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing General and Gameplay Programming Advertisement 
 Advertisement Are you ready to promote your game? Submit your game for Intel® certification by December 21, 2018 and you could win big!  Click here to learn more. Popular Tags 2D 3D Advice Algorithm C# C++ Character Concept Custom Design DX11 GameMaker Gameplay General Graphics Java Javascript Learning Mobile Music OpenGL PC SFX Unity Unreal Popular Contributors Week Month Year All Time 1 Septopus 27 2 jbadams 17 3 Rutin 13 4 Gnollrunner 13 5 DexterZ101 11 Show More Advertisement Popular Now 26 OpenGL Texture mapping acts weird on some images. By babaliarisStarted 20 hours ago 11 Inspiration for citybuilder game By Gilles MeiresonneStarted Tuesday at 06:23 PM 9 Angular velocity and water physics By lawnjellyStarted Sunday at 06:12 PM 9 Procedural Content Generation By NikitaSadkovStarted Saturday at 12:05 AM 11 Strange performance differences and crashes on different systems By MagoganStarted December 7 Forum Statistics Total Topics 633700 Total Posts 3013417 GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Forums Programming General and Gameplay Programming Problem making a character rotate towards a clicked point? 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry. Sign me up!