Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a simple question I think, if point a is to the right of another point I a dirx to be 1, otherwise -1. Likewise for a diry var, if it's above the other point I want it to be -1 otherwise 1. This is what I've worked out so far:

dirx = (abs(disx+1)/(disx+1))*-1;
diry = (abs(disy+1)/(disy+1))*-1;

The disx and disy are the distance from each other on x and y. I have a +1 to keep from having a division by 0 error, lastly I'm *-1 it all so that it'll match the coordinate system of the game screen where something higher has a lesser value than something lower.

My problem is that I'm still getting a division by 0 error with this code, I'm not sure why as I even added +1 to make sure it wasn't possible???

share|improve this question
    
I'm having trouble following, a picture would help? –  Tetrad Sep 7 '11 at 5:51
1  
Actually, the title implies something different than the actual question here... finding which side one vector is from another vector can be done using the dot product but your question is about points, not vectors... –  bummzack Sep 7 '11 at 6:44

3 Answers 3

if disX = -1, then you'll always get a divide by zero error using that code.

If you're programming in a c-based language, just use the ? operator, a la:

if(disX == 0)
{  
  dirX = 0;  
}
else
{  
  dirx = (disX > 0) ? 1 : -1;  
}
share|improve this answer
    
there just is one minor bug in your code, it returns -1 if disX==0. but we know 0 has no sign, it's neither positive nor negetive! –  Ali.S Sep 7 '11 at 17:30
    
Yeah, I wasn't sure how Isaiah wanted to handle 0. I'll put a disclaimer in. –  Jordaan Mylonas Sep 7 '11 at 20:39
    
This code is now identical to signum or sign which is a standard function in many languages. (Albeit not C/C++.) –  user744 Sep 8 '11 at 2:42

What you need is the Cross Product of the two points and check if it is bigger or smaller than zero (zero means it is on the same line):

Point a Point b

cp = a.x*b.y - a.y*b.x;

For example:

a= (2,5); b= (3,4) cp = 2*4 - 5*3 = 8 - 15 = -7

as cp < 0 the point a is on the left of point b

Your function can thus just do this to return -1 or 1:

cp = a.x*b.y - a.y*b.x;
if(cp<0)
   return -1;
else
   return 1;
share|improve this answer
    
BTW, if you just have a grid (points cant be 'anywhere') do 'if dirx < 0 return(-1) else return(1)' –  Valmond Sep 7 '11 at 8:38

If I understood your question correctly you just wanted to normalize a vector.

let me break your question into 3 parts :

  1. why do you get division by zero?
    it's simply because either disx or disy values could be exactly (-1). in that situation you computer will start computing (abs(-1+1)/(-1+1))*-1 which is basically (0/0)*-1!

  2. how to find sign of a number (or how to normalize 1D vector)?
    let's say you have a number 'X' and you want to check if this number is negative or positive. usually there is a function in math libraries named sign or sgn. They implement that function like this:

    int sign(int x)
    {
        if (x > 0)
            return 1;
            else
            if (x < 0)
                return -1;
            else
                return 0;
    }
    

    note that there is one extra condition where X is exactly zero, in that case sign function should return 0 since x doesn't have any sign!

  3. how to normalize a 2D Vector (or a vector in higher dimensions)?
    the basic idea is to create a vector which has a length of exactly 1 so the first step is to calculate length of current vector.

    length = sqrt(vec.x^2 + vec.y^2 + vec.z^2 + ...)
    

    the next step is to shorten (or stretch) your vector so that it's total length equals to exactly 1. and here is how it's done:

    vec.x = vec.x / length;
    vec.y = vec.y / length;
    vec.z = vec.z / length;
    .
    .
    .
    

again note that length is always some positive value but it can be equal to zero (where all elements in your vector are exactly zero). you have to check if a vector's length is zero you can't compute a normalized vector!

share|improve this answer
    
I believe what he's trying to do is return -1 if it's to the left, +1 if it's to the right. This is quite different to normalisation. –  Jordaan Mylonas Sep 7 '11 at 5:50
1  
@jordaan: that's exactly second part of my answer, and it's a normalization of 1D vector! –  Ali.S Sep 7 '11 at 5:58
    
Ah of course. Sorry, must've had a brain spasm. Forgot that -1 is a normalised 1D :S –  Jordaan Mylonas Sep 7 '11 at 6:12
    
why did I get a downvote? –  Ali.S Sep 7 '11 at 7:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.