rotating to a target direction

This is a discussion on rotating to a target direction within the Game Programming forums, part of the General Programming Boards category; I have an object defined with x,y, and th, right? What I want to be able to do is point ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    rotating to a target direction

    I have an object defined with x,y, and th, right? What I want to be able to do is point to another location and have the object turn until it's pointing towards that location in the shortest way possible. So far I've come up with:
    Code:
    if(th < targetth)
      th += dth;
    if (th > targetth)
      th -= dth;
    The problem with this is that when the target location goes across the positive y access the object spins all the way around the other way. How can I have it reach the targetth in the shortest possible distance every time?
    Illusion and reality become impartiality and confidence.

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Something along the lines of
    Code:
    dir = 1;
    if( th - target_th > 180 ) dir = -1;
    
    if(th < targetth)
      th += dth * dir;
    if (th > targetth)
      th -= dth * dir;
    If the angle difference is greather then 180, go the oppisite direction you would normally go.

    Need to toss in an ABS() some where in there.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Don't use euler angles. Euler's suffer from this problem in that it is not easy to rotate from say 345 degrees to 0 w/o the code causing the object to rotate in the direction of 270, or left, to get to 0 instead of simply choosing to rotate right.

    You can test for this if you are using exact euler values but things get tricky when you start using matrices and other representations. I would suggest looking up quaternions.

    Incidentally there is a thread on here about how to rotate to face a vector or point in space. D3DXMatrixLookAtLH and D3DXMatrixLookAtRH are what they are in Direct3D, but you can also derive them yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rotating around object (looat)
    By jabka in forum Game Programming
    Replies: 13
    Last Post: 06-18-2008, 05:02 PM
  2. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21