Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2009
    Posts
    9

    I love these C# tutorials - and a really easy question

    Ive learned more through 3d buzz then i have in my 3 - almost 4 - years of school -_-

    Ok so, you guys know that HelloWorld2 project? Ya the really easy one lol - the one with the button. Well i was adding some sound in there on my own, and that works great. Now i want the sound to stop when the button stops bouncing (with gravityApplied). The only problem is the ball never really stops, the velocity is a floating point so it just turns into this really long floating point decimal, even when it is pretty much motionless. The problem with this is i cant predict what the velocty of X will be, so i cant throw an if statement in there that says

    if (velocityX <= 0)
    playSound

    its never a deffinitive number.

    any suggestions?
    Last edited by Pugnap00; 08-18-2009 at 12:50 PM.

  2. #2
    Join Date
    May 2007
    Location
    Upstate, NY
    Posts
    504
    I'm not exactly sure what you meant by "i cant predict what the velocty of X will be" and "its never a deffinitive number", but it sounds like you're looking for something like this:

    if (velocityX < 0.1)
    playSound
    The spice must flow.

  3. #3
    Join Date
    Jul 2009
    Posts
    9
    Quote Originally Posted by Wren Akula View Post
    I'm not exactly sure what you meant by "i cant predict what the velocty of X will be" and "its never a deffinitive number", but it sounds like you're looking for something like this:

    if (velocityX < 0.1)
    playSound
    sorry if that made no sense. What i mean is the velocity is always different once the button starts slowing down when it is bouncing. Using a value of 0.1 doesnt work either because if you remember when the button collides with the edge of the client window, the bounce manager applis a -1 to the velocity to simulate the bouncing. THe vlocity before you apply gravity is set at 2, whcih means its constantly switching from 2 to -2.

  4. #4
    Join Date
    Jun 2003
    Posts
    434
    you could setup ball states bounce, roll, idle etc..
    My 3DS Max Sprite Sheet Renderer
    A government big enough to give you everything you want,
    is strong enough to take everything you have.
    -- Thomas Jefferson

  5. #5
    Join Date
    Jul 2009
    Posts
    9
    good idea, i didnt think of that. Sorta like a FSM. Ill let you know if it works.

  6. #6
    Join Date
    May 2007
    Location
    Upstate, NY
    Posts
    504
    Oh ok, maybe all you need is the absolute value. I'll see if I can go dig up that project and take a peek.

    EDIT:
    Well I took a look at mine, which probably didn't have many, if any, modifications from what the course had us build. So, ours should have been pretty similar as far as the "physics" mechanics go.

    Anyway, the velocities do seem to be all over the place when it has essentially stopped moving. And the way mine is written, when the absolute values of the velocities are less than 1, the box does not actually move. So I added this into my updateScene() method:

    Code:
    if (myObject.Bottom > 260) //checks that the box is at the bottom of the window
    {
        if (Math.Abs(velocityY) < 1)
            velocityY = 0f;
        if (Math.Abs(velocityX) < 1)
            velocityX = 0f;
    }
    If the box is near the bottom of the window, it will set either velocity to 0 if it's absolute value is currently less than 1. Since the box has ceased movement in that direction anyhow, there is no visual difference. We make sure the box is at the bottom of the window because otherwise velocityY would be set to 0 at the apex of the first bounce.

    NOTE: I had to tinker with the 260 value that I'm checking myObject.Bottom against. For whatever reason, with a 300x300 window, when the box (myObject) is on the bottom of the window, myObject.Bottom gives 261.

    So, with the velocities dropping to 0 when the box stops, it should now be fairly easy to get the sound effect playing at the appropriate time. Maybe it's not the most awesome solution, but I hope it helps.
    Last edited by Wren Akula; 08-18-2009 at 05:45 PM.
    The spice must flow.

  7. #7
    Join Date
    Jul 2009
    Posts
    9
    Quote Originally Posted by Wren Akula View Post
    Oh ok, maybe all you need is the absolute value. I'll see if I can go dig up that project and take a peek.

    EDIT:
    Well I took a look at mine, which probably didn't have many, if any, modifications from what the course had us build. So, ours should have been pretty similar as far as the "physics" mechanics go.

    Anyway, the velocities do seem to be all over the place when it has essentially stopped moving. And the way mine is written, when the absolute values of the velocities are less than 1, the box does not actually move. So I added this into my updateScene() method:

    Code:
    if (myObject.Bottom > 260) //checks that the box is at the bottom of the window
    {
        if (Math.Abs(velocityY) < 1)
            velocityY = 0f;
        if (Math.Abs(velocityX) < 1)
            velocityX = 0f;
    }
    If the box is near the bottom of the window, it will set either velocity to 0 if it's absolute value is currently less than 1. Since the box has ceased movement in that direction anyhow, there is no visual difference. We make sure the box is at the bottom of the window because otherwise velocityY would be set to 0 at the apex of the first bounce.

    NOTE: I had to tinker with the 260 value that I'm checking myObject.Bottom against. For whatever reason, with a 300x300 window, when the box (myObject) is on the bottom of the window, myObject.Bottom gives 261.

    So, with the velocities dropping to 0 when the box stops, it should now be fairly easy to get the sound effect playing at the appropriate time. Maybe it's not the most awesome solution, but I hope it helps.
    Awesome, that makes perfect sense! I was going brain dead trying figure out how to make it completely stop once it hits the bottom. Let me impliment this, and ill post up my code snippet if it works.

    Thanks again!

  8. #8
    Join Date
    Jul 2009
    Posts
    9
    Got it! Thanks for your help!

    Code:
    public partial class Form1 : Form
        {
            private float velocityX = 2;
            private float velocityY = 5;
            private bool gravityActive = false;
            private bool soundActive = true;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            #region methods
            private void myObject_Click(object sender, EventArgs e)
            {
                gravityActive = !gravityActive;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                UpdateScene();
            }
    
            private void myObject_MouseEnter(object sender, EventArgs e)
            {
                myObject.Text = "Apply Gravity";
            }
    
            private void myObject_MouseLeave(object sender, EventArgs e)
            {
                myObject.Text = "Hello World";
            }
    
            private void playTick()
            {
                SoundPlayer tick = new SoundPlayer(@"C:\Users\Shane\Documents\Visual Studio 2008\Projects\HelloWorld2\HelloWorld2\tick.wav");
    
                if (soundActive)
                {
                    tick.Play();
                }
                
    
                if (myObject.Bottom > 260) //checks that the box is at the bottom of the window
                {
                    if (Math.Abs(velocityY) < 1)
                    {
                        soundActive = false;
                        velocityY = 0f;
                        tick.Stop();
                    }
    
                    if (Math.Abs(velocityX) < 1)
                    {
                        soundActive = false;
                        velocityX = 0f;
                        tick.Stop();
                    }
                }
            }

  9. #9
    Join Date
    May 2007
    Location
    Upstate, NY
    Posts
    504
    Hey, I'm glad I was able to help. I may have done a few things differently, but if it does what you need it to, then great!

    Happy traveling on your C# journey.
    The spice must flow.

Posting Permissions

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