CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2011
    Posts
    6

    Movement Cos Wave

    Hi,

    Basically, what I'm trying to do is get an object to move vertically up the screen in a nice cos wave motion, swaying back and forth.

    At the moment I have it just going straight up and it looks alright but I think the wave would add another level to it.

    I have absolutely no idea how to do this and from what I've seen when looking around there isn't many people/anyone else trying to do the same thing.

    public void verticalSwimBehaviour()
    {


    Vector3 tokenPosition = this.PossessedToken.Position; //get the current position of the fish and assign it to tokenPosition variable
    tokenPosition.Y = tokenPosition.Y + ySpeed * mFacingDirection;
    if (tokenPosition.Y == 400)
    {
    tokenPosition.Y = 0;
    }
    this.PossessedToken.Position = tokenPosition; // assign the new position based on above calculation back to the fish position}
    }

    This takes a vector which is the current position of the fish and make it go vertically by multiplying it by the speed value and the facing direction which decides if it's going up or down.

    As I mentioned before. I have no idea how to turn this into a cos wave.

    Help!

    Thanks.

  2. #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

  3. #3
    Join Date
    Nov 2011
    Posts
    6

    Re: Movement Cos Wave

    So in your first bit of code, what value is Amplitude?

    And say my object moves at a speed of 2, so 2 up the Y axis until it hits the top which is at 250.

    So STEPS would be 125?

    Thanks for the quick answer btw, the only things that I couldn't really figure out..

  4. #4
    Join Date
    Nov 2011
    Posts
    6

    Re: Movement Cos Wave

    I've got it working right now with this.


    Code:
    public void verticalSwimBehaviour()
            {
                Random random = new Random();           // new random number generator
                    Vector3 tokenPosition = this.PossessedToken.Position; // get the current position of the fish and assign it to tokenPosition variable
                    tokenPosition.Y = tokenPosition.Y + ySpeed; // mSpeed is always a constant positive value, 
                    float stepNumber = tokenPosition.Y;
                    float Amplitude = random.Next(10, 50);
                    
                    double movement = (50/ 2) * Math.Sin((stepNumber++ % TOTAL_STEPS) * 2 * Math.PI / TOTAL_STEPS);
    
                    tokenPosition.X = (float)movement;
                
                
                     if (tokenPosition.Y >= 400)
                    {
                     
                        int bubbY = random.Next(-10, 10);       // random pos for Y
                        int bubbX = random.Next(-250, 250);     // random pos for X
                        tokenPosition.Y = bubbY;                //sets token pos to the random
                        tokenPosition.X = bubbX;                //sets token pos to the random
                    }
                   
                    this.PossessedToken.Position = tokenPosition; // assign the new position based on above calculation back to the fish position}
            }
    But the bubbles that spawn all spawn in the same place and do the same wave, how do I make it so the bubbles can spawn wherever they like and just move upwards in a wave motion?

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

    Re: Movement Cos Wave

    LOL what bubbles??? =P

    Ok, for the bubbles, you know where your fish is, so you can calculate how "deep" it is. What are the bubbles made of? Air? If that's the case, I don't think your bubbles should have any wave motion, they should just go up and just pop when they get to the surface, but that would mean they should be an entirely different object and treated differently.
    I'd just have a List<Bubbles> and Fish.EmitBubble() method, that would Add() a bubble to the list. The list handling would be independant of the Fish (plural) you have, and they all would go up and pop regardless of what is happening with all your fauna.
    You'd have something like Bubble bob = new Bubble(startingDepth);
    I'm assuming things anyway... I don't know what your app is aiming for.

    As for your previous post, by Amplitude I meant the total vertical movement span. The total depth of the pool (or whatever the fish is swiming in) if it will oscillate from top to bottom. The speed would be 2 if your fish is moving linearly. Since you want it to move in a Cos() (or Sin()) way, the size of each step will depend on the section of the cycle you're in. Near the surface (or bottom), the step will get closer to 0, and around the middle it will be max. The natural consequence of that is that the speed cannot be constant, since the step size is changing.

    If you're interested in the math behind it, check this link about Simple Harmonic Motion. How much you read is up to you, but the first part (especially the animations) should help you. There is a slight convention difference on how to define Amplitude, but the concept is the same.
    Also, check formula (6). Not the formula itself, but what it represents.

    I hope it helps.

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