As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Direction traveled in a circle

wallabeeXwallabeeX Registered User regular
edited July 2010 in Help / Advice Forum
I'm currently working on a script for a visual effects project I'm embarking on. This particular problem involves figuring out which way a bird would tip it's wings as it comes into a curve to bank. Essentially, we can assume the bird tips one of two ways, and I'm just trying to set up the algorithm that's capable of figuring that out.

This can be better boiled down to traveling in a circle - how do we mathmatically determine whether an object is traveling clockwise or counter-clockwise in a circle? I have access to most any statistic to determine this, though I'd imagine velocity and position, or perhaps just position, will do.

Can anyone shed some light on this?

wallabeeX on

Posts

  • Options
    Dropping LoadsDropping Loads Registered User regular
    edited July 2010
    I think you may be making this more complicated than it needs to be, or else I don't understand what you're actually trying to do. The "dip" direction is always the same as the direction you want to go. Turning right = banking right = right wing drops down.

    Dropping Loads on
    Sceptre: Penny Arcade, where you get starcraft AND marriage advice.
    3clipse: The key to any successful marriage is a good mid-game transition.
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    I think it's acceleration you'd actually want; neither (current) velocity nor position will tell you much about which way the bird is going (clockwise or counterclockwise). However it's acceleration will point towards the center of the circular path it's following.

    EDIT: In fact, I think this would work for most arbitrary curves...no?

    mcdermott on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    But in Math, there's no such thing as "right". Right is relative, dependent on the direction you're traveling in.

    If I'm trying to get to 1,0 from 0,-.5 I'll have to take a right. If I'm traveling from 0,.5 and trying to get to the same place, I'll have to take a left.

    I've written a script that procedurallly has a flock of seagulls chase a seagull someone's animated on their own. This means that the animator only has to animate ONE seagull and he gets the other 50, or 150, for free. To make it more believable, I'd love to have them bank dependent on the direction it's turned.

    But I have no idea what direction it's GOING to change to - just the position it's at, the position it was at, and it's current and prior velocity. That's to say it's easy to figure out you need to take a left if you're triyng to get to 1,0 from 0,-.5, but I don't have that information. Only the past.

    wallabeeX on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    Is acceleration just the current velocity - the old velocity? I got great results with that to determine if a turn was being made, but I couldn't determine in what direction from it.

    wallabeeX on
  • Options
    Dropping LoadsDropping Loads Registered User regular
    edited July 2010
    Are you doing this real-time, or do you get to iterate after the person has chosen the path for the first seagull? If they are moving the seagull that is in the front, then you do know left and right. I guess for some reason this seems more like a pathfinding problem to me than a physics problem. If you only know the past actions, how can you know if they will even be heading in a given direction, much less that they should be banking?

    Edit: the "real-time" thing shouldn't even matter, as you would have to get a chance to process his movements in order to even display them on screen.

    Double edit: I think I may be out of my depth here, sorry. I'm pretty interested in your problem though, and I would love to see what solution you end up with.

    Dropping Loads on
    Sceptre: Penny Arcade, where you get starcraft AND marriage advice.
    3clipse: The key to any successful marriage is a good mid-game transition.
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    I know left from right but the flying cloud of points behind it don't.

    The program I work with is capable of saying to a cloud of points (henceforth: particles) "follow that". I'm essentially doing that, although with some added complexity. These particles are capable of carrying the image of a seagull flapping - they can aim in the direction of their velocity, which they do, so the seagulls always point forward. I need a way to look at the values from the past frame, and the values from the current frame, and know whether it's heading in a straight line, or in a curve.

    wallabeeX on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    It occurs to me now that if I can tell the bird to point in the direction it's flying, then I'm capable of telling when it's turned. That is, compare the current rotation of the bird with the rotation of the bird from the last frame - if he's rotated at all, he's made some sort of turn. Since this happens on a single axis (Y), it's fairly easy to say "okay, he's turned left" or "he's turned right". It's binary.

    I'll let you know how it goes.

    wallabeeX on
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    wallabeeX wrote: »
    Is acceleration just the current velocity - the old velocity? I got great results with that to determine if a turn was being made, but I couldn't determine in what direction from it.

    I'm pretty sure if you look at the velocity as an (X,Y) value, and acceleration as (A,B), and compare the signs of the four (X/A and Y/B) you can get a direction (as in, is the bird turning left or right) based on the comparative signs.

    If that's not sufficient, you may need to go with looking at the angle between the two vectors ((X,Y) and (A,B)) to determine what direction that acceleration is turning the bird in (left or right, relative to the bird, would depend on whether the angle between the two was positive or negative).

    If that's not sufficient, you may need to go with something like a cross product. But I'm pretty sure that won't be necessary, particularly if we're just talking about 2D coordinates.


    EDIT: Actually, yeah...I think you're going to want to go with angles. Just find the angle of the current velocity, then the angle of the acceleration, subtract the two, and that'll give you clockwise or counterclockwise.

    mcdermott on
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    wallabeeX wrote: »
    It occurs to me now that if I can tell the bird to point in the direction it's flying, then I'm capable of telling when it's turned. That is, compare the current rotation of the bird with the rotation of the bird from the last frame - if he's rotated at all, he's made some sort of turn. Since this happens on a single axis (Y), it's fairly easy to say "okay, he's turned left" or "he's turned right". It's binary.

    I'll let you know how it goes.

    Is it always Y? If the bird goes 360 degrees, then a positive change in Y (assuming Z is height, and X and Y are directions of travel) may correlate to either a counterclockwise or clockwise turn depending on the direction of travel in the X axis.

    EDIT: If X direction is positive, then a positive change in Y means a counterclockwise rotation. If X is negative, then a positive change in Y means counterclockwise. Though that just makes it a two-step check, instead of one. The main problem then is dealing with your zeros (straight "north or south" so to speak). But that can just be a third step (if X is zero, check if why is positive or negative, then act accordingly). I still say calculating angles would work better, though.

    mcdermott on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    In this case X and Z are travel, Y is height.

    But you're correct. My current example scene DOES have the bird traveling in a 360 circle and the Y travels is entirely based on it's travel on the X axis. It's pretty worthless.

    I'm looking into your suggestion above but it's a bit above my head. This is the type of stuff that tends to make me wish I was still taking math courses. :)

    wallabeeX on
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    wallabeeX wrote: »
    In this case X and Z are travel, Y is height.

    But you're correct. My current example scene DOES have the bird traveling in a 360 circle and the Y travels is entirely based on it's travel on the X axis. It's pretty worthless.

    I'm looking into your suggestion above but it's a bit above my head. This is the type of stuff that tends to make me wish I was still taking math courses. :)

    It's not too tough, though. To find the angle, it's just arcsine(X/Z). Do this for velocity and acceleration. Make sure to add 180 degrees if Z is negative (taking Z to be "up/down" and X to be "left/right"). If the angle is 0 (and Z will be 0), then determine between 0 and 180 degrees based on whether X is positive or negative (0 if positive, 180 if negative).

    That's a pretty simple step-by-step process.

    Then at any given moment, you've got two angles, velocity and acceleration. If the angle of acceleration is greater than velocity, it's going clockwise, otherwise counterclockwise.

    mcdermott on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    For the current frame, the velocity is <<-553.492,0,1001.971>> and the acceleration is <<-763.608,0,-99.551>>. You can see why I might be confused how 180 would help 0 out a negative value.

    Really appreciate your help.

    wallabeeX on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    Ah, sorry, to the resulting value. I understand.

    wallabeeX on
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    Cool. Hope I was able to help.

    Also, you should be able to (if it's something your app supports) use the magnitude of that difference in angles to determine how hard they bank; greater the angle, greater the bank.

    mcdermott on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    I'm still having a hard time wrapping my head around this.

    From what I understand arcsine will only take real numbers, but you see the numbers I"m dealing with. From what I understand velocity is the difference in the current position and the last prior simulation time step (typically a frame), but these are hardly real numbers.

    wallabeeX on
  • Options
    enlightenedbumenlightenedbum Registered User regular
    edited July 2010
    If it's purely about traveling clockwise or counterclockwise in a circle, it's best to change things to polar coordinates.

    x = r cos (theta)
    z = r sin (theta)
    r ^2 = x ^ 2 + z ^ 2

    Find the derivative of theta with respect to time, which you can do yourself because I'm lazy.

    If dtheta/dt is positive, it's counterclockwise. If dtheta/dt is negative, clockwise. Again, only works if it's traveling in a circle, but any arc is part of some arbitrarily large circle. mcdermott's method should also work, I think.

    enlightenedbum on
    Self-righteousness is incompatible with coalition building.
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    wallabeeX wrote: »
    I'm still having a hard time wrapping my head around this.

    From what I understand arcsine will only take real numbers, but you see the numbers I"m dealing with. From what I understand velocity is the difference in the current position and the last prior simulation time step (typically a frame), but these are hardly real numbers.

    My bad. Arcsine/arccosine can only take numbers up to 1. You can still use them, but you'd have to get a unit vector. Maybe arctan will work?

    To get a unit vector, you simply find the length of the vector, then divide both values (X, Z) by that length...then you can use cos/sin (and arccos/arcsin). Cosine is adjacent/hypotenuse, but with a unit vector hypotenuse is 1, thus you can just use the Z value of the unit vector for your arccos. IIRC, we actually use arccos, because it's easier to work with for this.

    So, in your example:
    V= <<-553.492,0,1001.971>> A = <<-763.608,0,-99.551>>

    Velocity:
    length=(553^2+1001^2)^.5, or 1143.6
    So your unit vector is <-553/1143.6, 0, 1001/1143.6>, or <-.484, 0, .875>
    your angle will be arccos(-.484), or 118.9 deg.


    Accel:
    length=(763^2+99^2)^.5 or 769.4
    Unit vector is <-763/769.4,0,-99/769.4> or <-.992,0,-.129>
    angle is arccos(-.992), or 172.7 deg.
    Because Z is negative, though, that's actually -172.7 deg, or rather 187.3 deg (you can check this by thinking about where those vector values put you on a circle).


    Now, you've got your angles. I'm pretty sure it's not as easy as "greater than" or "less than" with those angles, though, because (duh) it's a circle. Forgot about that. But I know you can use those angles to determine which way you're rotating, I'll either leave it to you or somebody else to explain how. I forget.


    EDIT: And I think converting to polar and working that way would be harder, depending on what environment he's doing it in.

    mcdermott on
  • Options
    enlightenedbumenlightenedbum Registered User regular
    edited July 2010
    I was speaking purely mathematically, not from a coding perspective. But it shouldn't be too hard to work out the formula for dtheta/dt by hand and just plug stuff in, no?

    enlightenedbum on
    Self-righteousness is incompatible with coalition building.
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    I was speaking purely mathematically, not from a coding perspective. But it shouldn't be too hard to work out the formula for dtheta/dt by hand and just plug stuff in, no?

    I suppose this would be the case, yes. :P

    So yeah, go with that.

    mcdermott on
  • Options
    BoomShakeBoomShake The Engineer Columbia, MDRegistered User regular
    edited July 2010
    No need for angles. How about trying a plane rotation with your friend and mine, complex numbers! Essentially, we want to rotate our velocity and acceleration vectors such that velocity is pointing straight up. This way, to see whether we're turning left or right, we just have to check whether the real part of acceleration is positive or negative.

    Since we're just talking about turning, let's toss out the height so we're just left with vectors of two dimensions:
    Velocity: v <a,b>
    Acceleration: k <c,d> (i'm using k for acceleration to avoid confusion with the a component of v)

    Now, both of these can be represented on the complex plane, where the vertical axis is complex and horizontal is real numbers.

    v=a+bi
    k=c+di
    r=0+1i (reference, unit straight up)

    Let's let z represent the vector we need to help rotate things.

    z = v/r

    z = trojm.jpg (ignore that comma at the end)

    So here, we get:

    z = b - ai

    Now, we want to make z a unit vector so that we only rotate and not scale (in case you want to do proportional banking)

    z' = z/|z| = z/sqrt(b^2+a^2)

    If v' is the rotated (vertical) velocity and k' is the rotated acceleration

    v' = v/z'
    k' = k/z'

    Now, all you have to do is check the real part (or the x portion of the vector) of the rotated acceleration, k'. If it's positive, you're turning right. If it's negative, you're turning left. Bam.

    BoomShake on
  • Options
    wallabeeXwallabeeX Registered User regular
    edited July 2010
    You guys have been crazy awesome helpful. Sorry for the radio silence - I left work, but this hasn't left my mind.

    The scripting language I'm working with has a "unit" command that can turn my large numbers into a unit vector, which was what I was in the middle of before I had to leave for the day. I'm planning on going over these new details later today and hopefully find a solution. Comically, I've been moved to a different project, but my own curiosity will drive this towards a final solution.

    Thanks again, I'll keep you all posted. I'm also on my way to the bookstore to pick up a text book one this kind of math. This is trig, right? I've got a linear algebra text-book but I think that's even a bit high for this.

    wallabeeX on
  • Options
    DemerdarDemerdar Registered User regular
    edited July 2010

    If it's purely about traveling clockwise or counterclockwise in a circle, it's best to change things to polar coordinates.

    x = r cos (theta)
    z = r sin (theta)
    r ^2 = x ^ 2 + z ^ 2

    Find the derivative of theta with respect to time, which you can do yourself because I'm lazy.

    If dtheta/dt is positive, it's counterclockwise. If dtheta/dt is negative, clockwise. Again, only works if it's traveling in a circle, but any arc is part of some arbitrarily large circle. mcdermott's method should also work, I think.

    Yup.

    Demerdar on
    y6GGs3o.gif
  • Options
    enlightenedbumenlightenedbum Registered User regular
    edited July 2010
    It's all related. Linear Algebra probably has some sections on transformations that could be helpful. My solution is polar coordinates + calculus one. Where polar is covered depends on the text, but probably trig or pre-calc?

    I loves me some complex numbers, but probably not necessary. Same basic idea as mcdermott and myself though. Moving things to a more convenient coordinate system. I think my solution is the most elegant. :P

    enlightenedbum on
    Self-righteousness is incompatible with coalition building.
  • Options
    mcdermottmcdermott Registered User regular
    edited July 2010
    It's all related. Linear Algebra probably has some sections on transformations that could be helpful. My solution is polar coordinates + calculus one. Where polar is covered depends on the text, but probably trig or pre-calc?

    I loves me some complex numbers, but probably not necessary. Same basic idea as mcdermott and myself though. Moving things to a more convenient coordinate system. I think my solution is the most elegant. :P

    Upon re-examination, I agree.
    How is my math so rusty after just a year out of school?!

    mcdermott on
  • Options
    Gandalf_the_CrazedGandalf_the_Crazed Vigilo ConfidoRegistered User regular
    edited July 2010
    Couldn't you multiply the X acceleration by the Y acceleration? If the result is negative, it will always mean it's turning right and should dip its right wing. If the result is positive, just the opposite.

    EDIT: I have no experience in these things, this is just the first thing that popped into my head. And I didn't read the more complicated posts above mine, so they may have said the same thing or something better.

    EDIT2: Actually, I'm just plain wrong. If he's flying along the X axis negatively, a left turn would result in a negative product of the two accelerations. I'm a moron.

    Gandalf_the_Crazed on
    PEUsig_zps56da03ec.jpg
Sign In or Register to comment.