Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / quickly determining rotation directions

Author
Message
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 29th Jun 2010 15:55
I am looking for a quick way to determine if I should turn left, right, or not at all. I know I can use dbtanfull or something like that but I'm thinking I can use dot products to do it too - and quicker - since that's how back face culling works.

Does this seem possible? I only want yto determine direction of turn, not the angle.

How could I do that w/o using any of the trig functions?

Thanks in advance.

JTK
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 29th Jun 2010 18:18 Edited at: 29th Jun 2010 18:19
Quote: "I am looking for a quick way to determine if I should turn left, right, or not at all"


?? i really don't get what you want to achieve
is it something like
if angle y > 0
turn right
else if angle y < 0
turn left
else
go straight
?

ahh better explanation would help

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 29th Jun 2010 18:46
That pretty much sums it up, yeah but I don't want to have to do trig functions to get it - if I can avoid it...

I know we can determine if a point is on one side of the plane or the other using a vector dot product... No trig needed...

I'd like to take a similar approach when deciding to turn left or turn right...

Is that a better explaination?

JTK
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 29th Jun 2010 21:01 Edited at: 29th Jun 2010 21:01
So you want to get the difference between two angles in degrees?
Because if you have this difference you can say if one angle is either left (negative) or right (positive) from the other.
The problem is that you have to find a way around the issue with the range of degrees, which is from 0° to 360°. Because of this a simple < or > comparison won't do the job.
For example the angle 355° is bigger then 10° but it's still left of it. I think there was a function for this somewhere on these forums...

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 29th Jun 2010 21:28
you're talking about 2d, right?

OK, say your character's current direction is the 2d vector D, its position the 2d vector V1, and its target's position the 2d vector V2.

now, say vector V3=V2-V1 (basically translating v2 so that v1, the character's position, is at the origin). Now:
V3.D=|V3|*cos(theta)

where |v3| is the length of v3 and theta is the angle between the two vectors. Now, we're worried about the sign of cos(theta), and since |v3| is always positive (or zero), we can ignore it. If V3.D is negative, turn left. If v3.D is positive, turn right. If it's zero, go straight.

^^I'm pretty sure that's correct, but let me check. Basically:
direction_sign=sgn((V2-V1).D)
=sgn((xV2-xV1)*xD+(yV2-yV1)*yD)

unfortunately... if you have an angle... you need trig functions to calculate D, and the only other ways I can think of to do what you want to do use trig functions.

the above then simplifies to
=sgn((xV2-xV1)*cos(ang)+(yV2-yV1)*sin(ang))

I'm checking it now.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 29th Jun 2010 21:45
well it works.

[dbpro but the concept still applies]

Honestly... I'm not quite sure why the angle is 90 degrees over X_X


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 1st Jul 2010 20:48 Edited at: 1st Jul 2010 21:12
Not so scientific idea but I think it works: Calculate the difference between the two directions in absolute value. If it is greater then 180 degrees, then deduct from 360 to choose the smaller turn. Then check if you have to add or subtract the difference to the original direction to arrive at the destination direction. If you need to add it, then turn right, if you need to subtract it, turn left. If the difference is zero, do not turn. The problem with going over 360 degrees is handled by dbWrapValue, or you can write your own function. Something like this:



If you have floating point angles, it could still work if you regard only the integer part or if you replace the equality check with a range check (within a certain tolerance). dbWrapValue uses floats as well so you may need to modify the above code because of that.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 1st Jul 2010 22:52 Edited at: 1st Jul 2010 22:56
x#, y# represent the components of the direction in which the player is facing. dirx#, diry# represent the components of the desired direction.

result# = x#*diry#-y#*dirx#

If result# is negative, turn one way, if it is positive, turn the other way, if zero, then you are either pointing directly towards it or directly away from it, which should be a simple check.

(untested)

The theory is as follows:
Dot product returns a number representing how 'in-line' the vectors are, ranging from 1 being completely in line (if they are unit vectors) to -1 if they are facing in opposite directions (again for unit vectors).

We want to do the same thing, but with a 90 degree difference, so that left is maximum negative, and right is maximum positive, so to make a vector perpendicular to itself you do the 2d equivalent of the cross product:
newx# = -oldy#
newy# = oldx#

The dot product is:
result# = y1#*y2# + x1#*x2#

So substituting the cross product in gives the formula:
result# = x#*diry#-y#*dirx#

[b]
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 6th Jul 2010 00:28
Well neuro: I was talking about 3d so can you translate accordingly?

what I'm needing is a way to determine if a baddu should turn left/right or not at all when chasing a player in the game world.

I've tried ripping the turnTo whatever function from dark dungeon demo but it screws up my other rotations / animation code. Presumably due to the dbPointObject call - don't know exactly but in either case it doesn't work for me so I'm looking for another way...


Thanks again all, for your help...

JTK
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th Jul 2010 07:53
If you're just turning left and right, it IS a two dimensional problem. If it was a three dimensional problem... that would be like a game in outer space with spaceships, no discernable up and down, and the ability to turn up, down, left, right, and any diagonal. Just ignore the objects' y values and use their x and z values.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?

Login to post a reply

Server time is: 2024-07-04 10:54:02
Your offset time is: 2024-07-04 10:54:02