• 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
2
Question by dchen05 · Jun 18, 2013 at 07:30 PM · mathlinetrianglescosine

Create line that splits triangle

I have three joints that make up an arm. They can be in any position.

I find the angle of the middle point using the Law of Cosines. For example in these examples it would be around 115 degrees or so. http://en.wikipedia.org/wiki/Law_of_cosines

I now need to draw 2 lines (one in each direction) that are half of that angle, so they basically split the triangle in half. Like this.

alt text

I found a thread about creating lines at angles here, so I'm using similar code. http://answers.unity3d.com/questions/350971/how-to-project-an-angle-from-a-vector.html

Now as a test here is my code shooting lines at 0 and 180 and I get this. Seemes pretty odd that they aren't straight up and down.....

 pointer.transform.position = (Quaternion.Euler( 0, 0, 0) * bone_lelbow_lhand.transform.up) * range;
 pointer2.transform.position = (Quaternion.Euler( 0, 0, -180) * bone_lelbow_lhand.transform.up) * range;
 
 Debug.DrawLine(bone_lelbow_lhand.transform.position, pointer.transform.position, Color.green, 100, true);
 Debug.DrawLine(bone_lelbow_lhand.transform.position, pointer2.transform.position, Color.blue, 100, true);


alt text

Anyone have any ideas? I have a feeling I need to use atan2 or align transform.position.up with one of the other joints so the angle appears relative?

screen shot 2013-06-18 at 3.25.03 pm.png (8.0 kB)
Comment
Add comment · Show 2
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 robertbu · Jun 18, 2013 at 08:49 PM 0
Share

Let me make sure I understand. You are trying to draw the red line. You want the red line to bisect the angle formed by the three points. You want a line segment running in each direction through the middle point. Correct?

avatar image dchen05 · Jun 18, 2013 at 10:05 PM 0
Share

yes Correct!

1 Reply

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

Answer by robertbu · Jun 18, 2013 at 09:32 PM

My first though about solving this problem would be to use the cross product. Here is a bit of code:

 var trA : Transform;
 var trB : Transform;
 var trC : Transform;
 
 
 function Update() {
    Debug.DrawLine(trB.position, trA.position, Color.green);
    Debug.DrawLine(trB.position, trC.position, Color.green);
    
    var v3BA = trA.position - trB.position;
    var v3BC = trC.position - trB.position;
    
    var axis = Vector3.Cross(v3BA, v3BC);
    var angle = Vector3.Angle(v3BA, v3BC);
    
    var v3 = Quaternion.AngleAxis(angle / 2.0, axis) * v3BA;
    
    Debug.DrawRay(trB.position, v3);
    Debug.DrawRay(trB.position, -v3);
 }

Attach this script to an empty game object. Put three game objects in the scene and drag and drop them onto the trA, trB, and trC variables. Run the script. You can then move the object around in the inspector while the script is running.

You could also use the Angle Bisector Theorem to find point D on line segment AC where the bisector will intersect. Another example script:

 var trA : Transform;
 var trB : Transform;
 var trC : Transform;
 
 function Update() {
    Debug.DrawLine(trB.position, trA.position, Color.green);
    Debug.DrawLine(trB.position, trC.position, Color.green);
    
    var v3BA = trA.position - trB.position;
    var v3BC = trC.position - trB.position;
    var v3AC = trC.position - trA.position;
    
    var v3D = trA.position + v3AC * v3BA.magnitude / (v3BA.magnitude + v3BC.magnitude);
    
    var v3 = v3D - trB.position;
    
    Debug.DrawRay(trB.position, v3);
    Debug.DrawRay(trB.position, -v3);
 }   


Comment
Add comment · Show 1 · 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 dchen05 · Jun 18, 2013 at 10:11 PM 1
Share

Hell yeah! Awesome answer, both of those work flawlessly. Thanks a lot! I wish I could up vote this a billion times!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 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

Related Questions

Detect where line intersects outline shape 1 Answer

Angle between two lines in 2D 3 Answers

Trying to obtain sine/cosine by angles giving me imprecise values 1 Answer

Basic vector math addition 1 Answer

Draw a Vector line ??? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges