Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-11-2009
Member
 
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?
Reply With Quote
  #2  
Old 10-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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
Reply With Quote
  #3  
Old 10-11-2009
Member
 
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.
Reply With Quote
  #4  
Old 10-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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
Reply With Quote
  #5  
Old 10-11-2009
Member
 
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)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problems with Viewport.Height & Width"
Thread Thread Starter Forum Replies Last Post
How to know height of row in Excel? Dipanwita Windows Software 2 04-01-2012 07:19 PM
Why Apple QuickTime is swapping height and Width of Video playing? Bhuvnesh 1 Windows Software 5 26-12-2011 11:22 AM
MAX width and max height of a image using css Rutajit Software Development 2 22-05-2009 07:30 PM
Fixed height & width in new window Gustak Customize Desktop 2 08-12-2008 06:05 PM
Variable height in CSS Sentential Software Development 8 22-10-2008 02:51 PM


All times are GMT +5.5. The time now is 06:40 AM.