Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:13:26+00:00 2026-06-07T19:13:26+00:00

I have a sprite that moves along a vector (-0.7,-0.3). I have another point

  • 0

enter image description here

I have a sprite that moves along a vector (-0.7,-0.3). I have another point whose coordinates I have – let’s call them (xB|yB). Now, quite some time ago I learned to calculate the perpendicular distance from a vector to a point (first formula on this page http://en.wikipedia.org/wiki/Perpendicular_distance). However I tried it, and if I log it, it returns an unbelievably high value that is 100% false. So what do I do wrong ? Have a look at the image I provided.

incomingVector = (-0.7,-0.3) //this is the vector the sprite is moving along

bh.position is the point I want to calculate the distance to

Here is the code:

        // first I am working out the c Value in the formula in the link given above
        CGPoint pointFromVector = CGPointMake(bh.incomingVector.x*theSprite.position.x,bh.incomingVector.y*theSprite.position.y);
        float result = pointFromVector.x + pointFromVector.y;
        float result2 = (-1)*result;

        //now I use the formula
        float test = (bh.incomingVector.x * bh.position.x + bh.incomingVector.y * bh.position.y + result2)/sqrt(pow(bh.incomingVector.x, 2)+pow(bh.incomingVector.y, 2));

        //the distance has to be positive, so I do the following
        if(test < 0){
            test *= (-1);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:13:28+00:00Added an answer on June 7, 2026 at 7:13 pm

    let us implement the formula again, according to the contents of your original link.

    • we have a vector for the line: V(a; b)
    • we have a point on the line (the centre of the sprite): P(x1, y1)
    • we have another point somewhere else: B(xB, yB)

    for the testing here are two rows of random values:

    1. a = -0.7; b = -0.3; x1 = 7; y1 = 7; xB = 5; yB = 5;
    2. a = -0.7; b = -0.3; x1 = 7; y1 = 7; xB = 5.5; yB = 4;

    the numerator is the following then: (it seems you are calculating the numerator an unknown way, I don’t understand why you did it because this is the proper way to calculate the numerator for the linked formula, perhaps this is why you got totally wrong distances.)

    float _numerator = abs((b * xB) - (a * yB) - (b * x1) + (a * y1));
    // for the 1. test values: (-0.3 * 5) - (-0.7 * 5) - (-0.3 * 7) + (-0.7 * 7) = -1.5 + 3.5 + 2.1 - 4.9 = -0.8 => abs(-0.8) = 0.8
    // for the 2. test values: (-0.3 * 5.5) - (-0.7 * 4) - (-0.3 * 7) + (-0.7 * 7) = -1.65 + 2.8 + 2.1 - 4.9 = -1.65 => abs(-1.65) = 1.65
    

    the denominator is the following then:

    float _denomimator = sqrt((a * a) + (b * b));
    // for the 1. test values: (-0.7 * -0.7) + (-0.3 * -0.3) = 0.49 + 0.09 = 0.58 => sort(0.58) = 0.76
    // for the 2. test values: (-0.7 * -0.7) + (-0.3 * -0.3) = 0.49 + 0.09 = 0.58 => sort(0.58) = 0.76
    

    the distance is obvious now:

    float _distance = _numerator / _denominator;
    // for the 1. test values: 0.8 / 0.76 = 1.05
    // for the 2. test values: 1.65 / 0.76 = 2.17
    

    and these results (1.05 and 2.17) are the correct distances exactly for our random values, if you can draw the lines and the points on the paper you can measure the distance and you would get the same values, using standard ruler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a sprite that moves based on touch location. I am using
I've a sprite that on touch have to start move along the Y coord.
I have a sprite that moves on the bottom of the screen from left
I have a sprite loaded from a PNG that has an alpha channel. The
I have a class called Sprite, and ballSprite is an instance of that class.
I have a sprite inside another sprite SpriteB is inside SpriteA I would like
I have 2 1024x1024 sprite sheets, one containing several 480x320 background images and another
Ok so i have a character_mc that moves on mouse click, a logtxt dyanamic
I have a UIButton that moves around randomly on the screen. On clicking on
I have got this code that I used before, but now that it I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.