Author Topic: Find Direction To Rotate  (Read 1905 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Find Direction To Rotate
« on: August 15, 2007 »
I'm coding automated movement in my raycasting prog at the moment. It uses A* to find a path, then rotates to face the next tile to move to. I want to find out what direction to turn in will take the least rotation.

So say I know the target facing I want to reach. I have 2 player directions dir_x and dir_y, which range between -1 and 1. How can I find which direction will take the least rotation?

Heres an sketch. Ignore the blue writing its just random crap. The red dot represents the player. Theres an example target of dir_x = 0.5, dir_y = 0.5. Any facing in the green half of the grid would be rotated to the right, whereas anything in the turquoise half would be rotated to the left. 

I hope I explained this in an understandable way. If I dident, let me know. Cheers :)

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Find Direction To Rotate
« Reply #1 on: August 15, 2007 »
An idea I thought that might be on the right path, you could use the two positions and compare which is the closest ( higher / lower ) away from you.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1310
  • Karma: 96
    • View Profile
Re: Find Direction To Rotate
« Reply #2 on: August 15, 2007 »
Use the dot product of a vector at right angles to the way it is facing and the vector to the object you want to turn towards, if the result is positive then turn one way, if it's negative then turn the other way. Which way round that should be will depend on which side your right angle vector is pointing(left/right).

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1310
  • Karma: 96
    • View Profile
Re: Find Direction To Rotate
« Reply #3 on: August 15, 2007 »
You'd want to put in a case for it being equal to 0 too, that could mean that is either pointing directly towards or directly away from the target, that can be tested using the dot product of the direction it's pointing and the direction you want to point. If it's pointing directly away then you could turn a little either way and the rest would take over.
« Last Edit: August 15, 2007 by Stonemonkey »

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Find Direction To Rotate
« Reply #4 on: August 15, 2007 »
Clyde: Yeah thats what I started out trying, but it don't seem like it works that way.

Stonemonkey: I'm still not completely sure what the dot product is or what I do with it. But I think I solved it, thanks to you :) I just add the x and y values of the perpendicular angles to right or left and see if its negative or positive. As far as I can see this works on paper, I hope this works. Cheers.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Find Direction To Rotate
« Reply #5 on: August 15, 2007 »
Nope. I was wrong, that dosent work at all. It only works in one instance :( Guess I'll have to try and figure out what the dot product is after all.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1310
  • Karma: 96
    • View Profile
Re: Find Direction To Rotate
« Reply #6 on: August 15, 2007 »
something like this should work:

Code: [Select]
'get perpendicular vector
right_angle_vx=bot_dir_vy
right_angle_vy=-bot_dir_vx

'get vector to target
target_vx=target_x-bot_x
target_vy=target_y-bot_y

'calculate dot product
dot=right_angle_vx*target_vx+right_angle_vy*target_vy

'turn appropriately (may have to be reversed)
if dot<0.0 then turn_left
if dot>0.0 then turn_right

'test if pointing directly away from target and turn if necessary
if dot=0.0 then
  if bot_dir_vx*target_vx+bot_dir_vy*target_vy<0.0 then turn_left
end if
« Last Edit: August 15, 2007 by Stonemonkey »

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Find Direction To Rotate
« Reply #7 on: August 16, 2007 »
Thanks.I havent been able to get it working yet,  but its probably something wrong with my code. Theres so many things wrong with it at the moment, its a bit of a hideous mess :-X Everytime i change something somewhere; something somewhere else screws up. Anyway, I'm going to give up for the time being and work on finishing off some bits and pieces I was doing in C. Cheers.

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Find Direction To Rotate
« Reply #8 on: August 20, 2007 »
Stonemonkey has the right idea:
Code: [Select]
CrossProduct = (TarX-PosX)*(ViewY-PosY) - (VewX-posx)*(TarY-posy)
Where:
    PosX, PosY = current position
    TarX, TarY = destination
    ViewX, ViewY = any point along the current line of sight

Add CrossProduct to the players current rotation as is if you are using radians (scale it up if you are using degrees) dont change the polarity and DONT use any if statements cos the crossproduct will only return an absolute zero if you are exactly facing towards the destination or exactly away from it. even a minute value is enough to ensure that will never happen. start off by giving the player a non zero rotation something like 0.00001 radians that value is way smaller than a single degree but more than enough to make it impossible for the player to ever truely zone in on the target.

Challenge Trophies Won: