XNA Creators Club Online
Page 1 of 1 (16 items)
Sort Posts: Previous Next

What i'm doing wrong? (&& operator wrong!?)

Last post 11/16/2009 8:48 PM by dadoo Games. 15 replies.
  • 11/15/2009 7:44 PM

    What i'm doing wrong? (&& operator wrong!?)

    Hey guys, i have a problem with my game and i need help...

    this void since not working... any one can fixe this?

           private void GravidadeNomal()
            {
                if ((Caixa.Aceleração.Y != 1) && (G == true))
                {
                    Vector2 g = Gravidade / 10;
                    Caixa.Aceleração += g;
                }
            }

    PSs:
           "Caixa" is one textured box thats appear on the screen
           "Aceleração" is a Vector2D
           "Gravidade" is a Vector2D Gravidade = new Vector2D(0, 10)
  • 11/15/2009 8:01 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Is the problem that the code doesn't compile? If so, what is the error?
    Is the problem that the code compiles and runs, but doesn't do what you think it should?
    If so, what do you think it should do? And what is it doing? (Have you put a breakpoint in there and stepped through it?)
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 11/15/2009 8:52 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    In my game, the box ("Caixa") have to be drawed on the scren whit acceleration ("Aceleração")!

    Than the new value of Caixa.Aceleração will plus whit Caixa.Velocidade (Velocity)...

    And finale, Caixa.Velocidade will plus whit Caixa.Posição (Position)... To be Draw on the Screen!

    But the box initialize and stay in there original position ("Caixa.Posição")...

    Please... Any one help!

    PSs: This is the basic of the code ( realy simple )

    double Tempo

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
     
    bool G
     
    GameTime Milisegundo = new GameTime(); 
     
    Vector2 Gravidade = new Vector2(0, 10); 
     
    CaixaNormal Caixa// CaixaNormal is one simple Class 
     
            protected override void LoadContent() 
            { 
                // Create a new SpriteBatch, which can be used to draw textures. 
                spriteBatch = new SpriteBatch(GraphicsDevice); 
     
                Caixa = new CaixaNormal(Content.Load<Texture2D> 
                    ("Texturas\\Caixa"), 5); 
                Caixa.Posição = new Vector2(5, 10); 
     
                AtivarGravidade(); 
     
                // TODO: use this.Content to load your game content here 
            } 
     
            public void AtivarGravidade() 
            { 
                G = true
            } 
     
            private void Básico() 
            { 
                if (Tempo < 0.099) 
                { 
                    Tempo += Milisegundo.ElapsedGameTime.Milliseconds; 
                } 
                if (Tempo == 0.100) 
                { 
                    Caixa.Velocidade += Caixa.Aceleração
                    Caixa.Posição += Caixa.Velocidade
                    Tempo = 0.000; 
                } 
            } 
     
            private void GravidadeNomal() 
            { 
                if ((Caixa.Aceleração.Y != 1) && (G == true)) 
                { 
                    Vector2 g = Gravidade / 10; 
                    Caixa.Aceleração += g; 
                } 
            } 
     
            protected override void Update(GameTime gameTime) 
            { 
                // Allows the game to exit 
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
                    this.Exit(); 
     
                Básico(); 
     
                GravidadeNomal(); 
                 
                // TODO: Add your update logic here 
     
                base.Update(gameTime); 
            } 
     
            protected override void Draw(GameTime gameTime) 
            { 
                GraphicsDevice.Clear(Color.CornflowerBlue); 
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
                spriteBatch.Draw(Caixa.TexturaCaixa.Posição, Color.White); 
                spriteBatch.End(); 
     
     
     
                // TODO: Add your drawing code here 
     
                base.Draw(gameTime); 
            } 

    CaixaNormal.cs
    class CaixaNormal 
        { 
            public float Massa
            public Vector2 Posição
            public Vector2 Velocidade
            public Vector2 Aceleração
            public Texture2D Textura
     
            public CaixaNormal(Texture2D TexturaCarregadafloat MassaDaCaixaEmKg
            { 
                Posição = Vector2.Zero; 
                Velocidade = Vector2.Zero; 
                Aceleração = Vector2.Zero; 
                Textura = TexturaCarregada
                Massa = MassaDaCaixaEmKg
            } 
        } 

  • 11/15/2009 9:26 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    One thing that stands out is that you are comparing Tempo to 0.100 using the '==' operator.

    Generally, you don't want to compare floating point numbers with the equality operator, since because of floating point errors things will never be exactly a particular value.  I suspect that if you put a breakpoint in your velocity/position calculations you will find that code is never executed.

    So instead you might want to do:
    if (Tempo > 0.100)
    {
        Tempo -= 0.100;

        // do your velocity, position calculations, etc....
    }


    You also have the same problem in GravidadeNomal.  The "((Caixa.Aceleração.Y != 1)" will almost always be true.  I'm not sure if that's what you want.
  • 11/15/2009 10:47 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Ok, I done this changes, but anything changes!

    Thanks very much for the tip, mtnPhil, but i will rewrite my code and make this works.

    Thanks, again.
  • 11/16/2009 12:23 AM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Another problem is that you are using this:

    GameTime Milisegundo = new GameTime(); 

    But I don't ever see where the Milisegundo value is changed.  So the EllapsedGameTime will always be zero.

    You should instead be using the GameTime object passed to the Update method.
  • 11/16/2009 1:41 AM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    GameTime Milisegundo do not change! It's always 0.001...

    This variable is used in Básico(), so it's used in the Update method. ( if you had not seen )

            protected override void Update(GameTime gameTime)  
            { 
                          // Allows the game to exit  
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
                    this.Exit();  
      
                Básico(); 
      
                GravidadeNomal(); 
                  
                // TODO: Add your update logic here  
      
                base.Update(gameTime);  
            }  
  • 11/16/2009 4:38 AM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)


    "GameTime" is just a value type, like a "float" or a "Vector3." It will not change unless you assign a new value. That's why Update() passes in a new GameTime value for you. You can assign the GameTime passed into Update() to your Milisegundo member to make it change.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 11/16/2009 10:42 AM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    jwatte:

    "GameTime" is just a value type, like a "float" or a "Vector3."


    A minor nit pickingly correction to a mistake I made before, too: Apparently GameTime is a reference type, not a value type. Only the TimeSpans it defines are value types.
    We are boki. The rest is known.

    The not so known part of the rest: It is Björn or Bjoern, but never Bjorn.

    Twitter ~ Bnoerj ~ SharpSteer ~ SgtConker.com
  • 11/16/2009 3:29 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    But... Tempo += Milisegundo.ElapsedGameTime.Milliseconds; ... do not means Tempo += 0.001    ?

    PS: I already try put just this code... Tempo += 0.001 ... but nothing changes.

    "Tempo" is a double
  • 11/16/2009 3:40 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    You need to get friendly with your debugger. Put some breakpoints in your code and/or single-step through the functions in question. By seeing the flow of execution and examining your variables at each step, you should be able to easily tell what is going wrong.

    As for the Milisegundo variable, you should just pass the gameTime parameter from your Update method into any other method that needs access to game time. Your Milisegundo variable will not work as you are expecting it to.
  • 11/16/2009 3:50 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Milliseconds is guaranteed to not work with your code, because it returns an integer.  So, every value after that addition is going to be greater than 1.  TotalMilliseconds would be more appropriate, but still not correct.  With your usage, you want TotalSeconds. 

    Tempo += 0.001 is also likely to not work due to representation errors.  As stated before, there is rarely a good reason to use == with floating point comparisons.  Try changing your code to something like this:

    private void Básico()    
    {    
      if (Tempo < 0.099)    
      {    
        Tempo += Milisegundo.ElapsedGameTime.TotalSeconds;    
      }    
      if (Tempo >= 0.100)    
      {    
        Caixa.Velocidade += Caixa.Aceleração;    
        Caixa.Posição += Caixa.Velocidade;    
        Tempo -= 0.100;    
      }    
    }    
     
  • 11/16/2009 4:09 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Two other issues that I see are the previously mentioned issue with GameTime and an issue with your gravity function.

    First, the Milisegundo is only initialized with a new GameTime() call.  That should set it to all zeros.  It never gets updated in the code you have posted.  I don't know if you ever changed that.  So, it always being adding zero time.  You want to update it in the update function with:

    Milisegundo = gameTime; 

    Ideally, you wouldn't even do this and you would just pass the GameTime variable into your functions.  Or what I do, is to pull out the time variable that I'm interested in (TotalSeconds) and just pass that to my functions.

    The other issue has to do with your gravity function and it may not be an issue at all.  Assuming that no rounding errors occur in the math of the first call of GravidadeNormal(), then Caixa.Aceleração is going to have a Y value of 1.0f.  So, the acceleration value is going to change once and then your if statement will prevent further changes.  That might be what you want, but it is a poor way of going about it, imo.
  • 11/16/2009 8:21 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Oh! Realy thank you for the help, i got it!

    One more question... What the diference between this:

    if ((...) & (...)) {...} 

    and this:

    if ((...) && (...)) {...} 

                    ??
  • 11/16/2009 8:41 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    "&&" is the logical AND operator, meaing "if this is true AND that is true". "&" is the bitwise AND operator. For more information on bitwise operators in C#, see this article.
  • 11/16/2009 8:48 PM In reply to

    Re: What i'm doing wrong? (&& operator wrong!?)

    Also, in the context of booleans & means that both operands are always evaluated, while && allows for short-circuit evaluation

    bool IsA() { ... } 
     
    bool IsB() { ... } 
     
    void Foo() 
      // IsA is evaluated. If IsA returns false, IsB is never called 
      if(IsA() && IsB()) 
      { 
        .. 
      } 
     
      // IsA is evaluated. IsB is also evaluated regardless of what IsA returns 
      if(IsA() & IsB()) 
      { 
        .. 
      } 

Page 1 of 1 (16 items) Previous Next