Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sendatsu_Yoshimitsu · Jul 01, 2019 at 08:59 AM · vector3math

Calculating endpoints of directional vector

I have the feeling the solution is horribly obvious, but I'm having trouble understanding a bit of vector math: if I want a vector representing the distance between two points, I do something like this:

 Vector3 origin;
 Vector3 destination;
 Vector3 direction = (destination - origin).normalized * Vector3.distance(origin, destination);


So here's where I'm confused: if someone hands me direction without any of the accompanying values, how would I calculate origin and destination? Would origin simply be direction.normalized?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Jul 01, 2019 at 10:40 AM 0
Share

In addition to @Optrix answer:

you can simplify this to the following:

 Vector3 direction = destination - origin

This is equal to the line you posted. Normalizing does nothing else but to reduce a vector to length 1. So multiplication by the distance of origin and destination will only result in the same vector again.

So in other words, normalization is nothing else but dividing by the length of a vector.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bonfire-Boy · Jul 01, 2019 at 11:22 AM

You shouldn't think of the vector you get by subtracting destination from origin as a "direction vector". It's more than that. It's a vector representing the relative positions of the two points or "how to get from origin to destination". The direction vector is what you get when you normalise the relative position vector.

For any such relative position vector, there are an infinite number of origin-destination pairs that would give the exact same relative position vector when you subtract them. This is obvious from the fact that whatever your origin, you can add the relative position vector to get a new destination.

Therefore, you can't go backwards and what you ask for is not possible.

Another way to look at it is that you start with 6 pieces of information (2 sets of 3D coordinates). When you subtract them to get your relative position vector, you end up with just 3 pieces of information. Information must, therefore, have been lost and you cannot expect to be able to reconstruct the 6 that you started with.

So, what you ask for is not possible. You can't work out the origin or destination from such a vector.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Jul 01, 2019 at 05:31 PM 0
Share

Hmm, that makes complete sense but it leaves me unsure how to proceed- the reason I'm asking in the first place is because I'm working on an algorithmn which, given a 2d polygon represented as a collection of Edges where each edge is a Vector3 start, Vector3 end pair, bisects the polygon along its longest edge. After finding the longest edge, I can calculate the exact line along which I want to bisect it by doing this:

 Edge longestEdge;
 Vector3 bisecting = Vector3.Cross(longestEdge.endVector - longestEdge.startVector, Vector3.up);

Now that I have the bisecting segment, I need to portion one polygon into two by iterating through each edge and moving it to one poly or the other based on which side of bisecting it's on. However, there's one last, critical step: since bisecting has become a new outside edge in both polygons, I need to add its start and end points as vertices on each polygon. That's why I was trying to calculate the start/endpoints.

I suppose as a workaround, ins$$anonymous$$d of a single longest edge I could try to find the longest pair of roughly parallel edges, find the midpoint of each edge, then draw a vector between the two midpoints, but that doesn't seem as clean as the single bisection.

avatar image Bonfire-Boy Sendatsu_Yoshimitsu · Jul 01, 2019 at 06:23 PM 1
Share

Your bisecting is just a Vector3 so cannot represent a line or line segment (only 3 data points). It's actually just giving the direction of the bisecting line .

To define the line itself you also need a position on it. That'll be the midpoint
0.5f * (longestEdge.startVector + longestEdge.endVector).

So now you have one point on the line and its direction. To find the other end of the line, you could go through the other edges seeing where it crosses them. If the polygon is bisectable from the longest edge then there will only be one (unless it happens to hit a vertex in which case you'll get a hit from 2 consecutive edges, but you can check for that).

Hope that makes sense, I may not have 100% followed what you're doing. You don't seem to be catering for the possibility of the bisecting line dividing another line in 2 as well, which seems to be quite likely to me unless you're constraining your polygons in some way.

avatar image Sendatsu_Yoshimitsu Bonfire-Boy · Jul 01, 2019 at 06:37 PM 0
Share

Ooh thank you! That's definitely enough to fix what I was brain-stuck on- and yeah, I'm going to have to deal with the second line as well, but that's reasonably straightforward in comparison- thank you for the really helpful feedback! :)

avatar image
2

Answer by Optrix · Jul 01, 2019 at 09:47 AM

A ray or line is an origin and a direction - you CAN'T build a true line without the other, and you can't calculate one without the other.

But without knowing your application, you might be able to assume one. A direction vector that is used as a speed has its origin at the object. So sometimes the context lets you know what the origin is supposed to be.

Care to offer some more detail?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jul 01, 2019 at 11:26 AM 0
Share

This is wrong, it may be a language thing but the meanings of works matter here. People being sloppy about word meaning is what leads to people not getting it.

1) A ray is an an origin and a direction but the OP is not talking about rays. They're talking about lines defined by two points. Such a line is given by origin, direction, and distance (or, equivalently, by the start and end points).

2) You cannot "use a direction vector as a speed". Speed is a scalar so what would that even mean?

3) You could "use a direction vector as a velocity", but it would be a velocity of 1.0 so not very useful.

avatar image Optrix Bonfire-Boy · Jul 01, 2019 at 11:50 AM 0
Share

1) Yes, I agree. You need all three or endpoints - I was not accurate there.

2) That's technically correct (the best kind of correct?) but extremely pedantic, isn't it? Often "velocity" and "speed" are used interchangeably in most conversation. But yes - speed is the scalar magnitude of your velocity non-unit vector.

3) I did not say 'normalized' or 'unit' vector, so if we are being rather pedantic here allow me to reciprocate :) A unit vector would be quite useless for velocity, but since RigidBody stores velocity as a non-unit vector, I'd imagine it's evident that you can represent velocity as a vector. Even if it couldn't, the fact that Unity uses a Vector3 to represent velocity in Unity, it would still be an acceptable term.

avatar image Bonfire-Boy Optrix · Jul 01, 2019 at 02:30 PM 0
Share

Velocity is a vector, I never said anything different. What I said was that using a direction vector (which is a unity vector) as a velocity would be odd.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make something move away from click point. 2 Answers

Rotate 3d vector based on local forward 1 Answer

Constant total time over multiple Lerp functions 4 Answers

Extending a vector? 4 Answers

Average of Vectors [Math] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges