Results 1 to 5 of 5

Thread: Problems with Viewport.Height & Width

  1. #1
    Join Date
    Aug 2009
    Posts
    76

    Problems with Viewport.Height & Width

    I'm developing some codes in XNA, and makes an application where a sprite will bounce of the edges of the screen. I have the following code

    Code:
      if (Position.Y < mViewport.Y) 
      {
      mDirection.Y = -mDirection.Y; 
      }
      if (Position.X < mViewport.X) 
      {
      mDirection.X = -mDirection.X; 
      }
      if (Position.Y > mViewport.Y + mViewport.Height) 
      {
      mDirection.Y = -mDirection.Y; 
      }
      if (Position.X > mViewport.X + mViewport.Width) 
      {
      mDirection.X = -mDirection.X; 
      }
    The top 2 if clauses works fine, (if I comment out the 2 lower, it will bounce from left and top edge of the screen) but the bottom 2 only works if I hard code a value instead of mViewport.Width / Height, for example. 400 and then the image will bounce back when it comes to 400, but if I use display the value it just shakes the whole picture and does not move. Why is it so? What is the problem with my mViewport.Width / Height?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problems with Viewport.Height & Width

    See some of your IF test. I think the fact that the "<" and ">" have greater priority then a "+" and "-".

    I think your code is interpreted something like runtime

    if (True + mViewport.Width) // if pos.X is larger than Viewport.X
    if (False + mViewport.Width) // if pos.x is less a Viewport.X

    Try putting brackets around:
    Code:
    if (Position.X > (mViewport.X + mViewport.Width))
    ....
    Do not know if it works, but it is my tips

  3. #3
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Problems with Viewport.Height & Width

    I think it sounds strange that <> has a higher priority than + -.

    Try this:
    Code:
    if (Position.Y <mViewport.Y) 
    {
    mDirection.Y =-mDirection.Y; 
    }
    if (Position.X <mViewport.X) 
    {
    mDirection.X =-mDirection.X; 
    }
    if (Position.Y> mViewport.Y + mViewport.Height) 
    {
    Position.Y-= 5; 
    mDirection.Y =-mDirection.Y; 
    }
    if (Position.X> mViewport.X + mViewport.Width) 
    {
    Position.X-= 5; 
    mDirection.X =-mDirection.X; 
    }
    I can imagine that the problem is that when PX is larger than mV.X + mV.Width, so it stays there even if the direction is changed. Thus, it will only go into the lower block, over and over again, and change direction.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problems with Viewport.Height & Width

    I've always been of the opinion that the + and - located on the bottom of the priority list, but I've been wrong before

    But I tend to use parentheses anyway to be sure, empty when it comes to something as simple as multiplication

    Code:
    1 + 2 * 3 = 7 
    (1 + 2) * 3 = 9 
    
    This means that 1 + 2 * 3 is the same as 1 + (2 * 3) 
    Believed therefore that 1> 2 + 3 was the same as (1> 2) + 3

  5. #5
    Join Date
    May 2008
    Posts
    685

    Re: Problems with Viewport.Height & Width

    Arithmetic comes before the comparisons. The order is as follows:

    ., (), [], X + +, x -, new, typeof, checked, unchecked (Primary)
    +, -,:, ~, + + X, - y, (T) x (Unary, mathematical operations with only one operand)
    *, /,% (Multiplicative)
    +, - (Additive)
    <<,>> (Shift)
    <,>, <=,> =, Is, as (Relational and type testing)
    ==,! = (Equality)
    &, ^, | (Bitwise logical)
    & &, | | (Logical)
    ?: (Conditional)
    =, *=, / =,% =, + =, -=, <<=,> =, & =, ^ =, | = (Assignment)

Similar Threads

  1. How to know height of row in Excel?
    By Dipanwita in forum Windows Software
    Replies: 2
    Last Post: 04-01-2012, 07:19 PM
  2. Replies: 5
    Last Post: 26-12-2011, 11:22 AM
  3. MAX width and max height of a image using css
    By Rutajit in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 07:30 PM
  4. Fixed height & width in new window
    By Gustak in forum Customize Desktop
    Replies: 2
    Last Post: 08-12-2008, 06:05 PM
  5. Variable height in CSS
    By Sentential in forum Software Development
    Replies: 8
    Last Post: 22-10-2008, 02:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,398,686.03126 seconds with 17 queries