Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: mike_g 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.
(http://i92.photobucket.com/albums/l15/mikegrundel/rotationdir.png)
I hope I explained this in an understandable way. If I dident, let me know. Cheers :)
-
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.
-
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).
-
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.
-
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.
-
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.
-
something like this should work:
'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
-
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.
-
Stonemonkey has the right idea:
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.