Oscillating Sprite Movement in XNA
        Posted  
        
            by 
                Nick Van Hoogenstyn
            
        on Game Development
        
        See other posts from Game Development
        
            or by Nick Van Hoogenstyn
        
        
        
        Published on 2011-03-02T00:17:29Z
        Indexed on 
            2011/03/02
            7:32 UTC
        
        
        Read the original article
        Hit count: 502
        
I'm working on a 2d game and am looking to make a sprite move horizontally across the screen in XNA while oscillating vertically (basically I want the movement to look like a sin wave). Currently for movement I'm using two vectors, one for speed and one for direction. My update function for sprites just contains this:
Position += direction * speed * (float)t.ElapsedGameTime.TotalSeconds;
How could I utilize this setup to create the desired movement? I'm assuming I'd call Math.Sin or Math.Cos, but I'm unsure of where to start to make this sort of thing happened. My attempt looked like this:
    public override void Update(GameTime t)
    {
        double msElapsed = t.TotalGameTime.Milliseconds;
        mDirection.Y = (float)Math.Sin(msElapsed);
        if (mDirection.Y >= 0)
            mSpeed.Y = moveSpeed;
        else
            mSpeed.Y = -moveSpeed;
        base.Update(t, mSpeed, mDirection);
    }
moveSpeed is just some constant positive integer. With this, the sprite simply just continuously moves downward until it's off screen. Can anyone give me some info on what I'm doing wrong here? I've never tried something like this so if I'm doing things completely wrong, let me know!
© Game Development or respective owner