# Vector2 = (NaN, NaN) ?

Page 1 of 1

## 2 Replies - 5728 Views - Last Post: 30 September 2012 - 09:22 AM

### #1 superkb10

• D.I.C Regular

Reputation: 29
• Posts: 298
• Joined: 27-November 11

# Vector2 = (NaN, NaN) ?

Posted 29 September 2012 - 02:14 PM

Hey guys, I've been writing a program in XNA using a flick gesture to flick a ball over the screen and keep it up. But I've run in to an unexpected problem, here's the current code for the flick gesture (this is a Windows Phone Game)

```            if (Touch.State == TouchLocationState.Pressed)
{
curTouchPoint = new Point((int)Touch.Position.X, (int)Touch.Position.Y);
FlickActive = true;
}
if (Touch.State == TouchLocationState.Moved)
{
prevTouchPoint = curTouchPoint;
curTouchPoint = new Point((int)Touch.Position.X, (int)Touch.Position.Y);
if (FlickActive)
{
for (int i = 0; i < Balls.Length; i++)
{
if(Balls[i].Rect.Contains(curTouchPoint))
{
Balls[i].Direction = new Vector2(curTouchPoint.X - prevTouchPoint.X,
curTouchPoint.Y - prevTouchPoint.Y);
Balls[i].Direction.Normalize();
FlickActive = false;
Score += 100;
return;
}
}
}
}

```

So as you can see, basically what I do, is subtract the current point from the last point. However, what's happening is that sometimes, for some reason, the ball will go to the top left corner, what's supposedly (0,0). I had no idea why this happened, so I did some simple debugging, and found that the value of my direction Vector was NaN, along with my Position Vector. I'm not sure why this happens, I've done some research online and it says it's the Normalize() part of it, but I haven't been able to find why exactly this happens. I've heard it's something like the square root of something, however, it only happens certain times, when I'm flicking, I've been unable to debug a way to find out how exactly it occurs. Any explanation as to why this happens or a method to solve would be greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Vector2 = (NaN, NaN) ?

### #2 BBeck

• Here to help.

Reputation: 779
• Posts: 1,871
• Joined: 24-April 12

## Re: Vector2 = (NaN, NaN) ?

Posted 30 September 2012 - 06:47 AM

Well, I haven't looked at your code there. If what I say here doesn't help, say so and maybe one of us will take a look at the code. But I think maybe some understanding might help you find the problem.

First of all NaN means "Not a Number". Basically it means that it doesn't exist. It's nothing. Zero is a number; it's something. What you have is nothing, not zero. To humans the difference between zero and nothing is that they are the same thing. But to the computer a blank or a zero is something that it stores and keeps track of. For the computer that's very different from absolute nothing which we call "null" in computer talk. NaN is essentially "null" or absolute nothing.

The reason your Vector2 goes NaN is because you changed it's length to zero. When the length of a vector becomes zero it ceases to exist. That's a law of the real world, like the law of gravity. It's not a computer thing. It's part of the definition of what a vector is.

That may be a bit confusing if you don't know what a vector is, but it makes absolute sense if you understand vectors. A vector is a direction that's permanently tied to an amount. Nothing more and nothing less. You can best think of it as an arrow with it's tail at the origin (x=0,y=0) and the head of the arrow at the 2D point indicated in your Vector2. It "points" from the origin to the head at your Vector2 numbers. It's "amount" is the length of the arrow from the origin to the head at your Vector2 numbers.

When it's length becomes zero, then both the head and the tail are at 0,0. It not only ceases to have a length/amount at that point, but it also now can't point in any direction. Both of those conditions are requirements for it to exist and now neither of those requirements are met.

This is actually a pretty good argument as to why you should never use vectors to store positions. Although that's going to be a losing argument because everyone, especially professionals, does it.

There are actually advantages for storing positions as vectors. The main advantage is that it allows you to do vector math, which will come in very handy when you start doing physics. On the other hand, storing positions as vectors tends to confuse beginners into thinking that positions are vectors and they are not. A vector is two positions; the first position is the origin and the second position is the head. More importantly, vectors are not about positions at all but about amounts that are tied to directions. However - since the head is a position and the tail is understood rather than stored, you can easily store a position in the head of a vector. And that will actually work to your advantage when you start doing vector math for something like physics (which is probably why it's become an industry standard rather than storing points as points - which would still exist at 0,0).

Also, I think you can use "?" in some cases to accept null values, if that helps.

Anyway, if you multiply your vector by zero or you subtract an equal sized vector from it or you otherwise cause it's head to go to 0,0 or it's length to become zero, you have destroyed the vector and it no longer exists. Technically, you can't revive it at that point; you can only create a new vector to replace it.

This post has been edited by BBeck: 30 September 2012 - 06:51 AM

### #3 superkb10

• D.I.C Regular

Reputation: 29
• Posts: 298
• Joined: 27-November 11

## Re: Vector2 = (NaN, NaN) ?

Posted 30 September 2012 - 09:22 AM

Ah, I believe I see the problem now. I was storing the location of the touch, to a point and casting the floats to ints to convert from Vector2 to Point. That would cause it to round down at certain times, so prevTouchPoint would equal curTouchPoint. But I've solved it simply by converting from Point to Vector2. Thanks for the help!