CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #2
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Movement Cos Wave

    Assuming your speed value can be translated to some sort of "time unit" that defines the period of the movement, you would know how much it will have to move each time it steps.
    How long should it take to go from the very top to the very bottom and back? Let's say, how many calls to verticalSwimBehaviour() ?
    Each time you call that method, the fish will step. If you start with the fish at the center, you could have something like:
    Code:
    Position = (Amplitude / 2) * cos((stepNumber++ % TOTAL_STEPS) * 2 * pi() / TOTAL_STEPS)
    step 0 will have a position = Amplitude * cos(0) = Amplitude * 1 = Amplitude / 2.
    step TOTAL_STEPS/2 will be = Amplitude * cos(pi) = - Amplitude / 2
    step TOTAL_STEPS will be step 0 again.

    You could use
    Code:
    (stepNumber % TOTAL_STEPS)
    or not. Cos() is a periodic function so it really won't make a difference.

    This will not cover changes in speed or direction, it would only work for a constant uninterrupted movement. A change in direction would imply a +TOTAL_STEPS/2 increment of stepNumber, and a change of speed woule imply a change of the TOTAL_STEPS value.
    A bigger TOTAL_STEPS value will give you better resolution of course, but you'll have to call your stepping method more often to complete one cycle in the same time.

    Play around for a while and you should get it working without problems.
    Last edited by Nikel; November 21st, 2011 at 11:32 AM. Reason: just typos

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured